Using the new Arduino IDE for ESP8266 and found bugs, report them here

Moderator: igrr

User avatar
By lynton
#42295 Hi , I modified the example advancedwebserver.ino to use as an access point and webserver which worked but then I noticed that every so often there is a 3 second delay, usually this occurs within a few requests. It does not happen until at least 1 request has been made.

The delay occurs somewhere inside the server.handleClient() call. I added some print outs to the code as follows

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

// forward declarations required for 1,6,7 to compile.
void drawGraph();
void printFlush(const char*);

const char *ssid = "TSHIRT";
const char *password = "12345678";
ESP8266WebServer server ( 80 );

const int led = 13;

void handleRoot() {
  printFlush("handleRoot() BEGIN");
  digitalWrite ( led, 1 );
  char temp[400];
  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf ( temp, 400,

             "<html>\
  <head>\
    <meta http-equiv='refresh' content='5'/>\
    <title>ESP8266 Demo</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h1>Hello from ESP8266!</h1>\
    <p>Uptime: %02d:%02d:%02d</p>\
    <img src=\"/test.svg\" />\
  </body>\
</html>",

             hr, min % 60, sec % 60
           );
  server.send ( 200, "text/html", temp );
  digitalWrite ( led, 0 );
  printFlush("handleRoot() END");
}

void handleNotFound() {
  printFlush("handleNotFound() BEGIN");
  digitalWrite ( led, 1 );
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";

  for ( uint8_t i = 0; i < server.args(); i++ ) {
    message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
  }

  server.send ( 404, "text/plain", message );
  digitalWrite ( led, 0 );
  printFlush("handleNotFound() END");
}

void setup ( void ) {
  pinMode ( led, OUTPUT );
  digitalWrite ( led, 0 );
  Serial.begin ( 115200 );


  WiFi.softAP(ssid, password);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);

  WiFi.begin ( ssid, password );
  Serial.println ( "" );

  if ( MDNS.begin ( "esp8266" ) ) {
    Serial.println ( "MDNS responder started" );
  }

  server.on ( "/", handleRoot );
  server.on ( "/test.svg", drawGraph );
  server.on ( "/inline", []() {
    server.send ( 200, "text/plain", "this works as well" );
  } );
  server.onNotFound ( handleNotFound );
  server.begin();
  Serial.println ( "HTTP server started" );
}

void printFlush(const char* str) {
  Serial.println(str);
  Serial.flush();
}



void loop ( void ) {
  static long countdown = 0l;
  unsigned long before = millis();
  server.handleClient();
  unsigned long after = millis();

  printFlush(String(after - before).c_str());
  if (after - before > 100) {
    // start countdown
    countdown = 30;
  }

  if (countdown > 0) {
    countdown--;
    if (countdown == 1) {
      delay(100000);// stop so we can capture the surrounding output.
    }
  }

}

void drawGraph() {
  printFlush("drawGraph() BEGIN");
  String out = "";
  char temp[100];
  out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"400\" height=\"150\">\n";
  out += "<rect width=\"400\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n";
  out += "<g stroke=\"black\">\n";
  int y = rand() % 130;
  for (int x = 10; x < 390; x += 10) {
    int y2 = rand() % 130;
    sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2);
    out += temp;
    y = y2;
  }
  out += "</g>\n</svg>\n";

  server.send ( 200, "image/svg+xml", out);
  printFlush("drawGraph() END");

}


Some sample output from runs
0
0
3077
handleRoot() BEGIN
handleRoot() END
87
0
0
--------------------------

0
0
3066
0
0

Note that the request itself is handled normally (87ms in the first case above ) but the delay occurs randomly during server.handleClient() when there is no request going on.

Anyone got any ideas about this ?

If someone else could try this and report if they get the same result that would be great to see if its just my board or in fact a bug somewhere.

thanks
lynton
User avatar
By lynton
#42461 ok added a
Serial.setDebugOutput(true) which turns back on Wifi debugging after a Serial.begin() turns it off ...

and found the following occurring during the delays :

0
0
0
scandone
no TSHIRT found, reconnect after 1s
reconnect
f 0, scandone
no TSHIRT found, reconnect after 1s
reconnect
f -180, scandone
no TSHIRT found, reconnect after 1s
reconnect
f 0, scandone ... etc



So it looks like the webserver is unable to find the AP for a period of time ( Although the AP does stays connected to the laptop fine ) causing these extended delays.


Armed with this knowledge I found this on an expressif forum here
http://bbs.espressif.com/viewtopic.php?t=671#p2531

Yes,it may happen. This is the limitation that even though ESP8266 can work in station+softAP mode, it actually only has one hardware channel.

So when ESP8266 station is trying to find a target AP, it will scan on every channel, that means ESP8266 station is changing its channel to scan. This makes the channel of ESP8266 softAP is keep changing too..

So the connection may break..

You can set a timer to disable the reconnect of ESP8266 station, call wifi_station_disconnect it will stop connecting. Or call wifi_station_set_reconnect_policy and wifi_station_set_auto_connect to disable reconnect.


I don’t know how to achieve this wifi_station_set_reconnect_policy on arduino as these methods do not seem to exist, (unless they have different names)

So it looks like although you can theoretically use AP and STATION Mode together that practically you cannot :(

Has anyone been successfully able to use AP + STATION in the same sketch on arduino platform ?

Does anyone know of any arduino platform methods of stopping the STATION mode rescanning and causing this connection loss ?

Anyone got any ideas to work around this ?

What I am trying to do is create a web interface on the ESP8266 that can be connected to from any phone / browser from any where. As I do not have a fixed network address to communicate with I need the ESP8266 to function as an AP as well as a WEBSERVER ( or is there a better way ?) .

This looked like a great solution to replacing a bluetooth interface , but its unworkable as it is now.

thanks for any ideas or suggestions

lynton