ESP8266 Wifi Configuration Part 2
In Part 1 of the Wifi configuration tutorial we learned how to set up the device to read/write from non volatile areas of memory so that we can recall a new wifi ssid and password between reboots. In Part 2 we're going to provide an interface that allows you to set the new values from a web page. This tutorial is a combination of all the previous parts so I'll include links to the relevant parts rather than run through it all again.
The one part we missed from the last tutorial was actually creating a wifi network. We need to include the ESP8266Wifi library and open up a new access point. Do this after loading the new wifi values to ensure the network has the correct name.
The next step is to set up a webserver to provide pages to the user. We're going to create a default index page that's just simple text. For the tutorial this will serve to point the user in the right direction for the admin functions but in a full program this would be whichever page you like. We're also going to set up a static handler to serve the page 'admin.html' from SPIFFS and finally declare a handler to deal with incoming arguments from the admin page.
Once the form is successfully filled out it will POST a series of arguments to the ESP device. The form only needs to contain new ssid or new password information, and it is only input data with a name attribute that gets sent so we should be seeing upto 3 arguments, 'oldpassword', 'newssid' and 'newpassword'. Our handleAdmin() function will be sent the incoming data and it's ready for us to process, lets take a look at that function.
The first thing we do is look at all the arguments and pass them out to the serial port, a handy way to make sure that we're sending the values we think we're sending. The web server library has more specific functions that allow us to check and see if a specific arguments exists before we try to process the data. We can pass the argument name to the web server using the 'hasArgs()' function and check to see if the old password has been supplied. The first test is to make sure that the password matches the one we're currently using, if these don't match we should ignore all the rest of the data.
If the password matches we then need to copy the values from the newssid and newpassword arguments into our own variable. We can check at this point that the arguments are also the right length (and if they're not greater than zero they don't exist at all). We copy the new values over to our config variables and store them to EEPROM, it's as simple as that. It's polite to let the user know if there was success or failure at this point so we construct a short html response string with the pass or fail result, being html we can also add in a browser redirect back to the main page. I have looked at performing an automatic reboot at this point to start using the new values but the Wemos D1 doesn't appear to restart correctly, this is probably something to do with the USB serial device and the arduino programming.
Here is the whole program, the next step would be to roll all of this access point configuration into a single library that could be easily imported into any project.