-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node59_GarageClock.ino
822 lines (713 loc) · 25.6 KB
/
Node59_GarageClock.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!PICK ONE TO CHOOSE THE NODE !!!!!!!!!!!!!!!!!!!!!!!!
//*ESP8266
#include "Node59_GarageClock_Settings.h" //OTA deployed date:
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
///////////////PIN ASSIGNMENTS://///////////////////
//DIGITAL //
//------------------------------------------------//
// 8 //
// 7 //
// 6 //
// 5 //
// 4 DHT (Built in LED) //
// 3 //
// 2 //
// 1 //
// 0 //
////////////////////////////////////////////////////
////////////////////DEVICES:////////////////////////
//IN=SB=to node, OUT=NB=toward MQTT //
//------------------------------------------------//
//48 (OUT) TEMP //
//49 (OUT) HUMIDITY //
//50 (OUT) NTP TIME //
////////////////////////////////////////////////////
//////////////Jim's Customizations Comments/////////////////////////////////////////////////////////////////////////////
//2017/04 jim integrated enableable/disableable OTA system
//****ALERT*** KEEP SPIFFS AT 128, NO NEED FOR HIGHER AND IT MUST HAVE ENOUGH ROOM IN THE
// **PROGRAM AREA** NOT SPIFFS AREA FOR THE NEW FILE.
// **For Sonoff and ECOPLUGS, the proper setting for Flash Size is 1M (128K SPIFFS)
//seperated the header files from the code
//added on at startup option
//added send version at start
//customizable OTA name
//made the option for persistant messages or not.
// Reserved ranges for node devices, as implemented in the gateway are:
// 0 - 16 Node system devices
// 16 - 32 Binary output (LED, relay)
// 32 - 40 Integer output (pwm, dimmer)
// 40 - 48 Binary input (button, switch, PIR-sensor)
// 48 - 64 Real input (temperature, humidity)
// 64 - 72 Integer input (light intensity)
//////////////End Jim's Customizations Comments/////////////////////////////////////////////////////////////////////////
//////////////Begin Computourist Comments///////////////////////////////////////////////////////////////////////////////
// ESP_SONOFF_V1.2
//
// This MQTT client will is designed to run on a SONOFF device.
// https://www.itead.cc/smart-home/sonoff-wifi-wireless-switch.html
//
// It will connect over Wifi to the MQTT broker and controls a digital output (LED, relay):
// - toggle output and send status message on local button press
// - receive messages from the MQTT broker to control output, change settings and query state
// - periodically send status messages
//
// Several nodes can operate within the same network; each node has a unique node ID.
// On startup the node operates with default values, as set during compilation.
// Hardware used is a ESP8266 WiFi module that connects directly to the MQTT broker.
//
// Message structure is equal to the RFM69-based gateway/node sytem by the same author.
// This means both type of gateway/nodes can be used in a single Openhab system.
//
// The MQTT topic is /home/esp_gw/direction/nodeid/devid
// where direction is "sb" towards the node and "nb" towards the MQTT broker.
//
// Defined devices are:
// 0 uptime: read uptime in minutes
// 1 interval: read/set transmission interval for push messages
// 3 version: read software version
// 2 RSSI: read radio signal strength
// 5 ACK: read/set acknowledge message after a 'set' request
// 6 toggle: read/set select toggle / timer function
// 7 timer: read/set timer interval in seconds
// 10 IP: Read IP address
// 16 actuator: read/set relay output
// 40 button tx only: button pressed
// 41 Flash Switch: RX Only - This does an ON - wait - OFF/ON to keep motion type floodlights on
// 48 DHT Temp
// 49 DHT Humid
// 50 Fan Speed based on 48 TEMP
// 92 error: tx only: device not supported
// 91 error: tx only: syntax error
// 99 wakeup: tx only: first message sent on node startup
//
// Hardware connections as in Sonoff device:
// - pin 0 is connected to a button that switches to GND, pullup to VCC, also used to enter memory flash mode.
// - pin 13 is connected to LED and current limiting resistor to VCC, used to indicate MQTT link status
// - pin 12 is connected to the relais output
//
// version 1.0 by [email protected] June 2016
// version 1.1 by [email protected] July 2016
// specific client name to avoid conflicts when using multiple devices.
// added mqtt will to indicate disconnection of link
// version 1.2 by [email protected] July 2016
// fallback SSI and autorecovery for wifi connection.
// a connection attempt to each SSI is made during 20 seconds .
//
///////////////////////////////LEDMatrix///////////////////////////////////////////////////
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
//#define SENSORSHT
#define SENSORBME
// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define MAX_DEVICES 4
#define CLK_PIN D5 // or SCK
#define DATA_PIN D7 // or MOSI
#define CS_PIN D8 // or SS
// Hardware SPI connection
MD_Parola P = MD_Parola(CS_PIN, MAX_DEVICES);
// Arbitrary output pins
// MD_Parola P = MD_Parola(DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////SHT30 TEMP HUM SHIELD///////////////////////////////
#ifdef SENSORSHT
#include <WEMOS_SHT3X.h>
SHT3X sht30(0x45);
#endif
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////BME280 TEMP HUM SHIELD///////////////////////////////
#ifdef SENSORBME
#include "SparkFunBME280.h"
BME280 BMEsensor;
#endif
////////////////////////////////////////////////////////////////////////////////////////
long showPage, showNextPage;
String qPubErrorMessage="";
bool needTsync=true;
//#include <ESP8266WiFi.h>
#include "espWifiOTA.h"
#include <PubSubClient.h>
#include "NTP.h"
#define RESYNCMINS 30
#ifdef OTAenable
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#endif
// sensor setting
#define SERIAL_BAUD 115200
#define HOLDOFF 1000 // blocking period between button messages
// STARTUP DEFAULTS
long TXinterval = 90; // periodic transmission interval in seconds
long TIMinterval = 20; // timer interval in seconds
bool ackButton = false; // flag for message on button press
bool toggleOnButton = true; // toggle output on button press
// VARIABLES
int DID; // Device ID
int error; // Syntax error code
long lastPeriod = -1; // timestamp last transmission
long lastBtnPress = -1; // timestamp last buttonpress
long lastMinute = -1; // timestamp last minute
long upTime = 0; // uptime in minutes
int ACT1State; // status ACT1 output
bool mqttNotCon = true; // MQTT broker not connected flag
int signalStrength; // radio signal strength
bool wakeUp = true; // wakeup indicator
bool setAck = false; // acknowledge receipt of actions
bool curState = true; // current button state
bool lastState = true; // last button state
bool timerOnButton = false; // timer output on button press
bool msgBlock = false; // flag to hold button message
bool readAction; // indicates read / set a value
bool send0, send1, send2, send3, send5, send6, send7;
bool send10, send16, send40, send41, send48, send49, send50, send51, send52, send99; // message triggers
char buff_topic[30]; // mqtt topic
char buff_msg[32]; // mqtt message
char clientName[10]; // Mqtt client name
//These are moves outside local scopes because they are now used globally for the dispaly.
float temp;
float tempc;
float humi;
float ahpa;
float ainhg;
float aalt;
int minSinceSync=0;
String timeLast;
int pageFrequency[5]={10,10,5,5,5}; //SECONDS DURATION TO DISPLAY THE EXTRA "PAGES", base0=informational messages
#define PAGEDURATION 3
int showPageNo=1;
unsigned long previousMillis = 0; // will store last temp humi fan update
#define stInterval 30 // interval at which to recheck (milliseconds)
#define rtInterval 5 //adjustment to subtract from interval in case of fail
int retries=1; //set to 1 initially to get a quicker first read
////////////////////////////////////////////////////Jim Start/////////////////////////////////////////////////////////
void errorblink (int blinks){
errorblink(blinks,200);
}
void errorblink(int blinks, int blinkdelay){
int prevstat = digitalRead(MQCON);
int ebdelay=blinkdelay*1;
digitalWrite(MQCON,!prevstat);//it may be on...turn it off and wait a sec. remember the led is reversed of the state, 1=off
for (int i= 0; i < blinks; i++){
delay(ebdelay);
digitalWrite(MQCON,!prevstat);
delay(ebdelay);
digitalWrite(MQCON,prevstat);
}
}
#ifdef SENSORSHT
void setSHTvars() {
if(sht30.get()==0){
humi=sht30.humidity;
temp=sht30.fTemp;
tempc=sht30.cTemp;
Serial.print("Temperature in Celsius : ");
Serial.println(tempc);
Serial.print("Temperature in Fahrenheit : ");
Serial.println(temp);
Serial.print("Relative Humidity : ");
Serial.println(humi);
Serial.println();
}
else
{
Serial.println("SHT30 READ Error!");
}
delay(1000);
}
#endif
#ifdef SENSORBME
void setBMEvars() {
humi=(BMEsensor.readFloatHumidity());
tempc=(BMEsensor.readTempC());
temp=(BMEsensor.readTempF());
ahpa=(BMEsensor.readFloatPressure()/100); //hPa
ainhg=ahpa/33.86; //reference https://www.convertunits.com/from/hpa/to/inhg
aalt=(BMEsensor.readFloatAltitudeFeet());
//aalt(BMEsensor.readFloatAltitudeMeters(), 2);
#ifdef DEBUG
Serial.print("Temperature in Celsius : ");
Serial.println(tempc);
Serial.print("Temperature in Fahrenheit : ");
Serial.println(temp);
Serial.print("Relative Humidity : ");
Serial.println(humi);
Serial.print("Atmospheric Pressure : ");
Serial.print(ahpa);
Serial.println(" hPa");
Serial.print("Atmospheric Pressure : ");
Serial.print(ainhg);
Serial.println(" inhg");
Serial.print("Approx. Altitude = ");
Serial.print(aalt);
Serial.println(" ft");
#endif
delay(500);
}
#endif
////////////////////////////////////////////////////Jim End/////////////////////////////////////////////////////////
void mqttSubs(char* topic, byte* payload, unsigned int length);
// WiFiClient espClient;
PubSubClient client(mqtt_server, 1883, mqttSubs, espClient); // instantiate MQTT client
// FUNCTIONS
//===============================================================================================
void pubMQTT(String topic, String topic_val){ // publish MQTT message to broker
#ifdef DEBUG
Serial.print("topic " + topic + " value:");
Serial.println(String(topic_val).c_str());
#endif
client.publish(topic.c_str(), String(topic_val).c_str(), RETAINMSG); //use this line for retained messages
}
void pubErrorMessage(String immediateMessage) { // send error message
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev91", nodeId);
//sprintf(buff_msg, "MSG: %s", immediateMessage);
pubMQTT(buff_topic, immediateMessage);
}
void mqttSubs(char* topic, byte* payload, unsigned int length) { // receive and handle MQTT messages
int i;
error = 4; // assume invalid device until proven otherwise
#ifdef DEBUG
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
#endif
if (strlen(topic) == 27) { // correct topic length ?
DID = (topic[25]-'0')*10 + topic[26]-'0'; // extract device ID from MQTT topic
payload[length] = '\0'; // terminate string with '0'
String strPayload = String((char*)payload); // convert to string
readAction = (strPayload == "READ"); // 'READ' or 'SET' value
if (length == 0) {error = 2;} // no payload sent
else {
if (DID ==0) { // uptime
if (readAction) {
send0 = true;
error = 0;
} else error = 3; // invalid payload; do not process
}
//------------------
if (DID==1) { // transmission interval
error = 0;
if (readAction) {
send1 = true;
} else { // set transmission interval
TXinterval = strPayload.toInt();
if (TXinterval <10 && TXinterval !=0) TXinterval = 10; // minimum interval is 10 seconds
}
}
//------------------
if (DID ==2) { // RSSI
if (readAction) {
send2 = true;
error = 0;
} else error = 3; // invalid payload; do not process
}
//------------------
if (DID==3) { // version
if (readAction) {
send3 = true;
error = 0;
} else error = 3; // invalid payload; do not process
}
//------------------
if (DID==5) { // ACK
if (readAction) {
send5 = true;
error = 0;
} else if (strPayload == "ON") {
setAck = true;
if (setAck) send5 = true;
error = 0;
} else if (strPayload == "OFF") {
setAck = false;
if (setAck) send5 = true;
error = 0;
} else error = 3;
}
//------------------
if (DID==6) { // toggle / timer mode selection
if (readAction) {
send6 = true;
error = 0;
} else if (strPayload == "ON") { // select toggle mode
toggleOnButton = true;
if (setAck) send6 = true;
error = 0;
} else if (strPayload == "OFF") { // select timer mode
toggleOnButton = false;
if (setAck) send6 = true;
error = 0;
} else error = 3;
}
//------------------
if (DID==7) { // Timer interval
error = 0;
if (readAction) {
send7 = true;
} else { // set timer interval
TIMinterval = strPayload.toInt();
if (TIMinterval <5 && TIMinterval !=0) TIMinterval = 5; // minimum interval is 5 seconds
}
}
if (DID ==10) { // IP address
if (readAction) {
send10 = true;
error = 0;
} else error = 3; // invalid payload; do not process
}
//------------------
if (DID==16) { // state of actuator
if (readAction) {
send16 = true;
error = 0;
} else if (strPayload == "ON") {
ACT1State = 1;
digitalWrite(ACT1,ACT1State);
if (setAck) {
send16 = true;
send3 = true;
}
error = 0;
} else if (strPayload == "OFF") {
ACT1State = 0;
digitalWrite(ACT1,ACT1State);
if (setAck) send16 = true;
error = 0;
} else error = 3;
}
//------------------
//------------------
}
} else error =1;
if (error !=0) { // send error message
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev91", nodeId);
sprintf(buff_msg, "syntax error %d", error);
pubMQTT(buff_topic, buff_msg);
}
}
void connectMqtt() { // reconnect to mqtt broker
sprintf(buff_topic, "home/esp_gw/sb/node%02d/#", nodeId);
sprintf(clientName, "ESP_%02d", nodeId);
if (!client.loop()) {
mqttNotCon = true; // LED off means high voltage
#ifdef DEBUG
Serial.print("Connect to MQTT broker...");
#endif
if (client.connect(clientName,"home/esp_gw/disconnected",0,true,clientName)) {
mqttNotCon = false; // LED on means low voltage
client.subscribe(buff_topic);
#ifdef DEBUG
Serial.println("connected");
#endif
} else {
#ifdef DEBUG
Serial.println("Failed, try again in 5 seconds");
#endif
delay(5000);
}
digitalWrite(MQCON, mqttNotCon); // adjust MQTT link indicator
}
}
void sendMsg() { // send any outstanding messages
int i;
if (wakeUp) { // send wakeup message
wakeUp = false;
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev99", nodeId);
sprintf(buff_msg, "NODE %d WAKEUP: %s", nodeId, clientName);
send99 = false;
pubMQTT(buff_topic, buff_msg);
}
if (send0) { // send uptime
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev00", nodeId);
sprintf(buff_msg, "%d", upTime);
send0 = false;
pubMQTT(buff_topic, buff_msg);
}
if (send1) { // send transmission interval
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev01", nodeId);
sprintf(buff_msg, "%d", TXinterval);
send1 = false;
pubMQTT(buff_topic, buff_msg);
}
if (send2) { // send transmission interval
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev02", nodeId);
signalStrength = WiFi.RSSI();
sprintf(buff_msg, "%d", signalStrength);
send2 = false;
pubMQTT(buff_topic, buff_msg);
}
if (send3) { // send software version
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev03", nodeId);
for (i=0; i<sizeof(VERSION); i++) {
buff_msg[i] = VERSION[i];}
buff_msg[i] = '\0';
send3 = false;
pubMQTT(buff_topic, buff_msg);
}
if (send5) { // send ACK state
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev05", nodeId);
if (!setAck) sprintf(buff_msg, "OFF");
else sprintf(buff_msg, "ON");
pubMQTT(buff_topic, buff_msg);
send5 = false;
}
if (send6) { // send toggleOnButton state
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev06", nodeId);
if (!toggleOnButton) sprintf(buff_msg, "OFF");
else sprintf(buff_msg, "ON");
pubMQTT(buff_topic, buff_msg);
send6 = false;
}
if (send7) { // send timer value
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev07", nodeId);
sprintf(buff_msg, "%d", TIMinterval);
pubMQTT(buff_topic, buff_msg);
send7 = false;
}
if (send10) { // send IP address
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev10", nodeId);
for (i=0; i<16; i++) {
buff_msg[i] = IP[i];}
buff_msg[i] = '\0';
pubMQTT(buff_topic, buff_msg);
send10 = false;
}
if (send16) { // send actuator state
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev16", nodeId);
if (ACT1State ==0) sprintf(buff_msg, "OFF");
if (ACT1State ==1) sprintf(buff_msg, "ON");
pubMQTT(buff_topic, buff_msg);
send16 = false;
}
if (send40) { // send button pressed message
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev40", nodeId);
if (ACT1State ==0) sprintf(buff_msg, "OFF");
if (ACT1State ==1) sprintf(buff_msg, "ON");
pubMQTT(buff_topic, buff_msg);
send40 = false;
}
if (send48) { // Temperature
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev48", nodeId);
sprintf(buff_msg, "%0.1f", temp);
pubMQTT(buff_topic, buff_msg);
send48 = false;
}
if (send49) { // Humidity
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev49", nodeId);
sprintf(buff_msg, "%0.f", humi);
pubMQTT(buff_topic, buff_msg);
send49 = false;
}
if (send50) { // ntp time
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev50", nodeId);
setTimeStrings();
for (i=0; i<12; i++) {buff_msg[i] = NTPfulltime[i];}
buff_msg[i] = '\0';
pubMQTT(buff_topic, buff_msg);
send50 = false;
}
if (send51) { // ntp date
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev51", nodeId);
for (i=0; i<12; i++) {buff_msg[i] = NTPm_d_yr[i];}
buff_msg[i] = '\0';
pubMQTT(buff_topic, buff_msg);
send51 = false;
}
if (send52) { // Baro
sprintf(buff_topic, "home/esp_gw/nb/node%02d/dev52", nodeId);
sprintf(buff_msg, "%0.f", ainhg);
pubMQTT(buff_topic, buff_msg);
send52 = false; }
}
// SETUP
//===============================================================================================
void setup() { // set up serial, output and wifi connection
#ifdef DEBUG
Serial.begin(SERIAL_BAUD);
Serial.print("Starting Node ");
Serial.println(nodeId);
#endif
P.begin();
P.print("Start...");
// errorblink(5,200);
pinMode(MQCON,OUTPUT); // configure MQTT connection indicator
#ifdef SENSORBME
BMEsensor.settings.commInterface = I2C_MODE;
BMEsensor.settings.I2CAddress = 0x76;
BMEsensor.settings.runMode = 3; //Normal mode
BMEsensor.settings.tStandby = 0;
BMEsensor.settings.filter = 0;
BMEsensor.settings.tempOverSample = 1;
BMEsensor.settings.pressOverSample = 1;
BMEsensor.settings.humidOverSample = 1;
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
#ifdef DEBUG
Serial.print("Starting BME280... result of .begin(): 0x");
//Calling .begin() causes the settings to be loaded
Serial.println(BMEsensor.begin(), HEX);
Serial.print("Displaying ID, reset and ctrl regs\n");
Serial.print("ID(0xD0): 0x");
Serial.println(BMEsensor.readRegister(BME280_CHIP_ID_REG), HEX);
Serial.print("Reset register(0xE0): 0x");
Serial.println(BMEsensor.readRegister(BME280_RST_REG), HEX);
Serial.print("ctrl_meas(0xF4): 0x");
Serial.println(BMEsensor.readRegister(BME280_CTRL_MEAS_REG), HEX);
Serial.print("ctrl_hum(0xF2): 0x");
Serial.println(BMEsensor.readRegister(BME280_CTRL_HUMIDITY_REG), HEX);
Serial.print("\n\n");
#else
String resultsensor=String(BMEsensor.begin()); //start it if debug not on
#endif
#endif
// digitalWrite(MQCON,HIGH); // switch off MQTT connection indicator
#ifdef DEBUG
Serial.println("DIO Init Done");
#endif
send0 = false;
send1 = false;
send3 = true; //send version on startup
send5 = false;
send7 = false;
send10 = true; // send IP on startup
send16 = false;
send40 = false;
send48 = true;
send49 = true;
send50 = true;
send51 = true;
send52 = true;
delay(1000); //wait for DHT to settle
#ifdef DEBUG
Serial.println("Beginning Main Loop.");
#endif
P.print("Conn...");
yield();
}
// LOOP
//===============================================================================================
void loop() { // Main program loop
bool connStat=false;
if (WiFi.status() == WL_CONNECTED){connStat=true;}
if ( millis() > (30*60*1000) ){
if(!setAck){
#ifdef OTAnotimeout
wifiLoop(1); //its been running 30 mins, and ACKs are turned off, stop the OTA system - disable this function if notimeout defined
#else
wifiLoop(0); //its been running 30 mins, and ACKs are turned off, stop the OTA system
#endif
}else{
wifiLoop(1); //its been running 30 mins, and ACKs are turned on, OTA system enabled
}
}else{
wifiLoop(1); //its been running less than 30 mins so OTA is enabled
}
if (connStat==false){
P.print("AP=");
delay(1000);
yield;
P.print(WiFi.SSID());
showNextPage=millis()+(pageFrequency[0]*1000);
}
if (!client.connected()) { // MQTT connected ?
connectMqtt();
}
client.loop();
handleTime();
if (NTPfulltimeNS != timeLast){ //show the new time if it changed...but onlyid it's ben running for >1m
timeLast=NTPfulltimeNS;
if ((showPageNo=1) && (upTime>1)){P.print(NTPfulltimeNS);}
}
//pages: 1=time, 2=temp 3=humi 4=psi
//Variables used to change:
//#define PAGEFREQUENCY 20 /SECONDS DURATION TO DISPLAY THE EXTRA "PAGES"
//#define PAGEDURATION 3
//long showNextPage;
//int showPageNo=1;
if (millis()>=showNextPage){
showPageNo++;
if (showPageNo>4){showPageNo=1;}
#ifdef DEBUG
Serial.print("Page change to ");
Serial.println(showPageNo);
#endif
String buffer="";
if (showPageNo==1){
buffer=NTPfulltimeNS;
}
if (showPageNo==2){
buffer=String(int(temp))+" F";
}
if (showPageNo==3){
buffer=String(int(humi))+" %";
}
if (showPageNo==4){
buffer=String(int(ainhg))+" inhg";
}
#ifdef DEBUG
Serial.print("Display: ");
Serial.println(buffer);
#endif
P.print(buffer);
showNextPage=millis()+(pageFrequency[showPageNo]*1000);
}
// DETECT INPUT CHANGE
// TIMER CHECK
if (TIMinterval > 0 && timerOnButton) { // =0 means no timer
if ( millis() - lastBtnPress > TIMinterval*1000) { // timer expired ?
timerOnButton = false; // then end timer interval
ACT1State = LOW; // and switch off Actuator
digitalWrite(ACT1,ACT1State);
send16 = true;
}
}
// INCREASE UPTIME / MINUTE TIMER
if (lastMinute != (millis()/60000)) { // another minute passed ?
lastMinute = millis()/60000;
upTime++;
#ifdef SENSORSHT
setSHTvars();
#endif
#ifdef SENSORBME
setBMEvars();
#endif
}
// PERIODIC TRANSMISSION
if (TXinterval > 0){
int currPeriod = millis()/(TXinterval*1000);
if (currPeriod != lastPeriod) { // interval elapsed ?
lastPeriod = currPeriod;
// list of sensordata to be sent periodically..
// remove comment to include parameter in transmission
// send10 = true; // send IP address
send2 = true; // send RSSI
// send3 = true; // send version
send16 = true; // output state
send48 = true; // temp
send49 = true; //humidity
send50 = true; //NTP time
send51 = true; //NTP date
send52 = true; //pressure inhg
#ifdef DEBUG
Serial.println("Messages transmitted for interval.");
Serial.println();
#endif
}
}
unsigned long currentMillis = millis();
int interval=stInterval;
if (retries>0){interval=rtInterval;}
if ((currentMillis - previousMillis) >= (interval*1000)) {
previousMillis = currentMillis; // save the last time temp humi fan updated
}
if (qPubErrorMessage!=""){
pubErrorMessage(qPubErrorMessage);
qPubErrorMessage="";
}
sendMsg(); // send any mqtt messages
yield();
} // end loop