🛠 Scanner les réseaux WiFi et se connecter en station

Arduino MKR WiFi 1010 · intermediaire · Schéma : IMG-mkr-wifi-1010-intermediaire-1-mkrwifi-scan-wifi-sta.svg — MKR WiFi 1010 et son module NINA-W102 reliés sans fil à une box WiFi, liste de réseaux détectés avec puissances RSSI affichée dans le moniteur série.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials