#include <WiFi.h>
#include <EEPROM.h>
WiFiServer server(80);
String header;
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 2000;
String names[10] = {"Matus", "Jano", "Rudo", "Robo", "Vlado", "Milos", "Brano", "Jozo", "Fero", "Lukas"};
byte buttons[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
void setup() {
Serial.begin(9600);
WiFi.begin("Moja WIFI", "Bohtamiluje");
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
Serial.println("WiFi úspešne pripojené!");
Serial.print("IP adresa: ");
Serial.println(WiFi.localIP());
EEPROM.begin(512);
server.begin();
for(int i = 0; i < 10; i++){
buttons[i] = EEPROM.read(i+1);
Serial.print(buttons[i]);
}
}
void loop() {
WiFiClient client = server.available(); // aktualizovanie prijatých dát
if (client) // ak sa pripojil klient
{
currentTime = millis();
previousTime = currentTime;
Serial.println("Nový klient");
String currentLine = ""; // premená pre prijaté dáta
while (client.connected() && currentTime - previousTime <= timeoutTime) {
currentTime = millis();
if (client.available()) // test, či sú prijaté dáke dáta
{
char c = client.read(); // načítanie bajtu z buffera
Serial.write(c);
header += c;
if (c == '\n') // po ENTER je request
{
if (currentLine.length() == 0) // ak sa načítal prázdny riadok, odošli potvrdenie o pripojení
{
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println("");
for (int i = 0; i < 10; i++) {
String turnOn = "GET /r_" + names[i];
String turnOff = "GET /g_" + names[i];
if (header.indexOf(turnOn) >= 0) {
buttons[i] = 1;
} else if (header.indexOf(turnOff) >= 0) {
buttons[i] = 0;
}
EEPROM.write(i+1, buttons[i]);
Serial.print(buttons[i]);
}
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial; margin-left:auto; margin-right:auto;}");
client.println("button{ border: 0px, solid; border-radius: 10px; padding: 25px; font-size: 20px;}");
client.println("</style></head><body><h1>ESP32 with Buttons</h1>");
for(int i = 0; i < 10; i++){
if(buttons[i] == 0){
client.println("<p><a href='r_"+names[i]+"'><button style='background-color: #f44336; color: #ffffff;'>"+names[i]+"</button></a></p>");
}
else if(buttons[i] == 1){
client.println("<p><a href='g_"+names[i]+"'><button style='background-color: #4CAF50; color: #ffffff;'>"+names[i]+"</button></a></p>");
}
}
EEPROM.commit();
client.println("</body></html>");
client.println("");
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c; // add it to the end of the currentLine
}
}
}
header = "";
client.stop();
Serial.println("Klient je odpojený.");
Serial.println("");
}
}