按时间表激活继电器



我有一个问题,无法很好地理解这里发生了什么。我正在按照用户选择的时间表按时激活中继。问题是停止时间在午夜之后,而开始时间在午夜之前。我检查是否该切换的功能是:

bool lightsOn = false;
void checkOnOff() {
//Check time for ON/OFF the ligth
struct tm *curTime = getTime();
unsigned int now = curTime->tm_hour * 100 + curTime->tm_min;
unsigned int start = config.ligths_on_time.tm_hour * 100 + config.ligths_on_time.tm_min;
unsigned int stop = config.ligths_off_time.tm_hour * 100 + config.ligths_off_time.tm_min;
if ( (now >= start || now < stop) && (now <= stop) ) { //<-- this line is hard to have it in mind
if(!lightsOn) enableLights();
} else {
if(lightsOn) disableLights();
}
}
void enableLights() { //the same for disableLights() but with false, reverse HIGH/LOW and 0
lightsOn = true;
digitalWrite(EXTRA_LED_PIN, HIGH);
digitalWrite(ON_OFF_RELAY_PIN, HIGH);
sendLightsOnOffToServer(1);
Serial.println(F("Lights enabled."));
}
struct tm *getTime() {
//if (NTP_TIME_SETTED) {
time_t now = time(nullptr);
//gmtime_r(&now, &timeinfo); //utc
return localtime(&now);
//}
}

有人能向我解释怎么做吗?

  • 如果stop小于start,则将其调整24小时
  • 如果停止时间为明天,并且ON_OFF状态为ON,则将now时间调整24小时,并且现在<启动
  • 重新调整逻辑以使用ON_OFF状态来确定是检查启动还是停止
#define HOUR 100
#define DAY (HOUR * 24)
#define ON   true
#define OFF  false
unsigned int now = curTime->tm_hour * HOUR + 
curTime->tm_min ;
unsigned int start = config.ligths_on_time.tm_hour * HOUR + 
config.ligths_on_time.tm_min;
unsigned int stop = config.ligths_off_time.tm_hour * HOUR  + 
config.ligths_off_time.tm_min;
// Fix-up stop and now time to account for stop in next day
if( stop < start) 
{
stop += DAY ;
if( ON_OFF == ON && now < start )
{
now += DAY ;
}
}
// If OFF, check for start condition
if( ON_OFF == OFF && now >= start )
{
enableLight() ;
}
// ... otherwise if ON, check for stop condition
else if( ON_OFF == ON && now >= stop )
{
disableLight() ;
}

最新更新