You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.2 KiB
102 lines
2.2 KiB
#include <Arduino.h>
|
|
#include <LittleFS.h>
|
|
#include <WiFi.h>
|
|
#include <WebServer.h>
|
|
|
|
const char* ssid = "tksteti";
|
|
const char* password = "ProsimTeNevim";
|
|
WebServer server(80);
|
|
|
|
const int ledPin1 = 2;
|
|
const int ledPin2 = 4;
|
|
bool ledState1 = false;
|
|
bool ledState2 = false;
|
|
|
|
String loadHtml() {
|
|
File file = LittleFS.open("/index.html","r");
|
|
if(!file) {
|
|
Serial.println("Chyba při otevírání /index.html");
|
|
return "soubor nenalezen";
|
|
}
|
|
String html = file.readString();
|
|
file.close();
|
|
//String(ledState1) převede bool na String)
|
|
html.replace("STAVLED1", String(ledState1));
|
|
html.replace("STAVLED2", ledState2 ? "ON" : "OFF");
|
|
return html;
|
|
}
|
|
|
|
void handleRoot(){
|
|
server.send(200,"text/html",loadHtml());
|
|
}
|
|
|
|
void handleLED1() {
|
|
if(server.hasArg("state")) {
|
|
String state = server.arg("state");
|
|
if(state == "on") {
|
|
ledState1 = true;
|
|
digitalWrite(ledPin1,HIGH);
|
|
} else {
|
|
ledState1 = false;
|
|
digitalWrite(ledPin1,LOW);
|
|
}
|
|
server.sendHeader("Location","/");
|
|
server.send(302,"text/plain","");
|
|
} else {
|
|
server.send(400,"BAD REQUEST");
|
|
}
|
|
}
|
|
|
|
void handleLED2() {
|
|
if(server.hasArg("state")) {
|
|
String state = server.arg("state");
|
|
if(state == "on") {
|
|
ledState2 = true;
|
|
digitalWrite(ledPin2,HIGH);
|
|
} else {
|
|
ledState2 = false;
|
|
digitalWrite(ledPin2,LOW);
|
|
}
|
|
server.sendHeader("Location","/");
|
|
server.send(302,"text/plain","");
|
|
} else {
|
|
server.send(400,"BAD REQUEST");
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
pinMode(ledPin1,OUTPUT);
|
|
pinMode(ledPin2,OUTPUT);
|
|
digitalWrite(ledPin1,LOW);
|
|
digitalWrite(ledPin2,LOW);
|
|
|
|
WiFi.setHostname("ESP-Prijmeni");
|
|
WiFi.begin(ssid,password);
|
|
while(WiFi.status() != WL_CONNECTED){
|
|
delay(200);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("WiFi připojeno");
|
|
Serial.print("IP adresa: ");
|
|
Serial.print(WiFi.localIP());
|
|
|
|
if(!LittleFS.begin()){
|
|
Serial.println("Chyba při zavádění (mounting) LittleFS");
|
|
return;
|
|
}
|
|
|
|
server.on("/",handleRoot);
|
|
server.on("/led1",handleLED1);
|
|
server.on("/led2",handleLED2);
|
|
server.onNotFound([](){
|
|
server.send(404,"text/plain","Stranka nenalezena");
|
|
});
|
|
|
|
server.begin();
|
|
Serial.println("HTTP Server spuštěn");
|
|
}
|
|
|
|
void loop() {
|
|
server.handleClient();
|
|
}
|