🛠 Se connecter au Wi-Fi en mode station et suivre le RSSI
Objectives
Connect the ESP8266-ESP-12E to a Wi-Fi network in station mode (WIFI_STA), the only mode compatible with standard 2.4 GHz networks (the chip supports neither 5 GHz nor Bluetooth). Display the IP address obtained via DHCP, then the RSSI signal strength in dBm every two seconds, and implement automatic reconnection when the link drops. RSSI is an excellent diagnostic tool: beyond -80 dBm, the connection becomes unstable. This is also the foundation for all subsequent network tutorials (web server, MQTT, OTA).
Hardware
- 1 ESP8266-ESP-12E module (integrated PCB antenna)
- 1 2.4 GHz Wi-Fi router (5 GHz is not supported)
- 3.3 V power supply capable of delivering peaks ≥ 500 mA (Wi-Fi transmission causes sudden current spikes)
- Recommended 100 to 470 µF capacitor between 3.3 V and GND near the module
- USB serial console (CP2102/CH340)
Complete Code
#include <ESP8266WiFi.h>
const char* SSID = "mon-reseau";
const char* PASS = "mot-de-passe";
void connexion {
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASS);
Serial.printf("Connexion a %s", SSID);
unsigned long t0 = millis;
while (WiFi.status != WL_CONNECTED && millis - t0 < 20000) {
delay(250);
Serial.print(".");
yield;
}
if (WiFi.status == WL_CONNECTED) {
Serial.printf("\nIP = %s RSSI = %d dBm\n", WiFi.localIP.toString.c_str, WiFi.RSSI);
} else {
Serial.println("\nEchec : verifie SSID, mot de passe et distance du routeur");
}
}
void setup {
Serial.begin(115200);
connexion;
}
void loop {
static unsigned long derniere = 0;
if (WiFi.status != WL_CONNECTED) {
Serial.println("\nLien perdu, reconnexion...");
WiFi.disconnect;
connexion;
}
if (millis - derniere > 2000) {
derniere = millis;
Serial.printf("RSSI = %d dBm canal = %d\n", WiFi.RSSI, WiFi.channel);
}
delay(10);
yield;
}
Expected Verification
Upon power-up, the console displays progress dots followed by the local IP address (for example, 192.168.1.42) and the RSSI. Then, an RSSI line is displayed every two seconds. Move the module away from the router: the RSSI should decrease (for example, from -45 to -85 dBm); if too far, the connection drops and the module displays "Link lost, reconnecting..." before reconnecting automatically.
Common Errors
- Insufficient 3.3 V power supply: on the first Wi-Fi attempt, the module reboots in a loop with messages like "c0 3 3 3" or "rd RST" (brownout detector). Solution: use a power supply ≥ 500 mA and a decoupling capacitor.
- Trying to connect to a 5 GHz network: the ESP8266 is strictly 2.4 GHz 802.11 b/g/n.
- Forgetting WiFi.mode(WIFI_STA): by default, the module may remain in access point mode and never connect.
- Passwords with accented characters or hidden SSIDs: behavior is erratic; test first with a simple network.
- Blocking the loop with long delay calls: the Wi-Fi stack needs to run; use yield or short delays.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinorespiration.inoArduinodht11.inoArduinoluxmetre.inoArduinoserveur-web.inoArduino