Ovládání Přes Web

Instalace knihovny

pio pkg install --library "esp32async/ESPAsyncWebServer@^3.7.7"

Zdrojový kod

#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>

#define LED_GPIO 33

const char* ssid = "Eduingo-Mainboard";
const char* password = "12345678";

AsyncWebServer server(80);

void setup() {
  pinMode(LED_GPIO, OUTPUT);
  digitalWrite(LED_GPIO, LOW); // LED začíná zhasnutá

  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.print("Připojuji se na Wi-Fi");

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println();
  Serial.print("Připojeno! IP adresa: ");
  Serial.println(WiFi.localIP());

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    String html = "<!DOCTYPE html><html><head><title>ESP32 LED</title></head><body>";
    html += "<h1>Ovládání LED</h1>";
    html += "<a href=\"/led/on\"><button>LED ON</button></a>";
    html += "<a href=\"/led/off\"><button>LED OFF</button></a>";
    html += "</body></html>";
    request->send(200, "text/html", html);
  });

  server.on("/led/on", HTTP_GET, [](AsyncWebServerRequest *request){
    digitalWrite(LED_GPIO, HIGH);
    request->send(200, "text/plain", "LED ZAPNUTA");
  });

  server.on("/led/off", HTTP_GET, [](AsyncWebServerRequest *request){
    digitalWrite(LED_GPIO, LOW);
    request->send(200, "text/plain", "LED VYPNUTA");
  });

  server.begin();
}

void loop() {
  // Není potřeba dělat nic v loop() díky AsyncWebServeru
}

Tato stránka byla naposledy upravena 2025-12-30 12:53

Běží na Wiki|Docs

This page was last edited on 2025-12-30 12:53

Eduingo
info@eduingo.com

Běží na Wiki|Docs