Server / Client problem
Posted: Mon May 23, 2016 8:47 am
Hi,
I want to have ans ESP8266 working as an client and also as an server. I made this code and it works good until I try to send an code the the client or open its web page. After that it gets unresponsive.
You see any problem in this code?
I want to have ans ESP8266 working as an client and also as an server. I made this code and it works good until I try to send an code the the client or open its web page. After that it gets unresponsive.
You see any problem in this code?
Code: Select all
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html><HEAD><TITLE>WEB SITE</TITLE> </HEAD>";
s += "<BODY><H1>WEB SITE ESP8266 SYSTEM</H1>";
s += "</b><br>Current ON Time: ";
s += millis() * 0.000016667;
s += " min";
s += "<br>";
s += "</BODY>";
s += "</html>\n";
client.print(s);
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
if (req.indexOf("relay1_ON") != -1) {
Relay1Status = 1;
digitalWrite(Relay1, 0);
}
if (req.indexOf("relay1_OFF") != -1) {
Relay1Status = 0;
digitalWrite(Relay1, 1);
}
else {
Serial.println("invalid request");
client.stop();
return;
}
client.flush();
// Prepare the response
String s2 = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n Command Done ";
s2 += "</html>\n";
client.print(s2);
delay(1);
Serial.println("Client disconnected");