ct-Open-Source / Basecamp

An Arduino library to ease the use of the ESP32 in IoT projects
GNU General Public License v3.0
254 stars 48 forks source link

Reusable webserver #51

Closed holgerpieta closed 6 years ago

holgerpieta commented 6 years ago

Hey

Quite similar to the idea discussed in Issue 35, I want to use the webserver to display and set values that are used to control sensors and actuators.

Turned out the code was nearly, but not quite there yet. In detail, the only real problem was the hard-coded reboot when the webserver recieved a new configuration. Good for setting WiFi passwords, bad for controlling actuators. So I modified the webserver to not perform a hard-coded rebott and instead optionally execute an external function when a configuration is recieved. Basecamp uses this function to perform said reboot, so everything should be backwards compatible.

To remove the non-needed fields from the webserver, I added a reset() function to the webserver. But I currently think it does not work properly, because the AsyncWebServer would need to be reset at well. But unfortunately that crashes, so there seems to be a bug somewhere in the AsyncWebServer. Anyway, it still can be used as intended: When Wifi is not configured, the default webserver should be started to configure WiFi. When WiFi has been configure and the device has been rebooted, the default webserver should not be started, so there is nothing to be reset and the modified interface can be used by the user.

By default this would lead to frequent reads and writes to the EEPROM for configuration values that are not actually supposed to be kept across reboots. So I also extended the Configuration class to support a memory-only operation mode, where no EEPROM reads and writes are performed.

Example usage for all that can be found here: WifiBlinds

Additionally I noticed that according to the documentation the webserver should first be configured and then started by calling begin(), not the other way around. So I changed that as well.

Cheers, Holger