wifi-http-server.ino

M5Stack PaperDIY Complete Kit · File : wifi-http-server.ino · Language : C++ (Arduino) · Use case : Créer un serveur web minimaliste pour contrôler un GPIO via le réseau WiFi.

Embedded HTTP server allowing pin state change via a web browser.

Modify ssid and password with your WiFi credentials. The ESP32-S3 natively handles WiFi 4. Open the IP shown in the serial monitor in a browser. ON/OFF buttons control pin 2.

wifi-http-server.ino

#include <WiFi.h>
#include <WebServer.h>

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
WebServer server(80);

void handleRoot() {
  String html = "<h1>PaperDIY Control</h1>";
  html += "<a href='/led/on'><button>LED ON</button></a>";
  html += "<a href='/led/off'><button>LED OFF</button></a>";
  server.send(200, "text/html", html);
}

void handleLed() {
  String state = server.uri().substring(5);
  if (state == "on") digitalWrite(2, HIGH);
  else digitalWrite(2, LOW);
  server.sendHeader("Location", "/");
  server.send(302);
}

void setup() {
  Serial.begin(115200);
  pinMode(2, OUTPUT); // Supposons une LED sur GPIO2
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(WiFi.localIP());
  server.on("/", handleRoot);
  server.on("/led", handleLed);
  server.begin();
}

void loop() {
  server.handleClient();
}

← Back to the board