RPi Quadcopter blog

RPi Quadcopter blog
Click to open
Showing posts with label linux. Show all posts
Showing posts with label linux. 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 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!