wifi_web_server.ino

PaperDIY Assembly Kit · File : wifi_web_server.ino · Language : C++ (Arduino) · Use case : Créer un serveur web local pour contrôler la carte via un navigateur.

Minimalist web server to control an output and display status.

Modify SSID and PASSWORD with your WiFi credentials. Upload the code. Open Serial Monitor to get the local IP address. Access this IP from a browser on the same network to interact with the board.

wifi_web_server.ino

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

const char* ssid = "VOTRE_WIFI_SSID";
const char* password = "VOTRE_WIFI_PASSWORD";

WebServer server(80);
String message = "Hello from PaperDIY ESP32-S3";

void handleRoot() {
  String html = "<!DOCTYPE html><html><head><title>PaperDIY</title></head><body>";
  html += "<h1>PaperDIY Assembly Kit</h1>";
  html += "<p>" + message + "</p>";
  html += "<a href='/led_on'>LED ON</a> | <a href='/led_off'>LED OFF</a>";
  html += "</body></html>";
  server.send(200, "text/html", html);
}

void handleLedOn() {
  digitalWrite(5, HIGH);
  message = "LED is ON";
  server.sendHeader("Location", "/", true);
  server.send(303);
}

void handleLedOff() {
  digitalWrite(5, LOW);
  message = "LED is OFF";
  server.sendHeader("Location", "/", true);
  server.send(303);
}

void setup() {
  Serial.begin(115200);
  pinMode(5, OUTPUT);
  
  WiFi.begin(ssid, password);
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected");
  Serial.println(WiFi.localIP());

  server.on("/", handleRoot);
  server.on("/led_on", handleLedOn);
  server.on("/led_off", handleLedOff);
  server.begin();
}

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

← Back to the board