#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
WiFiServer server(80);
String header;
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 3600
#define UTC_OFFSET_DST 3600 // posun o 1hod
struct tm cas;
int r = 5;
int g = 18;
int b = 19;
String cas_ulozenie;
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 2000;
void setup()
{
Serial.begin(9600);
pinMode(2,OUTPUT);
lcd.init();
lcd.backlight();
pinMode(r, OUTPUT);
pinMode(g, OUTPUT);
pinMode(b, OUTPUT);
WiFi.begin("soc2019","socka2019");
while(WiFi.status() != WL_CONNECTED)
{
delay(250);
Serial.print(".");
}
Serial.println("WiFi uspesne pripojena!");
Serial.print("IP adresa: ");
Serial.println(WiFi.localIP());
server.begin();
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
}
void loop()
{
lcd.setCursor(0,1);
lcd.print(vypisCas());
WiFiClient client = server.available(); // aktualizovanie prijatych dat
if (client) // ak sa pripojil klient
{
currentTime = millis();
previousTime = currentTime;
Serial.println("Novy klient");
String currentLine = ""; // premenna pre prijate data
while (client.connected() && currentTime - previousTime <= timeoutTime)
{
currentTime = millis();
if (client.available()) // test ci su prijate dake data
{
char c = client.read(); // nacitanie bajtu z buffera
Serial.write(c);
header += c;
if (c == '\n') // po ENTER je request
{
if (currentLine.length() == 0) // ak sa nacital prazdny riadok, odosli potvrdenie o spojeni
{
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println("");
client.println("<!DOCTYPE html><html>");
client.println("<head><style>body { font-family: Arial, sans-serif; text-align: center; margin: 0; padding: 20px; background-color: #f0f0f5; color: #333; } button { padding: 12px 24px; font-size: 16px; border: none; border-radius: 8px; cursor: pointer; color: white; margin: 10px; transition: opacity 0.3s, transform 0.2s; } button:hover { opacity: 0.9; transform: scale(1.05); } a[href=\"/led/R\"] button { background-color: red; } a[href=\"/led/G\"] button { background-color: green; } a[href=\"/led/B\"] button { background-color: blue; }</style></head><body>");
client.println("<h1>ESP32 <a href=\"https://www.spsknm.sk/ssknm/\" target=\"_blank\">web</a> server</h1>");
client.println("<p><a href=\"/led/R\"><button>Nastav</button></a></p>");
client.println("<p><a href=\"/led/G\"><button>Nastav</button></a></p>");
client.println("<p><a href=\"/led/B\"><button>Nastav</button></a></p>");
client.println("<p><a href=\"/refresh\"><button>Refresh</button></a></p>");
client.println("</body></html>");
client.println("");
break;
}
else
{
currentLine = "";
}
}
else if (c != '\r')
{
currentLine += c;
}
}
}
if (header.indexOf("GET /led/R") >= 0) {
lcd.clear();
vypni();
Serial.println("Zapinam LED");
lcd.setCursor(0,0);
lcd.print("Cervena");
digitalWrite(r, HIGH);
} else if (header.indexOf("GET /led/G") >= 0) {
lcd.clear();
vypni();
Serial.println("Zapinam LED");
lcd.setCursor(0,0);
lcd.print("Zelena");
digitalWrite(g, HIGH);
} else if (header.indexOf("GET /led/B") >= 0) {
lcd.clear();
vypni();
Serial.println("Zapinam LED");
lcd.setCursor(0,0);
lcd.print("Modra");
digitalWrite(b, HIGH);
} else if (header.indexOf("GET /refresh") >= 0) {
cas_ulozenie = vypisCas();
}
client.print("<h1>Je: ");
client.print(cas_ulozenie);
client.print("</h1>");
header = "";
client.stop();
Serial.println("Klient je odpojeny.");
Serial.println("");
}
}
void vypni(){
digitalWrite(r,LOW);
digitalWrite(g,LOW);
digitalWrite(b,LOW);
}
String vypisCas()
{
struct tm cas;
if (!getLocalTime(&cas))
{
Serial.println("PROBLEM SO ZISKANIM UDAJOV ZO SERVERU!");
return "Chyba";
}
char casString[9];
strftime(casString, sizeof(casString), "%H:%M:%S", &cas);
return String(casString);
}