Having to hard code the WiFi network name and password details in the sketch is something that
is not ideal when you want to create finished projects like a WiFi lamp, clock or something
similar. In this video, we will learn how to use the Auto connect library that allows
us to connect to and manage WiFi access points using a smartphone.
The github repository gives you an overview of how it functions and it also gives you
an example to help you convert existing sketches. In summary here’s what happens.
When the ESP32 board boots, it checks to see if any previous network credentials have been
stored in the FLASH. By default, it will try to connect to them and if successful then
it will print the IP address to the Serial port. Your sketch will then operate as normal.
If it is unable to connect to a WiFi network then it will create an access point that you
can connect to and manage the WiFi credentials.
Managing WiFi networks is also possible when the board is connected to a WiFi network.
You can do this by simply typing the board IP address into the web browser. The board
prints the IP address to the terminal but access to the serial terminal is not always
available and you would then have to check your router to obtain the IP address assigned
to the board. This is still not convenient and so we will assign a hostname to the board
and this way, all you have to do is type in the hostname to manage the wifi settings.
Let's start by installing the required libraries in the Arduino IDE.
We need to install the Autoconnect library. Open up the library manager and type in Autoconnect.
Install the library that shows up. The Autoconnect library needs the pagebuilder library for
it to function so type in pagebuilder and install that as well. Then, download and open
up the sketch for this project. You don’t have to change anything here but if you want
to, then you can assign a different hostname for your board. It’s now time to upload
the sketch. Connect the board using the following diagram, open up the serial terminal and press
the reset button. You will receive a message like this. Hit the upload button and wait
for it to complete. Keep the serial terminal open, remove the boot jumper and press the
reset button.
At this point, either two things will happen. If previous network information was stored
in the flash memory then the board would automatically connect to the network. However, if it was
a new board or if there was no valid information then it would create an access point.
Here we have the board trying to connect to a WiFi network but it cannot do so as it
does not have the credentials stored. However, it will create a WiFi access point called
esp32ap that we can connect to and this normally shows up after about 30 seconds. Connect to
it by using the default password of 12345678. It should automatically redirect you to the
management page or else, you can use the IP address 172.217.28.1 to get to it. The page
gives you some information about the board like the MAC address, memory status and so
on. The menu gives you a number of options: The ability to configure new access points
or networks. View saved SSIDs or networks. Disconnect from the current network. Reset
or restart the board. Change the time zone. And also go to the home page which simply
displays the time.
Tap the configure new AP option. Select the access point from the list and type in the
password. Once done, hit apply and the board should connect to the network and give you
the network details like so. The IP address will also be printed to the serial terminal
along with the host name.
The next time you boot the board, it will automatically connect to a WIFI network and
your sketch will work as expected. This gives us the first scenario.
There are a couple of ways you can access the management page after it has connected
to an existing network. If you know the IP address of the board then you can type that
into the browser and it will take you to the home page. Click the settings icon and you
will reach the management page. Instead of the IP address, you can also type in the host
name and that should take you to the home page. Keep in mind that some browsers will
carry out a google search with the hostname instead of redirecting you to the board. In
this case, simply specify an http connection before the hostname. This should take you
to the home page, which will allow you to get to the management page. Once in the management
page, you can view the board status and do everything as before.
I have not found a simple way to erase the stored SSID details from the flash by using
this management page. Once way to do this is by restoring the board to factory defaults
by using the esptool which can be used on Windows and Mac. Doing this means that you
will have to upload your sketch again. If you’re running Windows then there’s an
easier way for you to do this. You can download and install the ESP32 flash tool. Open it
up, select the ESP32 DownloadTool as we are working with the ESP32-CAM board. Make sure
you have the right settings for your board, and then select the COM port. Put the board
in the download mode by adding the jumper and pressing the reset button. Then, hit the
erase button and it should complete relatively quickly. Hit the reset button again to put
the board into download mode and press the upload button to load the sketch. Once uploaded,
restart the board and you should be back to the state where it does not have any network
details and creates an access point for you to get started.
The flash download tool does not work for a mac so the only option you have is using
the esptool. You will need to install it first, by using the terminal and you can then erase
the flash by specifying the port.
And that’s how you can add the Autoconnect feature to the ESP32 boards. Like I said,
this is so much better than simply having to keep typing your network credentials into
a sketch. If you’ve liked this video, then please consider subscribing. Thank you for
watching and I will see you in the next one.