-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP32_4ChannelRelay.ino
407 lines (372 loc) · 12.7 KB
/
ESP32_4ChannelRelay.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* This is some Arduino IDE code for an ESP32 4 channel relay. The relay used ON=Low OFF=High
* using ESP32 with Tasmota or ESPHome with the LOW=ON relay it was always turning on for a split second on boot. I wanted all relays always off on boot.
*
* This code is setup for an ESP32 Wemos D1 mini
*/
#include "EspMQTTClient.h"
#include "ArduinoJson.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <Update.h>
#include <ArduinoQueue.h>
/****************** CONFIGURATIONS TO CHANGE *******************/
static const char* host = "esp32"; // hostname defaults is esp32
static const char* ssid = "SSID"; // WIFI SSID
static const char* password = "Password"; // WIFI Password
static String otaUserId = "admin"; // user Id for OTA update
static String otaPass = "admin"; // password for OTA update
static WebServer server(80); // default port 80
static std::string ESPMQTTTopic = "relayMQTT"; // MQTT main topic
static EspMQTTClient client(
ssid,
password,
"192.168.1.XXX", // MQTT Broker server ip
"MQTTUsername", // Can be omitted if not needed
"MQTTPassword", // Can be omitted if not needed
"ESPRELAYMQTT", // Client name that uniquely identify your device
1883 // MQTT Port
);
static const char * relay1 = "relay1";
static const char * relay2 = "relay2";
static const char * relay3 = "relay3";
static const char * relay4 = "relay4";
static std::string mqttDeviceTopic = ESPMQTTTopic + "/relays/";
static const int switchPin1 = 16;
static const int switchPin2 = 17;
static const int switchPin3 = 18;
static const int switchPin4 = 19;
/*************************************************************/
/*
Login page
*/
static const String versionNum = "1.0";
static const String loginIndex =
"<form name='loginForm'>"
"<table width='20%' bgcolor='A09F9F' align='center'>"
"<tr>"
"<td colspan=2>"
"<center><font size=4><b>Relay ESP32 MQTT version: " + versionNum + "</b></font></center>"
"<center><font size=2><b>(Unofficial)</b></font></center>"
"<br>"
"</td>"
"<br>"
"<br>"
"</tr>"
"<tr>"
"<td>Username:</td>"
"<td><input type='text' size=25 name='userid'><br></td>"
"</tr>"
"<br>"
"<br>"
"<tr>"
"<td>Password:</td>"
"<td><input type='Password' size=25 name='pwd'><br></td>"
"<br>"
"<br>"
"</tr>"
"<tr>"
"<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
"</tr>"
"</table>"
"</form>"
"<script>"
"function check(form)"
"{"
"if(form.userid.value=='" + otaUserId + "' && form.pwd.value=='" + otaPass + "')"
"{"
"window.open('/serverIndex')"
"}"
"else"
"{"
" alert('Error Password or Username')/*displays error message*/"
"}"
"}"
"</script>";
/*
Server Index Page
*/
static const String serverIndex =
"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
"<input type='file' name='update'>"
"<input type='submit' value='Update'>"
"</form>"
"<div id='prg'>progress: 0%</div>"
"<script>"
"$('form').submit(function(e){"
"e.preventDefault();"
"var form = $('#upload_form')[0];"
"var data = new FormData(form);"
" $.ajax({"
"url: '/update',"
"type: 'POST',"
"data: data,"
"contentType: false,"
"processData:false,"
"xhr: function() {"
"var xhr = new window.XMLHttpRequest();"
"xhr.upload.addEventListener('progress', function(evt) {"
"if (evt.lengthComputable) {"
"var per = evt.loaded / evt.total;"
"$('#prg').html('progress: ' + Math.round(per*100) + '%');"
"}"
"}, false);"
"return xhr;"
"},"
"success:function(d, s) {"
"console.log('success!')"
"},"
"error: function (a, b, c) {"
"}"
"});"
"});"
"</script>";
static std::string esp32Str = ESPMQTTTopic + "/ESP32";
static std::string lastWillStr = ESPMQTTTopic + "/lastwill";
static const char* lastWill = lastWillStr.c_str();
static std::string controlStdStr = ESPMQTTTopic + "/control";
static bool firstBoot = true;
static const String controlStr = controlStdStr.c_str();
struct to_lower {
int operator() ( int ch )
{
return std::tolower ( ch );
}
};
struct QueueCommand {
std::string payload;
std::string topic;
};
static int queueSize = 50;
ArduinoQueue<QueueCommand> commandQueue(queueSize);
void setup () {
pinMode(switchPin1, OUTPUT); // Relay Switch 1
digitalWrite(switchPin1, HIGH);
pinMode(switchPin2, OUTPUT); // Relay Switch 2
digitalWrite(switchPin2, HIGH);
pinMode(switchPin3, OUTPUT); // Relay Switch 3
digitalWrite(switchPin3, HIGH);
pinMode(switchPin4, OUTPUT); // Relay Switch 4
digitalWrite(switchPin4, HIGH);
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
/*use mdns for host name resolution*/
if (!MDNS.begin(host)) { //http://esp32.local
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
/*return index page which is stored in serverIndex */
server.on("/", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", loginIndex);
});
server.on("/serverIndex", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", serverIndex);
});
/*handling uploading firmware file */
server.on("/update", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
}, []() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.printf("Update: %s\n", upload.filename.c_str());
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
/* flashing firmware to ESP*/
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
}
});
server.begin();
client.setMqttReconnectionAttemptDelay(100);
client.enableLastWillMessage(lastWill, "Offline");
client.setKeepAlive(60);
Serial.begin(115200);
}
void loop () {
server.handleClient();
client.loop();
processQueue();
updateStatuses();
delay(1);
}
static long lastRescan = 0;
static int rescanTime = 30;
void updateStatuses() {
if (!firstBoot) {
if ((millis() - lastRescan) >= (rescanTime * 1000)) {
int state1 = digitalRead(switchPin1);
int state2 = digitalRead(switchPin2);
int state3 = digitalRead(switchPin3);
int state4 = digitalRead(switchPin4);
if (state1 == LOW) {
client.publish((mqttDeviceTopic + relay1).c_str(), "{\"status\":\"ON\"}");
}
else if (state1 == HIGH) {
client.publish((mqttDeviceTopic + relay1).c_str(), "{\"status\":\"OFF\"}");
}
if (state2 == LOW) {
client.publish((mqttDeviceTopic + relay2).c_str(), "{\"status\":\"ON\"}");
}
else if (state2 == HIGH) {
client.publish((mqttDeviceTopic + relay2).c_str(), "{\"status\":\"OFF\"}");
}
if (state3 == LOW) {
client.publish((mqttDeviceTopic + relay3).c_str(), "{\"status\":\"ON\"}");
}
else if (state3 == HIGH) {
client.publish((mqttDeviceTopic + relay3).c_str(), "{\"status\":\"OFF\"}");
}
if (state4 == LOW) {
client.publish((mqttDeviceTopic + relay4).c_str(), "{\"status\":\"ON\"}");
}
else if (state4 == HIGH) {
client.publish((mqttDeviceTopic + relay4).c_str(), "{\"status\":\"OFF\"}");
}
lastRescan = millis();
}
}
}
bool processQueue() {
struct QueueCommand aCommand;
while (!commandQueue.isEmpty()) {
aCommand = commandQueue.getHead();
if (aCommand.topic == ESPMQTTTopic + "/control") {
controlMQTT(aCommand.payload);
}
commandQueue.dequeue();
}
return true;
}
bool is_number(const std::string & s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
void controlMQTT(std::string payload) {
Serial.println("Processing Control MQTT...");
StaticJsonDocument<100> docIn;
deserializeJson(docIn, payload);
if (docIn == nullptr) { //Check for errors in parsing
char aBuffer[100];
StaticJsonDocument<100> docOut;
Serial.println("Parsing failed");
docOut["status"] = "errorParsingJSON";
serializeJson(docOut, aBuffer);
client.publish(esp32Str.c_str(), aBuffer);
}
else {
const char * aName = docIn["id"];
const char * value = docIn["value"];
std::string deviceStr = mqttDeviceTopic + aName;
std::string aName2 = aName;
std::string value2 = value;
bool isNum = is_number(aName);
bool isNum2 = is_number(value);
int aVal = 99;
int aVal2 = 99;
if (isNum ) {
sscanf(aName, "%d", &aVal);
}
if (isNum2) {
sscanf(value, "%d", &aVal2);
}
std::transform(aName2.begin(), aName2.end(), aName2.begin(), to_lower());
std::transform(value2.begin(), value2.end(), value2.begin(), to_lower());
aName = aName2.c_str();
value = value2.c_str();
if (aVal == 1 || (strcmp(aName, relay1) == 0)) {
if (aVal2 == 1 || (strcmp(value, "off") == 0) ) {
digitalWrite(switchPin1, HIGH);
client.publish(deviceStr.c_str(), "{\"status\":\"OFF\"}");
}
else if (aVal2 == 0 || (strcmp(value, "on") == 0)) {
digitalWrite(switchPin1, LOW);
client.publish(deviceStr.c_str(), "{\"status\":\"ON\"}");
}
}
else if (aVal == 2 || (strcmp(aName, relay2) == 0)) {
if (aVal2 == 1 || (strcmp(value, "off") == 0) ) {
digitalWrite(switchPin2, HIGH);
client.publish(deviceStr.c_str(), "{\"status\":\"OFF\"}");
}
else if (aVal2 == 0 || (strcmp(value, "on") == 0)) {
digitalWrite(switchPin2, LOW);
client.publish(deviceStr.c_str(), "{\"status\":\"ON\"}");
}
}
else if (aVal == 3 || (strcmp(aName, relay3) == 0)) {
if (aVal2 == 1 || (strcmp(value, "off") == 0) ) {
digitalWrite(switchPin3, HIGH);
client.publish(deviceStr.c_str(), "{\"status\":\"OFF\"}");
}
else if (aVal2 == 0 || (strcmp(value, "on") == 0)) {
digitalWrite(switchPin3, LOW);
client.publish(deviceStr.c_str(), "{\"status\":\"ON\"}");
}
}
else if (aVal == 4 || (strcmp(aName, relay4) == 0)) {
if (aVal2 == 1 || (strcmp(value, "off") == 0) ) {
digitalWrite(switchPin4, HIGH);
client.publish(deviceStr.c_str(), "{\"status\":\"OFF\"}");
}
else if (aVal2 == 0 || (strcmp(value, "on") == 0)) {
digitalWrite(switchPin4, LOW);
client.publish(deviceStr.c_str(), "{\"status\":\"ON\"}");
}
}
}
delay(100);
client.publish(esp32Str.c_str(), "{\"status\":\"idle\"}");
}
void onConnectionEstablished() {
if (firstBoot) {
firstBoot = false;
client.publish((mqttDeviceTopic + relay1).c_str(), "{\"status\":\"OFF\"}");
client.publish((mqttDeviceTopic + relay2).c_str(), "{\"status\":\"OFF\"}");
client.publish((mqttDeviceTopic + relay3).c_str(), "{\"status\":\"OFF\"}");
client.publish((mqttDeviceTopic + relay4).c_str(), "{\"status\":\"OFF\"}");
lastRescan = millis();
}
client.subscribe(controlStr, [] (const String & payload) {
Serial.println("Control MQTT Received...");
if (!commandQueue.isFull()) {
struct QueueCommand queueCommand;
queueCommand.payload = payload.c_str();
queueCommand.topic = ESPMQTTTopic + "/control";
commandQueue.enqueue(queueCommand);
}
else {
client.publish(esp32Str.c_str(), "{\"status\":\"errorQueueFull\"}");
}
});
}