WiFiManager with ESP32 - Stop Hard-coding WiFi Credentials!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 [Music] 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 i squared s 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 to 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 onto 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 esp-32 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 smart config wps and local ap smart config was originally developed by texas instruments with smart config you require an external ios or android application running the smart config software this application will hold all of your wi-fi credentials the application will then broadcast the encoded credentials to the esp-32 once the esp-32 successfully connects to the network it'll 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 is initially set up as a wi-fi access point it provides unsecured access to a page at 192.168. the web page 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 web page 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 dev kit 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 and a solderless 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 espi 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 esp-32 cam though it has a built-in usb which the esp32 cam does have and 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 micro sd 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 in the esp32 cam because of its built-in usb port so 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 dev kit 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 the 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 counter-intuitive 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 almost to 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 com 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 in 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 named 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 yi and i think they meant yay but we're going to say ye over here and so um 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 to remember what that is and here we have our wi-fi manager screen and as you can see we've got uh a button for configuring the wi-fi an info button and 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 auto connect 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 will of course require an esp32 any esp32 will do what i'm showing the development kit and we'll need a push button switch to 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've attached the switch to we've attached to 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 esp-32 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 set config portal timeout 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 on demand ap notice in this example they don't use a password for it so this is not going to be a password protected uh 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 your connected yi 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 i'm 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 espi 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 needed a jumper that goes from pin 0 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 espi board to the computer right now and i've opened up the camera web server sketch now if you've ever used an esp 32 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 mind but of course that has nothing to do with wi-fi manager that's just how you set the sketchup 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 on my sketch of course i changed for the camera i'm using the espi 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 it 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 uh that's basically it if it fails we'll print up to the serial monitor but otherwise if it connects uh it should define everything that the sketch needs the uh the login credentials for the network and the sketch will just work so i'm going to upload that now to my espi and we'll watch it in action so i have uploaded everything to my esp32i 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 and 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 let's 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 and 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 where it's going to define one text box and we 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 it's 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 it's going to print above the text box so it describes what the purpose of the text box is so you could right 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 they're 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 descend 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 boxes to make certain that it worked and we can do that over here we to custom text box get value 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 esp-32 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 and 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 spiffs now spiffs 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 spiffs and json and then see how we can use them to save the data that we 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 spiffs 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 spiff's file system is formatted or erased it sets all of the bits to a 1. during writing process only the 0s are written so the ones that need to be 0s are pulled down to 0. the only time a 0 is pulled to a one is when it is erased spiffs performs a wear 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 spiff's file system so you can store a fair bit of configuration data so now let's see how we can use both json and spiffs 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's 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 field 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 it's this library over here that i've installed it's by someone named benoit blanchal 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 uh 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 one letter at a time so to speak and so we call it 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 uh 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 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 were 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 we accept we use spiffs and we use a spiffs 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 opened 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 disclose 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 spiff'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 microsd card except we're using spiffs 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 okay we're parsing the json and we'll copy the uh the test string and the test number from that file and uh then we'll return it 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 uh we will enter our configuration mode uh we'll we print that to the serial monitor excuse me and then we printed the serial monitor the ssid that we have 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 want to do a number of changes you might want to set that to true and we get a boolean back from load config files 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 a 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 uh 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 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 keynum 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 two ad 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 these 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 we'll do that we've gotten into the wi-fi manager and you can see we've got a bit of data up in the serial monitor as well i'll hit configure wi-fi the serial monitor says the wi-fi scan sync is 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 we're told the text value the second one a numerical one so let's go through the configuration process through 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 and 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 in 9876 so my data's 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 were marked 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 esp-32'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 [Music] you
Info
Channel: DroneBot Workshop
Views: 417,639
Rating: undefined out of 5
Keywords: esp32, esp32 wifi manager, esp32 wifimanager, wifimanager, esp8266, wifi manager
Id: VnfX9YJbaU8
Channel Id: undefined
Length: 54min 57sec (3297 seconds)
Published: Sun Mar 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.