Tuesday, July 19, 2011

Controlling LED from a web page with Arduino and Ethernet Shield

I figured out how to make my Arduino into a web server pretty quickly, especially since the example program does just that. What was hard was to go the other way, controlling it from a web page. I got that working finally by cobbling together bits and pieces of this project and this project. What I wanted was a couple of form input buttons that would turn on and off an LED. That's what this does. One issue I still have is the first time you submit the Arduino responds immediately but from then on you have a passed value in the URL and for some reason you have to click twice to get the Arduino to respond. Actually it does respond the first click but goes back to its previous state, then stays on the new state on the second click. Something more complicated is going on than I can yet understand. Anyway, it's cool and now I think lots of possibilities are opened up.
Well, I tried posting the code here but the WYSIWYG eats a lot of it, so here it is in a txt file, linked here.
UPDATE: Ha, an anonymous reply (thanks!) solved my double-click problem. Add a break; after each digitalWrite which makes sense now:

if(c == '0') {
digitalWrite(9, LOW);
if(c == '1') {
digitalWrite(9, HIGH);


Anonymous said...

Don't know if you care or if you have already found it... But if you add 'break;' after each digitalWrite, you won't need to click twice.

Erik N. said...

Hey that is perfect, thanks! I had put this down for a while so I hadn't figured it out yet.

Anonymous said...

No problem. Thanks for posting your code!

Alnoor said...

can i get info on how to control 8 leds.


spuder said...

I am trying to implement your code, but the code is unable to create a Server Object.

I suspect that I do not have the same libraries, that you do or perhaps it has something to do with the newly released arduino 1.0

Are the libraries you are using in any way custom?

By the way, the captcha engine your blog uses is unbelievably hard.

Anonymous said...

Hi, i'm trying to add to your code the feature to turn on and off the led at certain time set in the web page, can you help me?

Erik N. said...

Can you be more specific? Do you mean time based on the program running time with mills() or something else? I know you can use a Date library that gets UTC time over the ethernet. Never done that, though.