forked from scottjgibson/esp8266Switch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp8266Switch_smartthings.ino
66 lines (55 loc) · 1.49 KB
/
esp8266Switch_smartthings.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
59
60
61
62
63
64
65
66
#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("switch", "switch");
Button button1(PIN_BUTTON); // Connect your button between pin 2 and GND
bool lightOnHandler(HomieRange range, String value) {
if (value == "on") {
digitalWrite(PIN_RELAY, HIGH);
switchNode.setProperty("state").send("on");
Serial.println("Light is on");
} else if (value == "off") {
digitalWrite(PIN_RELAY, LOW);
switchNode.setProperty("state").send("off");
Serial.println("Light is off");
} 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_setFirmware("ecoplug", "1.0.0");
switchNode.advertise("state").settable(lightOnHandler);
button1.begin();
pinMode(PIN_BUTTON, INPUT);
Homie.setup();
}
void loop() {
Homie.loop();
if (button1.pressed())
{
if (digitalRead(PIN_RELAY) == HIGH)
{
digitalWrite(PIN_RELAY, LOW);
switchNode.setProperty("state").send("off");
Serial.println("Light is off");
} else if (digitalRead(PIN_RELAY) == LOW)
{
digitalWrite(PIN_RELAY, HIGH);
switchNode.setProperty("state").send("on");
Serial.println("Light is on");
}
}
}