🛠 Scanner les réseaux WiFi et se connecter en station
Objectives
Use the WiFiNINA library to control the u-blox NINA-W102 radio module (an ESP32 reprogrammed by Arduino): list nearby networks with WiFi.scanNetworks, then join a WPA2 access point in station mode and display the obtained IP address. This is the foundation of all network tutorials in this series.
Hardware
- 1 × Arduino MKR WiFi 1010
- A 2.4 GHz WiFi network (the NINA does not see 5 GHz)
- Arduino IDE with the SAMD core and the WiFiNINA library installed
- Up-to-date firmware on the NINA module (see the "NINA firmware update" tutorial)
Complete Code
#include <WiFiNINA.h>
const char* SSID = "votre-ssid";
const char* PASS = "votre-mot-de-passe";
void setup {
Serial.begin(115200);
while (!Serial && millis < 3000);
int n = WiFi.scanNetworks;
Serial.printf("%d reseaux trouves\n", n);
for (int i = 0; i < n; i++)
Serial.printf("%s (%d dBm)\n", WiFi.SSID(i).c_str, WiFi.RSSI(i));
Serial.print("Connexion...");
WiFi.begin(SSID, PASS);
while (WiFi.status != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.printf("\nConnecte ! IP: %s\n",
WiFi.localIP.toString.c_str);
}
void loop {
if (WiFi.status != WL_CONNECTED) {
Serial.println("Reconnexion...");
WiFi.reconnect;
}
delay(2000);
}
Expected Verification
The serial monitor displays the list of SSIDs with their power in dBm, then the local IP address (typically 192.168.x.x) after a few progress dots. If you cut the WiFi connection on the router, the sketch displays "Reconnecting..." and re-establishes the link when the connection returns.
Common Errors
WiFi.beginreturns/remains in status 255: almost always due to outdated or incompatible NINA firmware with the WiFiNINA version — update the module via the Firmware Updater in IDE 2.- 5 GHz network: the NINA-W102 is 2.4 GHz only, so the 5 GHz SSID will never appear in the scan.
- Accented characters or symbols in the password: due to encoding issues, test first with an ASCII password.
- Static IP with
WiFi.configbeforeWiFi.begin: historically ignored (WiFiNINA bug #5, DHCP won) — check your library version before relying on this.
Other tutorials for this board
Code examples for this board
scan-wifi.inoArduinoconnexion-wpa2.inoArduinohttp-get.inoArduinoserveur-web-led.inoArduinohorloge-ntp.inoArduinoudp-diffusion.inoArduino