Sunday, September 27, 2009

Blinking an LED over the web, with python firmata, php, and apache.

This was done on a mac, however it should be the same or very similar on a linux system as well. Either way you need a webserver with php running, and an arduino conneceted via ftdi(usb probably) to your computer. You will also need to install the python firmata module, which is pretty straight forward, and you can get that here:

This is to show you how to make an led blink, using a web page you have coded, and an arduino. Why would I show you how to do something so insignificant? The idea, is that this would be a place to start, with any sort of, over the web arduino fun. There are a list of caveats at the end.

Open the arduino program, then select File->Examples->Firmata->Standard Firmata. Then upload this to your board. If you have problems uploading, be sure you have the right board and serial port selected under tools. My cpanel.php file looked like this:

<form action="cpanel2.php" method="post">
On <input type="radio" name="light" />Light
<input type="submit" />
$password = $_POST["password"];
$light_on = $_POST["light"];
$execute_light = `/Users/dustycarver/python/`;

$execute_light should equal the pathname to your python firmata file, and should be surrounded my backticks(`), not single quotes(').Your python file should look like this:

#! /usr/bin/env python
Simple LED blinking example using Python Firmata
Copyright (C) 2008 laboratorio (

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see .

from firmata import *

a = Arduino('/dev/tty.usbserial-FTE4W74Q')
a.pin_mode(13, firmata.OUTPUT)

a.digital_write(13, firmata.HIGH)
a.digital_write(13, firmata.LOW)

You can see I stole the code from the python firmata example, however I took out the loop so it would do only one sequence of blinks. Your "a =" line should have the address of your Arduino device.

Obviously this has limitations, it has to negotiate the connection with the arduino each time and that's a lot of overhead, so this is probably a good way to get started on something like turning your lights on at home before you get there, and less real time stuff. I am working on a mod_python deal, so that the connection is ever present and will submit that, if it ever becomes fruitful.

1 comment:

Prash said...

Great article.One small question though.. in your python script.. can you write a python script to just keep pin 13 HIGH until another script tells it to go LOW ?I tried to create a python script which makes only pin 13 HIGH and end the script.. but soon after pin 13 goes LOW , it doesnt STAY HIGH.. is there any way to keep a pin HIGH until another script is called which makes it go back low? or something like that?