RPi Quadcopter blog

RPi Quadcopter blog
Click to open

May 28, 2011

Fujlight ir-codes

Found this remotecontrolled led -bulb for cheap: http://www.verkkokauppa.com/fi/product/8159/cgfmm

The remote is  "MCL Remote Controller" and it seems to be used in various other products. These codes should then work with other products with the same remote.

Infrared NEC codes:

speed +: 0xFF08F7
speed -: 0xFFC03F
Pause: 0xFF807F
Off: 0xFF609F
skip 1: 0xFF906F
skip 2: 0xFFB847
Fade: 0xFFF807
Strobe: 0xFFB04F
Red1: 0xFF9867
R2: 0xFFE817
R3: 0xFF02FD
R4: 0xFF40BF
R5: 0xFF50AF
Green1: 0xFFD827
G2: 0xFF48B7
G3: 0xFF12ED
G4: 0xFFA05F
G5: 0xFF7887
Blue1: 0xFF8877
B2: 0xFF6897
B3: 0xFF20DF
B4: 0xFF2AD5
B5: 0xFF708F
White: 0xFFA857
100%: 0xFF32CD
75%: 0xFF00FF
50%: 0xFFB24D
25%: 0xFF58A7

See how to send these NEC-codes with Arduino!



The whole top part of the controller is glued sheet of plastic with buttons. You may see this remote with different layouts, so I included .odt/excel file where you can see the codes in button order. Download

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 25, 2011

Arduino: How to read serial to string

String reserves a lot of memory for it self. Avoiding string type variables is a good idea. This example is good for debugging purposes when you don’t know how long strings you are sending to arduino.  when your code becomes longer and you know how long strings you are sending, use char[20].
String inData;
void setup()
{
 Serial.begin(115200);
}
void loop(){
inData="";
     if (Serial.available() > 0) {
         int h=Serial.available();   
        // if you are getting escape -characters try h--; here
    
for (int i=0;i<h;i++){
             inData += (char)Serial.read();
         }
    // if you are getting escape -characters try Serial.read(); here
    }
    //print it out
    Serial.println(inData);
}

May 24, 2011

How to set up a webcam stream in Ubuntu

1. Download and install ffmpeg
2. Edit ffserver.conf
3. Start streaming

1. Download and install ffmpeg

It is important to get the latest (even RC) version of ffmpeg, because version 0.6.3 (the current stable) doesn’t work. I used FFmpeg 0.7-rc1 in this example.

Before installing new version, remove the old one:

$ sudo apt-get remove ffmpeg


Download and extract the tarball.
Open terminal (ctrl+alt+t) and type these commands to install:

$ cd /extracted/folder/ffmpeg-0.7-rc1
$ ./configure
$ make
$ sudo make install



2. Configuring

Open /etc/ffserver.conf with gedit. In this example i used flash to stream:



Port 8090
BindAddress 0.0.0.0
MaxHTTPConnections 2000
MaxClients 1000
MaxBandwidth 1000
CustomLog -
NoDaemon


###############################################################
# FEED

File /tmp/myfeed.ffm
FileMaxSize 5M
# allow only localhost to stream
ACL allow 127.0.0.1

# this starts the stream automaticly:
#Launch ffmpeg -r 15 -s 640x480 -f video4linux2 -i /dev/video0



##################################################################
# FLASH STREAM

Feed myfeed.ffm
Format swf
VideoCodec flv
VideoFrameRate 15
VideoBufferSize 80000
VideoBitRate 100
VideoQMin 1
VideoQMax 5
VideoSize 640x480
PreRoll 0
Noaudio



###################################################################
# Redirect index.html to the appropriate site
URL http://www.ffmpeg.org/


3. Start streaming

Start the ffserver:

$ ffserver


Start ffmpeg:

$ ffmpeg -r 15 -s 640x480 -f video4linux2 -i /dev/video0 http://localhost:8090/myfeed.ffm


-r 15 = frame rate 15fps (must be same as in config)
-s 640x480 = width x height
-f video4linux2 = format
-i /dev/video0 = the location of your web-cam.
http://localhost:8090/myfeed.ffm is the output.

If there were no errors, open the stream: http://localhost:8090/mystream.swf

Optionally you can comment out this row and just run ffserver
#Launch ffmpeg -r 15 -s 640x480 -f video4linux2 -i /dev/video0


Last words

I used several hours googling and trying everything to fix some weird errors without succeeding. Solution was to download the LATEST version. So for “segmentation fault”, “streams doesn’t match” or “pixel format” errors, try installing the latest version of ffmpeg!

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.