ESP32 ArduinoCore Interface WiFi
The ESP32 ArduinoCore interface for Wi-Fi provides a straightforward and flexible way to enable Wi-Fi connectivity in projects developed using the ESP32 microcontroller platform with the Arduino IDE. With this interface, developers can easily incorporate Wi-Fi functionality into their projects, enabling devices to connect to wireless networks, access the internet, and communicate with other devices over Wi-Fi.
Features and Capabilities
- Access Point (AP) Mode: The ESP32 can act as an access point, allowing other devices to connect to it and access resources or services hosted by the ESP32.
- Station (STA) Mode: The ESP32 can connect to existing Wi-Fi networks as a client, enabling devices to access the internet or communicate with other networked devices.
- Soft Access Point (SoftAP) Mode: This mode enables the ESP32 to simultaneously act as both an access point and a station, allowing it to connect to an existing Wi-Fi network while also providing Wi-Fi access to other devices.
- Wi-Fi Protected Setup (WPS): The ESP32 supports WPS, a method for easily configuring Wi-Fi network security settings without needing to enter a password manually.
- Advanced Configuration Options: The interface provides access to advanced configuration options for fine-tuning Wi-Fi settings, such as specifying static IP addresses, setting up captive portals, and configuring Wi-Fi sleep modes to optimize power consumption.
- Event Handling: Developers can implement event handlers to respond to Wi-Fi-related events, such as successful connections, disconnections, or errors, allowing for more robust and responsive Wi-Fi functionality.
- Security Features: The ESP32 ArduinoCore interface supports various Wi-Fi security protocols, including WPA and WPA2, to ensure secure communication over Wi-Fi networks.
Overall, the ESP32 ArduinoCore interface for Wi-Fi simplifies the process of adding Wi-Fi connectivity to ESP32-based projects, making it easier for developers to create IoT devices, home automation systems, and other wireless applications.
Scan WiFi
Note: You can use Arduino example code instead of the below code because both are the same (File > Example > WiFi> WiFiScan)
/* https://aruneworld.com/embedded/espressif/esp32
* Tested By : Arun(20170429)
* Example Name : AEW_WiFi_Scan.ino
* This sketch demonstrates how to scan WiFi networks.
* The API is almost the same as with the WiFi Shield library,
* the most obvious difference being the different file you need to include:
*/
#include "WiFi.h"
void setup()
{
Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
}
void loop()
{
Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
delay(10);
}
}
Serial.println("");
// Wait a bit before scanning again
delay(5000);
}
Serial Terminal Output
scan start scan done 5 networks found 1: ArunEworld (-34)* 2: Exuber_365 (-59)* 3: Bangalore Police (-66)* 4: Tagos-2.4 (-80)* 5: RRL_Internet (-93)*
Code Explanation
Certainly! Here’s the code with explanations provided as snippets:
#include "WiFi.h"
This line includes the WiFi library necessary for working with WiFi on the ESP32.
void setup()
{
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
}
In the setup() function:
- The code initializes serial communication at a baud rate of 115200.
- The WiFi mode is set to station mode (
WIFI_STA) and any previous connection is disconnected usingWiFi.disconnect(). - The code adds a delay of 100 milliseconds.
- The code prints “Setup done” to the serial monitor.
void loop()
{
Serial.println("scan start");
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
delay(10);
}
}
Serial.println("");
delay(5000);
}
In the loop() function:
- The code begins by printing “scan start” to the serial monitor.
- Next, the ESP32 scans for nearby WiFi networks using the
WiFi.scanNetworks()function, storing the number of networks found in variablen. - After completing the scan, the code prints “scan done” to the serial monitor.
- The code prints the number of networks found when it detects networks, followed by details of each network, including index, SSID (network name), RSSI (signal strength), and encryption type.
- If no networks are found (when
n == 0), the message “no networks found” is printed. - When networks are found, the code prints the number of networks found, followed by details of each network, including index, SSID (network name), RSSI (signal strength), and encryption type.
- Finally, the code adds a delay of 5 seconds before initiating the next scan.
These snippets provide an overview of how the code initializes WiFi and continuously scans for nearby networks, printing details to the serial monitor.