ETH - web server refresh onclick

Hello guys,

Please advise.

I’m building a small project on Arduino web serwer with some buttons.

When I’m clicking on “ON”, the web page changes ftom eg. 192.168.0.50 to 192.168.0.50/LED_ON
then the Arduino code lights the LED. - this is good.
and back when I click on “OFF” buton the page redirects to 192.168.0.50/LED_OFF and the Arduino code lights the LED off. - thats;s good.

but,

I want to add some status on the web page (to know if the LED is ON or OFF). I’m doing this by adding a variable and setting it to 0 or 1 after turning the led on or off.

My problem is to immediately update it to the web page.

I know I can do this by HTML META DATA, but sensible value is 3 or 5 sec. but cannot wait ant want to have the information ASPA just after clicking.

I found a resolution saying that can do this by sucha a code:

client.println("<a href="/LIVING_LED_ON"><button onclick=“myFunction()”>On ");

The problem is that this coce refreshes the main page (192.168.0.50) and don’t redirect to 192.168.0.50/LIVING_LED_ON and in effect the led doesn’t lights.

OUESTION:

Is there a way to first change the page to the subpage /LIVING_LED_ON so the LED lights and then reload a page so the status chanes immediately,

or

is there another way to pass a information from the web server to rest of Arduino code that buton was clicked - not by subpages like /LIVING_LED_ON or /LIVING_LED_OFF, then I could stay always on main page and just reloading it,

or

can I pass some variable from the web server after buton click to the rest code controlling Arduino pins that I can use reloading script.

Just please help you are my last chanse…

The code in new post…

Here is part of my code

byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address
byte ip = { 192, 168, 0, 50 }; // IP
byte gateway = { 192, 168, 0, 1 }; // GETEWAY
byte subnet = { 255, 255, 255, 0 }; // MASK
EthernetServer server(80); // PORT

String readString;
int led2 = 2;

int L2_status;

void setup()
{

pinMode(led2, OUTPUT);

client.println("");
client.println("");
client.println(“HOME CENTER”);
client.println("<meta http-equiv=“refresh” content=“5”>");

client.println("");

// WEB PAGE

client.println("");

client.println("");
client.println("<table border=10 width=“960” height=“400” cellspacing=20 cellpadding=10>");
client.println(" ");

// LIVING ROOM

client.println(" “);
client.println(” LIVING ROOM
“);
client.println(” status");
client.println(L2_status);
client.println(" LIGHT <a href="/LIVING_LED_ON"><button class=“button button_on”>ON <a href="/LIVING_LED_OFF"><button class=“button button_off”>OFF
“);
client.println(” SHUTTER <a href="/LIVING_SHU_UP"><button class=“button button_on”>UP <a href="/LIVING_SHU_DOWN"><button class=“button button_off”>DOWN
");

// Here is code that refreshes the page but stays on main page and don’t redirect to /LIVING_LED_ON
client.println(" <a href="/LIVING_LED_ON"><button onclick=“myFunction()”>On ");
client.println(" <a href="/LIVING_LED_OFF"><button onclick=“myFunction()”>Off ");

client.println(" ");

client.println(" “);
client.println(”");
client.println("");
client.println("");
client.println("");
delay(10);

//stopping client
client.stop();

// CONTROLING ARDUINO PINS

if(readString.indexOf(“LIVING_LED_ON”) > 0)
{
digitalWrite(led2, HIGH);
L2_status = 1; // HERE I’M CHANGING THE STATUS IF THE LED IS ON OR OFF - HOW TO ASAP UPDATE IT TO THE WEB PAGE

}

else

if(readString.indexOf(“LIVING_LED_OFF”) > 0 )
{
digitalWrite(led2, LOW);
L2_status =0;
}

//clearing string for next read
readString="";

}
}
}
}
}

Here is part of my code

Here is part of my answer.

You need to change line

If you want to see ALL of the answer, post ALL of your code.

Sending data to the client in setup() makes NO sense.

Here is all of my code:

#include <SPI.h>
#include <Ethernet.h>
byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address
byte ip = { 192, 168, 0, 50 }; // IP
byte gateway = { 192, 168, 0, 1 }; // GETEWAY
byte subnet = { 255, 255, 255, 0 }; // MASK
EthernetServer server(80); // PORT
String readString;
int led2 = 2;
int L2_status;

void setup()
{
pinMode(led2, OUTPUT);
//start Ethernet Shield
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
}

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

//read char by char HTTP request
if (readString.length() < 100)
{ //store characters to string
readString += c;
}

//if HTTP request has ended– 0x0D is Carriage Return \n ASCII
if (c == 0x0D) {
client.println(“HTTP/1.1 200 OK”); //send new page
client.println(“Content-Type: text/html”);
client.println();
client.println("");
client.println("");
client.println(“HOME CENTER”);
client.println("<meta http-equiv=“refresh” content=“5”>");
client.println("");
client.println(“function Reload() {location.reload();}”);
client.println("");
client.println("");
client.println("");
client.println("");
client.println("<table border=10 width=“960” height=“400” cellspacing=20 cellpadding=10>");
client.println(" ");

// LIVING ROOM

client.println(" “);
client.println(” LIVING ROOM
");
client.println(" status");
client.println(L2_status); // THIS HAS TO BE UPDATED AFTER BUTTON CLICK

// THIS BUTTONS LIGHTS THE LEDS / REDIRECTED TO /LIVING_LED_ON… BUT DON’T REFRESHES THE WEB PAGE

client.println("<a href="/LIVING_LED_ON">ON <a href="/LIVING_LED_OFF">OFF
“);
client.println(”<a href="/LIVING_SHU_UP">UP <a href="/LIVING_SHU_DOWN">DOWN
");

// THIS BUTTONS REFRESHES THE PAGE, BUT THE MAIN PAGE - 192.168.0.50 AND DO NOT TURN THE LIGHTS ON AND OFF
// BECAUSE THEY DON’T REDIRECTED TO THE SUBPAGE.
client.println("<a href="/LIVING_LED_ON"><button onclick=“myFunction()”>On ");
client.println("<a href="/LIVING_LED_OFF"><button onclick=“myFunction()”>Off ");

client.println(" “);
client.println(” “);
client.println(”");
client.println("");
client.println("");
client.println("");
delay(10);

//stopping client
client.stop();

// CONTROLING ARDUINO PINS

if(readString.indexOf(“LIVING_LED_ON”) > 0)
{
digitalWrite(led2, HIGH);
L2_status = 1; // HERE I’M CHANGING THE STATUS IF THE LED IS ON OR OFF - HOW TO ASAP UPDATE IT TO THE WEB PAGE

}

else

if(readString.indexOf(“LIVING_LED_OFF”) > 0 )
{
digitalWrite(led2, LOW);
L2_status =0;
}

//clearing string for next read
readString="";
}
}
}
}
}

I need a buton that light the LED on and then refresh the page so the L2_status will be updated on the web page.

client.println(L2_status); // THIS HAS TO BE UPDATED AFTER BUTTON CLICK

That comment is wrong. The value in L2_status needs to be updated after dealing with the GET request but BEFORE sending this reply to the client.

You need to deal with the incoming GET request BEFORE you send the response.

ok. Maybe simplier.

Can I just do something like this:

ON</buton
OFF</buton

I added a STEPPER MOTOR and the effect after clicking buton is that the motor runs every time after page refteshes, because web serwer stays on subpage /LIVING_SHU_UP.

Can I just do something like this:

You do NOT need to ask us for permission to experiment.

Try that. It will either make your incorrectly implemented program work or it won't. I know which I'd be betting on.