Web server question

Hey guys/gals. I am relatively new to arduino but I'm definitely loving the possibilities. I am, however, having an issue with 2 pieces of code. I'll only ask one to start in case the answer to the first answers the second.

First let me say that I have looked through hundreds of sample code and forum references with no luck. If I missed one that was obvious, my apologies in advance.

I can make the webserver that has buttons that controls a light for example, piece of cake. I can hit the button and it turns on, incrementally if I choose, and hit it and it turns off. Simple stuff.

What I can not do is make the light stay on (output pin stay high) ONLY while I'm pushing the button on the web server. When the button is released, it should turn off (output pin goes low). Did I overlook some easy piece of code in my searches?

Any help would be appreciated.


You need to send two messages one on a button press and the other on the button release.

Keep track of the button's last state and then only do an action when the current button state is not what it was the last time.

What I can not do is make the light stay on (output pin stay high) ONLY while I'm pushing the button on the web server.

A client (your browser) only makes GET requests when events happen. What event is happening while you are holding the button? None.

So.....The GET requests only happens on the release of a clicked button.


Than a web server to do what i want sounds like it may not be the correct path.

Would I need to write a program send these commands to the Arduino to do this or can you think of an easier way to accomplish this? It has been a while since I dove deep into programming so I am very rusty. The web server seemed like an easy way but maybe not.

- Grumpy_Mike stated

"You need to send two messages one on a button press and the other on the button release.

Keep track of the button's last state and then only do an action when the current button state

is not what it was the last time"

PaulS, does your comment make Grumpy_Mike's response invalid? Maybe my question was misunderstood.

Grumpy_Mike, would you happen to have an example of what you were referring?

Thank you for the responses by the way. I appreciate the assist.

PaulS, does your comment make Grumpy_Mike’s response invalid? Maybe my question was misunderstood.

No, it doesn’t. You’ll need to use some java scripting on your page, which is beyond the scope of this forum, to generate a mouse click AND a mouse release event. The standard form button does not generate separate press and release events. That is assuming, of course, that that is what you are using. Actually having posted some code, as you were supposed to do when posting in this part of the forum, would have been the proper thing to do, so we didn’t have to make assumptions.

Thank you for the answer. I thought that the question was broad enough. I was attempting not to waste anyone's time combing through the code I was manipulating. I will post code.

//include necessary libraries



// Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; IPAddress ip(192,168,1,177); IPAddress gateway(192,168,1,2); IPAddress subnet(255,255,255,0);

// Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80);

String webClickRequest;

void setup(){

// initialize the digital pin as an output. pinMode(9, OUTPUT);

// Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only }

// start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); }

void loop(){ // Create a client connection EthernetClient client = server.available(); if (client) { while (client.connected()) { if (client.available()) { char c = client.read();

//read the incoming HTTP request if (webClickRequest.length() < 100) {

//store the request to a string webClickRequest += c;


//check to see if the request has come to an end if (c == '\n') {

//Begin drawing out the website using //using basic HTML formatting with some CSS client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.println(""); client.println(""); client.println("LED TOGGLE"); client.println(""); client.println(""); client.println(""); client.println("

Internet Relay Toggle

"); client.println(" "); client.println(" "); client.println("Toggle On"); client.println(" "); client.println(" "); client.println(" "); client.println(" "); client.println("Toggle Off"); client.println(" ");

client.println(""); client.println(""); //Stop loading the website delay(1); client.stop();

//check to see if any of the led commands have been sent //from the webpage to the Arduino

if(webClickRequest.indexOf("?LEDON") > 0){ Serial.println("hello"); LEDON();


else if(webClickRequest.indexOf("LEDOFF") >0){ LEDOFF(); }

//clear the string for the new incoming data webClickRequest="";

} } } } }

//led on/off functions //here on down

void LEDON(){ digitalWrite(9, HIGH); }

void LEDOFF(){ digitalWrite(9, LOW); }

I have gone through many revisions of this with multiple toggled relays and have implemented with motor controls but this is the basic concept. Turn something on and turn something off with a single click. Once I get that down, I can change it accordingly to fit my needs.

I have never played with java so i might try another path. Would Java be the easiest way? If so, I'll get the tutorials playing. Thanks


Ill check that out. Thanks

A snippet of html code from an old project:


Zoomkat, Thanks for the code.

I really liked your posts about the routerbot that you made several years back. Have you made any advancements on that? I am going to read up a little more on that bot and see how far you post about it. I am making something similar. I like the router idea instead of Wifi-Shield as the access points that I am going to use is very high powered so range will be ridiculously far. I will use the PWM for speed control and can turn the motors on/off with forward/reverse with the pololu h-bridge which will suffice for movement, piece of cake. I just can't get a good "hold the button down to move" control interface. Me being extremely rusty on C++ and new to java and android app building doesn't help. I thought I could use the web server easy enough but the good interface to control it is eluding me. I thought that I could get this "only on while pushing the button" thing worked out easily and implement this into the control side for a simple control of the bot.

I have found several apps and arduino code that make use of the wifi shield but not the ethernet shield in the manner that works for me. Or I might be over looking its use. I have been going through a lot of arduino code and been looking up the MIT App Inventor 2 for control but I have a lot to learn.

I will see what I can do with that code. I appreciate the post and input.

Below is another html code section from a similar web control page for a robotic arm gripper. Note that the mouse cursor needs to be over the button for for the mouse down/up clicks to work. This was used with an ssc-32 servo controller that accepts servo movement speed commands and a stop movement command.

<TITLE>Zoomkat's SSC-32 arm control</TITLE>
Zoomkat's SSC-32 arm control 4/15/10

0<input type="button" value="<<<" 
<input type="button" value="<<" 
<input type="button" value="<" 

<input type="button" value="S" 

<input type="button" value=">" 
<input type="button" value=">>" 
<input type="button" value=">>>" 
('');"/> Gripper


Best to send your query through ajax. Changing location.href like above like above might refresh the html page and cause issues.

Thanks again. I'm at work so I can't plug code yet but quick question if you don't mind.

Is there a way to open an input box similar to the serial monitor but not having the need to hit enter after every key press? Over a network I mean, not USB interface cause that would be lame having a cord and all.

Oh, and I hope I am not being offensive by saying the following: Assuming there is nothing against this in the terms of the forum, I'd be happy to compensate for time for assistance on writing the code for the arduino just to speed me up in getting a working example. Just a basic working sketch and something for a controller, preferably keyboard entry so I can use an x-arcade controller cause that would be cool. lol

Seriously, let me know. I work a lot and would love to jump to live/working code I can tweak.

and I have a page open now (at work.....lol) looking up ajax to see what it can do for me.

Basaku, if you want to learn AJAX in connection with Arduino/WebServer, all you need to do is to read/test everything that is listed in the link that was posted by mistergreen in the 6th reply

mistergreen: http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/

This is how I learned most of the stuff.

Like the JavaScript onmouseup event, you can have a onkeyup event to a text field. Again, that event would trigger an ajax query to the arduino server.

Thanks. I already looked briefly at that link yesterday but didn't have time to go through it. I'll start going through it today. I have several hours to throw on that project later this afternoon.

I'll see how far I can get and how much I can learn.

Thanks guys