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: http://github.com/lupeke/python-firmata.

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:


<html>
<head>
</head>
<body>
<form action="cpanel2.php" method="post">
On <input type="radio" name="light" />Light
<input type="submit" />
</form>
<?php
$password = $_POST["password"];
$light_on = $_POST["light"];
if(isset($light_on)){
$execute_light = `/Users/dustycarver/python/led2.py`;
}
?></body>
</html>



$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 (info@laboratorio.us)

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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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.delay(2)

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


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.

Caveats:
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:

Unknown said...

Hey,
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?
Thanks..