-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
157 lines (137 loc) · 3.23 KB
/
main.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Bounce2.h>
#include <EEPROM.h>
const char* ssid = "ssid";
const char* password = "password";
const char* mqtt_server = "ip-address";
WiFiClient switch_light;
PubSubClient client(switch_light);
long lastMsg = 0;
char msg[50];
int value = 0;
const char* outTopic = "home/light/sub";
const char* inTopic = "home/light/pub";
int relay_pin = D7;
int button_pin = D3;
int wifi_led_pin = D1;
bool relayState = LOW;
Bounce debouncer = Bounce();
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
int extButton();
for (int i = 0; i<500; i++) {
extButton();
delay(1);
}
Serial.print(".");
}
digitalWrite(wifi_led_pin, LOW);
delay(500);
digitalWrite(wifi_led_pin, HIGH);
delay(500);
digitalWrite(wifi_led_pin, LOW);
delay(500);
digitalWrite(wifi_led_pin, HIGH);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
if ((char)payload[0] == '0') {
digitalWrite(relay_pin, LOW);
client.publish(outTopic, "0");
Serial.println("relay_pin -> LOW");
relayState = LOW;
EEPROM.write(0, relayState);
EEPROM.commit();
}
else if ((char)payload[0] == '1') {
digitalWrite(relay_pin, HIGH);
client.publish(outTopic, "1");
Serial.println("relay_pin -> HIGH");
relayState = HIGH;
EEPROM.write(0, relayState);
EEPROM.commit();
}
else if ((char)payload[0] == '2') {
relayState = !relayState;
digitalWrite(relay_pin, relayState);
Serial.print("relay_pin -> switched to ");
Serial.println(relayState);
EEPROM.write(0, relayState);
EEPROM.commit();
}
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP8266")) {
Serial.println("connected");
client.subscribe(inTopic);
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
for (int i = 0; i<5000; i++) {
int extButton();
delay(1);
}
}
}
}
void extButton() {
debouncer.update();
if (debouncer.fell()) {
Serial.println("Debouncer fell");
relayState = !relayState;
digitalWrite(relay_pin, relayState);
EEPROM.write(0, relayState);
EEPROM.commit();
if (relayState == 1) {
client.publish(outTopic, "1");
}
else if (relayState == 0) {
client.publish(outTopic, "0");
}
}
}
void setup() {
EEPROM.begin(512);
pinMode(relay_pin, OUTPUT);
pinMode(button_pin, INPUT_PULLUP);
pinMode(wifi_led_pin, OUTPUT);
relayState = EEPROM.read(0);
digitalWrite(relay_pin, relayState);
debouncer.attach(button_pin);
debouncer.interval(50);
digitalWrite(wifi_led_pin, LOW);
delay(500);
digitalWrite(wifi_led_pin, HIGH);
delay(500);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
extButton();
}