#include "MyMainLight.h"
#include "MyMotionSensor.h"
#include "MyAlarmClock.h"
#include "MyWiFi.h"
#include "MyLCD.h"
IOTStates iotStates;
PinsMainLight pinsMainLight;
MyMainLight myMainLight(pinsMainLight); // Переключаетель дневного света по кнопкам и реле.
PinsMotionSensor pinsMotionSensor1;
MyMotionSensor myMotionSensor1(pinsMotionSensor1); // Датчик движения для дневного света.
PinsMotionSensor pinsMotionSensor2;
MyMotionSensor myMotionSensor2(pinsMotionSensor2); // Датчик движения для ночного света.
PinsAlarmClock pinsAlarmClock;
MyAlarmClock myAlarmClock(pinsAlarmClock);
WiFiInit wifiInit;
MyWiFi myWiFi(wifiInit);
LiquidCrystal_I2C lcd(0x27, 16, 2);
PinsJoystick pinsJoystick;
MyLCD myLCD(lcd, pinsJoystick, iotStates);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
myMotionSensor1.activate(iotStates.dayLightState);
myMotionSensor1.setPeriodWork(convertTimeToInt("07:01"), convertTimeToInt("22:59"));
myMotionSensor1.setDelay(iotStates.dayLightDelay);
myMotionSensor1.mySetup();
myMotionSensor2.setDelayMax(iotStates.dayLightDelayTimeMax);
myMotionSensor2.activate(iotStates.nightLightState);
myMotionSensor2.setPeriodWork(convertTimeToInt("23:00"), convertTimeToInt("07:00"));
pinsMotionSensor2.pinRelayIN = 4;
myMotionSensor2.setPins(pinsMotionSensor2);
myMotionSensor2.setDelay(iotStates.nightLightDelay);
myMotionSensor2.mySetup();
myAlarmClock.activate(iotStates.alarmClockState);
myAlarmClock.setAlarmTime(iotStates.alarmTime);
myAlarmClock.setDelayTime(iotStates.alarmClockDelay);
myAlarmClock.mySetup();
myWiFi.mySetup();
myLCD.mySetup();
}
int relayState = LOW;
bool isRiseEmbeddedTime = false;
int startEmbeddedTime = 0;
void riseEmbeddedTime()
{
if (millis() - startEmbeddedTime >= 60000)
{
iotStates.currentTime++;
startEmbeddedTime = millis();
}
}
void loop() {
myWiFi.start();
if (!myWiFi.isConnected())
{
if (!isRiseEmbeddedTime)
{
isRiseEmbeddedTime = !isRiseEmbeddedTime;
startEmbeddedTime = millis();
riseEmbeddedTime();
}
}
else
{
if (isRiseEmbeddedTime)
{
isRiseEmbeddedTime = !isRiseEmbeddedTime;
}
iotStates.currentTime = convertTimeToInt(myWiFi.getCurrentTime());
}
myMainLight.setRelayState(relayState);
myMainLight.process();
relayState = myMainLight.getRelayState();
myMotionSensor1.activate(iotStates.dayLightState);
myMotionSensor1.setDelay(iotStates.dayLightDelay);
myMotionSensor1.setCurrentTime(iotStates.currentTime);
myMotionSensor1.setRelayState(relayState);
myMotionSensor1.process();
relayState = myMotionSensor1.getRelayState();
myMotionSensor2.activate(iotStates.nightLightState);
myMotionSensor2.setDelay(iotStates.nightLightDelay);
myMotionSensor2.setCurrentTime(iotStates.currentTime);
myMotionSensor2.process();
myAlarmClock.setAlarmTime(iotStates.alarmTime);
myAlarmClock.activate(iotStates.alarmClockState);
myAlarmClock.setDelay(iotStates.alarmClockDelay);
myAlarmClock.setCurrentTime(iotStates.currentTime);
myAlarmClock.process();
myLCD.setIOTStates(iotStates);
myLCD.process();
iotStates = myLCD.getIOTStates();
delay(10); // this speeds up the simulation
}
int convertTimeToInt(const String& time)
{
if (time.length() != 5)
{
return -1;
}
int indexMM = time.indexOf(':');
int HH = time.substring(0, indexMM).toInt();
int MM = time.substring(indexMM + 1, indexMM + 3).toInt();
int result = HH * 60 + MM;
return result;
}