-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathesp8266Switch.ino
58 lines (47 loc) · 1.28 KB
/
esp8266Switch.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <Bounce2.h>
#include <Homie.h>
#include <Button.h>
const int PIN_RELAY = 15;
const int PIN_LED = 2;
const int PIN_BUTTON = 13;
HomieNode switchNode("plug", "switch");
Button button1(PIN_BUTTON); // Connect your button between pin 2 and GND
bool lightOnHandler(HomieRange range, String value) {
if (value == "true") {
digitalWrite(PIN_RELAY, HIGH);
Homie.setNodeProperty(switchNode, "on").send("true");
Serial.println("Light is on");
// switchState = true;
} else if (value == "false") {
digitalWrite(PIN_RELAY, LOW);
Homie.setNodeProperty(switchNode, "on").send("false");
Serial.println("Light is off");
// switchState = false;
} else {
Serial.print("Error Got: ");
Serial.println(value);
return false;
}
return true;
}
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
//pinMode(PIN_BUTTON,INPUT_PULLUP);
pinMode(PIN_RELAY, OUTPUT);
digitalWrite(PIN_RELAY, LOW);
Homie.setLedPin(PIN_LED, LOW);
//Homie.setResetTrigger(PIN_BUTTON, LOW, 5000);
Homie_setFirmware("ecoplug", "1.0.0");
switchNode.advertise("on").settable(lightOnHandler);
button1.begin();
Homie.setup();
}
void loop() {
Homie.loop();
if (button1.pressed())
{
digitalWrite(PIN_RELAY, !digitalRead(PIN_RELAY));
}
}