// #include <ESP8266WiFi.h>
// #include <ESPAsyncWebServer.h>
// #include <time.h>
// ----- USER PINS (uprav si) -----
const int RELAY_PIN = 5; // GPIO5 = D1
const int BUTTON_PIN = 4; // GPIO4 = D2
const int BUZZER_PIN = 10; // GPIO14 = D5
// ----- CONFIG -----
// const char* ssid = "your_ssid";
// const char* pass = "your_password";
// AsyncWebServer server(80);
// ----- TIMERS -----
unsigned long lastBeep = 0;
unsigned long lastButton = 0;
unsigned long startTime = 0;
const unsigned long BEEP_INTERVAL = 1UL * 60UL * 1000UL; // 5 min
const unsigned long MAX_TIME_NO_BUTTON = 5UL * 60UL * 1000UL; // 25 min
unsigned long allowedTime = MAX_TIME_NO_BUTTON; // čas, kedy sa vypne relé
// =====================================================================
// SETUP
// =====================================================================
void setup() {
Serial.begin(115200);
delay(200);
// Pins
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Rele zapnute od startu
digitalWrite(BUZZER_PIN, LOW);
// WiFi
// WiFi.mode(WIFI_STA);
// WiFi.begin(ssid, pass);
Serial.print("Connecting WiFi");
// while (WiFi.status() != WL_CONNECTED) {
// delay(300);
// Serial.print(".");
// }
// Serial.println("\nWiFi connected");
// Serial.println(WiFi.localIP());
// NTP
// configTime(0, 0, "pool.ntp.org", "time.nist.gov");
Serial.println("Waiting for time...");
// time_t now = time(nullptr);
// while (now < 100000) {
// delay(200);
// Serial.print(".");
// now = time(nullptr);
// }
Serial.println("\nTime synchronized");
// AsyncWebServer - skeleton only
// server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
// request->send(200, "text/plain", "ESP8266 alive");
// });
// server.begin();
// Initialize timers
startTime = millis();
lastBeep = millis();
lastButton = millis();
}
// =====================================================================
// LOOP
// =====================================================================
void loop() {
unsigned long now = millis();
// ----- Bzučiak každých 5 minút -----
if (now - lastBeep >= BEEP_INTERVAL) {
lastBeep = now;
tone(BUZZER_PIN, 2000, 150); // krátky beep
Serial.println("Beep!");
}
// ----- Tlačidlo -----
if (digitalRead(BUTTON_PIN) == LOW) {
lastButton = now;
allowedTime += 5UL * 60UL * 1000UL; // +5 minút
tone(BUZZER_PIN, 1500, 80);
delay(200); // debounce
Serial.println("Button pressed, extra 5 minutes added.");
}
// ----- Kontrola času bez tlačidla -----
if (now - startTime >= allowedTime) {
Serial.println("Time expired. Turning relay OFF.");
digitalWrite(RELAY_PIN, LOW);
}
}
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1