Connect ESP32 to WiFi - Step-By-Step Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone i'm thomas and welcome to my web development cloud and iot education channel today i'm gonna show you the simplest method to connect your esp32 to the wi-fi network in a blocking and non-blocking way using visual studio code platform io and arduino framework let's get started first of all let's make sure we have all the tools required to do the job so what we need is esp32 development board i suggest using one of the most popular that you can find on amazon or aliexpress like node mcu-32s the one that i'm using apart from the board you're gonna need usb to micro usb cable to connect the board to the computer and in terms of software we need visual studio code with platform io extension and for the details regarding setting up the board choosing the right cable installing visual studio code or installing additional drivers that might be required for windows and mug users i'd like to recommend one of my previous videos called getting started with esp32 you can find the link in the description right and now let's create a new project in visual studio code so here i am on my computer let's create a new project with platform io so the first step is to locate platform io icon it's this one over here i'm going to click on it then from the quick access menu i'm gonna click on open then i'm gonna close this quick access menu by clicking on the platform io icon again to make some space over here then on the new project from this quick access for the project name i'm going to pick something like esp32 wi-fi connect the board is the board that i'm using which is not mcu so i'm gonna start typing nodemcu and there we go we've got node mcu 32s that's the one i'm using framework i'm going to keep arduino and location is going to be default location so now i'm going to go ahead and click on finish and after few seconds i should have my project created and there's gonna be few tabs opened automatically like get started and in a few seconds i should see platform io.ini and platform io home actually gets started tab has been closed automatically right and what we've got here is the main configuration a file of our project that i'm going to modify in a second but before i do that i'm going to open main.cpp that's the file where we're going to keep all our calls so this is where i'm going gonna write the the code to connect to wi-fi this is a arduino framework program structure right we've got a setup function this one is executed only once at the beginning and the loop function that is gonna be executed repeatedly okay let's go back to platformio.ini and let's set up a serial communication so we can see the messages sent from the microcontroller printed out on the screen in a serial monitor here in visual studio code okay in order to do that i'm gonna add monitor speed and i'm going to set it to 921600 that's going to be the value that i'm going to copy and paste it into serial dot begin so that initializes the serial communication okay so that's the first step of a setup the second one is going to be setting the pin mode on a built-in diode i'm gonna change it to output so we're gonna take control over the voltage that is sent to this diode and we're going to use this diode as a status indicator right as the blue diode on the development board and what i want to do is to switch this on when we have a wi-fi connection and switch it off when we don't have wi-fi connection and now the third step of an initial setup is going to be to define two constants the first one is going to be wi-fi ssid and the second constant wi-fi password these are the wi-fi network credentials let me just explain let me just provide the value and i'm going to explain so ssid is the name of the wi-fi network that you want to connect to and the password is basically the password to this wi-fi i'm gonna keep them these values as a constant so i can pass them to a relevant method to connect to to wi-fi okay so the initial setup is done now let me show you how to connect to wi-fi using the blocking method so what we need first is to include wi-fi multi dot h this is the library that is built on top of the core wifi library of esp 32 on arduino framework and this one requires creating an instance of the wi-fi multi so that's what i'm going to do just now so we're going to create it and assign it to wi-fi multivariable and once i have just done that what i need to do is to call a method on this wi-fi multi instance that method is called at ap that means add access point it requires two arguments the first one is ssid that's the name of the wi-fi so i'm just going to pass the constant value the one that we defined in here right wi-fi ssid the second argument is wi-fi password okay this library actually allows for specifying multiple access points so it is more resilient basically what you can do is to like specify three of them right if you have three access points and if an attempt to connect to the first one fails it tries the second one right then if the second one fails it tries the third one right so it's gonna keep trying until it connects to one of the access points added to this wi-fi multi but because i have only one access point i'm just gonna keep this like i've got it right so just wi-fi ssid and wi-fi password okay and now after adding access point this is the actual blocking code that's gonna be a while loop with wi-fi multi again but this time a run method that a result we checking against wl connected right so we compare if the result of wi-fi multi-run a run method is different than wl connected and if it is we're gonna do a small delay of 100 milliseconds and that loop is just gonna keep trying right keep executing this wi-fi multi run until the result of it is wl connected that means we are connected right and this is the blocking part because no code is gonna be executed after this while loop until the board the microcontroller actually connects to wi-fi right and this is why i'm gonna add a serial print ln connected right so when we are connected the program is gonna exit this loop and then this line is gonna be executed to tell us that we are connected and finally we can do a simple trick in a loop function with digital write to use the built-in diode as a status indicator so let me just provide this the first argument which is the led built in and the second argument is gonna be something new here so i'm gonna use the core wi-fi instance status i have access to it because wi-fi multi is built on top of it so i can actually access methods on wi-fi straight away and i'm gonna check if that status is connected so basically what this is gonna do is continuously check if the wi-fi status is connected and if it is connected the result of this comparison is gonna be one so the diode will switch on if the status is not connected let's say we connect at the beginning but then the connection drops for some reason right let's say we move away from the from the router and and the the board cannot reach our router the connection drops and the result of this comparison is gonna be false this is zero the diode should switch off one thing to note though is when we move closer again for example the wi-fi multi is gonna take care of reconnecting to wi-fi so we should see the diode on after few seconds because it's just gonna try to connect and if it succeeds we will see the diode on okay let's see if this code works and then let's move to the non-blocking way right so in order to deploy this code first we want to make sure that our development board is connected to the computer using usb cable and once we make sure it is we can just click on this button the arrow button here platform io upload so that's what i'm gonna do just now and we should see a compiling message over here because it compiles all of the code okay the code compiled and now we've got connecting and when it is connecting it is important to press the boot button on the board and hold it right okay now we can release the button because it's deploying the code to the board and there we go everything deployed we can check the serial monitor now it's this button over here serial monitor so i'm going to click on it that should connect automatically but you may see something like ic and then what you are interested in is this bit where you can find cp2102 that's the name of the driver used for the serial communication so in my case this is number three i'm just gonna type three and enter and after a few seconds we should see a connected message and the blue diode should be switched on right so that was the blocking way let me show you the non-blocking way now okay what i'm gonna do is to just remove wi-fi multi-code and we're going to use something else and that's something else is the core wi-fi class of esp32 an arduino framework which is in a wi-fi dot h this is actually what the wi-fi multi has been built on top of but we going to use the core functionalities so by including this wifi.h i have access now to a wi-fi instance and on this wi-fi instance i can call a begging method that method requires ssid again wi-fi ssid and wi-fi password this is how to initialize the wi-fi connection this is not blocking so after that you can execute the code if you want so we could do something like starting something like this and then we can also execute in the execute the code in the loop function and this is where i'm going to write all the behavior of this program which is going to be slightly different to the previous one because what i want to do is to blink this blue led diode when the the controller is connecting to wi-fi and then just kept it switched on when it is connected right when the connection drops i want again for the led diode to blink so that's going to be the behavior and let me just program it so what we need to do is to use wi-fi dot status a status method then we can that we can check if it's connected and if it is connected we can for example print out connected message we can do the digital write to change the built-in diode the blue one right to high so switch it on and if it's not connected i'm gonna explain why i'm doing it in a two if statement in a second when it is not connected what i'm going to do instead is to print a dot that's going to indicate that it's connecting and then i'm going to blink the diode in the most efficient way and that most efficient way to blink the diode is to again use the led built in as a pin but as the value what we're going to do is to use the opposite value of the digital delete of led built in so basically what it does it reads the voltage of a built-in led and retains it and then tens this value around right so if if it's high then we're gonna get low here if it's low then we're gonna get high right so that's what's gonna make the diode blink but apart from this we also need a delay right so there's going to be one second delay for the for the blinking itself okay so now we have two contradictory if statements right if this is happening this is not happening but there is one piece missing which is on the transition of the state so basically we don't really want to have a situation when we have a wi-fi connected to run this code every single time right we just want to run it once when we have a connection the first time the status changes to wl connected we want to execute this code and that's it we don't want to execute it again unless the connection drops then we're going to go back to this but that's going to happen anyway so it's going to blink when we have the connection back i want this code to be executed only once on a transition of the state only when the status changes from not connected to connected so what i have to do to achieve this and not see like you know millions of connected messages in the serial monitor is to add additional flag in here so that's gonna be a boolean variable that i'm going to set to false as a default that i'm going to check if it's not true and that i'm going to check to change to true once the diode is on and the message is printed out and also change to false when the connection drops let me explain this again step by step so after the initialization of the serial the built-in led and the wi-fi what's happening is we've got this loop function that is executed continuously the board is trying to connect and we check the status if the status is not connected this is what's going to happen at the beginning we're just going to keep printing dots and we're going to keep blinking the diode this flag is going to remain false the moment we have a transition of state so the status changes from not connected to connected because the isconnected flag is false this code is going to be executed so the connect is going to be printed out the diode is going to change to high it's going to be switched on and is connected is going to change to true the next call of the loop because the is connected is going to be set to true this code won't execute again unless we for example move away from the router the connection drops we have the transition of the state of the wi-fi from connected to not connected then what's gonna happen this code is gonna be executed again right so we'll see the dot we will see the diode switching off delay we're going to have a connected to false and that's going to continue until it is connected again and this is the moment where we again just once print out connected change the diode to to switched on and again setting the flag to true okay so that's that's finished essentially and let me deploy this code now and let's test this solution so yeah again make sure your board is connected to the computer and then what we need to do is to click on upload and once we see connecting message connecting here we need to press the boot button on the board and hold it and now we can release it and that code should be deployed quickly right so i can now see this diode blinking let me connect to the serial monitor okay there we go we can see starting then three dots i could see the diode blinking and we have connected again a simple experiment that we can do is to connect the board to the power bank move away from the router until connection drops when it drops it should start blinking then we should move back closer to the router and we should see the diode on that indicates it's connected back and this is it for today now with these two methods of connecting esp32 to wi-fi we have a nice template that i can reuse in any wi-fi based project that i'm gonna build in the future thanks for watching if you find this content useful don't forget to subscribe to my channel if there is a subject you would like me to make a video about let me know in the comments cheers bye
Info
Channel: Tomasz Tarnowski
Views: 60,314
Rating: undefined out of 5
Keywords: connect esp32 to wifi, getting started with esp32, esp32 tutorial, getting started with the esp32, getting started with esp32 arduino, arduino tutorial, how to get started with micropython, esp32 servo motor web server with arduino ide, basic tutorial, esp32 uart tutorial, esp32 tutorial step by step, random nerd tutorials, wifi tutorial, arduino esp32 tutorial, esp32, arduino wifi, esp32 wifi, learn esp32
Id: aAG0bp0Q-y4
Channel Id: undefined
Length: 21min 47sec (1307 seconds)
Published: Thu Jan 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.