#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriBraces.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6
WebServer server(80);
const int ldrPin = 12; // pin fotorezistoru
const int openLed = 26; // pin polarity motorku
const int closeLed = 25; // pin polarity motorku
const int ENA = 5; // pin rychlosti motorku
const int limitButtonUp = 34; // pin horního limit swiche
const int limitButtonDown = 35; // pin dolního limit swiche
const int buttonUp = 18; // pin přepínače
const int buttonDown = 23; // pin přepínače
const int DHT22_PIN = 27; //pin teokiner
int SPEED = 150; // rychlost motorku 0 - 255
int lastState = HIGH;
int limitUpState = 1;
int limitDownState = 1;
int goUpState = 1;
int goDownState = 1;
int toggleState;
int otevreno = 0;
unsigned long previousTime = millis();
long timeInterval = 4500; // interval millis (čas zavírání dvířek)
int i;
int n = 1200; //doba čekání v sekundách (před otevřením, nebo zavřením dvířek)
int m = n/60;
int s = n - (60 * m);
// LDR Characteristics
const float gama = 0.7;
const float rl10 = 50;
void sendHtml() {
String response = R"(
<!DOCTYPE html><html>
<head>
<title>ESP32 Web Server Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html { font-family: sans-serif; text-align: center; background-color: #222; color: #fff; }
body { display: inline-flex; flex-direction: column; }
h1 { margin-bottom: 1.2em; }
h2 { margin: 0; }
div { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto auto; grid-auto-flow: column; grid-gap: 1em; }
.btn { background-color: #5B5; border: none; color: #fff; padding: 0.5em 1em;
font-size: 2em; text-decoration: none; border-radius: 10px; }
.btn.OFF { background-color: #333; }
a { color: #fff; }
</style>
</head>
<body>
<h1>OVLADATOR KURNIKU</h1>
<div>
<h2>UP</h2>
<a href="/toggle/1" class="btn LED1_TEXT">LED1_TEXT</a>
<h2>DOWN</h2>
<a href="/toggle/2" class="btn LED2_TEXT">LED2_TEXT</a>
</div>
</body>
</html>
)";
response.replace("LED1_TEXT", goUpState ? "ON" : "OFF");
response.replace("LED2_TEXT", goDownState ? "ON" : "OFF");
server.send(200, "text/html", response);
}
void closingDoor () { // zavírání dveří spustí motorek jedním směrem
LCD.setCursor(0, 0);
LCD.print(" ");
LCD.print("zavirani");
LCD.print(" ");
digitalWrite(openLed, LOW);
digitalWrite(closeLed, HIGH);
}
void openingDoor () { // zavírání dveří spustí motorek druhým směrem
LCD.setCursor(0, 0);
LCD.print(" ");
LCD.print("otevirani");
LCD.print(" ");
digitalWrite(openLed, HIGH);
digitalWrite(closeLed, LOW);
}
void error () { // chyba
LCD.setCursor(0, 0);
LCD.print(" ");
LCD.print("neco je spatne");
LCD.print(" ");
digitalWrite(openLed, LOW);
digitalWrite(closeLed, LOW);
delay (4000);
openingDoor();
delay (400);
}
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void setup(void) {
Serial.begin(115200);
pinMode(openLed, OUTPUT);
pinMode(closeLed, OUTPUT);
pinMode(limitButtonUp, INPUT_PULLUP);
pinMode(limitButtonDown, INPUT_PULLUP);
pinMode(buttonDown, INPUT_PULLUP);
pinMode(buttonUp, INPUT_PULLUP);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
spinner();
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", sendHtml);
server.on(UriBraces("/toggle/{}"), []() {
String led = server.pathArg(0);
Serial.print("Toggle LED #");
Serial.println(led);
switch (led.toInt()) {
case 1:
goUpState = !goUpState;
digitalWrite(buttonUp, goUpState);
break;
case 2:
goDownState = !goDownState;
digitalWrite(buttonDown, goDownState);
break;
}
sendHtml();
});
server.begin();
Serial.println("HTTP server started");
LCD.clear();
LCD.setCursor(2, 2);
LCD.print(" Online");
}
void loop(void) {
server.handleClient();
delay(10);
int ldrStatus = digitalRead(ldrPin);
int value = digitalRead((limitButtonUp));
float rozbresk = ldrStatus > 20 && ldrStatus < 50; // nastavení intenzity fotorezistoru - ROZEDNENI
float day = ldrStatus == 0; // nastavení intenzity fotorezistoru - DNE
float sero = ldrStatus < 50 && ldrStatus > 5; // nastavení intenzity fotorezistoru - SETMENI
float night = ldrStatus == 1; // nastavení intenzity fotorezistoru - NOCI
unsigned long currentTime = millis();
Serial.println(ldrStatus);
/// OVLADANI DVERI
limitUpState = digitalRead(limitButtonUp);
limitDownState = digitalRead(limitButtonDown);
goUpState = digitalRead(buttonUp);
goDownState = digitalRead(buttonDown);
if (limitDownState == 0){
otevreno = 0;
}
else {
otevreno = 1;
}
while(goDownState == 0){
if (otevreno == 1){
closingDoor ();
}
if (limitDownState == 0){
digitalWrite(openLed, LOW);
digitalWrite(closeLed, LOW);
LCD.setCursor(0, 0);
LCD.print(" ");
LCD.print("zavreno");
LCD.print(" ");
otevreno = 0;
}
break;
}
while(goUpState == 0){
if (otevreno == 0){
openingDoor ();
}
if (limitUpState == 0){
digitalWrite(openLed, LOW);
digitalWrite(closeLed, LOW);
LCD.setCursor(0, 0);
LCD.print(" ");
LCD.print("otevreno");
LCD.print(" ");
otevreno = 1;
}
break;
}
while (goUpState == 1 && goDownState == 1){
if (sero && limitUpState == 0){
for (i = 0; i < n; i++) {// Loop to do "something" n times
if (s == 00){
s=60;
m = m-1;
}
LCD.setCursor(0, 0);
LCD.print(" zavre za ");
LCD.print(m);
LCD.print(":");
LCD.print(s);
LCD.print(" ");
delay (1000);
s = s-1;
}
m = n/60;
s = n - (60 * m);
}
if (rozbresk && limitDownState == 0){
for (i = 0; i < n; i++) {// Loop to do "something" n times
if (s == 00){
s=60;
m = m-1;
}
LCD.setCursor(0, 0);
LCD.print(" otevre za ");
LCD.print(m);
LCD.print(":");
LCD.print(s);
LCD.print(" ");
delay (1000);
s = s-1;
}
m = n/60;
s = n - (60 * m);
}
while (night) {
if (limitUpState == 0){
closingDoor ();
}
if (limitDownState == 0){
digitalWrite(openLed, LOW);
digitalWrite(closeLed, LOW);
LCD.setCursor(0, 0);
LCD.print(" ");
LCD.print("zavreno");
LCD.print(" ");
}
unsigned long currentTime = millis();
if (currentTime - previousTime > timeInterval) {
if (limitDownState == 1 && limitUpState == 1){
error ();
}
previousTime = currentTime;
}
break;
}
while (day) {
if (limitDownState == 0){
openingDoor ();
}
if (limitUpState == 0){
digitalWrite(openLed, LOW);
digitalWrite(closeLed, LOW);
LCD.setCursor(0, 0);
LCD.print(" ");
LCD.print("otevreno");
LCD.print(" ");
}
break;
}
break;
}
}