ESP8266 Storing Wi-Fi Settings In Flash Auto-Switch AP/Station Modes | Arduino (Mac OSX and Windows)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi I'm Cisco with AK robotic and I'm here to share with you another tip for working with your esp8266 microcontroller in another video we saw how the esp8266 can operate in two different modes the first one the most common one is known as station mode and that's when it connects to an existing Wi-Fi network the second one known as access point mode is when it creates its own wireless network so that other devices can connect to it in that video we didn't see how we can switch between the two so the goal for today is to create an application that will allow us to do so and as a step-by-step breakdown the first thing we all want to do is for the esp8266 to create its own wireless network right after booting and once that's done we can connect to the esp8266 and access a web page where we can enter the SSID and password so that it can connect to an existing Wi-Fi network as an optional step we're going to be storing those credentials in flash memory the reason that's an optional step is because when we just try to connect to that SSID and password those settings are going to be already stored in flash memory lastly I'm going to show you how we can retrieve the data and use it to connect to an existing Wi-Fi network for today I'm going to be using the women's d1 mini development board as you probably know already that's one of my favorite development boards for the esp8266 because of its small form factor and the wide availability of shields which allow me to work on projects without doing any wiring I'll leave a link in the description of the video so you can check it out in my little Amazon shop as I usually do I'll go ahead and download the repository for the tip series on my youtube channel I'll leave a link to the repository in the description of the video I'll go ahead and open the web server HTML file so I have some boilerplate code that I can start with I'll go ahead and save it onto my desktop and I'll name it Wi-Fi underscore modes underscore switch I'm going to be using a couple of libraries the first one is the one for the file system where we're going to be storing our Wi-Fi credentials and that's built in to the arduino ide so we can just go ahead and include it the second one is for formatting the data using json as i've done in other videos make sure you go through the sketch menu include library manage libraries search for arduino jason and install it before doing so make sure you select version 5.30 so because we're working with the way most v1 mini will switch the LED pin to be number two i'll go ahead and put in my Wi-Fi network SSID and password I'll also be adding a second SSID which I'll call my SSID and that will correspond to the network that will be created by the esp8266 and I'll call it AI dash esp8266 I'll go ahead and create three variables of type IP address which will allow me to set up the local network that will be set up by the esp8266 first one will be the IP address which we'll use to access it the second one is the gateway to address that I'll be using lastly I'll need the net mass that we'll be using next I'll be working on the web page that will be serving for inputting the network credentials so we'll need a couple of boxes for putting in the SSID and the password and a submit button so the form will not need an action go ahead and delete that and for the text displayed on the button I'll you say save I'll go ahead and make a couple of boxes by using the input tags I'll give each one a label and lastly I'll enclose them inside diff elements just to make the code a little bit nicer a little bit more readable the first box will be for entering the SSID so it'll have an empty value initially I'll give it the ID SSID and a little placeholder text that will read SSID then the label will be for that input tag so I'll use the same ID and I'll just use SSID for the actual label similarly for the password box I will give it the ID password so the label will refer to it it will have an empty value initially and we'll specify that is of type password so that the text that entered is not displayed we'll also have some placeholder text that'll say password and the label will read password as well lastly when the button is clicked I want to call a JavaScript function that will allow me to pass the data that enter to the server running on the esp8266 so I'll use the unclick attribute to call a function that I'll arbitrarily name my function I'll go ahead and define that function using the script tags for debugging purposes I'll use log a message to the console saying that the button was clicked then I'll collect the data that was entered as I mentioned before we'll format it using Jason so we can just write it out in that format and lastly we'll send the data using Ajax as we saw in the video where we combine HTML and JavaScript will be creating a route that will be called settings so I'll set the URL to be exactly that also keep in mind that I could have used WebSockets for this task so I'll leave it up to you to decide which one you want to use once the data is sent I will respond back with an acknowledgement message which I'll just for now print out on the console I can access that message by using the response text attribute of the xhr object one thing that I'll do differently is that instead of using the standard get method I'll use post and the reason you will see a little bit later when I'm creating the server side of the code but I'll send the data a synchronously to the URL settings that we defined before and that requires a combination of both the open method of the class as well as the send method and this is built in JavaScript to ensure that we're formatting the data as Jason that's about it for the HTML and the JavaScript that's going to be served to the client and it's going to be used to collect the data for the SSID and password of the network that we want to connect you now we work on the server side of things and to do that I'm going to make a few changes in the setup function I'll start by initializing the file system object by calling its begin method and instead of attempting to connect to our network in the setup function I'll be defining a function that will handle the switching between the two modes of operation so I'll go ahead and get rid of this and instead of serving this string when we access the root path I'll be sending the web page that we just created and remember that because that's stored in flash memory we need to change the method to send underscore P I'll also define the route where the data is going to be sent which on the forum we named settings so we need to receive it on the server side it's going to be of type post as I mentioned before and it's going to call a function that I'll name handle settings updates and I'll define that function a little bit later the loop function stays the same we just want to monitor any traffic that's sent to the server and the first function that we'll work on is the handle settings update we'll go ahead and get the data that's sent to the server by using the art method of the server object and this is a little bit of trickery it doesn't quite make sense but this is a way of receiving the JSON data that is being passed I'll go ahead and use the dynamic jason buffer class to instantiate an object that I'll name J buffer I'll use that object to parse the data the way that's done is by creating a pointer to the JSON object class and we'll use the first object method of the J buffer object once we've used Arduino JSON library and get the data store in this JSON object instance then we can use the flash file system object to store the data in a text file so I'll create an instance of the file cloud that'll name config file I'll do so by using the open method of the spi ffs object and I'll give it the name config dot jason and because we want to toward the credentials for the Wi-Fi network in it I'll need to give it the W argument for writing data to it I can use the print - method of the JSON object to store the data directly into the file once that's done I'll go ahead and close the file I mentioned before how storing the SSID and the password was an optional step and there were a couple of choices on how to do that one was using the flash file system the other one was using a EEPROM and if you compare the code you can see why I choose to use the flash file system we saw how you seen simply three lines of code we can store the data there and if we compare it with using something like a EEPROM is going through this auto connect credential library we see that we need to keep track of addresses and to be honest I just don't see the point of using something so complex where the goal is to just store data that's available when the device loses power so I choose to always use the flash file system to achieve that purpose so once the data is stored I'll just send back a message to the client saying hey I received the data and I'll use the send method with code 200 and the message itself will be also formatted in Jason I'll simply have a key name status and its value will be okay to give a little bit of time to the server to send that response I'll delay for half a second and after the credentials are stored in flash memory I'll go ahead and run the function that I'll define next call Wi-Fi connect so let's go ahead and define that function where we're going to try to connect to the data that was stored and if that's unsuccessful then we'll create our own wireless network the first thing is that we want to reset all the networking instead of checking in which mode were operating I'll go ahead and simply disconnect from both the access point mode as well as a station mode I'll give it a second delay to make sure that that happens and the first thing I'll do is check whether there is credentials data stored in the config dot JSON file so that I can try to connect to a network so if the file exists I'll create two character pointer arrays to store both the SSID and the password data I'll initialize them to be empty and if the file exists then I'll go ahead and try to open it notice that I'm not being very careful to check whether this methods work correctly so in your application I encourage you to do so the way to get the data out of the configure JSON file is a little bit odd but nothing like working with a EEPROM as I showed you before so bear with me as I write the code that's necessary to do so you can read the documentation if you're interested in finding out more information but I won't go into detail as to what I'm doing once the data is read I'll go ahead and close the file and make use of the arduino json library once again to get the data and try to connect using the credentials that are stored the process is similar to what we did before to receive the data from the client so if that actually is successful then we'll go ahead and extract data from that jason object we can go ahead and first store it into the variables we created switch the mode to station mode and then use the begin method of the Wi-Fi object to try to connect to the network using the credentials that were stored in the file as usual we'll wait around a few seconds to see if the connection actually happened so I'll need a variable called start time that I'll set it to the output of Millie's use the status method of the Wi-Fi object to monitor whether the connection is successful or not we want to give it half a second in between checks and we'll just print something out to the zero monitor and as a visual cue I'll go ahead and toggle the LED on and off just to know that we're actually trying to connect to a network we'll wait for five seconds to see if the connection actually happens you can wait for a little bit longer if you wish and that's about it for using the station mode so now we can check if that was successful then we can just turn off the LED right we want a visual cue to know whether we're connected to a network or were creating our own network using the esp8266 so I'll use the built in LED for status indication it'll be off when we're in station mode successfully connected and I'll be on when it's in AP mode creating its own wireless network so if it's not connected to a network then we want to start the AP mode to do that we set the mode to Wi-Fi ap then we configure that access point with the variables of type IP address that we created in the beginning using the method soft a come fig and we'll start the AP Network using the soft AP method of the Wi-Fi object using the SSID that we defined in the beginning that I needed a I dash esp8266 and I'll use the same password that I'm using for accessing my own Wi-Fi network as we did before we'll use the LED to indicate the mode that the esp8266 is operating in and in this case the LED will be on for indicating that it's operating on their access point mode lastly I'll go ahead and output some data on to the serial monitor and instead of writing a bunch of print statements I'll simply use the print DIAC method of the Wi-Fi object which will tell me the most important data about the network whether I'm under access point or station and we're finally ready to test things out go ahead and connect the wheels or whatever development board you're using to usb go to the Tools menu and make sure that the correct board and the correct port are selected as usual make sure you've seen in my other videos or tutorials around the web how to configure your development board to communicate with your computer so that this step works correctly hit upload after the code is uploaded we can open up the serial monitor and as we would expect the first thing that the esp8266 does is start out in access point mode and the reason for that is because we haven't given it any Wi-Fi credentials so that we can try to connect to our own Wi-Fi network if we go ahead and open the Wi-Fi menu for our computers we should see it pop up under the available nearby Wi-Fi networks we can go ahead and connect to it use whatever password you use wait a few seconds go to your browser and use the IP address that we specified for the AP configuration remember that we want to access the webpage we created which is served at the top root path we see the two input boxes we created as well as the button I'll go ahead and open the developer tools I'm using Google Chrome and the console just to see the messages that are sent back and forth for debugging purposes I'll go ahead and enter the SSID and password of my local Wi-Fi network hit safe and I get that response text that we specified on our web page if we go back to the Arduino IDE we can see that the Wi-Fi connect function that we defined run again in this case because the file was created after clicking the button it went on to station mode and try to use the credentials that we stored in flash memory to connect to a network it successfully did so because I can see with the print dialogue that it's now under station mode and it's using the data that I pass to it you'll also notice that if you check what Wi-Fi network you're connected to is not going to be the ap1 running on the esp8266 the reason is because even though it shows here that wireless network doesn't exist anymore because it's operating on their station mode my computer automatically jumped onto my own Wi-Fi network and if I want to reach the esp8266 once again I'll need its IP address so if I go back to the browser I'll need to change the IP address to the 1.11 which is my own network in your case this might be different but I see that I can access my esp8266 once again and to test things farther I'm actually gonna give it the wrong information so that's an SSID that doesn't exist and I'll give it whatever password it's safe I see that I get the response text back and if I go to the Reno IDE I see that because the file exists and has some data then it was trying to connect to a Wi-Fi network however because that data was bogus it failed after five seconds and started its own wireless network under access point mode so once again if I go through my Wi-Fi menu I'll be able to connect to the AI dash esp8266 so there we have it we've successfully built an application that allows to switch between station mode and access point mode with the esp8266 there's actually a very good library called Wi-Fi Manager I'll leave the link in the description of the video that allows you to do the same things and even more but I like to keep my code simple and adding this to another application is probably the way I would but you have the choice between the two if you liked my videos I invite you to go to my patreon page and chip in a buck or two it really helps me putting more time and make our videos release them quicker but whatever you do don't forget to Like subscribe or leave me a comment I'm also very active on social media so I encourage you to follow me on Twitter Facebook and Instagram and you can ask me questions or suggest what should be the next topic I should do on a video and a lot of people have been using the community tab of the channel so you can jump on there as well and I will see you next time thanks for watching
Info
Channel: ACROBOTIC
Views: 35,728
Rating: undefined out of 5
Keywords: ESP32, ESP8266, Arduino, IDE, NodeMcu, Tutorials, Makers, Learn, Guide, IoT, Internet Of Things, Raspberry Pi, Wireless, Communication, Weather, Creative, Sensor, Python, Programming, C++, Beginner, Electronics, DIY, MicroPython, 3d Printing, CAD, Fusion 360, Education, Tutorial, Intro, Introduction, Web, Developer, HTML, Datalogger, Project, Sensors, Data, Visualization, Webserver, Server, Client, App, WebApp, Wemos, Robot, Robotics, Robots, Easy, Adafruit, Sparkfun, OS X, Windows, Linux, Embedded, Kickstarter, RC, Fun, Build, Engineering
Id: lyoBWH92svk
Channel Id: undefined
Length: 23min 50sec (1430 seconds)
Published: Thu Oct 04 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.