connexion-wpa2.ino

Arduino MKR WiFi 1010 · File : connexion-wpa2.ino · Language : Arduino · Use case : Se connecter à un point d'accès WPA2

The basic WPA2-personal connection: this sketch boots the WINC1500, associates the board with your access point and waits for the DHCP IP. It also prints channel and RSSI, two precious indicators when the link is moody.

Hardware

An Arduino MKR WiFi 1010 (SAMD21 microcontroller, WINC1500 Wi-Fi module, no Bluetooth, USB or LiPo power on J6). MKR WiFi 1010, 2.4 GHz access point (the WINC1500 cannot see 5 GHz), up-to-date credentials.

Wiring

No wiring; bring the board close to the router for the first test before checking range.

How it works

WiFi.begin() starts association without blocking; the loop polls WiFi.status() until WL_CONNECTED or a 20 s timeout. localIP comes from DHCP, getChannel/RSSI describe the link.

Running it

Install Arduino SAMD Boards and the WiFi101 library, select the board, upload. Wi-Fi runs on 2.4 GHz only: check that your access point is not 5 GHz only.

Variants

Add WiFi.setPins() only if you drive the enable/reset pins yourself, or test WPA2-enterprise (rare at home).

connexion-wpa2.ino

// Connexion WPA2 classique avec la MKR WiFi 1010 (WiFi101).
#include <SPI.h>
#include <WiFi101.h>

const char SSID[] = "MonReseau";
const char PASS[] = "motdepassewifi";

void setup() {
  Serial.begin(9600);
  while (!Serial);
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println(F("Module WiFi absent"));
    while (true);
  }
  Serial.print("Connexion a " + String(SSID) + " ...");
  int statut = WiFi.begin(WPA_PASS_PHRASE, SSID, PASS);
  // begin() est non bloquant : on attend l'etat CONNECTED
  unsigned long debut = millis();
  while (statut != WL_CONNECTED && millis() - debut < 20000) {
    delay(100);
    statut = WiFi.status();
  }
  if (statut == WL_CONNECTED) {
    Serial.println(F(" connecte !"));
    Serial.print(F("IP : "));
    Serial.println(WiFi.localIP());
    Serial.print(F("Canal : "));
    Serial.println(WiFi.getChannel());
    Serial.print(F("RSSI : "));
    Serial.print(WiFi.RSSI());
    Serial.println(F(" dBm"));
  } else {
    Serial.println(F("ECHEC (SSID/mot de passe ?)"));
  }
}

← Back to the board