🛠 Se connecter au Wi-Fi en mode station et suivre le RSSI

ESP8266 (ESP-12E module) · intermediaire · Schéma : IMG-esp8266-esp-12e-intermediaire-1-esp8266-wifi-sta-rssi.svg — Module ESP8266-ESP-12E relié à un point d'accès Wi-Fi 2,4 GHz, antenne PCB, condensateur de découplage sur le rail 3,3 V, console série sur UART0.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials