serveur-web-led.ino

Arduino MKR WiFi 1010 · File : serveur-web-led.ino · Language : Arduino · Use case : Commander une LED depuis une page web locale

The MKR WiFi 1010 becomes an IoT device: it hosts a lite HTML page with two links that switch an LED on pin 6. No cloud infrastructure — the page opens from any device on the local network, phones included.

Hardware

An Arduino MKR WiFi 1010 (SAMD21 microcontroller, WINC1500 Wi-Fi module, no Bluetooth, USB or LiPo power on J6). MKR WiFi 1010, LED + 330 Ω resistor on pin 6, same WiFi network as the phone.

Wiring

LED (+) → 330 Ω resistor → pin 6, cathode to GND; the serial-printed IP opens in the browser.

How it works

WiFiServer listens on port 80; each HTTP request is read to its first line, compared to /on or /off, then a minimal page is returned. Refreshing shows the current state.

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 query parameters (?value=512) for a digital potentiometer, or a meta refresh tag for a self-updating state.

serveur-web-led.ino

// Serveur web MKR WiFi 1010 : /on et /off pilotent la LED broche 6.
#include <SPI.h>
#include <WiFi101.h>

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

WiFiServer serveur(80);
const int LED = 6;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  WiFi.begin(WPA_PASS_PHRASE, SSID, PASS);
  while (WiFi.status() != WL_CONNECTED) delay(100); // IP via DHCP
  Serial.println(WiFi.localIP()); // a ouvrir dans le navigateur
  serveur.begin();
}

void loop() {
  WiFiClient client = serveur.available();
  if (!client) return;
  String requete = client.readStringUntil('
'); // ligne GET /...
  if (requete.startsWith("GET /on")) digitalWrite(LED, HIGH);
  if (requete.startsWith("GET /off")) digitalWrite(LED, LOW);
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html\r\n");
  client.println("<a href='/on'>ALLUMER</a><br>");
  client.println("<a href='/off'>ETEINDRE</a><br>");
  client.println(digitalRead(LED) ? "LED : ON" : "LED : OFF");
  delay(10);
  client.stop();
}

← Back to the board