I've been trying to detect unusual goings on in my programs and started adding counters in all the sections to show me what is happen and came across this weird thing.
If you run the below you will notice that the counter connected available RAPIDLY runs crazy while the other two act expectedly.
As an example, here is what I have after a few clicks on the screen
ifClient is 29
ClientCn is 379350
ClientAvl is 12688
However, you uncomment out the Serial.println("ClientCn"); and Serial.println("ClientAvl"); guess what, the numbers are more normal looking.
My guess is there must be some delay somewhere but why and how is this fixed. Plus I think it might be tripping the watchdog timer.
Another anomaly is that the ifClient counter always increments by two.
Any ideas?
Thanks for looking. Device is esp8266
#include <ESP8266WiFi.h>
const char ssid[] = "whatever";
const char password[] = "whatever";
uint32_t ifClient = 0;
uint32_t ClientAvl = 0;
uint32_t ClientCn = 0;
WiFiServer server(80);
void setup() {
Serial.begin(115200);
Serial.println("hello");
wificonnect();
Serial.println(WiFi.localIP());
}
void loop() {
WiFiClient client = server.available();
if (client) {
ifClient++;
Serial.println("ifClient");
boolean currentLineIsBlank = true;
while (client.connected()) {
ClientCn++;
//Serial.println("ClientCn");
if (client.available()) {
ClientAvl++;
//Serial.println("ClientAvl");
char c = client.read();
Serial.write(c);
// send a standard http response header
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println("Refresh: 85");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("<br> <span style=\"font-size: 26px\";>ifClient is ");
client.println(ifClient);
client.println("</span><br />");
client.print("<br> <span style=\"font-size: 26px\";>ClientCn is ");
client.println(ClientCn);
client.println("</span><br />");
client.print("<br> <span style=\"font-size: 26px\";>ClientAvl is ");
client.println(ClientAvl);
client.println("</span><br />");
client.println("</html>");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
Serial.println(" client.stop ");
}
}
void wificonnect() {
uint8_t wifi_retry = 0; // COUNTER SOLVES ESP32-BUG WITH CERTAIN ROUTERS: CONNECTION ONLY ESTABLISHED EVERY SECOND TIME
yield();
while (WiFi.waitForConnectResult() != WL_CONNECTED && wifi_retry < 3) {
WiFi.begin(ssid, password);
delay(3000);
wifi_retry++;
}
if (wifi_retry >= 3) {
ESP.restart();
}
yield();
if (WiFi.waitForConnectResult() == WL_CONNECTED) {
server.begin();
Serial.println("WiFi connected");
}
}