Steuerung einer LED über das WLAN
#include <WebServer.h>
const char* ssid = "Name des Netzwerks";
const char* password = "geheimes Passwort";
const int httpPort = 80;
WebServer server(httpPort);
void startSeite() {
server.send(200, "text/html", htmlText());
}
String htmlText() {
String txt;
txt = "<!DOCTYPE html><html><head><title>ESP32</title>";
txt = txt + "<style>body{text-align:center;}button{margin:10px;padding:10px}</style>";
txt = txt + "</head><body>";
txt = txt + "<h1>ESP</h1>";
txt = txt + "<a href=\"/an\">";
txt = txt + "<button>LED an</button>";
txt = txt + "</a>";
txt = txt + "<a href=\"/aus\">";
txt = txt + "<button>LED aus</button>";
txt = txt + "</a>";
txt = txt + "</body></html>";
Serial.println(txt);
return txt;
}
void ledAn() {
digitalWrite(22, HIGH);
startSeite();
}
void ledAus() {
digitalWrite(22, LOW);
startSeite();
}
void setup() {
pinMode(22, OUTPUT);
Serial.begin(115200);
Serial.print("Verbindung zu WLAN: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Verbindung aufgebaut");
Serial.print("IP-Addresse des ESP32: ");
Serial.println(WiFi.localIP());
server.on("/", startSeite);
server.on("/an", ledAn);
server.on("/aus", ledAus);
server.begin();
}
void loop() {
server.handleClient();
}
zurück