ESP32 BASICS · LESSON 09
Connecting to Wi-Fi (Station Mode)
Joining your home network, checking signal strength, and handling a dropped connection gracefully.
Joining an existing Wi-Fi network — "station mode" — is the most common way an ESP32 gets online, whether to fetch data, send sensor readings, or receive commands.
wifi_station.ino
#include
const char* ssid = "your-network-name";
const char* password = "your-network-password";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(300);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
}
Handling a dropped connection
Wi-Fi networks occasionally drop a device, especially on battery-powered boards moving in and out of range. Checking WiFi.status() periodically and reconnecting when it isn't WL_CONNECTED is far more robust than assuming the connection made in setup() will last forever.
KEY IDEA
The ESP32's Wi-Fi radio only supports 2.4 GHz networks — a 5 GHz-only network will never be found.