forked from hgyorgy/esp01s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp01s_mqqt.ino
416 lines (351 loc) · 10.9 KB
/
esp01s_mqqt.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
408
409
410
411
412
413
414
415
416
/*
ESP8266 Web server with Web Socket to control a RELAY, and conntrolled by MQTT.
The web server keeps all clients' RELAY status up to date and any client may
turn the RELAY on or off.
For example, clientA connects and turns the RELAY on. This changes the word
"RELAY" on the web page to the color red. When clientB connects, the word
"RELAY" will be red since the server knows the RELAY is on. When clientB turns
the RELAY off, the word LED changes color to green on clientA and clientB web
pages.
EEPROM Writing
Stores values read from input 0 into the EEPROM. These values will stay in
the EEPROM when the board is Turned off or on and may the relay will switch back
its original state after a reboot.
...and of course, fully MQTT controlled
References:
https://github.com/Links2004/arduinoWebSockets
Created: hgyorgy
https://github.com/hgyorgy/esp01s
*/
/****************************************
Include Libraries
****************************************/
#include <ESP8266WiFi.h>
#include <WebSocketsServer.h>
#include <Hash.h>
#include <MQTTClient.h> //https://github.com/256dpi/arduino-mqtt
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <EEPROM.h>
/****************************************
Define Constants
****************************************/
/* WIFI Settings */
static const char wifiSSID[] = "your wifi's ssid"; // Your WifiSSID here
static const char wifiPassword[] = "your wifi's passwd"; // Your wifi password here
/* MQTT Settings */
static const char mqttServer[] = "ip of mqtt server"; //your MQTT server IP or network name
const int mqttPort = 1883; //1883 is the default port for MQTT. Change if necessary
static const char mqttDeviceID[] = "Device Name"; //Unique device ID
// constants won't change. Used here to set a pin number:
const int ledPin = 2;// the number of the LED pin
// GPIO#0 is for Adafruit ESP8266 HUZZAH board. Your board RELAY might be on 13.
const int RELAYPIN = 0;
// Commands sent through Web Socket
const char RELAYON[] = "relayOn";
const char RELAYOFF[] = "relayOff";
/****************************************
Define Variables
****************************************/
// Variables will change:
char* subscribeTopic = "SmartHouse/utilities/relay01/command"; // Topic where send commands
char* stateTopic = "SmartHouse/utilities/relay01/state"; // Topic for listening commands
static void writeLED(bool);
// Current RELAY status
bool RELAYStatus;
bool relayState;
int mqqtRetry = 0;
int wifiRetry = 0;
/****************************************
Constructors
****************************************/
//Initialize Wifi
WiFiClient WiFiLan;
//Initialize MQTT
MQTTClient mqttClient;
//Webserver init
ESP8266WebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);
//MDNS Responder init
MDNSResponder mdns;
/****************************************
Webpage
****************************************/
static const char PROGMEM INDEX_HTML[] = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0">
<title>ESP8266 WebSocket 1</title>
<style>
"body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }"
</style>
<script>
var websock;
function start() {
websock = new WebSocket('ws://' + window.location.hostname + ':81/');
websock.onopen = function(evt) { console.log('websock open'); };
websock.onclose = function(evt) { console.log('websock close'); };
websock.onerror = function(evt) { console.log(evt); };
websock.onmessage = function(evt) {
console.log(evt);
var e = document.getElementById('RELAYstatus');
if (evt.data === 'relayOn') {
e.style.color = 'red';
}
else if (evt.data === 'relayOff') {
e.style.color = 'black';
}
else {
console.log('unknown event');
}
};
}
function buttonclick(e) {
websock.send(e.id);
}
</script>
</head>
<body onload="javascript:start();">
<h1>ESP8266 WebSocket (relay control)</h1>
<div id="RELAYstatus"><b>RELAY</b></div>
<button id="relayOn" type="button" onclick="buttonclick(this);">On</button>
<button id="relayOff" type="button" onclick="buttonclick(this);">Off</button>
</body>
</html>
)rawliteral";
/****************************************
Auxiliar Functions
****************************************/
//Websocket events
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length)
{
Serial.printf("webSocketEvent(%d, %d, ...)\r\n", num, type);
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\r\n", num);
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\r\n", num, ip[0], ip[1], ip[2], ip[3], payload);
// Send the current RELAY status
if (RELAYStatus) {
webSocket.sendTXT(num, RELAYON, strlen(RELAYON));
}
else {
webSocket.sendTXT(num, RELAYON, strlen(RELAYOFF));
}
}
break;
case WStype_TEXT:
Serial.printf("[%u] get Text: %s\r\n", num, payload);
if (strcmp(RELAYON, (const char *)payload) == 0) {
writeRELAY(true);
mqttClient.publish(stateTopic, "ON");
}
else if (strcmp(RELAYOFF, (const char *)payload) == 0) {
writeRELAY(false);
mqttClient.publish(stateTopic, "OFF");
}
else {
Serial.println("Unknown command");
}
// send data to all connected clients
webSocket.broadcastTXT(payload, length);
break;
case WStype_BIN:
Serial.printf("[%u] get binary length: %u\r\n", num, length);
hexdump(payload, length);
// echo data back to browser
webSocket.sendBIN(num, payload, length);
break;
default:
Serial.printf("Invalid WStype [%d]\r\n", type);
break;
}
}
void handleRoot()
{
server.send_P(200, "text/html", INDEX_HTML);
}
void handleNotFound()
{
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
// Relay write
static void writeRELAY(bool relayOn)
{
RELAYStatus = relayOn;
// Note inverted logic for Adafruit HUZZAH board
if (relayOn) {
digitalWrite(RELAYPIN, 0); //Switch off relay
write_eeprom(true); //save relay's state
}
else {
digitalWrite(RELAYPIN, 1); //Switch on relay
write_eeprom(false); //save relay's state
}
}
//Write to eeprom
void write_eeprom(bool relayState) {
EEPROM.write(0, relayState); // Write state to EEPROM
EEPROM.commit();
}
//mqtt_messages recived
void messageReceived(String &subscribeTopic, String &payload)
{
String msgString = payload;
if (msgString == "ON")
{
digitalWrite(RELAYPIN, 0);
write_eeprom(true); //save relay's state
delay(1000);
}
else if (msgString == "OFF")
{
digitalWrite(RELAYPIN, 1);
write_eeprom(false); //save relay's state
delay(1000);
}
}
//Check the mqtt connection
void mqtt_checkconnect() {
// Loop until we're reconnected
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
// Set the MQTT server to the server stated above ^
if (mqttClient.connect(mqttDeviceID, true)) {
Serial.println("connected");
mqqtRetry = 0;
} else {
Serial.print("failed Retry.. ");
Serial.println(".");
mqqtRetry = mqqtRetry + 1;
delay(1000);
}
if (mqqtRetry > 50) {
Serial.println("Rebooting..");
delay(1000);
ESP.reset();
}
}
}
//Check the wifi connection
void wifi_checkconnect() {
//Start Wifi connecting
Serial.println( "Check WIFI connetion with " );
Serial.print( wifiSSID );
Serial.println(" ...");
while ( WiFi.status() != WL_CONNECTED ) {
if (WiFi.reconnect() == true ) {
Serial.println ("Success!");
wifiRetry=0;
break;
} else {
delay(1000);
Serial.print(".");
wifiRetry = wifiRetry + 1;
}
if (wifiRetry > 50) {
Serial.println("Rebooting..");
delay(1000);
ESP.reset();
}
}
}
/****************************************
Main Functions
****************************************/
void setup()
{
// Useful for debugging purposes
Serial.begin(9600);
EEPROM.begin(512); // Begin eeprom to store on/off state
relayState = EEPROM.read(0);
pinMode(RELAYPIN, OUTPUT);
writeRELAY(relayState);
//Serial.setDebugOutput(true);
//Serial.println();
//Serial.println();
//Serial.println();
for(uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] BOOT WAIT %d...\r\n", t);
Serial.flush();
delay(1000);
}
//Start Wifi connecting
Serial.println( "Connecting to " );
Serial.print( wifiSSID );
Serial.println(" ...");
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin( wifiSSID, wifiPassword);
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
}
Serial.println("Success!");
Serial.print("Connected to WIFI:");
Serial.println( wifiSSID );
Serial.print("Locale IP address: ");
Serial.println(WiFi.localIP());
//Connect to MQTT Server
Serial.print("Connecting to MQTT server: ");
Serial.print(mqttServer);
Serial.println(" ...");
mqttClient.begin(mqttServer, mqttPort, WiFiLan);
mqttClient.onMessage(messageReceived);
//connect();
while (!mqttClient.connect(mqttDeviceID))
{
Serial.print(".");
delay(1000);
}
Serial.println("Success!");
Serial.print("Connected to MQTT Server: ");
Serial.println(mqttServer);
//Subscribe
Serial.print("Subscribe to a topic:");
Serial.println(subscribeTopic);
mqttClient.subscribe(subscribeTopic);
//MDNS responder start
if (mdns.begin("espWebSock", WiFi.localIP())) {
Serial.println("MDNS responder started");
mdns.addService("http", "tcp", 80);
//mdns.addService("ws", "tcp", 81);
}
else {
Serial.println("MDNS.begin failed");
}
Serial.print("Connect to http://espWebSock.local or http://");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.onNotFound(handleNotFound);
server.begin();
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
void loop()
{
wifi_checkconnect();
//MQTT loop
mqttClient.loop();
mqtt_checkconnect();
delay(10);
webSocket.loop();
server.handleClient();
}