RPi Quadcopter blog

RPi Quadcopter blog
Click to open
Showing posts with label send. Show all posts
Showing posts with label send. Show all posts

May 28, 2011

Arduino and PHP

To comunicate between arduino and pc, you have many options. Php works really well for sending data to arduino, but I'd say you cannot get data straight from arduino without 100% cpu usage and days of trial and error.

The code below requires "php_serial.class.php". Download it to the same folder where you save this code:

// hello from php.php
 <?php
if(isset($_REQUEST['message'])){
$msg=$_REQUEST['message'];
require("php_serial.class.php");
$serial = new phpSerial();
$serial->deviceSet("/dev/ttyACM0"); // Arduino usb-port
$serial->confBaudRate(115200);  //baud rate
$serial->confParity("none");  //Parity
$serial->confCharacterLength(8); //Character length  
$serial->confStopBits(1);  //Stop bits
$serial->confFlowControl("none");
$serial->deviceOpen(); // open connection

$serial->sendMessage($msg); //send the message
}
?>
<html><body>
<h3>If you're running Linux: remember to give permissions to www-data to access usbport ($ sudo chmod 777 /dev/ttyACM0)</h3>
<a href="hello_from_php.php?message=HelloFromPhp">Send hello message</a>
</body></html>
If you are not getting any data to Arduino, you must first (and every time you connect usb) give permissions to www-data to access your Arduino usb-port.

Try the example above with "Arduino: How to read serial to string" for best results!

May 23, 2011

Receiving and sending IR with Arduino

Sending IR with Arduino means that you can control various different devices in your home. Controlling different devices with IR can be part of your Arduino smart home.

Reading the IR signals is fairly easy. All you need is a IR receiver. The most commonly used infrared carrier signal is 38 kHz, but it should still work fine, if the receiver and sender use different frequencies.


Receiver:
38khz

I used IRremote library to send and receive infrared signals. You can download and read more instructions about IRremote library here.

Use the IRrecieveDump -demo to dump all the data read from your remotecontroller. If the data could be identified as RC5, Sony or NEC it will also be very simple to send.

To send IR, you need an Infrared LED with a resistor. You don't want to burn your LED so use this counter to calculate the best resistor for it.


IR-Led



Example:

Start the IRrecieveDump -demo and open serial monitor.
Point your remote towards the receiver and press a button.
The program should print out for example the NEC -code and the raw data.
The NEC -code should look like this: "FF906F".

Now you can try sending the same code by using sendNEC() method:
Irsend.sendNEC(0xFF906FF, 38); 

UPDATE:
the 38 above is wrong. The second parameter is the lenght in bits. The Nec function will always send 38khz. Correct way is:

Irsend.sendNEC(0xFF906FF, 32);

Because the code you are sending is a HEX code you need to put the "0x" before it.
"38" in the constructor means that you want to send it with 38khz carrier frequensy.


Some devices use their own protocols such as Samsung, but you can get the raw data from them. To clean the raw data you get from the dump -demo, you need to erase the first number, then convert all negative numbers to positive and place commas between the numbers.

Here's an example:

Raw data: 4802, -620, 500, -420, 600, -440....

Sending:

unsigned int[68] power_ON ={620,500,420,600,440... ...};
Irsend.sendRaw(power_ON, 68, 38);

68 is the length of the unsigned integer.