ESP32 Webserver - Client keeps reconnecting after client.stop
Posted: Wed Oct 10, 2018 9:13 am
Hi Guys,
I'm having this issue with one of the webserver examples (turns out most examples are written this way), that even though I send "Connection: close" in the header, and even though I call Client.stop(); the broweser keeps reconnecting, and keeping the Webserver busy, making it unable to connect from a different source (another browser). It only frees the server when I close the Tab in browser.
What I want to do really is that when I send the data to the client, it will stop the client when data send is complete, and closes the connection, making it available for other devices to do it again.
Note that I tried to fix it by moving client.stop() right after the data I send, but the effect is the same.
Thank for the help
Vader[BEN]
Code:
I'm having this issue with one of the webserver examples (turns out most examples are written this way), that even though I send "Connection: close" in the header, and even though I call Client.stop(); the broweser keeps reconnecting, and keeping the Webserver busy, making it unable to connect from a different source (another browser). It only frees the server when I close the Tab in browser.
What I want to do really is that when I send the data to the client, it will stop the client when data send is complete, and closes the connection, making it available for other devices to do it again.
Note that I tried to fix it by moving client.stop() right after the data I send, but the effect is the same.
Thank for the help
Vader[BEN]
Code:
Code: Select all
//Original author
/*********
Rui Santos
Complete project details at http://randomnerdtutorials.com
*********/
// Load Wi-Fi library
//#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_Si7021.h"
Adafruit_Si7021 sensor = Adafruit_Si7021();
//WIFI MODES
#define STAmode 1
#define SoftAP 0
#if defined STAmode
// Replace with your network credentials
const char* ssid_pub = "mySSID";
const char* password_pub = "myPW";
#elif defined SoftAP
const char* ssid_loc = "ESP32_Tempserver";
const char* password_loc = "serverPW";
IPaddress local_ip(192,168,0,1);
IPaddress gateway(192,168,0,1);
IPaddress subnet(255,255,255,0);
#endif
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
void setup() {
Serial.begin(115200);
// wait for serial port to open
while (!Serial) {
delay(10);
}
Serial.println("Si7021 test!");
if (!sensor.begin()) {
Serial.println("Si7021 not derected");
while (true)
;
}
Serial.print("Found: ");
switch(sensor.getModel()) {
case SI_Engineering_Samples:
Serial.print("SI engineering samples"); break;
case SI_7013:
Serial.print("Si7013"); break;
case SI_7020:
Serial.print("Si7020"); break;
case SI_7021:
Serial.print("Si7021"); break;
case SI_UNKNOWN:
default:
Serial.print("Unknown");
}
Serial.print(" Rev(");
Serial.print(sensor.getRevision());
Serial.print(")");
Serial.print(" Serial No.#"); Serial.print(sensor.sernum_a, HEX); Serial.println(sensor.sernum_b, HEX);
#if defined STAmode
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to... ");
Serial.println(ssid_pub);
WiFi.begin(ssid_pub, password_pub);
Serial.print("Setting WiFi connection...");
Serial.print("Connecting...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//IF SoftAP mode is used
#elif defined SoftAP
Serial.println("WiFi AP starting")
Serial.print("SoftAP configuration... ");
Serial.println(WiFi.softAPConfig(local_ip, gateway, subnet) ? "Ready" : "Messed up");
Serial.print("Soft-AP setting up ... ");
Serial.println(WiFi.softAP(ssid_loc, password_loc) ? "Ready" : "Messed up");
Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());
#endif
//starting server
Serial.println("Server start");
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New client."); // print a message out in the serial port
Serial.println(client.remoteIP());
String currentLine = ""; // make a String to hold incoming data from the client
while(client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.println(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the table
client.println("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial;}");
client.println("table { border-collapse: collapse; width:35%; margin-left:auto; margin-right:auto; }");
client.println("th { padding: 12px; background-color: #0043af; color: white; }");
client.println("tr { border: 1px solid #ddd; padding: 12px; }");
client.println("tr:hover { background-color: #bcbcbc; }");
client.println("td { border: none; padding: 12px; }");
client.println(".sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px; }");
// Web Page Heading
client.println("</style></head><body><h1>ESP32 TEMP and HUM server</h1>");
client.println("<table><tr><th>MEASUREMENTS</th><th>VALUE</th></tr>");
client.println("<tr><td>Celsius</td><td><span class=\"sensor\">");
client.println(sensor.readTemperature());
client.println(" *C</span></td></tr>");
client.println("<tr><td>Fahrenheit</td><td><span class=\"sensor\">");
client.println(1.8 * sensor.readTemperature() + 32);
client.println(" *F</span></td></tr>");
/* client.println("<tr><td>Pressure</td><td><span class=\"sensor\">");
client.println(bme.readPressure() / 100.0F);
client.println(" hPa</span></td></tr>");
client.println("<tr><td>Approx. Altitude</td><td><span class=\"sensor\">");
client.println(bme.readAltitude(SEALEVELPRESSURE_HPA));
client.println(" m</span></td></tr>");
*/
client.println("<tr><td>Humidity</td><td><span class=\"sensor\">");
client.println(sensor.readHumidity());
client.println(" %</span></td></tr>");
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
client.stop();
Serial.println("Closing client connection");
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
// client.stop();
Serial.println("Waiting for client connection.");
Serial.println("");
}
}