Web browser not updating server

Hi Folks.

In the code below which contains 2 bottons to turn on and off an LED and to display "On" or "Off" on the screen depending on which was pressed. If I type the complete address "192.168.0.10/fff" or "192.168.0.10/nnn" in the address bar it works fine, the LED goes on/off and the relevant text is displayed on the webpage. If however I press the buttons the LED does change but the text on the webpage does not. Even a refresh only stores the initial reference so will not update either. It clearly is going through the program ok as the LED's are changing accordingly but does not seem to implement 'writeWebPage'. Why is this??

Thanks,

Mark.

P.S. I have used a digital read to monitor the status which works but I want the text to appear when the user presses the button as later on it will contain other information.

#include <SPI.h>
#include <Ethernet.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 10); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

String readString; 
int i = 0;

char one[10];
char two[10];
char three[10];
char combined[30] = {0};

void setup()
{
    pinMode(4, OUTPUT);
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients

      //enable serial data print 
  Serial.begin(9600); 
  Serial.println("servertest1"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  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; 
         // Serial.print("string   ");
         // Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println("readstring");
          Serial.println(readString); //print to serial monitor for debuging 

        if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204");
               client.println();
               client.println();
             }

          ///////////////////// control arduino pin
          if(readString.indexOf("n") >0)//checks for on
          {
            digitalWrite(4, HIGH);    // set pin 4 high
          //  Serial.println("Led On");
             writeWebPage("<H1>","Onnnnnnnnnnnn","<H1> ");
             
          }
          else if(readString.indexOf("f") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
           // Serial.println("Led Off");
             writeWebPage("<H1>","Off","<H1> ");
          }
          else
          {
            writeWebPage("<H1> ","Error","<H1> ");
           // Serial.println("Nothing");
          }
          
          //clearing string for next read
          readString="";

          delay(1);
          //stopping client
          client.stop();

        }
      }
    }
  }
} 

void writeWebPage(char one[],char two[],char three[]){
          strcat(combined, one);
          strcat(combined, two);
          strcat(combined, three);

          EthernetClient client = server.available();
          
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>LED Control</H1>");
          
          //client.println("<a href=\"/?nnn\" target=\"inlineframe\">ON</a>"); 
          //client.println("<a href=\"/?fff\" target=\"inlineframe\">OFF</a>"); 

          client.println("<a href=\"/?nnn\" >ON</a>"); 
          client.println("<a href=\"/?fff\" >OFF</a>"); 

          client.println("<IFRAME name=inlineframe style=\"display:none\" >");          
          client.println("</IFRAME>");

          client.println("<H1>Status</H1>");
          client.println("<H2> test</H2>");
          client.println(combined);

          client.println("</BODY>");
          client.println("</HTML>");

          Serial.println(combined);

          for( int i = 0; i < sizeof(combined);  ++i ){  // clear buffer
            combined[i] = (char)0;
          }
}

MarkG123:
nd the relevant text is displayed on the webpage. If however I press the buttons the LED does change but the text on the webpage does not. Even a refresh only stores the initial reference so will not update either. It clearly is going through the program ok as the LED's are changing accordingly but does not seem to implement 'writeWebPage'. Why is this??

HTTP communication is based strictly on client/server architecture and works basically like that:

  • webserver sits and waits for client requests
  • upon receiving a client connection, the server will do three things
  • read the incoming client request
  • handle the incoming client request
  • send a response back to the client

The HTTP client (webbrowser) acts like that:

  • establish connection to a webserver
  • send HTTP request to the webserver
  • receive response and show the response page in the webbrowser
    close connection

Each action of the operator using a webbrowser , like pressing "virtual buttons" in a HTML user interface will do the same steps always:

  • establish connection to a webserver
  • send HTTP request to the webserver
  • receive response and show the response page in the webbrowser
    close connection

webbrowsers are standard software and work accordingly to HTTP and HTML standards.

If you create an "Arduino webserver" you just have to do the same:

  • create a webserver software which acts accordingly to HTTP and HTML standards.

Your current webserver code seems to lack something, if it doesn't work like you expect.

Either the code has problems

  • detecting the actual client request
  • parse the actual client request
  • handle the actual client request
  • send proper response back to the client

A couple of days ago I sent a webserver code for Arduino und W5100 Ethernet Shield, controlling four relays.

I appended the code in this message:http://forum.arduino.cc/index.php?topic=472494.msg3242163#msg3242163

In fact, the code works quite well, but the user interface does not offer buttons for "ON" and "OFF", instead it works like that:

  • the user interface shows "ON" and "OFF" state of the relays and a "Toggle" button.

In case the user presses the "Toggle" button, this will happen:

  • related relay toggles its state
    -updated HTML page will be sent instantly, displaying the new states in the user interface.

Please give it a try and tell me what you think about!

In case you really want "ON" and "OFF" buttons instead of "ON/OFF display and a "Toggle" button, the user interface can easily be changed to display something else.