Период

main
Bill1389 1 year ago
parent 31b9ba52fb
commit 5d8283387e

@ -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 секунда
// каждую секунду будем обновлять заголовок

@ -0,0 +1,165 @@
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <EEPROM.h>
const char *ssid = "Bill1389";
const char *password = "B4cvxw43bt";
#define GH_INCLUDE_PORTAL
#include <GyverHub.h>
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));
}
}
Loading…
Cancel
Save