WiFiManager with ESP32 - Stop Hard-coding WiFi Credentials!

Rek XIX
40 followers
6 Video Views·Oct 31, 2022
WiFiManager with ESP32 - Stop Hard-coding WiFi Credentials!
Today in the workshop I'll introduce you to Wi-Fi Manager.
You'll see how Wi-Fi Manager makes it possible to create ESP32 projects without hard-coding
your Wi-Fi connection information.
We'll also look at how you can capture other configuration settings using Wi-Fi Manager.
We're making some great connections today, so welcome to the workshop.
Well hello and welcome to the workshop and today we're working with our old friend the
ESP32.
Now if you're not familiar with the ESP32, I have done a video and an article on it and
I suggest you check those out before we proceed.
But assuming you are, then you already know that the ESP32 is a pretty amazing microcontroller.
It's got a wealth of GPIO ports, it's got 12-bit analog-to-digital converters, it's
got a digital-to-analog converter, it does I2C, it does I2S, it does SPI, it has UARTs,
it even has built-in touch switch capability.
But the thing about the ESP32 that most people focus on is its connectivity options because
it has both Bluetooth and Wi-Fi capabilities.
And it's the Wi-Fi capabilities that we're going to be working with today.
Now when we build an ESP32 project, we can run our Wi-Fi in two different modes.
We can run it in access point mode, and in that case the ESP32 provides its own Wi-Fi,
so other devices can connect to it.
Or we can run it in station mode, and in station mode the ESP32 becomes a device that connects
to your own Wi-Fi.
And when we run it in station mode, we need to connect to that Wi-Fi and provide some
credentials.
And we usually do this by giving it an SSID, which is your network name, and a password
inside our code.
And so we hard-code these credentials.
Now hard-coding the credentials is fine when we're experimenting, but it is a bit problematic
if we want to use the device for something else.
For example, if we want to take the device and use it on another Wi-Fi network, we have
to go back into our code and recompile it with the details for that network.
So if I want to give the device to one of my friends, or use it in an office, I've got
to change it around.
It would be even more problematic if you tried to make a commercial device, because of course
you wouldn't know people's Wi-Fi credentials, and you can't expect them to tell you that.
And so we need another way of getting on to a Wi-Fi network.
Now the art of doing this is called Wi-Fi provisioning, and there actually are a number
of different ways of doing it.
I'll be covering that in a moment, but the thing that we're going to cover today is something
called Wi-Fi Manager.
And with Wi-Fi Manager, you can provide these credentials externally from the ESP32 so you
don't need to encode them into your code, so you could build a commercial product, or
a product that you could reuse on another Wi-Fi network.
So let's go and take a look at the features of Wi-Fi Manager and of provisioning.
Wi-Fi Manager is a library for the ESP8266 and the ESP32.
The purpose of the Wi-Fi Manager library is to handle what is called Wi-Fi provisioning.
The library can be easily used with existing ESP32 sketches.
It eliminates the need to hard code the SSID and password in your sketch.
Wi-Fi Manager can also allow for custom fields in addition to SSID and password information.
Now Wi-Fi provisioning describes the method of attaching an ESP32 or any other device
to a Wi-Fi network.
This is used by commercial IoT and computer devices.
Wi-Fi provisioning provides the Wi-Fi SSID and password to your ESP32, eliminating the
need to hard code it in your sketch.
Now some provisioning methods require an external device.
There's also a provisioning method that requires a compatible router.
We'll take a look at three different methods of Wi-Fi provisioning.
SmartConfig, WPS, and Local AP.
SmartConfig was originally developed by Texas Instruments.
With SmartConfig you require an external iOS or Android application running the SmartConfig software.
This application will hold all of your Wi-Fi credentials.
The application will then broadcast the encoded credentials to the ESP32.
Once the ESP32 successfully connects to the network, it will return a message to the application
to indicate the process is complete.
WPS or Wi-Fi Protected Setup was originally called Wi-Fi Simple Config.
WPS requires a router that has WPS capabilities.
It was developed in 2006 and the original method could use either a push button or a
pin number.
In 2011, a major security flaw was discovered with the pin method, so most manufacturers
these days just use the button.
To use WPS, the user first presses a WPS button on their router.
After that, they need to press a button that's been connected to the ESP32, and they need
to do this within 60 seconds.
Assuming they do, the router will connect without the need to exchange any additional
security information.
With the local AP or access point method, the ESP32 was initially set up as a Wi-Fi
access point.
It provides unsecured access to a page at 192.168.4.1.
The webpage delivered at 192.168.4.1 will contain the configuration screens.
The user will then enter the configuration information such as the Wi-Fi SSID, password,
and any additional configuration information that your application requires.
Once this information has been received by the ESP32, it will reconfigure itself as a
Wi-Fi client using that information.
This is the method of provisioning that we'll be using today with Wi-Fi Manager.
Now here's a chart comparing the three types of Wi-Fi provisioning systems.
They each have their own advantages and disadvantages.
Both Smart Config and Local Access Point require an additional device such as a phone or a
tablet.
You can also use a computer as a device with Local Access Point, but you can't do that
with Smart Config.
With WPS, however, you need to be in proximity to your router or have someone else that you're
in communications with who's in proximity to the router, as you need to do it all within
60 seconds.
With Smart Config, you need an external application, the Smart Config app that you run on your
iPhone or Android phone.
WPS requires additional hardware in the form of a push button to be added to your ESP32.
No additional hardware is required for the other two methods.
WPS also requires a special router that is WPS capable, and not all routers are.
Very few mesh network systems, for example, are WPS capable.
With the Access Point method used by Wi-Fi Manager, the ESP32 will create a Wi-Fi access
point with its own SSID, and you'll need to log into this.
Optionally, you can choose to create one that doesn't require a password.
Once you've logged into the Access Point, your device will show the Wi-Fi Manager network,
and you'll have to hit Configure Wi-Fi in order to configure the parameters for the
ESP32 to connect to the Wi-Fi network.
You'll enter those parameters into your application, and those will be passed back to the ESP32.
Assuming you entered the correct parameters, the Access Point will disappear, and the ESP32
will configure itself as a Wi-Fi client, and will be connected to your network.
The webpage at 192.168.4.1 is created by the Wi-Fi Manager library, so you don't need
to write any HTML.
You configure your Access Point SSID and password inside your sketch, and you can choose to
optionally not use a password if you wish.
You can also add additional custom fields to the Wi-Fi Manager screen, in addition to
the SSID and password.
For example, if you need an API key, this could be very useful.
Another feature of Wi-Fi Manager is that it also allows over-the-air update of your ESP32 code.
So let's grab a few ESP32 devices, and start working with Wi-Fi Manager.
Now for today's experiments, you can use pretty well any ESP32 module, but I wanted to quickly
show you the two that I'm going to be using today, because I haven't used either of these
on a previous DroneBot Workshop video, so I just wanted to show them off to you.
Now this first one over here is the ESP32 DevKit board, and this is a really neat board.
In many respects, this is a lot like an Arduino Uno, in that it has a series of female connectors
on it.
And on the bottom over here, it's actually got a few pads, because this has been made
for experimenting.
And so you can just put your jumper wires, if you need them, into the female connectors
over here, as you would with an Uno, and of course it's already got a USB port on it.
And so it's a very cool little way to experiment with the ESP32, and a lot easier in some respects
than using an ESP32 on a solidless breadboard, where because of the size of it, the ESP32
takes up a lot of space.
And the other board I'm using over here is called the ESP-I, and it's this one over here,
and as you can see, it has a small camera on it, and in many respects, this is like
the ESP32-Cam.
In fact, we can use the same software that we use in the ESP32-Cam.
It does have a few features above the ESP32-Cam, though.
It has a built-in USB, which the ESP32-Cam does have, but it also has a switch connected
to one of the I-O ports, a push-button switch.
It's also lacking in a few features, though.
It does not have the microSD card that the ESP32-Cam has, nor does it have access to
any of the GPIO ports, so it's pretty well a self-contained board.
One other feature it does have, though, is a built-in MEMS microphone, and we'll be using
this board in future projects, but I'm going to use it today just because it's a little
easier to use than the ESP32-Cam because of its built-in USB port.
Well, there you go, the two boards that we're going to be using in today's experiments with
Wi-Fi Manager.
So now that we know how Wi-Fi Manager works, it's time to start working with it.
So grab an ESP32 module.
I'm going to use the ESP32-DevKit that I showed you earlier, but any module will do.
Get your Arduino IDE all fired up, which I assume you've already configured for the ESP32,
and I'll show you how we can install the Wi-Fi Manager library and start using it.
Now before we can start using the Wi-Fi Manager with the ESP32, we'll need to, of course,
install the library, and I'm in the Arduino IDE 1.8, the classic Arduino IDE.
Now you need to be using at least version 1.8.9.
I'm using 1.8.13.
You can also, of course, use the new Arduino IDE 2.0.
Regardless of which one you're using, you'll have to have your IDE already set up to use
the ESP32 by having installed the ESP32 Boards Manager, and I won't show you how to do that
because I'm assuming you already have.
If you have not, I've got a video and an article that'll tell you exactly what you need to
do to get the ESP32 running on your Arduino IDE.
Now that we have the IDE open, we're going to go into the Library Manager, and we're
going to filter our search by Wi-Fi Manager, all one word, and we'll get a number of results.
There are a number of different Wi-Fi Manager libraries, but one we're looking for is down
in the W's and is actually just called Wi-Fi Manager, and here it is over here, and it's
version 2.0.9 beta at the moment.
I'm going to click the Install button to install that library, and it installs pretty quickly,
and then we can go and confirm our installation by going down into our examples and going
to the very, very bottom because it is a W, and there it is, Wi-Fi Manager, with a number
of different examples.
We're going to open the basic example, and we're going to use that to learn how to use
the Wi-Fi Manager.
Now the basic sketch is the basic way of running the Wi-Fi Manager, and as you'll see, it's
actually pretty simple to use.
Now, of course, we're going to start off by including the Wi-Fi Manager library, and then
we'll go into Setup, and in Setup, we explicitly set our ESP32 mode to Station mode.
Now that may seem counterintuitive because, of course, we want to go into Access Point
mode for Wi-Fi Manager to initially work, but this is the mode we want our ESP32 to
end up in, so we set that explicitly.
Then we'll go and we'll start a serial monitor, so we do that here, and we create an object
to represent our Wi-Fi Manager, which we're calling WM, but you could call anything that
you want.
Now, here's a line over here that I'm going to uncomment, and it's Reset Settings, and
this is something you'll do when you're developing and playing around with Wi-Fi Manager because
if you have already installed an IP address and a network onto your ESP32, it's going
to retain that, so when you're experimenting, Wi-Fi Manager will only work the first time
if you don't take this off.
In production, you probably wouldn't use Reset Settings, but this will set everything back
to zero.
And then over here, this is basically how we connect, and we have a number of different
ways to do an auto-connect, and the most basic way is just to call the Auto-Connect property,
and that will generate a Wi-Fi access point with a random name.
The name comes from the chip ID, so I wouldn't really recommend using that.
Then you can do Auto-Connect and provide a name for your access point, and they're calling
it Auto-Connect AP.
Now this will be without any password protection on that network.
Now remember, this access point network is just for setting up the Wi-Fi, so you may
indeed want to do that and forget about the need for having a password, but the way they've
shown over here and the way that we're going to use is to Auto-Connect, pass it the name
that we want, and you can change that access point name to anything that you want, and
then the password we're going to use, and in this case it's a very basic one, just password.
And then we take a look to see if this object has actually been created, and if it is, we've
connected.
If it has not been created, we'll just print to the serial monitor that we failed to connect.
Otherwise, we're going to print connected ye, and I think they meant yay, but we're
going to say ye over here.
And so I'm going to load that up to my ESP32, remember I've uncommented this line to do
reset settings, so I've got my ESP32 connected, I'm uploading right now.
And so we've uploaded, so I'm going to open up my serial monitor right now, and you won't
see anything initially on the serial monitor, and what we have to do next is go over to
our device and look for our new Wi-Fi network.
And here it is, Auto-Connect AP, so we're going to click on that, and it requires a
password, remember what that is.
And here we have our Wi-Fi manager screen, and as you can see, we've got a button for
configuring the Wi-Fi, an info button, an exit, and an update, where you can actually
update over the air the ESP32, which is a pretty cool feature.
What we're going to do right now is configure the Wi-Fi, and you'll notice that we have
some information now on our serial monitor.
So I hit configure Wi-Fi, and there seems to be a delay, but what it's doing is it's
scanning for all the available Wi-Fi networks, and you'll see up on the serial monitor, it's
printed basically what it has printed on the screen, and it's a very nice display, it gives
me the signal strength and everything of all the different Wi-Fi networks.
So I'm going to choose this top one, that's my network, and I'll have to give it a password.
And I was successful, and you can see up on my screen right now on the serial monitor,
I was successful, I got an IP address, here's my IP address over here, and right now the
AutoConnect AP has disappeared off of my phone now because the access point has gone away,
the ESP is now connected as a station on my Wi-Fi network.
Now the Wi-Fi manager will run only when there's no Wi-Fi information available.
As we saw in the previous code, you can use the reset settings method to make it run every
time the ESP32 is started, because this will wipe the available Wi-Fi information.
We can also wire up a switch to make it run on demand. This can be useful when you want
to change your Wi-Fi credentials. Wiring up our switch is pretty easy, we'll
of course require an ESP32, any ESP32 will do, but I'm showing the development kit.
And we'll need a push button switch, a normally open momentary contact push button.
We'll connect GPIO0 to one side of the switch. You could use a different GPIO pin if you
wish, but you'll need to change the code. And the other side of the switch will connect
to one of the ESP32 grounds. And that's all there is to wiring up an on-demand configuration.
Now let's go and see how we code for it. Now the sketch to make our switch work is
very simple because it's one of the example sketches that comes with the Wi-Fi manager
library. And it's called on-demand config portal.
And you can get to it through the examples, just go into examples, go to the bottom of
examples for custom libraries where you see Wi-Fi manager.
And then go over to on-demand, and you'll see on-demand config portal, and I've already
got it open up in my Arduino IDE. And it's a pretty simple sketch, it starts
off of course by including the Wi-Fi manager library.
And then we define the pin that we attach the switch to, we've attached a GPIO0. If
you wish to use a different pin, you could just change the number here.
We're also defining an integer for a timeout, and that's the period that the portal is going
to be open for, because we don't want to accidentally push the button and have it in the portal
mode forever. Now in the setup, we'll set the Wi-Fi up as
a station to make it explicitly a station. That's something that we've seen before in
the previous code. And we'll also set up our serial monitor
and print to the serial monitor, and define the trigger pin, the pin from the push button
as an input, and we'll use the internal pull-ups in the ESP32 so that we didn't need to bother
wiring a pull-up resistor ourselves. So this input will be pulled high unless the
switch is pressed, and then it will go low. And then we go into the loop, because we're
going to do everything in the loop this time, and we take a look to see if that trigger
pin is low. And if the trigger pin is low, then someone
has pushed a button. We define a Wi-Fi manager object, and this
is the line that you can blank out or use for reset settings.
If you remember for testing purposes, we usually uncomment that so that it takes the configuration
data we have and wipes it every time so we can start again.
But in production, you wouldn't use that. But I'm going to take that out right now,
because, of course, we are in testing mode right now.
We'll set the timeout over here. So remember, we had a value called timeout,
which we set to 120. We'll just use a property called setConfigPortalTimeout,
and we can set that up. And then we go through basically the same
thing we did before. We're going to set up an access point called
onDemandAP. Notice in this example, they don't use a password
for it. So this is not going to be a password-protected
network. We just need to go and connect to it with our device, and we won't have to input
a password, which is probably a good idea if we're only going to let it run for 120 seconds.
If we fail, we print a failure message. We delay, and then we restart the ESP32.
But if we're successful, they print the usual, you're connected, ye message,
and that's basically it. So we're basically doing the same thing
that we did in the previous sketch, except now we're waiting for a button to trigger it.
So let's load this up to an ESP32 and check it out.
Okay, so let's demonstrate our on-demand Wi-Fi portal.
Now, if you take a look at the screen on my phone, I do not have my Wi-Fi portal up right now,
but I've got over here, of course, my ESP32 wired to a push button.
And so if I press the button, and we wait a moment, and you'll see that we've
got some data on our serial monitor, and there's on-demand APs now come up on my phone.
And so I'll click onto that, and I get my Wi-Fi manager screen.
So I was able to bring up Wi-Fi manager just with the push of a button.
Now, we're going to start timing out over here. You see there was a line already on
the serial monitor that said portal timeout in 120 seconds.
So we're going to wait the actual two minutes to timeout.
Of course, I'm going to fast forward this video for you,
so you don't really have to sit here for two minutes and twiddle your thumbs.
But we'll just see what happens after everything times out.
And now we've timed out, and if I go back into my network settings over here,
on-demand AP has just disappeared right now.
And of course, I can bring it back again just with the push of a button, and there it is.
And so as you can see, this can make it very convenient if you need to bring up the Wi-Fi
manager because you want to change some other configuration settings or because you want to
join a different network. And so it's very easy to do.
All you needed was a push button wired to your ESP32 module.
So now we've seen a few demonstration sketches that showed how the Wi-Fi manager works.
I think the next thing we want to do is modify one of our own sketches to use Wi-Fi manager.
And the sketch I've chosen to do this with
is one that should be familiar to anybody who has used the ESP32 cam before.
And that's the camera sketch. Now the camera sketch is one of the example sketches that
gets installed when you install the ESP32 board into your Arduino IDE.
And it's a pretty sophisticated sketch that gives you full control over the ESP32 cam.
But like any other ESP32 sketch, it requires Wi-Fi credentials.
And in this sketch, you would normally just put your Wi-Fi credentials
into two different lines on the code.
Instead, we're going to modify the sketch to use Wi-Fi manager.
Now I'm going to be using the ESP-I for this demonstration because,
as I said, it's a little easier to work with in the ESP32 cam board.
But you could use the ESP32 cam board.
Just remember for that board, you would need an FTDI adapter.
And you also need a jumper that goes from pin zero to ground while you're uploading.
But other than that, it'll work identically.
So let's go and modify the camera sketch to use Wi-Fi manager.
So I've connected my ESP-I board to the computer right now.
And I've opened up the camera web server sketch.
Now, if you've ever used an ESP32 that is camera equipped, such as the ESP32 cam,
you're probably very familiar with this sketch because it's the one we all load onto our cameras
so that we can have a web page that gives us a complete control over the camera.
Now, what I want to do is show you the changes I need to make to this common sketch
in order to use Wi-Fi manager.
But first, let's just review the changes we would need to make under normal circumstances.
Now, the only thing you normally need to do when you set up this sketch first
is to define your camera model.
And it comes by default with this one, the WR Rover.
And that's the correct one for a lot of applications.
The AI thinker is the one that you would normally use if you used an ESP32 cam.
And I'm going to be using an ESP-I.
So I'm going to change that line in mine.
But of course, that has nothing to do with Wi-Fi manager.
That's just how you set the sketch up.
The only other thing you need to do in this sketch is provide your SSID and your password.
But of course, we want to use Wi-Fi manager to alleviate the need of doing that.
So let's go and see what I did to change this.
Now, I created a copy of this.
And we're going to compare them side by side to see what the changes are.
So the first change, I think, is relatively obvious.
I need to include the Wi-Fi manager library.
So I just included the Wi-Fi manager library.
And then I ignored everything else, including the SSID and password,
and went all the way down over here to where it says Wi-Fi begin,
and it passes the SSID and password value.
So in my sketch, of course, I changed for the camera.
I'm using the ESP-I.
And then down over at that same location,
I remarked out this line because we're no longer using it.
Instead, we're going to use Wi-Fi manager.
So I did Wi-Fi manager and created an object for Wi-Fi manager called WM.
I did the reset settings in mind.
Now, as I explained earlier, you wouldn't do this in a production thing.
If you're doing this as a final thing, don't have that line in there.
But what this will do is it will reset the settings.
So it has to be forced to open up the Wi-Fi manager on your mobile device
or on your computer or whatever you're controlling it with
so that you can enter in your network credentials.
But if you don't need to do that, and under normal circumstances,
you won't take that line out.
So that after the first time it runs, it just remembers those credentials.
And then the only other thing I had to do was put this in over here.
And basically, it's the same as we saw in the example sketch before.
I'm doing an auto connect.
I'm passing it over the name of the network,
and I'm calling mine and giving it an SSID of my camera.
And then keeping the password as password.
And that's basically it.
If it fails, we'll print up to the serial monitor.
But otherwise, if it connects, it should define everything that the sketch needs,
the login credentials for the network, and the sketch will just work.
So I'm going to upload that now to my ESP-I, and we'll watch it in action.
So I have uploaded everything to my ESP32-I, and I'm on my phone.
And you can see I've got a network here with an SSID of my camera.
So I'm going to click on that.
I have to give it a password, which is password.
And I've got the configure Wi-Fi screen over here.
We're going to go configure my Wi-Fi.
And if you look again on my serial monitor, you can see the Wi-Fi scan has started.
And it gives me my Wi-Fi network.
So I'm going to pick my own network and give it its password.
And we'll save it.
And I'll observe our serial monitor.
And we've got an IP address.
And as you can see, it says here the camera is ready.
We can go to HTTP, this address here, open up a web browser.
And there we have our camera.
And if we go down here, we can start the stream.
And there it is.
And there's my smiling face right now.
And so this works as it normally would, except I didn't have to provide a static IP address.
Instead, what I used was the Wi-Fi manager in order to do that.
And so I think that's a pretty neat thing to do and very useful with an ESP32 camera.
Now, as you've already seen, Wi-Fi manager is great for inputting your Wi-Fi credentials.
But sometimes when you're setting up an ESP32 project,
you have other configuration information you want to pass to it as well.
For example, you might need to pass an API key to it.
Or you might want to set up a parameter,
like a sensor that you want to set to read temperature in either Fahrenheit or Celsius.
Well, Wi-Fi manager allows you to modify the screen that you're seeing
in order to add additional fields to capture those additional configuration parameters.
So let's go ahead and see how we would do something like that.
Now, here's the code that we're going to use to add an extra element
to our Wi-Fi manager page.
And we're just going to add a text box to the page.
So we start off, of course, by including the Wi-Fi manager library.
We always need to do that.
And then we'll jump into our setup.
We'll set up our serial monitor.
And we'll create an object.
And we're going to call the object WFM.
This is our Wi-Fi manager object.
Then this command over here suppresses the debug information.
Now, you remember all that information that was being printed to the serial port
when we used Wi-Fi manager before it printed out the names of all of the networks that
were available to me and their signal strength, plus a number of other pieces of information.
Well, we could suppress that if we want to.
Now, you don't need to do this.
But I just thought I'd demonstrate it to you.
So this is set debug output.
And I'm setting it to false.
You can also set it to true, which is the default.
Or you can set it to different levels of debug.
Now, this is the command I showed you earlier that we're only using during testing.
You wouldn't use it in production.
But it removes any of the previous network settings.
So it'll boot up and take away the connection that it previously had to the network.
If you don't have this, it'll just retain and try that connection.
And if it succeeds, it'll never bring up the Wi-Fi manager console,
which is what we want to do during testing.
But of course, in a production system, you wouldn't use this command.
Now, we come to what we're trying to do.
We're going to define a text box.
We're just going to define one text box.
And we'll use the Wi-Fi manager parameter command.
And we give it the following parameters.
First of all, the name of our text box.
And I'm calling mine custom underscore text underscore box.
You can give it any name you want.
And then the parameters of that text box.
Now, the first one, my underscore text, is the name of the text box for HTML.
So it's its HTML name.
Every element on an HTML page needs a distinct name.
So if you're building more than one text box,
you'll have to give this a unique name for each one.
And it can be anything that you want.
Enter your string here, which is the next parameter,
is the text that's going to print above the text box.
So it describes what the purpose of the text box is.
So you could write enter your API key or whatever it is you're using your text box for.
Default string is the default data that is going to be printed in the text box.
And if people don't overwrite it, that'll be taken as the value of the text box.
So that'll just be sitting in the text box.
And we'll need to overwrite that to put our own data in.
And the 50 indicates the maximum number of characters
that we're allowing in our text box.
I'm allowing 50 characters over here.
So once we've defined this, we need to add it.
There are two separate steps.
So the next line, we do that.
We do our object.
We use an add parameter method.
And then we add the custom text box.
So custom text box is the name that we give it up here.
And again, if you're going to add more than one of them,
you would just repeat this.
But you'd have a unique name for this, a unique name for this.
And then you'd add the parameter with that unique name.
And then after this is basically the same stuff that we've seen before.
We're going to auto connect.
We're going to build a Wi-Fi access point.
And this is the SSID.
We're going to give it over here.
And then we're going to give it a password of password.
Now again, if you leave out the password and just send this over here,
you will build an access point that doesn't require a password,
which may be what you want to do.
Now if we happen to fail, we're going to print failed to connect.
And we're hitting a timeout.
We're going to do a restart of the entire ESP.
And then we're going to delay everything for about a second.
If we do connect, however, we're going to go and print Wi-Fi connected.
And then we'll print IP address.
And this is how we get our local IP address.
Because we want some stuff up on our serial monitor.
And then we're going to print the value that we entered into our custom text box
just to make certain that it worked.
And we can do that over here.
And we'll get the value for that text box.
And that's the end of the setup where all of our code runs.
There's nothing here in the loop.
So let's load that up to our ESP32 development board and see how it works.
Okay, so I'm running this right now.
And as you can see, I've got my ESP32 test underscore AP wireless source.
And I'm going to connect to that.
I need to give it its password.
And here's our Wi-Fi manager.
And so now let's go over to configure Wi-Fi.
And as you see, I've got the same screen I had before.
Except now I've got an addition.
The thing that says enter your string over here.
And it's got default string printed in it.
Because that's what my default string was.
So let's give it some data here.
Let's first of all select the network I'm going to use and give it my password.
And now let's go and type a default string over here.
Let's over type that with something.
And we'll save that.
And so now if you take a look at my serial monitor.
It says that I've got the Wi-Fi connected.
I've got an IP address.
And my custom text box entry is hello world.
So we successfully not only logged on to our network.
We've also sent back some data from a custom text box using the Wi-Fi manager.
So you just saw how we could take extra text fields on the Wi-Fi manager screen.
And pass the data values from those back to our ESP32.
But actually that's only half the battle.
Because we haven't actually saved those values anywhere.
They'll be able to be used by the ESP32 as long as the ESP32 has been powered up.
But as soon as we power it off, they're going to go away.
And we wouldn't want to have to run our configuration
every single time that we power it back on.
So what we need is a method of storing those data variables on the ESP32.
A non-volatile method.
So that they're available to us when we power it off and power it on again.
Now the ESP32 comes with non-volatile memory.
It comes with non-volatile flash memory.
And we can use that memory in order to save those variables.
Now we're going to use something called SPFs.
Now SPFs is a file system that looks a lot like working with a SD or a micro SD card.
And we're going to use that in combination with something called JSON.
Which is a file format that we can use to save data.
So let's learn a bit about SPFs and JSON.
And then see how we can use them to save the data
that we've captured with our Wi-Fi manager screen.
JSON is an abbreviation for JavaScript Object Notation.
JSON is a lightweight data interchange format.
The beauty of JSON is it's very simple for both humans and machines to read.
It's considered to be a language independent text format.
So it's used to exchange data between different computers
running different software and different operating systems.
Now the format of JSON is very simple to understand.
As you can see it consists of a number of text entries.
And these text entries are enclosed within brackets.
And groups of brackets are enclosed within square brackets.
The text entries consist of name value pairs separated by commas.
SPFs is the SPI Flash File System.
This is a file system used for SPI NOR gate flash memory in embedded systems like the ESP32.
When a SPFs file system is formatted or erased it sets all of the bits to a one.
During writing process only the zeros are written.
So the ones that need to be zeros are pulled down to zero.
The only time a zero is pulled to a one is when it is erased.
SPFs performs a where leveling function to extend the life of the NOR gate flash memory.
On the ESP32 approximately one and a half megabytes of memory
are reserved for the SPFs file system.
So you can store a fair bit of configuration data.
So now let's see how we can use both JSON and SPFs
to store configuration data from the Wi-Fi manager in our ESP32.
Now here's a sketch that we're going to use to create a couple of text boxes
and save the values in them so that we can use them later on in our program.
And that they'll still stay there even after the power has been turned off.
And we're going to create two text boxes.
One that holds text and one that holds some numbers.
Now a number of the functions in this sketch have been taken from a sketch
written by a fellow named Brian Lau who's got all of his work up on GitHub.
And there's some really cool stuff over here.
If we just take a peek at it.
He's got a number of sketches for the Wi-Fi manager.
And including some fancy ones where he creates different HTML elements
like drop downs and check boxes.
And even one that incorporates JavaScript for a more kind of an Ajax feel to everything.
So he's done some great work.
You're going to want to check that out.
And there's a link not only in the code to his GitHub page,
but also in the resources page on the article that accompanies this video
on the dronebotworkshop.com website.
Now back to our sketch.
In order to run this sketch, we're going to need to add a library to work with JSON.
And we're going to use an Arduino JSON library.
So go into your manage libraries.
And then filter your search by Arduino JSON, one word.
And it's this library over here that I've installed.
It's by someone named Benoit Blanchon.
And it's a library that allows you to work with JSON strings.
And so you're going to need that for this sketch.
So go ahead and install that.
And once you do, we can get back to our sketch.
Now we do include a number of different libraries,
many of which were already included in our IDE.
The Wi-Fi library was.
The FS library, which is the file system library.
And the Spiffs library were both included when you installed the ESP32 board.
So you won't need to install those.
And of course, we've already installed our Wi-Fi manager library.
And we've now installed the Arduino JSON library.
Then we create a config file name.
Because basically, we are working with memory.
But we're working with it in the way that we would work with a SD card.
And so our configuration file needs an actual name.
And this is what the name of it's going to be.
And we assign it to a constant over here, JSON config file.
Then we create a Boolean flag to indicate that we should save the config.
And we'll set it by default to false.
Then we're going to set two variables that hold the data from our custom text boxes.
Now, the first one is a string variable.
And we use a character array for this.
Because that's how we are actually getting data, one letter at a time, so to speak.
And so we call test string.
It's set to a character array of up to 50 elements.
Because that's the number of elements we're going to allow in our box.
And we're going to set it to a default value of test value.
Now, this is the text that's going to appear in the box when you first launch it.
So you could change this to something else if you want,
as long as it doesn't exceed the 50 characters.
We're also going to define test number.
And this is just an integer.
And this represents the number that we're going to put in the second text box.
And again, you can put any number you wish.
Or you could even just leave that blank if you wanted to.
But this will appear in the text box when we launch the application.
We'll define a Wi-Fi manager object.
We're calling WM.
And then we have a number of different functions.
The first function is the one that saves the configuration file
in a JSON format called save config file.
So we'll print to the serial monitor just to let us know that's what's happening.
Then we create a JSON document.
Now, this document is being created in memory.
And we define our JSON document over here.
And it's got two elements to it, the test string and the test number.
And we'll assign the values to that variable that we have already defined over here.
So if we haven't changed the variable, this is going to be the default values.
And then we open the actual config file.
And you are working with memory in the same way you would work with a SD or micro SD card.
So we open a file exactly the same way, except we use SPIFs.
And we use a SPIFs open.
And we pass the name of that config file that we defined earlier.
And we pass it a parameter of W, which means we want to write to the file.
Now, if we didn't get the file open, we have an error.
And we'll print that out to the serial monitor.
But assuming we did, we're going to have to serialize our JSON data to write it to the file.
Because again, we're working with this as we would an SD or a micro SD card.
And they accept serialized data.
So there's the commands we use to serialize it.
And an error message will print if it fails.
And then if that's worked, we'll just close the file.
Now, we have another function over here called load config file.
And this loads the existing configuration file.
And it's a Boolean.
So it gives back a true or a false as to whether we succeeded.
Now, we can uncomment this line if we want.
If we need to format the actual SPF's memory area, you probably won't need to do this.
But if you have problems getting this working with a brand new ESP32,
you might want to run it one time and uncomment this line.
So we're going to read from the file system.
We'll print to the serial monitor to let it know what we're doing.
And then down over here, we'll see if the file system exists.
And if it does exist, we'll print out that we're going to read it.
And again, we read in a similar fashion to the way that we work with a micro SD card,
except we're using SPFs.
And we open the file.
And this time, we pass it an R for read.
And if we get the file, if this config file is indeed populated,
we'll say that we've opened the configuration file.
We'll create a JSON document with the data.
And we've deserialized everything into the JSON file.
And if we don't get an error, we'll say, OK,
we're parsing the JSON.
And we'll copy the test string and the test number from that file.
And then we'll return a true over here.
If we do fail, we'll return a false.
And then we'll go into our next function down over here,
which is save config callback.
Now, this is one of the two callback functions.
And a callback, you can think of a little bit like a software interrupt.
When something happens, these functions will be called.
And so this is happening when we need to save the configuration.
This callback will be called.
And we'll print out to the serial monitor that we should save the configuration.
And all we do is we set a boolean to true.
And there's another callback we're using.
And this is called when the configuration mode is launched, call mode callback.
And we will enter our configuration mode.
We'll print that to the serial monitor, excuse me.
And then we print to the serial monitor the SSID that we got and the IP address.
Because at that point, we have gotten those.
Now, we can go into the setup.
And this is where pretty well most of our code is actually running.
We've got a boolean here called force config,
which would force it into configuration mode every time you run.
Now, it's set to false normally.
But if you're doing some experimenting with this,
you want to do a number of changes, you might want to set that to true.
And we get a boolean back from load config file.
So if we are ready to load the config file and it's true,
we are forcing config mode and we set force config mode to true over here.
We'll set the Wi-Fi mode as we did in our previous sketch.
We'll set up our serial monitor.
And again, the reset settings, as you recall,
will wipe all of the settings you have for network settings from the ESP32.
In normal mode, you would have this commented out.
But as we're in development mode, we don't have it commented out.
And here's where we set up the callbacks.
And as I said, callbacks are very similar to interrupts.
So we do sets.
There's a set save config callback.
And we assign it to the function that we've given.
And another one called set AP callback.
And we set that to the callback function that we have defined earlier.
And here's our two custom elements as we did in the previous sketch
where we created the custom text element.
We have two of them this time.
The first one is very similar to what we did in the last sketch.
Again, we use Wi-Fi manager parameter.
And this first one we're calling custom text box.
And the key text is the HTML value of that.
And then enter your string here is what's going to print above the text box on the display.
And instead of passing a string over here,
we pass test string, which is a string value or a character value actually.
And then we define 50 as being the maximum number of characters we can use.
Now, when we display the one for the number,
first we need to convert the number to a string in order to display it in the text box.
So that's what we do over here.
And then we set up a text box for the number.
And we're calling a custom text box num.
And the HTML name is key num.
We've got our text that we print before the box.
Enter your number here.
Converted value is the value that we got over here from our number.
And we're allowing up to seven numerical characters.
And then we add those defined parameters.
So we have to add parameters this time because we're adding two different boxes.
And then over here is basically what we have seen before to run the Wi-Fi manager.
And if we force config, either if we do or we don't, we still want to run the configuration.
And so we're going to set up a config portal called new test AP.
And we're going to give it a password of password.
We have a message if we fail to connect.
And so we do that whether or not we're forcing a configuration.
If we get this far, we're connected to the Wi-Fi.
And so we print out a number of things to our serial monitor.
We print out that we're connected and we get our IP address.
And now we get the config values.
And we're going to copy these config values and print those ones out to the serial monitor as well.
So now we're going to get the values and print them out over here.
We're going to save all our custom parameters.
So we call the save config file.
And that's basically it for the setup.
And everything runs in setup again.
There's nothing running in the loop.
So there's a lot of code over here.
But if you break it down section by section,
it's really not that difficult to comprehend how to use it.
And how to adapt it to your own application.
And so let's load that up to an ESP32 and take a look at it running.
All right, I've got my ESP32 connected.
I've got this program loaded into it.
The save text demo.
And I can see that I've got a new test AP access point here.
So I'll go into that.
I'll have to give it its password.
And we'll do that.
And we've gotten into the Wi-Fi manager.
And you can see we've got a bit of data up on the serial monitor as well.
I'll hit configure Wi-Fi.
The serial monitor says the Wi-Fi scan sync has started.
It's printing out all my networks.
And there they are.
And here we have my screen.
You'll notice I have my two extra text box at the bottom.
Well, the first one would hold the text value.
The second one a numerical one.
So let's go through the configuration process of the Wi-Fi.
I'll select my network.
Okay, and now we can go and modify the values we have here.
We'll modify test value.
We'll say hello world.
And we'll modify our numerical value.
And then we'll hit save.
And you'll notice my serial monitor has captured the data.
It's now trying to connect to my network.
It's connected.
It has an IP address.
And it prints out the JSON string that we are saving as a configuration.
And it looks like we've got our data.
Now to prove that we've got our data, what I'm going to do is I'm going to disconnect
the ESP32.
So now there's no power on the ESP32 at all.
And of course, my network has disappeared off of the screen here.
And I'll connect it again.
And in a moment, we should have my network back on the screen here.
And there it is, new test AP.
We'll go into it.
We won't need to enter a password now because we've already done that.
Hit configure Wi-Fi.
And as you can see at the bottom of the screen now, I've got hello world and 9876.
So my data has been saved.
Also remember in code, I did not remark out the line to reset the network parameters.
So they're no longer in there.
But if I didn't have that remarked out, if I kept it remarked,
my network name and password would also have been populated right now.
And so I wouldn't need to enter that again.
And I could have gone in and just modified my text strings again.
So this is a pretty neat way of storing extra configuration data on your ESP32
using the Wi-Fi Manager.
Well, that about wraps it up for a look at Wi-Fi Manager.
And I hope that it's illustrated to you that you no longer need to hard code
your Wi-Fi credentials into your ESP32 sketches.
And by the way, Wi-Fi Manager will work equally as well with the ESP32's cousin, the ESP8266.
Now, if you want some more information about Wi-Fi Manager,
if you want to grab the sketches I used, just head over to the DroneBotWorkshop.com website.
And you'll find an article that accompanies this video.
And there's a link to that article right below the video.
While you're on the website, please consider signing up for my newsletter.
It's not a sales letter.
It's certainly not a spam letter.
It's just my way of letting you know what goes on here in the workshop.
And if you want to discuss the Wi-Fi Manager or anything about the ESP32
or microcontrollers or electronics, head over to the DroneBot Workshop forum.
And you'll find a link to the forum below the video as well.
And finally, if you have not subscribed to the YouTube channel, please consider doing that.
All you need to do is hit the subscribe button.
And also, if you click on that little bell notification,
and if you have your notifications enabled on YouTube,
you will get notified every time that I make a new video.
So until we meet again the next time, please take very good care of yourself.
Please stay safe.
And I will see you again very soon here in the DroneBot Workshop.
Goodbye for now.
