- [Lecturer] The ESP32 is an amazing chip for building IoT devices
because it has WiFi built-in. But how do you use it? In this video, I'll
show you how to connect the ESP32 to your WiFi network. The first step is to
include the WiFi headers. I will do that at the top of my file, and I will say include WiFi dot H. Now to actually be able
to connect to WiFi, we need to know two things, the name of the network
that you want to connect to and the password of that network. So at the top of my file, I
will define two constants. I will define WiFi underscore network with the name of my network, and I will define WiFi underscore password with the password of my network. Next, I will also define a timeout called WiFi underscore a timeout underscore, MS. And this will control how long the ESP32 can attempt to connect to a WiFi network. You don't want it to keep trying forever. Instead, when you fail to connect to WiFi, it might be better to restart the ESP or to send it into deep sleep
mode to save the battery, if you have one. Now I will set this to 20 seconds. So in milliseconds, that becomes 20,000. Okay, now we're ready to start connecting. I will create a new function
called, connect to WiFi, and this will take care of
all the necessary steps. Now, before we do anything,
I will print something to the serial so that we
know what the ESP is doing. I'll say serial dot
print, connecting to WiFi. Now the first step is to set
the mode of the WiFi chip. There are basically two modes you can use, STA or station mode and
AP or access point mode. Now, when you want to connect
to an existing WiFi network, you have to use station mode. So that's what I'll do here. I'll say WiFi dot mode and I will pass it WiFi underscore STA. AP mode can be interesting
if you want the ESB to create its own WiFi network. And this can be useful to
let someone else configure it by connecting to it. But that's for another video. Now we can ask the ESB to
connect to our WiFi network by calling WiFi dot begin, and passing along our
network name, and password. So WiFi underscore network
and WiFi underscore password. This will start the
WiFi connection process. Next up, we want to keep
track of how long the ESP has been trying to connect to WiFi so that we can enforce our timeout. I will do that by creating
an unsigned long variable called, start attempt time. And I will set this to millis. Now millis is a function
that will return the uptime of your ESP32. And so in the start attempt time variable, we will store the time at which we started our WiFi connection attempt. Now we're going to create a
wild loop that keeps looping as long as we don't have a WiFi connection and as long as we haven't
exceeded our timeout. Now we can check the WiFi status
by calling WiFi dot status. And I'll say that we're gonna
loop while it is not equal to WL underscore connected. Then to make sure that we're
still within our timeout, I will take the current
uptime of our board and I will subtract our
start attempt time from it. And then we're gonna see
and we're gonna make sure that this is still smaller than our WiFi timeout, MS variable. So once more, this will
keep looping while the WiFi isn't connected and
while we haven't reached our timeout yet. Now inside this loop, I'm
going to print a single dot to the serial so that we
can see some progress. And then I will also
add a small delay to it so that this function or this
loop doesn't run too fast. Now at the end of this while loop, we have one of two possible situations. Either we are connected
to WiFi or we've timed out and we're not connected. So I will check the status
again by calling WiFi status and I will check if it's now connected. So I will say, "If WiFi dot status "does not equal WL underscore connected," then I will print something to the serial. I will say, "Serial
dot print line failed," to say that we've failed
connecting to WiFi. And after this line, you
probably want to take some action, like put the ESP
into deep sleep for a while or rebooted it completely. Now, this depends a bit on
your use case and whether or not your board is battery powered. If you're not worried about battery life, then you might not need
to time out at all. And you can just let the
ESP32 keep trying to connect to WiFi forever. So I will leave this up to you. Now, if the WiFi is connected, I want to print that
to the serial as well. So I'm gonna say, "Serial
dot print connected." And we can also print the
current IP address of our board by printing WiFi dot local IP. And that is our connect to
WiFi function, all done. The last thing we have to
do before we can flash it is we have to call this function somewhere and I will call it in the setup function. I will say, when the ESP
boots, we're gonna initialize our serial monitor, and I'm going to say, "Connect to WiFi." I'm gonna save the file,
and let's now flash it to our board. Quick side note. Behind the scenes, I flashed
this exact program to my board, but I did change the WiFi credentials so that they would match with my setup. Now, once the flashing is
done, I'm gonna open up the serial monitor. I'm gonna attach it to my board. And as you can see, it
says, "Connecting to WiFi," then it says, "Connected," followed by the IP address of our board. So that's how you
establish a WiFi connection with the ESP32. Thanks for watching. Don't forget to subscribe and check out the rest of these series if you want to learn more about Arduino and the ESP32.