wifi-http-server.ino

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

Servidor HTTP incrustado que permite cambiar el estado del pin a través de un navegador web.

Modifique ssid y password con sus credenciales de WiFi. El ESP32-S3 gestiona nativamente WiFi 4. Abra la IP mostrada en el monitor serie en un navegador. Los botones ON/OFF controlan el 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();
}

← Volver a la ficha