ESP8266 Webserver Project

Moderator: Sprite_tm

User avatar
By Wouter
#68844 Hi,

Hope I'm in the correct section - feel free to move my thread if not.

I'm trying to implement the ESP8266WebServer to have it server pages. It works fine as long as everything is in my core script, but not when it's moved into the external library (where, for my project, this functionality belongs). When I do that it serves one and only one page, after that Firefox says "Firefox can’t establish a connection to the server" and that's it, until I reset the thing. In the meantime it does however respond to pings.

As my code is spread out over classes and way too big to post here all (and I don't have git set up yet), hereby the relevant snippets. Setup is not complete (it adds code for OTA and connecting to the WiFi, all of which works great). Function loop is complete, this is all that's in there.

Other notable behaviour of the ESP8266WebServer is that the very first request takes about 5 seconds to be served, while any subsequent requests come instantly. Connecting my PC and the ESP8266 to the same WiFi router (the PC has a wired connection).

.ino script:
Code: Select allvoid setup() {
  master.begin();
}

void loop() {
  master.execute();
  ArduinoOTA.handle();
}


from master.cpp:

Code: Select allvoid HydroMonitorMaster::begin() {

  Serial.print(F("Size (in bytes) of settings: "));
  Serial.println(String(sizeof(settings)));

  // Initialise the EEPROM storage.
  EEPROM.begin(EEPROM_SIZE);
 
  // Start by trying to read the existing settings from flash storage.
  readSettings();

  // Set up the http request handlers.
  server.on("/", std::bind(&HydroMonitorMaster::handleRoot, this));
  server.begin();

  yield();
  return;
}


void HydroMonitorMaster::execute() {
 
  // Check for incoming connections.
  server.handleClient();
}


from master.h:
Code: Select allpublic:
    void execute(void);

private:
    void handleRoot();


Now when I add the following to master, it works:
Code: Select allvoid HydroMonitorMaster::handleRoot() {
  server.send(200, "text/html", "Request for ROOT received.");
}


But when I want to offload the html response to a second class it fails. The page is loaded the first time I request it, but not any more after that.

Alternative code for master.cpp:
Code: Select allvoid HydroMonitorMaster::handleRoot() {
  network.htmlResponse(server, "test HTML network class response!");
}


This calls a function in HydromonitorNetwork.cpp:

Code: Select allvoid HydroMonitorNetwork::htmlResponse(ESP8266WebServer server, String response) {
  server.send(200, "text/html", response);
  return;
}
User avatar
By philbowles
#68998 I understand your reluctance to post all of the code, but it's very difficult to spot the problem unless you do - have you considered dumping it into e.g. pastebin?

Also I'm not sure exactly what you mean by "when it's moved into the external library" can you elaborate?

I have a project using ESP8266WebServer which is in an Arduino library format and it took me a while to get it working...had problems with scope of the global server variable...how / where / when are you instantiating the server? Where is your webServer's handleClient(); call? etc etc...
User avatar
By Wouter
#69003 I'm trying to get it on Github instead. Some versioning control can come in handy at times. So given some time (too busy with other things the last few days) and it'll be there in all its ugliness.

Haven't been able to really solve the issue, may look at it again later. I've moved the http send/receive functions to the master class for now, at least it works. I've ran into issues with port extender libraries as well and am slowly but surely starting to get a vague idea on how those pointers/dereferences/etc. work. Sooner or later it'll click.