ESP32 WiFi Connect Example
Introduction
ESP32 Wi-Fi Connect -
The ESP32 board can act as Wi-Fi Station, Access Point or both. To set the Wi-Fi mode, use WiFi.mode() and set the desired mode as argument.
Wi-Fi Station -
WiFi.mode(WIFI_STA) - station mode: the ESP32 connects to an access point
When the ESP32 is set as a Wi-Fi station, it can connect to other networks (like your router). Your can connect to ESP using the IP address assigned by router.
Wi-Fi Connect -
The ESP32 can connect to Wi-Fi networks within its Wi-Fi range.
Step 1: Connect Board to Laptop
Step 2: Wi-Fi Connect Example Code
Select boar ESP32 DEV Module from Tools >> Boards menu, then select appropriate com port. Upload this program to ESP32.
#include "WiFi.h"
const char* ssid = "yourNetworkName"; //enter your wireless ssid
const char* password = "yourNetworkPass"; //enter your wireless password
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
// Print local IP address
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //show ip address on serial monitor
}
void loop() {}
Points to note -
Use WiFi.status() != WL_CONNECTED to check if ESP32 is connected to wifi
In case WiFi Connection lost then ESP32 WiFi Library takes care of re-connection to WiFi.
Do not use any function that takes longer time in loop(), it will reset the ESP with Error: WDT reset
ESP32 Connects to only 2.4GHz WiFi