OC
OceanRemote
Low-code IoT platform
← Back to Course

Connecting ESP32 to WiFi Network

Connecting ESP32 to WiFi Network

šŸ“¶ Connecting ESP32 to WiFi Network

šŸ“” What You'll Learn:

  • šŸ”Œ Connect ESP32 to WiFi network in seconds
  • šŸ” Add error handling and auto-reconnection
  • šŸ“Š Access sensor data from anywhere via cloud
  • āš ļø Avoid common WiFi connection issues

WiFi connectivity is what makes ESP32 an IoT device! Connect your soil moisture sensors, weather stations, and irrigation controllers to the internet and access data from anywhere in the world.

šŸ“– Basic WiFi Connection Code

#include <WiFi.h>

const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

void setup() {
    Serial.begin(115200);
    delay(1000);
    
    Serial.println("\nšŸ“” Connecting to WiFi...");
    WiFi.begin(ssid, password);
    
    // Wait up to 10 seconds for connection
    int attempts = 0;
    while (WiFi.status() != WL_CONNECTED && attempts < 20) {
        delay(500);
        Serial.print(".");
        attempts++;
    }
    
    Serial.println();
    
    if (WiFi.status() == WL_CONNECTED) {
        Serial.println("āœ… WiFi connected!");
        Serial.print("šŸ“” IP address: ");
        Serial.println(WiFi.localIP());
    } else {
        Serial.println("āŒ WiFi connection failed!");
        Serial.println("   Check SSID, password, and signal strength");
    }
}

void loop() {
    // Your sensor reading code here
    delay(60000);
}
    

šŸ” Secure WiFi with Auto-Reconnection

#include <WiFi.h>

const char* ssid = "YOUR_WIFI";
const char* password = "YOUR_PASSWORD";
unsigned long lastAttempt = 0;
bool wifiConnected = false;

void connectToWiFi() {
    Serial.print("šŸ“” Connecting to WiFi");
    WiFi.begin(ssid, password);
    
    int attempts = 0;
    while (WiFi.status() != WL_CONNECTED && attempts < 20) {
        delay(500);
        Serial.print(".");
        attempts++;
    }
    
    Serial.println();
    
    if (WiFi.status() == WL_CONNECTED) {
        Serial.println("āœ… WiFi connected!");
        Serial.print("šŸ“” IP address: ");
        Serial.println(WiFi.localIP());
        wifiConnected = true;
    } else {
        Serial.println("āŒ WiFi connection failed!");
        wifiConnected = false;
    }
}

void setup() {
    Serial.begin(115200);
    connectToWiFi();
}

void loop() {
    // Check connection status
    if (WiFi.status() != WL_CONNECTED) {
        Serial.println("āš ļø WiFi disconnected!");
        
        // Retry every 30 seconds
        if (millis() - lastAttempt > 30000) {
            Serial.println("šŸ”„ Attempting to reconnect...");
            connectToWiFi();
            lastAttempt = millis();
        }
    } else {
        // WiFi is connected - do your work here
        // Read sensors, send data to cloud, etc.
    }
    
    delay(1000);
}
    

šŸ“Š WiFi Status Codes

Status CodeMeaningAction
WL_CONNECTED (3)Connected successfullyReady to send data
WL_NO_SSID_AVAIL (1)Network not foundCheck SSID name
WL_CONNECT_FAILED (4)Wrong passwordVerify password
WL_DISCONNECTED (6)DisconnectedAuto-reconnect needed
šŸ’” Important Notes for African Farms:
  • ESP32 supports 2.4GHz WiFi only (not 5GHz). Most rural WiFi uses 2.4GHz.
  • Weak signal? Use external antenna version (ESP32-WROOM) for better range.
  • Save power: Connect, send data, then disconnect with WiFi.disconnect(true)
  • Static IP: Saves 1-2 seconds of connection time - use WiFi.config()
āš ļø Common Issues & Solutions:
  • Connection timeout: Move ESP32 closer to router or use external antenna
  • Wrong password: Check case-sensitive characters (WiFi is case-sensitive)
  • 5GHz network: ESP32 doesn't support it - change router to 2.4GHz
  • Hidden SSID: Use WiFi.begin(ssid, password, channel, bssid)
šŸ“– Success Story - Remote Weather Station:

A farm in rural Tanzania installed an ESP32 weather station 500 meters from the main house:

  • šŸ“ Challenge: Weak WiFi signal at sensor location
  • šŸ”§ Solution: Used ESP32 with external antenna and WiFi repeater
  • šŸ“” Result: Stable connection, data sent every 15 minutes
  • šŸ’° Savings: No need for cellular data plan - free WiFi monitoring
šŸŽ‰ Key Takeaways:
  • āœ… WiFi.begin(ssid, password) → Connect to network
  • āœ… WiFi.status() == WL_CONNECTED → Check connection
  • āœ… WiFi.localIP() → Get IP address
  • āœ… Always add timeout and auto-reconnect logic
  • āœ… ESP32 = 2.4GHz WiFi only (not 5GHz)
šŸ’” Key Takeaways:
  • Apply these concepts directly to your farm or project.
  • Take notes on important details for the quiz.
  • Use the button below to track your progress.