i'm not really a bumblebee
cookies
romillyhills.co.uk uses cookies! cookies are used for the login system and removing this bubble.
http%20arduino.jpg
clock23:55 on 29.01.12 eye397 speech bubble0
http control is a requirement for one of the classic electronics projects; the internet controlled lamp. i only used an led for fear of mains voltage, but with some relays and a bit of cleverness anything is possible. control your house from your phone? yes.

so, things you'll need:

an arduino [i used an uno]
10 micro farrad capacitor

you'll notice an ethernet shield isn't on that list. i don't have one. to get around this i set up my computer as an apache webserver which could run python as cgi so while technically all this was running localy in principle if i changed the port of the server, any internet ready device could control the arduino.

once all that is ready upload the arduino code:
int moo=0;
int pin=13;

void setup() {
pinMode(pin, OUTPUT);
Serial.begin(9600);
}

void loop(){
if (Serial.available() > 0) {
moo = Serial.read();
Serial.print(moo);
}
if(moo==119){
digitalWrite(pin, HIGH);
}
if (moo==115){
digitalWrite(pin, LOW);
}
}

and put the python script into your cgi-bin:
#!/Python27/python
print 'Content-type: text/html\n\n'

import serial
import msvcrt
import cgi
from time import sleep

print 'starting usb...'
s = serial.Serial('COM3', 9600,timeout=1) #start the serial port
sleep(2) #required while the arduino does it's thing

print 'ready'

form = cgi.FieldStorage()
t=int(form["i"].value)

print t

if t==1:
moo='w'
elif t==2:
moo='s'
s.write(moo.encode('ascii'))


the arduino code is fairly straight forward; it just turns on/off the on-board led, but there are some important lines in the python.
#!/Python27/python
print 'Content-type: text/html\n\n'

is required for the python script to be run as cgi and to output html. html isn't important right now, but if you want to use this for any real projects it'll be much easier to get output via ajax.

form = cgi.FieldStorage()
t=int(form["i"].value)


this gets the data sent in the url, then turns it into a number required for the if statement, this is how data is read, then sent to the arduino.

now that's done just open your browser and enter the url of the python script with ?i=1 to turn on the led and ?i=2 to turn it off.

e.g http://localhost:8080/cgi-bin/moo.py?i=1

you'll notice when you turn the led on, it turns on, when you turn it off, it turns off. however, if you turn the led on, then on again, it will turn off for about 2 seconds before turning on again. this is becasue whenever the server makes a serial connection the arduino board is resetting, which means anything it was doing before te connection stops.

this is obviously a problem if you want to control more then one thing.

but there is a wonderfully crude solution! documented here, you can jam a 10 micro farrad capacitor between the reset and ground pins of your board [the fix is slightly different if you're not using an uno]. and it's fixed, no more resseting and you have control of as many leds as you want.

of corse this approach is limited, there is no real-time control, that will require something i havn't thought of yet, but for a simple internet controlled lamp, or even full home automation, this will work beutifully.
what happened to my arms!?