Browse Source

add simple web led control

master
Jakub Škrabánek 2 months ago
parent
commit
f7bfd60d71
  1. 53
      06_webserver_ledbuiltin/06_webserver_ledbuiltin.ino
  2. 69
      06a_webserver_ledbuiltin_v2/06a_webserver_ledbuiltin_v2.ino

53
06_webserver_ledbuiltin/06_webserver_ledbuiltin.ino

@ -0,0 +1,53 @@
#include <WiFi.h>
#include <WebServer.h>
#define LED_BUILTIN 2
const char* ssid = "tksteti";
const char* password = "ProsimTeNevim";
WebServer server(80);
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN,OUTPUT);
WiFi.begin(ssid,password);
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("Připojeno k WiFi");
Serial.print("IP Adresa:");
Serial.println(WiFi.localIP());
//jednotlivé odkazy
server.on("/", handleRoot);
server.on("/on", handleOn);
server.on("/off", handleOff);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("Wwebovy server spusten");
}
void loop(){
server.handleClient(); //Obsluha klientů
}
//"metoda" pro výpis stránky pro "požadavek"
void handleRoot() {
server.send(200, "text/plain", "Vitej na webove strance tveho ESP32");
}
void handleOn() {
digitalWrite(LED_BUILTIN, HIGH);
server.send(200, "text/plain", "LED zapnuta");
}
void handleOff() {
digitalWrite(LED_BUILTIN, LOW);
server.send(200, "text/plain", "LED vypnuta");
}
void handleNotFound() {
server.send(404, "text/plain", "Stranka nenalezena");
}

69
06a_webserver_ledbuiltin_v2/06a_webserver_ledbuiltin_v2.ino

@ -0,0 +1,69 @@
#include <WiFi.h>
#include <WebServer.h>
#define LED_BUILTIN 2
const char* ssid = "tksteti";
const char* password = "ProsimTeNevim";
WebServer server(80);
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN,OUTPUT);
WiFi.begin(ssid,password);
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("Připojeno k WiFi");
Serial.print("IP Adresa:");
Serial.println(WiFi.localIP());
//jednotlivé odkazy
server.on("/", handleRoot);
server.on("/on", handleOn);
server.on("/off", handleOff);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("Webovy server spusten");
}
void loop(){
server.handleClient(); //Obsluha klientů
}
//"metoda" pro výpis stránky pro "požadavek"
void handleRoot() {
String html = "<!DOCTYPE html><html><head>";
html += "<title>ESP32 LED</title>";
html += "</head><body>";
html += "<h1> Ovladani LED </h1>";
html += "<p> Stav LED: ";
if(digitalRead(LED_BUILTIN) == HIGH){
html += "ON";
} else {
html += "OFF";
}
html += "</p>";
html += "<a href='/on'>Zapnout</a> <br>";
html += "<a href='/off'>Vypnout</a>";
html += "</body></html>";
server.send(200, "text/html",html);
}
void handleOn() {
digitalWrite(LED_BUILTIN, HIGH);
server.sendHeader("Location", "/");
server.send(302, "text/plain", "");
}
void handleOff() {
digitalWrite(LED_BUILTIN, LOW);
server.sendHeader("Location", "/");
server.send(302, "text/plain", "");
}
void handleNotFound() {
server.send(404, "text/plain", "Stranka nenalezena");
}
Loading…
Cancel
Save