🛠 Serveur web embarqué : piloter la matrice depuis le navigateur

Arduino UNO R4 WiFi · intermediaire · Schéma : IMG-uno-r4-wifi-intermediaire-3-ur4wifi-webserver-matrice.svg — UNO R4 WiFi reliée en WiFi à une box, un navigateur affichant un bouton ON/OFF, flèche HTTP GET /led=on vers la carte, matrice LED allumée ou éteinte.

Objectives

Turn the board into a web server: the WiFiS3 library provides WiFiServer to listen on port 80, receive an HTTP request from a browser, and return an HTML page. You will control the LED matrix using simple GET links — the backbone of any embedded control interface.

Hardware

Complete code

#include <WiFiS3.h>
#include "Arduino_LED_Matrix.h"

ArduinoLEDMatrix matrix;
WiFiServer server(80);

const char* SSID = "votre-reseau";
const char* PASS = "votre-cle";

void setup {
  Serial.begin(115200);
  WiFi.begin(SSID, PASS);
  while (WiFi.status != WL_CONNECTED) { delay(200); }
  Serial.print(F("Ouvrir http://")); Serial.println(WiFi.localIP);
  matrix.begin;
  server.begin;
}

void loop {
  WiFiClient client = server.available;
  if (!client) { return; }

  String requete = "";
  while (client.connected && !requete.endsWith("\r\n")) {
    if (client.available) {
      requete = client.readStringUntil('\r');   // 1re ligne = GET /chemin HTTP/1.1
      break;
    }
  }

  bool on = requete.indexOf("led=on") >= 0;
  matrix.beginFrame;
  for (uint8_t c = 0; c < 12; c++) matrix.setColumn(c, on ? B11111111 : 0);
  matrix.show;

  client.println(F("HTTP/1.1 200 OK"));
  client.println(F("Content-Type: text/html"));
  client.println(F("Connection: close"));
  client.println;
  client.println(F("<html><body style='font-family:sans-serif'>"));
  client.println(on ? F("<h2>Matrice ALLUMEE</h2>") : F("<h2>Matrice ETEINTE</h2>"));
  client.println(F("<a href='/led=on'><button>ON</button></a> "));
  client.println(F("<a href='/led=off'><button>OFF</button></a>"));
  client.println(F("</body></html>"));
  client.stop;   // indispensable, sinon le navigateur tourne en boucle
}

Expected verification

The Monitor displays a URL http://192.168.x.x ; opened in the browser, it shows two buttons. Clicking ON turns on all 96 LEDs, OFF turns them off. Each click generates a request visible if you display requete on the Monitor.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials