-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonoff_socket.ino
261 lines (233 loc) · 6.94 KB
/
sonoff_socket.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "config.h"
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Ticker.h>
#include "PubSubClient.h"
#define STATLED 13
#define RELAY 12
#define BUTTON 0
#define SPIFFS_ALIGNED_OBJECT_INDEX_TABLES 1
ESP8266WebServer server(80);
WiFiClient espClient;
PubSubClient client(espClient);
String macAddress = "";
int relayState = 0;
unsigned long lastMQTTReconnectAttempt = 0;
unsigned long lastWiFiReconnectAttempt = 0;
Ticker btn_timer;
unsigned long btnCount = 0;
String MQTT_UPDATE_TOPIC_FULL = "";
String MQTT_DEVICE_TOPIC_FULL = "";
String MQTT_STATE_TOPIC = "";
boolean requireRestart = false;
bool publishToMQTT(String topic, String payload, bool retain = true);
void setRelay(boolean state) {
digitalWrite(RELAY, state);
relayState = state;
publishToMQTT(MQTT_STATE_TOPIC, (relayState ? "on" : "off"), true);
}
void serveJSON() {
String json = "{\"buildDate\": \"" + String(BUILD_DATE) + "\","
+ "\"deviceName\": \"" + String(DEVICE_NAME) + "\","
+ "\"deviceDescription\": \"" + String(DEVICE_DESCRIPTION) + "\","
+ "\"mac\": \"" + macAddress + "\","
+ "\"sketchSize\": \"" + String(ESP.getSketchSize()) + "\","
+ "\"freeSketchSize\": \"" + String(ESP.getFreeSketchSpace()) + "\","
+ "\"flashChipSize\": \"" + String(ESP.getFlashChipSize()) + "\","
+ "\"realFlashSize\": \"" + String(ESP.getFlashChipRealSize()) + "\","
+ "\"updateTopic\": \"" + MQTT_UPDATE_TOPIC_FULL + "\","
+ "\"relayState\": \"" + (relayState ? "on" : "off") + "\","
+ "\"mqttState\": \"" + (client.connected() ? "" : "dis") + "connected\""
+ "}";
server.send(200, "application/json", json);
}
void mqttDisConnect() {
if (client.connected()) {
client.disconnect();
delay(10);
}
serveJSON();
}
void mqttReConnect() {
if (client.connected()) {
client.disconnect();
delay(50);
}
connectToMQTT();
lastMQTTReconnectAttempt = 0;
delay(10);
serveJSON();
}
boolean connectToMQTT() {
Serial.print("Trying to connect MQTT broker...");
MQTT::Connect con(DEVICE_NAME);
con.set_clean_session();
con.set_will(MQTT_DEVICE_TOPIC_FULL, "");
con.set_keepalive(30);
if (MQTT_USER) {
con.set_auth(MQTT_USER, MQTT_PASSWORD);
}
if (client.connect(con)) {
Serial.println(" success");
client.subscribe(MQTT_TOPIC);
client.subscribe(MQTT_UPDATE_TOPIC_FULL);
String deviceData = "{\"name\": \"" + String(DEVICE_NAME) + "\", \"deviceDescription\": \"" + String(DEVICE_DESCRIPTION) + "\", \"ip\": \"" + WiFi.localIP().toString() + "\", \"mac\": \"" + macAddress + "\", \"buildDate\": \"" + String(BUILD_DATE) + "\"}";
publishToMQTT(MQTT_DEVICE_TOPIC_FULL, deviceData, true);
publishToMQTT(MQTT_STATE_TOPIC, (relayState ? "on" : "off"), true);
} else {
Serial.println(" failed");
}
return client.connected();
}
void receiveFromMQTT(const MQTT::Publish& pub) {
Serial.print("Message arrived [");
Serial.print(pub.topic());
Serial.print("] Size: " + String(pub.payload_len()) + " B");
Serial.println();
digitalWrite(STATLED, false);
if (pub.topic() == MQTT_TOPIC) {
if (pub.payload_string() == "on") {
setRelay(true);
} else {
setRelay(false);
}
} else if (pub.topic() == MQTT_UPDATE_TOPIC_FULL) {
ESP.wdtFeed();
uint32_t size = pub.payload_len();
if (size == 0) {
Serial.println("Error, sketch size is 0 B");
} else {
Serial.println("Receiving firmware update of " + String(size) + " bytes...");
Serial.setDebugOutput(false);
ESP.wdtFeed();
if (ESP.updateSketch(*pub.payload_stream(), size, false, false)) {
ESP.wdtFeed();
Serial.println("Clearing retained message.");
publishToMQTT(MQTT_UPDATE_TOPIC_FULL, "", true);
ESP.wdtFeed();
Serial.println("Update success");
requireRestart = true;
}
}
}
digitalWrite(STATLED, true);
}
bool publishToMQTT(String topic, String payload, bool retain) {
if (!client.connected()) {
connectToMQTT();
}
if (retain) {
client.publish(MQTT::Publish(topic, payload).set_retain());
} else {
client.publish(topic, payload);
}
client.loop();
return true;
}
boolean connectToWiFi() {
WiFi.mode(WIFI_STA);
Serial.println();
Serial.print("Connecting to WiFi network ");
Serial.println(sta_ssid);
WiFi.begin(sta_ssid, sta_password);
int i = 0;
while (WiFi.status() != WL_CONNECTED) {
ESP.wdtFeed();
delay(500);
Serial.print(".");
i++;
if (i >= 20) break;
}
Serial.println("");
if (WiFi.status() == WL_CONNECTED) {
Serial.print("WiFi connected, IP address: ");
Serial.println(WiFi.localIP());
return true;
} else {
Serial.print("Unable to connect\n");
return false;
}
}
void handleButton() {
if (!digitalRead(BUTTON)) {
btnCount++;
} else {
if (btnCount > 1 && btnCount <= 40) {
setRelay(!relayState);
} else if (btnCount > 40){
requireRestart = true;
}
btnCount=0;
}
}
void setup() {
macAddress = WiFi.macAddress();
ESP.wdtDisable();
pinMode(STATLED, OUTPUT);
digitalWrite(STATLED, false);
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, false);
pinMode(BUTTON, INPUT);
Serial.begin(115200);
delay(100);
Serial.println("");
Serial.println("Build date: " + String(BUILD_DATE));
Serial.println("MAC address: " + macAddress + "\n");
connectToWiFi();
server.on("/", serveJSON);
server.on("/mqttreconnect", mqttReConnect);
server.on("/mqttdisconnect", mqttDisConnect);
server.begin();
MQTT_DEVICE_TOPIC_FULL = MQTT_DEVICE_TOPIC + String(DEVICE_NAME);
MQTT_UPDATE_TOPIC_FULL = MQTT_DEVICE_TOPIC_FULL + String("/update");
MQTT_STATE_TOPIC = MQTT_TOPIC + String("/state");
client.set_server(MQTT_BROKER_ADDRESS, MQTT_BROKER_PORT);
client.set_callback(receiveFromMQTT);
lastMQTTReconnectAttempt = 0;
btn_timer.attach(0.05, handleButton);
Serial.println("MQTT update topic: " + MQTT_UPDATE_TOPIC_FULL);
digitalWrite(STATLED, true);
ESP.wdtEnable(5000);
}
void loop() {
ESP.wdtFeed();
if (requireRestart) {
client.loop();
requireRestart = false;
Serial.println("Reboot...");
client.disconnect();
ESP.wdtDisable();
delay(200);
ESP.restart();
while (1) {
ESP.wdtFeed();
delay(200);
digitalWrite(STATLED, !digitalRead(STATLED));
}
}
if (WiFi.status() == WL_CONNECTED) {
server.handleClient();
if (client.connected()) {
client.loop();
} else {
unsigned long now = millis();
if (now - lastMQTTReconnectAttempt >= 5000) {
if (connectToMQTT()) {
lastMQTTReconnectAttempt = 0;
} else {
lastMQTTReconnectAttempt = millis();
}
}
}
} else {
unsigned long now = millis();
if (now - lastWiFiReconnectAttempt >= 2000) {
if (connectToWiFi()) {
lastWiFiReconnectAttempt = 0;
} else {
lastWiFiReconnectAttempt = millis();
}
}
}
}