OC
OceanRemote
Low-code IoT platform
← Back to Course

Connecting ESP8266 to WiFi

Connecting ESP8266 to WiFi

šŸ“¶ Connecting ESP8266 to WiFi

šŸ“” What You'll Learn:

  • šŸ”Œ Connect ESP8266 to WiFi network
  • šŸ“Š Check connection status and get IP address
  • āš ļø Handle connection timeouts and errors

šŸ“– Basic WiFi Connection Code

#include <ESP8266WiFi.h>

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

void setup() {
    Serial.begin(115200);
    delay(10);
    
    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 and data sending code here
    delay(60000);
}
    
šŸ’” Important Notes:
  • ESP8266 only supports 2.4GHz WiFi networks (not 5GHz)
  • Rural farm WiFi is typically 2.4GHz - you're good!
  • For battery operation, connect, send data, then disconnect to save power
  • Use WiFi.setSleep(true) to reduce power consumption
āš ļø Common Issues:
  • Connection timeout: Weak signal - move closer to router
  • Wrong password: Check case-sensitive characters
  • 5GHz network: ESP8266 doesn't support it - use 2.4GHz
  • SSID hidden: Must use WiFi.begin(ssid, password, channel, bssid)
šŸŽ‰ Quick Reference:
  • WiFi.begin(ssid, password) → Start connection
  • WiFi.status() == WL_CONNECTED → Check if connected
  • WiFi.localIP() → Get IP address
  • WiFi.setSleep(true) → Enable power saving
šŸ’” 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.