Toggle Switch on Arduino Webserver

Hi,

i have finished my arduino project which involves an Arduino Uno rev3 + ethernet shield (with sd card).
I'm not using the SD card but only ethernetshield and arduino.
My sketch shows a webpage with html buttons and an IFRAME where results of temp and humid sensors are read and updated constantly.

What i hate about all this are the BUTTONS i made. I hade to make an ON and OFF button or a PLUS or MINUS button to change temperature. While PLUS and MINUS has a sense, i don't feel so intelligent the ON and OFF button when i could just have a unique Button if state is pressed it's ON, if unpressed it's OFF.
These ON and OFF buttons are 8 totally for 4 functions (turn On or turn OFF 4 different features).

Is there anyway to get a nice pushbutton instead of a button for ON and one for OFF?
Actually my memory available is as follows :

Lo sketch usa 31.292 byte (97%) dello spazio disponibile per i programmi. Il massimo è 32.256 byte.
Le variabili globali usano 1.374 byte (67%) di memoria dinamica, lasciando altri 674 byte liberi per le variabili locali. Il massimo è 2.048 byte.

In attachment the code. Any suggestions are greatly apreciated.
I have an 2 gb sd card if needed.

att.txt (31 KB)

Is there anyway to get a nice pushbutton instead of a button for ON and one for OFF?
Actually my memory available is as follows :

Yes. The web page could show a button that says Toggle. The value would be the button number. When the Arduino receives something like GET /?Toggle=3 it would remember the current state of the pin, and toggle it.

PaulS:
Yes. The web page could show a button that says Toggle. The value would be the button number. When the Arduino receives something like GET /?Toggle=3 it would remember the current state of the pin, and toggle it.

Could you please supply an example, i'm not an expert with html?

would it be something like this?

           if (readString.indexOf("/?mainOnOff") > 0)       
          
                                
                            if (mainOnOff==0) 
                             {  
                              mainOnOff=1;
                              lbl_mainOnOff = "Main On";
                             }
                           else
                 
                            if (mainOnOff==1) 
                             {
                              mainOnOff=0;
                              lbl_mainOnOff = "Main Off";
                             }

and would the code for showing the button be like this ?

          client.println(F("<input type=button value=lbl_mainOnOff style=background-color:#ff9999;width:105px;height:42px onmousedown=location.href='/?mainOnOff'>"));

Maybe i should do this instead for the buttons :

if (mainOnOff==0)   client.println(F("<input type=button value='Main On' style=background-color:#ff9999;width:105px;height:42px onmousedown=location.href='/?mainOn'>"));
if (mainOnOff==1)   client.println(F("<input type=button value='Main Off' style=background-color:#ff9999;width:105px;height:42px onmousedown=location.href='/?mainOff'>"));

and this for the code :

if (readString.indexOf("/?mainOn") > 0)  mainOnOff=1;
if (readString.indexOf("/?mainOff") > 0)  mainOnOff=0;

But if it's this way i have to do it, i will probably run out of memory because i'm duplicating html strings and they will all occupy more memory or not?

marcomaroso:
Maybe i should do this instead for the buttons :

if (mainOnOff==0)   client.println(F("<input type=button value='Main On' style=background-color:#ff9999;width:105px;height:42px onmousedown=location.href='/?mainOn'>"));

if (mainOnOff==1)  client.println(F("<input type=button value='Main Off' style=background-color:#ff9999;width:105px;height:42px onmousedown=location.href='/?mainOff'>"));




and this for the code :


if (readString.indexOf("/?mainOn") > 0)  mainOnOff=1;
if (readString.indexOf("/?mainOff") > 0)  mainOnOff=0;




OK, this last my solution seems to work but not correctly until i update the page. The button works and does what it shall do, but the label of the button won't change untile i update the page...and that's not a clever solution for me since the buttons are on a main page (static) and the rest of sensor readings is in a IFRAME that keeps updating values, what i obtain is a flickering continuous page for buttons and for IFRAME if i tell to update also the mainpage after button pressure.

How can i say after button pressure to update only once the button page?
Should be better to only have that button label updated without the need to update the whole button page again.

Any suggestions.

Right now i have :



if (readString.indexOf("/?mainOn") > 0)  generale=1;  client.print(F(""));




But if it's this way i have to do it, i will probably run out of memory because i'm duplicating html strings and they will all occupy more memory or not?

PaulS:
Yes. The web page could show a button that says Toggle. The value would be the button number. When the Arduino receives something like GET /?Toggle=3 it would remember the current state of the pin, and toggle it.

Have you seen what i wrote?
Probabbly you say with a button named "Toggle Mains" i can power on or off something.
I know that but i also wanted to update the button label and if power is ON the button should say "Main = On" or should just turn to "Main Off" so that i turn off if i press it.
If power is Off then Button label should be "Main = Off" or "Main On"

But i guess you can't get the lable changed without updating the whole page of buttons, am i wrong?
I mean, certainly i could get only the label updated but i think i will have to use ajax or java script and that will result in alot more code running out of memory and facing the issue that on non Java Enabled devices it could not work.

For toggle code, search for toggle in the forum search box.