This commit is contained in:
11
src/main.cpp
11
src/main.cpp
@ -5,14 +5,17 @@ WiFiUDP udpClient;
|
||||
Syslog syslog(udpClient, SYSLOG_PROTO_IETF);
|
||||
|
||||
const int PIN_RELAY = D1;
|
||||
#define OUTPUT(x) digitalWrite(PIN_RELAY, x ? LOW : HIGH);
|
||||
int timer = 0;
|
||||
HomieNode powerNode("power", "switch");
|
||||
|
||||
bool powerStateHandler(const HomieRange& range, const String& value) {
|
||||
if (value != "true" && value != "false") return false;
|
||||
bool on = (value == "true");
|
||||
digitalWrite(PIN_RELAY, on ? LOW : HIGH);
|
||||
OUTPUT(on);
|
||||
powerNode.setProperty("state").send(value);
|
||||
if(!on)
|
||||
powerNode.setProperty("timer").send("0");
|
||||
Homie.getLogger() << "Power is " << (on ? "on" : "off") << endl;
|
||||
|
||||
return true;
|
||||
@ -26,16 +29,16 @@ bool powerTimerHandler(const HomieRange& range, const String& value) {
|
||||
powerNode.setProperty("timer").send(String(settimer));
|
||||
powerNode.setProperty("state").send("on");
|
||||
Homie.getLogger() << "Power is on for " << settimer << "minutes" << endl;
|
||||
digitalWrite(PIN_RELAY, on ? LOW : HIGH);
|
||||
OUTPUT(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void setupHandler() {
|
||||
pinMode(PIN_RELAY, OUTPUT);
|
||||
digitalWrite(PIN_RELAY, HIGH);
|
||||
|
||||
OUTPUT(false);
|
||||
powerNode.setProperty("state").send("off");
|
||||
powerNode.setProperty("timer").send("0");
|
||||
}
|
||||
|
||||
void onHomieEvent(const HomieEvent& event) {
|
||||
|
Reference in New Issue
Block a user