diff --git a/src/main.cpp b/src/main.cpp index 25a7daf..d03a5d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,6 +55,7 @@ int statePeriodLamp; int minutePeriodLamp; int minuteOnPeriodLamp; + int statePeriodFanCooling; int minutePeriodFanCooling; int minuteOnPeriodFanCooling; @@ -369,8 +370,37 @@ void build(gh::Builder& b) { } } +bool checkPeriod(int startMinute, unsigned long &previousMillis) { + const unsigned long interval = 60000UL * startMinute; // Интервал в миллисекундах для выполнения действия каждые startMinute минут + + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; // Обновляем время последнего выполнения + return true; + } else { + return false; + } +} + +unsigned long previousMillisPeriodLamp = 0; +unsigned long previousMillisStopPeriodLamp = 0; + +void checkPeriodLight() { + if (statePeriodLamp == 1) { + if (checkPeriod(minutePeriodLamp, previousMillisPeriodLamp)) { + Serial.println("ON"); + Serial.println(previousMillisPeriodLamp); + previousMillisStopPeriodLamp = previousMillisPeriodLamp; + } + + if (checkPeriod(minuteOnPeriodLamp, previousMillisStopPeriodLamp)) { + Serial.println("OFF"); + } + } +} void periodLight() { + //Serial.println(statePeriodLamp); if(statePeriodLamp == 1){ Serial.println(statePeriodLamp); @@ -478,7 +508,7 @@ void loop() hub.tick(); - + checkPeriodLight(); static gh::Timer tmr(1000); // период 1 секунда // каждую секунду будем обновлять заголовок diff --git a/test/main copy.cpp b/test/main copy.cpp new file mode 100644 index 0000000..b35e295 --- /dev/null +++ b/test/main copy.cpp @@ -0,0 +1,165 @@ +#include +#include +#include +#include + +const char *ssid = "Bill1389"; +const char *password = "B4cvxw43bt"; + +#define GH_INCLUDE_PORTAL +#include +GyverHub hub("MyDevices", "ESP", ""); + +WiFiUDP ntpUDP; +NTPClient timeClient(ntpUDP, "pool.ntp.org"); + +const int ledPin = D4; // Указывайте пин, на котором подключен светодиод + +void writeIntToEEPROM(int address, int value) +{ + // EEPROM.begin(sizeof(value)); + EEPROM.put(address, value); + EEPROM.commit(); + // EEPROM.end(); +} + +int readIntFromEEPROM(int address) +{ + int value; + // EEPROM.begin(sizeof(value)); + EEPROM.get(address, value); + // EEPROM.end(); + return value; +} + +int switchLightState; +int switchLightStateOld; +int currentTimeSec; +int startTime; +int stopTime; +int startTimeOld; +int stopTimeOld; + +char currentTime[10]; // Выделение памяти под строку + +void checkLight() +{ + // Получаем текущее время + int currentHour = timeClient.getHours() + 3; + int currentMinute = timeClient.getMinutes(); + int currentSeconds = timeClient.getSeconds(); + + if (startTimeOld != startTime) + { + writeIntToEEPROM(sizeof(int), startTime); + startTimeOld = startTime; + } + + if (stopTimeOld != stopTime) + { + + writeIntToEEPROM(2 * sizeof(int), stopTime); + stopTimeOld = stopTime; + } + + snprintf(currentTime, sizeof(currentTime), "%02d:%02d", currentHour, currentMinute); + currentTimeSec = currentHour * 3600 + currentMinute * 60 + currentSeconds; + + // Проверяем условие освещения + + if (switchLightState != switchLightStateOld) + { + writeIntToEEPROM(0, switchLightState); + switchLightStateOld = switchLightState; + } + + switch (switchLightState) + { + case 0: + digitalWrite(ledPin, LOW); // Включаем светодиод + break; + case 1: + digitalWrite(ledPin, HIGH); // Выключаем светодиод + break; + case 2: + if((startTime <= currentTimeSec) and ( currentTimeSec <= stopTime)) + { + digitalWrite(ledPin, LOW); // Включаем светодиод + } + else + { + digitalWrite(ledPin, HIGH); // Выключаем светодиод + } + break; + + default: + digitalWrite(ledPin, HIGH); // Выключаем светодиод + break; + } +} + + +void build(gh::Builder &b) +{ + + b.Title_(F("title1")).value("Освещение"); + if (b.beginRow()) + { + b.Time_("startTime", &startTime).label("Время включения").size(2); + b.Label_(F("currentTime")).label("Текущее время").color(0xd55f30).size(2); + + b.Time_("stopTime", &stopTime).label("Время отключения").size(2); + b.endRow(); + } + + { + gh::Row r(b); + b.Tabs_("tab", &switchLightState).text("Включить;Выключить;По таймеру").label("Выключатель света").size(3); + } +} + +void setup() +{ + Serial.begin(10400); + EEPROM.begin(128); + + switchLightState = readIntFromEEPROM(0); + startTime = readIntFromEEPROM(sizeof(int)); + stopTime = readIntFromEEPROM(2 * sizeof(int)); + + // Подключение к WiFi + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) + { + delay(1000); + Serial.println("Connecting to WiFi..."); + } + Serial.println("Connected to WiFi"); + + // Инициализация светодиода + pinMode(ledPin, OUTPUT); + + // Настройка NTP + timeClient.begin(); +hub.mqtt.config(IPAddress(95, 30, 222, 200), 1883); + hub.config(F("MyDevices"), F("ESP"), F("")); + // подключить билдер + hub.onBuild(build); + + // запуск! + hub.begin(); +} + +void loop() +{ + timeClient.update(); // Обновляем время + checkLight(); + hub.tick(); + static gh::Timer tmr(1000); // период 1 секунда + + if (tmr) + { + + hub.update(F("currentTime")).value(String(currentTime)); + } +} \ No newline at end of file