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