ā Back to Course
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 connectionWiFi.status() == WL_CONNECTEDā Check if connectedWiFi.localIP()ā Get IP addressWiFi.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.
×