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;
}
}