Prototype of timer implementation
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
42
src/main.cpp
42
src/main.cpp
@ -6,17 +6,22 @@ Syslog syslog(udpClient, SYSLOG_PROTO_IETF);
|
||||
|
||||
const int PIN_RELAY = D1;
|
||||
#define OUTPUT_SET(x) digitalWrite(PIN_RELAY, x ? LOW : HIGH);
|
||||
int timer = 0;
|
||||
unsigned int timer = 0;
|
||||
unsigned int next_timer_update = 0;
|
||||
HomieNode powerNode("power", "switch");
|
||||
|
||||
bool powerStateHandler(const HomieRange& range, const String& value) {
|
||||
if (value != "true" && value != "false") return false;
|
||||
bool on = (value == "true");
|
||||
OUTPUT_SET(on);
|
||||
if (value != "on" && value != "off") return false;
|
||||
powerNode.setProperty("state").send(value);
|
||||
if(!on)
|
||||
powerNode.setProperty("timer").send("0");
|
||||
Homie.getLogger() << "Power is " << (on ? "on" : "off") << endl;
|
||||
timer = 0;
|
||||
if(value == "on") {
|
||||
OUTPUT_SET(true);
|
||||
Homie.getLogger() << "Power is on" << endl;
|
||||
} else {
|
||||
OUTPUT_SET(false);
|
||||
Homie.getLogger() << "Power is off" << endl;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -26,6 +31,8 @@ bool powerTimerHandler(const HomieRange& range, const String& value) {
|
||||
if(settimer == 0 || settimer > 120)
|
||||
return false;
|
||||
|
||||
timer = millis() + (settimer * 1000 * 60);
|
||||
next_timer_update = millis() + 60*1000;
|
||||
powerNode.setProperty("timer").send(String(settimer));
|
||||
powerNode.setProperty("state").send("on");
|
||||
Homie.getLogger() << "Power is on for " << settimer << "minutes" << endl;
|
||||
@ -41,6 +48,25 @@ void setupHandler() {
|
||||
powerNode.setProperty("timer").send("0");
|
||||
}
|
||||
|
||||
void loopHandler() {
|
||||
if(timer)
|
||||
{
|
||||
if(timer < millis())
|
||||
{
|
||||
timer = 0;
|
||||
OUTPUT_SET(false);
|
||||
powerNode.setProperty("timer").send("0");
|
||||
}
|
||||
if(millis() > next_timer_update) {
|
||||
next_timer_update = millis() + 60*1000;
|
||||
unsigned int time_remaining = timer - millis();
|
||||
unsigned int min_left = time_remaining / (60*1000);
|
||||
powerNode.setProperty("timer").send(String(min_left));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void onHomieEvent(const HomieEvent& event) {
|
||||
switch(event.type) {
|
||||
case HomieEventType::OTA_FAILED:
|
||||
@ -50,9 +76,6 @@ void onHomieEvent(const HomieEvent& event) {
|
||||
case HomieEventType::OTA_SUCCESSFUL:
|
||||
syslog.log(LOG_INFO, "OTA completed");
|
||||
break;
|
||||
case HomieEventType::ABOUT_TO_RESET:
|
||||
// Do whatever you want when the device is about to reset
|
||||
break;
|
||||
case HomieEventType::WIFI_CONNECTED:
|
||||
// Do whatever you want when Wi-Fi is connected in normal mode
|
||||
syslog.deviceHostname(Homie.getConfiguration().name);
|
||||
@ -87,6 +110,7 @@ void setup() {
|
||||
powerNode.advertise("timer").settable(powerTimerHandler);
|
||||
Homie.setSetupFunction(setupHandler);
|
||||
Homie.onEvent(onHomieEvent); // before Homie.setup()
|
||||
Homie.setLoopFunction(loopHandler);
|
||||
Homie.setup();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user