-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_esp8266.ino
452 lines (401 loc) · 12.2 KB
/
sample_esp8266.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include <Ticker.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
#include <PubSubClient.h>
#include <SimpleTimer.h>
#include <ArduinoJson.h>
#define APSSID "WIFI NAME" //AP SSID
#define APPASSWORD "WIFI PASSWORD" //AP password
#define SERVERPORT 80 //Web server port
#define WWWUSERNAME "admin" // Set www user name
#define WWWPASSWORD "admin" // Set www user password
#define OTAUSER "OTAadmin" // Set OTA user
#define OTAPASSWORD "OTApassword" // Set OTA password
#define OTAPATH "/firmware"// Set path for update
#define RELAYPIN 15 // GPIO12 relay pin -> GPIO15
#define LEDPIN 16 // GPIO13 GREEN LED (active low)-> GPIO16 change to wifi connect
#define BUTTONPIN 5 // GPIO0 button pin -> GPIO5
#define CLICKPIN 0 //test button
#define BUTTONTIME 0.05 // [s] time between button read
#define BUTTONON "color: green; border: 3px #fff outset;"
#define BUTTONOFF "color: red; border: 3px #fff outset;"
#define BUTTONNOACT "color: black; border: 7px #fff outset;"
#define BUTTONDEBOUNCE 1 //Minimum number of seconds between a valid button press or relay switch.
#define mqtt_server "192.168.0.1"
#define mqtt_port 1883
#define mqtt_id "test"
#define mqtt_topic "MyHome/Light/test"
#define mqtt_topic_sta "MyHome/Light/Sub/Server/State"
#define mqtt_topic_con "MyHome/Light/Sub/Server/Connect"
#define mqtt_topic_sub "MyHome/Light/Pub/test"
bool LEDState = true; // Green LED off
bool RelayState = false; // Relay off
bool ButtonFlag = false; // Does the button need to be handled on this loop
char ButtonCount = 0; // How many cycles/checks the button has been pressed for.
String OnButt;
String OffButt;
String SendButt;
volatile bool RelayState = false; // Relay off
bool LEDState = true; // Green LED off
bool ButtonFlag = false; // Does the button need to be handled on this loop
int ButtonCount = 0; // How many cycles/checks the button has been pressed for.
String OnButt;
String OffButt;
//Setup classes needed from libraries.
MDNSResponder mdns;
Ticker buttonTick;
ESP8266WebServer server(SERVERPORT);
ESP8266HTTPUpdateServer httpUpdater;
//mqttclient
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup(void){
// Init
pinMode(BUTTONPIN, INPUT);
pinMode(LEDPIN, OUTPUT);
pinMode(RELAYPIN, OUTPUT);
Serial.begin(115200);
delay(5000);
//Start wifi connection
Serial.println("Connecting to wifi..");
WiFi.begin(APSSID, APPASSWORD);
//Print MAC to serial so we can use the address for auth if needed.
printMAC();
// Wait for connection - Slow flash
Serial.print("Waiting on Connection ...");
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(LEDPIN, LOW);
delay(500);
Serial.print(".");
//Serial.println(WiFi.status());
digitalWrite(LEDPIN, HIGH);
delay(500);
}
attachInterrupt(digitalPinToInterrupt(BUTTONPIN), Test_Interrupt, RISING);
setLED(false);
//Print startup status and network information
Serial.println("");
Serial.print("Connected to: ");
Serial.println(APSSID);
Serial.print("IP: ");
Serial.println(WiFi.localIP());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
Serial.print("Subnet: ");
Serial.println(WiFi.subnetMask());
Serial.print("Device ID: ");
Serial.println(ESP.getChipId());
if (mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("MDNS: Responder Started");
}
//Setup HTTP Server Endpoints
server.on("/", HTTP_GET, handleGET);
server.on("/device", HTTP_POST, handleStatePOST);
server.on("/device", HTTP_GET, handleStateGET);
server.on("/state",HTTP_GET,RelayStateGET);
server.on("/change",HTTP_GET,RelayChange);
server.onNotFound(handleNotFound);
httpUpdater.setup(&server, OTAPATH, OTAUSER, OTAPASSWORD); //OTA Update endpoint
//Start the web server
server.begin();
client.setServer(mqtt_server,mqtt_port);
client.setCallback(callback);
//Start up blink of LED signaling everything is ready. Fast Flash
for (int i = 0; i < 10; i++) {
setLED(!LEDState);
delay(100);
}
Serial.println("Server is up.");
Serial.println(digitalRead(BUTTONPIN));
//Enable periodic watcher for button event handling
buttonTick.attach(BUTTONTIME, buttonFlagSet);
}
//mqtt
void mqtt_publish(char* message,const char* sender){
if(!client.connected()){
reconnect();
}
client.loop();
char output[256];
StaticJsonDocument<256> doc;
doc["sender"] = sender;
doc["message"] = message;
doc["room"] = mqtt_id;
serializeJson(doc, output);
client.publish(mqtt_topic, output);
delay(100);
}
void callback(char* topic, byte* payload, unsigned int length) {
String Msg = "";
int i=0;
while (i<length) Msg += (char)payload[i++];
StaticJsonDocument<256> doc;
DeserializationError error = deserializeJson(doc, Msg.c_str(), length);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
char error_put[64];
StaticJsonDocument<64> doc;
doc["sender"] = "self";
doc["message"] = error.c_str();
doc["room"] = mqtt_id;
client.publish(mqtt_topic, error_put);
return;
}
const char* sender = doc["Light"]["sender"];
const char* message = doc["Light"]["message"];
const char* destination = doc["Light"]["destination"];
String message_str = message;
String destination_str = destination;
if(message_str.equals("ON")){
if(RelayState == true){
mqtt_publish("already On", sender);
}
else{
setRelay(!RelayState);
mqtt_publish("On", sender);
}
}
else if(message_str.equals("STATE")){
if(RelayState == true){
mqtt_publish("On", sender);
}
else{
mqtt_publish("Off", sender);
}
}
else{
if(message_str.equals("OFF")){
if(RelayState == true){
setRelay(!RelayState);
mqtt_publish("Off", sender);
}
else{
mqtt_publish("already Off", sender);
}
}
}
}
void reconnect() {
// Loop until we're reconnected
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqtt_id)) { //change to ClientID
Serial.println("connected");
// ... and resubscribe
client.subscribe(mqtt_topic_sub);
// Once connected, publish an announcement...
client.publish(mqtt_topic_con, "{\"sender\":\"self\",\"message\":\"reconneted\",\"room\":\"test room\"}");
} else {
Serial.print("failed, rc=");
Serial.println(client.state());
// Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
// delay(5000);
}
}
/*
* printMAC
* Print the device MAC address to the serial port.
*/
void printMAC(void) {
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC: ");
Serial.print(mac[0],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.println(mac[5],HEX);
}
/*
* handleNotFound
* Return a 404 error on not found page.
*/
void handleNotFound() {
server.send(404, "text/plain", "404: Not found");
}
/*
* handleMainPage - GET
* Return Text for main page on GET
*/
void handleGET() {
//Quick LED Flash
//setLED(!LEDState);
//Serve Page
Serial.println("Serviced Page Request");
String buff;
buff = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
buff += "<html><head>\n";
buff += "<style type=\"text/css\">\n";
buff += "html {font-family: sans-serif;background:#f0f5f5}\n";
buff += ".submit {width: 10%; height:5vw; font-size: 100%; font-weight: bold; border-radius: 4vw;}\n";
buff += "@media (max-width: 1281px) {\n";
buff += "html {font-size: 3vw; font-family: sans-serif;background:#f0f5f5}\n";
buff += ".submit {width: 40%; height:20vw; font-size: 100%; font-weight: bold; border-radius: 15vw;}}\n";
buff += "</style>\n";
buff += "<meta content=\"text/html; charset=utf-8\">\n";
buff += "<title>Mcity - Wifi Power Switch</title></head><body>\n";
buff += "</pre>\n";
buff += "Wifi-enabled IIoT Power Switch\n";
buff += "<form action=\"/device\" method=\"POST\">\n";
buff += "<h2>Device ID: " + String(ESP.getChipId()) + "</h2>\n";
buff += "<h2>Device topic: " + String(mqtt_topic_sub) + "</h2>\n";
buff += "<h2>Relay State: ";
if (RelayState) {
buff += "ON</h2>\n";
} else {
buff += "OFF</h2>\n";
}
buff += "<input type=\"hidden\" name=\"return\" value=\"TRUE\">";
buff += "<input type=\"submit\" name=\"state\" class=\"submit\" value=\"ON\" style=\"" + OnButt + "\">\n";
buff += "<input type=\"submit\" name=\"state\" class=\"submit\" value=\"OFF\" style=\"" + OffButt + "\">\n";
buff += "</form></body></html>\n";
server.send(200, "text/html", buff);
//Quick LED Flash
delay(20);
//setLED(!LEDState);
}
void RelayStateGET(){
String buff;
if (RelayState) {
buff = "ON\n";
} else {
buff = "OFF\n";
}
server.send(50, "text/html", buff);
}
void RelayChange(){
setRelay(!RelayState);
RelayStateGET();
}
/*
* handleStatePOST
* Modify state on POST
*/
void handleStatePOST() {
/* request for www user/password from client */
if (!server.authenticate(WWWUSERNAME, WWWPASSWORD))
return server.requestAuthentication();
if (server.arg("state") == "ON") setRelay(true);
if (server.arg("state") == "OFF") setRelay(false);
//Redirect to home page is user requests it.
if (server.arg("return") == "TRUE") handleGET();
else handleStateGET();
}
/*
* handleStateGET
* Print state on GET
*/
void handleStateGET() {
//Serve Page
Serial.println("Serviced API Request");
//Print Relay state
String buff;
if (RelayState) {
buff = "ON\n";
} else {
buff = "OFF\n";
}
server.send(200, "text/html", buff);
//Quick LED Flash
delay(20);
//setLED(!LEDState);
}
/*
* setRelay
* Sets the state of the Relay
*/
void setRelay(bool SetRelayState) {
//Switch the HTML for the display page
if (SetRelayState == true) {
OnButt = BUTTONON;
OffButt = BUTTONNOACT;
}
if (SetRelayState == false) {
OnButt = BUTTONNOACT;
OffButt = BUTTONOFF;
}
//Set the relay state
RelayState = SetRelayState;
digitalWrite(RELAYPIN, RelayState);
//Set the LED to opposite of the button.
//setLED(!SetRelayState);
}
/*
* setLED
* Sets the state of the LED
*/
void setLED(bool SetLEDState) {
LEDState = SetLEDState; // set green LED
digitalWrite(LEDPIN, LEDState);
}
/*
* ButtonFlagSet
* Sets a variable so that on next loop, the button state is handled.
*/
void buttonFlagSet(void) {
ButtonFlag = true;
}
ICACHE_RAM_ATTR void Test_Interrupt() {
Serial.println("Test Interrupt!!!");
setRelay(!RelayState); // change relay
}
/* Read and handle button Press*/
void getButton(void) {
// short press butoon to change state of relay
if (digitalRead(BUTTONPIN) == false ) {
++ButtonCount;
}
if (digitalRead(BUTTONPIN) == false && ButtonCount > 1 && ButtonCount < 12 ) {
Serial.println(RelayState);
if(RelayState==false){
client.publish(mqtt_topic,"{\"sender\":\"self\",\"message\":\"On\",\"room\":\"test room\"}");
}
else{
client.publish(mqtt_topic,"{\"sender\":\"self\",\"message\":\"Off\",\"room\":\"test room\"}");
}
ButtonCount = 0;
delay(500);
}
/* long press button restart */
if (ButtonCount > 12) {
setLED(!LEDState);
buttonTick.detach(); // Stop Tickers
/* Wait for release button */
while (!digitalRead(BUTTONPIN)) yield();
delay(100);
ESP.restart();
}
if (digitalRead(BUTTONPIN) == true) ButtonCount = 0;
ButtonFlag = false;
}
/*
* loop
* System Loop
*/
void loop(void){
server.handleClient(); // Listen for HTTP request
// if (ButtonFlag) getButton();// Handle the button press
if(!client.connected()){
reconnect();
}
else {
if(ButtonFlag) {
getButton();
}
}
client.loop();
}