-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogoswitch.ino
494 lines (406 loc) · 11.8 KB
/
ogoswitch.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
MIT License
Copyright (c) 2017 kaebmoo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <PubSubClient.h>
#include "ESP8266WiFi.h"
#include <BlynkSimpleEsp8266.h>
#include <Time.h>
#include <TimeLib.h>
#include <Timer.h>
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
// const char *ssid = "CAT-Mobile";
const char *ssid = "CAT-Register";
const char *password = "";
//const char* mqtt_server = "192.168.43.252";
const char* mqtt_server = "192.168.2.7";
//const char* mqtt_server = "192.168.8.50";
const int relayPin = D1;
const int statusPin = D2;
const int buzzer=D5; //Buzzer control port, default D5
const long interval = 1000;
int ledState = LOW;
unsigned long previousMillis = 0;
const int MAXRETRY=4; // 0 - 4
byte mac[6] { 0x60, 0x01, 0x94, 0x82, 0x85, 0x54};
IPAddress ip(192, 168, 9, 101);
IPAddress gateway(192, 168, 9, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress server(192, 168, 9, 1);
WiFiClient espClient;
PubSubClient client(espClient);
char *mqtt_user = "chang";
char *mqtt_password = "chang";
char *myRoom = "room/1";
char *room_status = "room/1/status";
char *room_start = "room/1/start";
char *room_stop = "room/1/stop";
char *room_currenttime = "room/1/currenttime";
int mqtt_reconnect = 0;
int wifi_reconnect = 0;
// int flagtime = 0;
boolean ON = false;
boolean bstart = false;
boolean bstop = false;
boolean bcurrent = false;
boolean force = false;
unsigned long starttime;
unsigned long stoptime;
unsigned long currenttime;
unsigned long topic_currenttime;
Timer t_settime;
void buzzer_sound()
{
analogWriteRange(1047);
analogWrite(buzzer, 512);
delay(100);
analogWrite(buzzer, 0);
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, LOW);
delay(100);
analogWriteRange(1175);
analogWrite(buzzer, 512);
delay(300);
analogWrite(buzzer, 0);
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, LOW);
delay(300);
}
void callback(char* topic, byte* payload, unsigned int length) {
String strStart = "";
String strStop = "";
String strCurrenttime = "";
int i;
int statusvalue;
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
if(strcmp(topic, myRoom) == 0) {
// Switch on the RELAY if an 1 was received as first character
if ((char)payload[0] == '1') {
relay(true);
// force to open ; do not check start time, stop time
force = true;
bstart = false;
bstop = false;
} else if ((char)payload[0] == '0') { // 0 - turn relay off
// force to close ; do not check start time, stop time
relay(false);
force = true;
bstart = false;
bstop = false;
} else if ((char)payload[0] == '2') { // 2 - get status from D2 (connect D2 <---> D1) or D1 (relay pin)
statusvalue = digitalRead(relayPin);
if (statusvalue == HIGH) {
client.publish(room_status, "ON");
}
else {
client.publish(room_status, "OFF");
}
}
}
if (strcmp(topic, room_start) == 0) {
// receive start time message
for(i = 0; i < length; i++) {
strStart += (char)payload[i];
}
starttime = strStart.toInt();
strStart = "";
bstart = true;
}
if (strcmp(topic, room_stop) == 0) {
// receive stop time message
for(i = 0; i < length; i++) {
strStop += (char)payload[i];
}
stoptime = strStop.toInt();
strStop = "";
bstop = true;
}
if (bstart && bstop) {
force = false;
}
if (strcmp(topic, room_currenttime) == 0) {
//receive current time message
for(i = 0; i < length; i++) {
strCurrenttime += (char)payload[i];
}
topic_currenttime = strCurrenttime.toInt();
strCurrenttime = "";
if (timeStatus() != timeSet) {
setTime((time_t) topic_currenttime);
Serial.println("Time Set");
}
time_t t = now();
Serial.print(hour(t));
Serial.print(":");
Serial.print(minute(t));
Serial.print(":");
Serial.print(second(t));
Serial.print(" diff ");
Serial.print(t - (time_t) topic_currenttime);
Serial.println();
if ( (t - (time_t) topic_currenttime) != 0) {
setTime((time_t) topic_currenttime);
Serial.println("Time Set");
}
bcurrent = true;
}
}
void relay(boolean set)
{
if (set) {
ON = true;
digitalWrite(relayPin, HIGH);
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
client.publish(room_status,"ON", true);
Serial.print(room_status);
Serial.println(" : ON");
buzzer_sound();
}
else {
ON = false;
digitalWrite(relayPin, LOW);
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
client.publish(room_status,"OFF", true);
Serial.print(room_status);
Serial.println(" : OFF");
delay(500);
digitalWrite(BUILTIN_LED, LOW);
delay(500);
digitalWrite(BUILTIN_LED, HIGH);
}
}
void setup_wifi() {
WiFiManager wifiManager;
String APName;
delay(10);
//sets timeout until configuration portal gets turned off
//useful to make it all retry or go to sleep
//in seconds
wifiManager.setTimeout(300);
APName = "OgoSwitch-"+String(ESP.getChipId());
if(!wifiManager.autoConnect(APName.c_str()) ) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
/*
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
//WiFi.config(ip,gateway,subnet); // fix ip address
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
wifi_reconnect++;
if (wifi_reconnect > MAXRETRY) {
wifi_reconnect = 0;
break;
}
}
*/
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
/*
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);
*/
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(myRoom, mqtt_user, mqtt_password)) {
Serial.print("connected : ");
Serial.println(mqtt_server);
// Once connected, publish an announcement...
client.publish(room_status, "hello world");
// ... and resubscribe
client.subscribe(myRoom);
client.subscribe(room_start);
client.subscribe(room_stop);
client.subscribe(room_currenttime);
buzzer_sound();
buzzer_sound();
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.print(" try : ");
Serial.print(mqtt_reconnect+1);
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
mqtt_reconnect++;
if (mqtt_reconnect > MAXRETRY) {
mqtt_reconnect = 0;
break;
}
}
}
BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// process received value
Serial.println(pinValue);
if (pinValue == 1)
buzzer_sound();
}
void reconnectBlynk() {
if (!Blynk.connected()) {
if(Blynk.connect()) {
BLYNK_LOG("Reconnected");
} else {
BLYNK_LOG("Not reconnected");
}
}
}
void checkvalidtime()
{
Serial.print(bstart);
Serial.print(" ");
Serial.print(bstop);
Serial.print(" ");
Serial.print(bcurrent);
Serial.print(" ");
Serial.print(force);
Serial.print(" ");
Serial.print(starttime);
Serial.print(" ");
Serial.print(stoptime);
Serial.print(" ");
Serial.println(currenttime);
//if (bstart && bstop && !bcurrent && !force) {
// setTime((time_t) starttime);
// bcurrent = true;
//}
if (bstart && bstop && bcurrent && !force) {
if ( (currenttime >= starttime) && (currenttime <= stoptime) ) {
if (!ON) {
relay(true);
}
}
else if (ON) {
relay(false);
}
}
}
void takeSettingTime()
{
setTime((time_t) topic_currenttime);
Serial.println("Time Set");
}
void blink()
{
unsigned long currentMillis = millis();
// if enough millis have elapsed
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// toggle the LED
ledState = !ledState;
digitalWrite(BUILTIN_LED, ledState);
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(relayPin, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(statusPin, INPUT);
pinMode(buzzer, OUTPUT);
// t_settime.every(5000, takeSettingTime);
Serial.println(myRoom);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
if (client.connect(myRoom, mqtt_user, mqtt_password)) {
client.publish(room_status,"hello world");
client.subscribe(myRoom);
client.subscribe(room_start);
client.subscribe(room_stop);
client.subscribe(room_currenttime);
}
delay(500);
//Blynk.config(auth); // in place of Blynk.begin(auth, ssid, pass);
//Blynk.connect(3333); // timeout set to 10 seconds and then continue without Blynk, 3333 is 10 seconds because Blynk.connect is in 3ms units.
//pinMode(BUILTIN_LED, OUTPUT);
buzzer_sound();
digitalWrite(BUILTIN_LED, LOW); // turn on LED with voltage HIGH
delay(200); // wait one second
digitalWrite(BUILTIN_LED, HIGH); // turn off LED with voltage LOW
delay(200);
time_t t = now();
Serial.print("start : ");
Serial.print(second(t));
Serial.println();
}
void loop() {
// put your main code here, to run repeatedly:
if (WiFi.status() != WL_CONNECTED) {
setup_wifi();
}
else {
if (!client.connected()) {
reconnect();
}
}
client.loop();
//Blynk.run();
//t_settime.update();
currenttime = (unsigned long) now();
checkvalidtime();
if(!ON) {
blink();
}
else {
digitalWrite(BUILTIN_LED, HIGH); // on LED D4 pin
}
}