This project is here to help demystify the lines of codes behind the Wi-Fi Scan Library. The purpose of these sketch is to help you scan for available Wi-Fi Network with the ESP-32
The ESP-32 is a very powerful board that allows you to add Wi-Fi and Bluetooth access to your project.
I will be segmenting the code and using screenshots for easy pictorial understanding. The full code can be found here on my GitHub Repo.
I will be showing you my redefined code to help you understand the Wi-Fi Scan Library...so let's get to it...
First of we include the Wi-Fi.h library which gives us access to get Wi-Fi features, Then in our "void setup" we start our Serial Monitor for displaying the information we need to see, Next we set the ESP-32 to STATION MODE (aka STA) using the "WiFi" keyword and ".mode()" properties or methods and so that the ESP-32 will be able to search and connect to various Access Points (Hotpots and Wi-Fi Routers)
We also have to make sure we disconnect the ESP-32 from any existing connection using "WiFi.disconnect()"
Now in the "void loop" we get to scan for available network... First off we use our "Serial.print" statement then we create an integer variable called network to store the number of available network, We then print it out on our Serial Monitor then we move to our if /else condition. If there is no network available it prints "No networks found" but if there are network(s) available (else)
Else...we print the number of available networks found, inside the else loop we keep a "forloop" statement to loop through the number of networks repeatedly
For the For Loop we check if our counter "i" is less than the number of Network available....the essence of the counter is to get the numbering pattern, Multiple Wi-Fi SSID and RSSI and the Encryption Type of the networks around you
Still on the 3rd screenshot we print out the various Network information gotten from the scan e.g. As the For loop cycles from Zero to the maximum number of networks it stores its respective values in the counter "i" then we can tap into the values when we use the keyword (WiFi) and methods( dot notation e.g.mode(), .disconnect(), .status() etc)
This will generate the SSID ( Network Name), RSSI (Network Strength), Encryption Type(open or secured) for each network
Serial.print(WiFi.SSID(i));
Serial.println(WiFi.RSSI(i));
Serial.println(WiFi.encryptionType(i));
Then for checking the Wi-Fi encryption type we make use of a Ternary operator to check if Wi-Fi Encryption of the particular network is open then it prints OPEN and if it's secured it prints Password Required.
Then the final part of the code we set a delay a little then scan again
So the final result of the sketch on the Serial Monitor depending on the Wi-Fi Network(s) around you should be like...
I hope this helps anyone that's getting started with the ESP-32 and if you have any comment or suggestion feel free to reach me on https://twitter.com/_carlos_dev Thanks
Comments
Please log in or sign up to comment.