-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAQEV2FW_NO2CO_ESP.ino
8051 lines (7011 loc) · 276 KB
/
AQEV2FW_NO2CO_ESP.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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Wire.h>
#include <SPI.h>
#include <ESP8266_AT_Client.h>
#include <SdFat.h>
#include <RTClib.h>
#include <RTC_DS3231.h>
#include <Time.h>
#include <TimeLib.h>
#include <TinyWatchdog.h>
#include <SHT25.h>
#include <WildFire_SPIFlash.h>
#include <CapacitiveSensor.h>
#include <LiquidCrystal.h>
#include <PubSubClient.h>
#include <util/crc16.h>
#include <math.h>
#include <TinyGPS.h>
#include <SoftwareSerial.h>
#include <jsmn.h>
#include <SoftReset.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#define INCLUDE_FIRMWARE_INTEGRITY_SELF_CHECK
#include <MCP342x.h>
#include <LMP91000.h>
// semantic versioning - see http://semver.org/
#define AQEV2FW_MAJOR_VERSION 2
#define AQEV2FW_MINOR_VERSION 3
#define AQEV2FW_PATCH_VERSION 7
#define WLAN_SEC_AUTO (10) // made up to support auto-config of security
// the start address of the second to last 4k page, where config is backed up off MCU
// the last page is reserved for use by the bootloader
#define SECOND_TO_LAST_4K_PAGE_ADDRESS 0x7E000
int esp8266_enable_pin = 23; // Arduino digital the pin that is used to reset/enable the ESP8266 module
Stream * at_command_interface = &Serial1; // Serial1 is the 'stream' the AT command interface is on
Stream * at_debug_interface = &Serial;
ESP8266_AT_Client esp(esp8266_enable_pin, at_command_interface); // instantiate the client object
TinyWatchdog tinywdt;
SHT25 sht25;
WildFire_SPIFlash flash;
CapacitiveSensor touch = CapacitiveSensor(A0, A1);
LiquidCrystal lcd(A3, A2, 4, 5, 6, 8);
char g_lcd_buffer[2][17] = {0}; // 2 rows of 16 characters each, with space for NULL terminator
char last_painted[2][17] = {" "," "};
byte mqtt_server_ip[4] = { 0 };
PubSubClient mqtt_client;
char mqtt_client_id[32] = {0};
boolean wifi_can_connect = false;
uint8_t wifi_connect_attempts = 0;
boolean user_location_override = false;
boolean gps_installed = false;
boolean display_offline_mode_banner = false;
RTC_DS3231 rtc;
SdFat SD;
#define MAX_SAMPLE_BUFFER_DEPTH (120) // 10 minutes @ 5 second resolution
#define NO2_SAMPLE_BUFFER (0)
#define CO_SAMPLE_BUFFER (1)
#define NUM_SAMPLE_BUFFERS (2)
MCP342x adc;
LMP91000 lmp91000;
float no2_ppb = 0.0f;
float instant_no2_v = 0.0f;
boolean no2_ready = false;
boolean init_no2_afe_ok = false;
boolean init_no2_adc_ok = false;
void set_no2_slope(char * arg);
void set_no2_offset(char * arg);
void set_no2_sensitivity(char * arg);
void no2_baseline_voltage_characterization_command(char * arg);
void no2_negz(char * arg);
const char cmd_string_no2_sen[] PROGMEM = "no2_sen ";
const char cmd_string_no2_slope[] PROGMEM = "no2_slope ";
const char cmd_string_no2_off[] PROGMEM = "no2_off ";
const char cmd_string_no2_blv[] PROGMEM = "no2_blv ";
const char cmd_string_no2_negz[] PROGMEM = "no2_negz ";
void collectNO2(void);
boolean publishNO2(void);
float convert_no2_sensitivity_to_slope(float sensitivity);
float co_ppm = 0.0f;
float instant_co_v = 0.0f;
boolean co_ready = false;
boolean init_co_afe_ok = false;
boolean init_co_adc_ok = false;
void set_co_slope(char * arg);
void set_co_offset(char * arg);
void set_co_sensitivity(char * arg);
void co_baseline_voltage_characterization_command(char * arg);
void co_negz(char * arg);
const char cmd_string_co_sen[] PROGMEM = "co_sen ";
const char cmd_string_co_slope[] PROGMEM = "co_slope ";
const char cmd_string_co_off[] PROGMEM = "co_off ";
const char cmd_string_co_blv[] PROGMEM = "co_blv ";
const char cmd_string_co_negz[] PROGMEM = "co_negz ";
void collectCO(void);
boolean publishCO(void);
float convert_co_sensitivity_to_slope(float sensitivity);
TinyGPS gps;
SoftwareSerial gpsSerial(18, 18); // RX, TX
Adafruit_BMP280 bme;
int sensor_enable = 17;
boolean gps_disabled = false;
#define GPS_MQTT_STRING_LENGTH (128)
#define GPS_CSV_STRING_LENGTH (64)
char gps_mqtt_string[GPS_MQTT_STRING_LENGTH] = {0};
char gps_csv_string[GPS_CSV_STRING_LENGTH] = {0};
boolean mqtt_stay_connected = true;
uint32_t update_server_ip32 = 0;
char update_server_name[32] = {0};
unsigned long integrity_num_bytes_total = 0;
unsigned long integrity_crc16_checksum = 0;
uint32_t flash_file_size = 0;
uint16_t flash_signature = 0;
boolean downloaded_integrity_file = false;
boolean integrity_check_succeeded = false;
boolean allowed_to_write_config_eeprom = false;
unsigned long current_millis = 0;
char firmware_version[16] = {0};
uint8_t temperature_units = 'C';
float reported_temperature_offset_degC = 0.0f;
float reported_humidity_offset_percent = 0.0f;
float temperature_degc = 0.0f;
float relative_humidity_percent = 0.0f;
float pressure_pa = 0.0f;
float instant_temperature_degc = 0.0f;
float instant_humidity_percent = 0.0f;
float instant_pressure_pa = 0.0f;
float instant_altitude_m = 0.0f;
float gps_latitude = TinyGPS::GPS_INVALID_F_ANGLE;
float gps_longitude = TinyGPS::GPS_INVALID_F_ANGLE;
float gps_altitude = TinyGPS::GPS_INVALID_F_ALTITUDE;
unsigned long gps_age = TinyGPS::GPS_INVALID_AGE;
float user_latitude = TinyGPS::GPS_INVALID_F_ANGLE;
float user_longitude = TinyGPS::GPS_INVALID_F_ANGLE;
float user_altitude = TinyGPS::GPS_INVALID_F_ALTITUDE;
float sample_buffer[NUM_SAMPLE_BUFFERS][MAX_SAMPLE_BUFFER_DEPTH] = {0};
uint16_t sample_buffer_idx = 0;
uint32_t sampling_interval = 0; // how frequently the sensorss are sampled
uint16_t sample_buffer_depth = 0; // how many samples are kept in memory for averaging
uint32_t reporting_interval = 0; // how frequently readings are reported (to wifi or console/sd)
#define TOUCH_SAMPLE_BUFFER_DEPTH (4)
float touch_sample_buffer[TOUCH_SAMPLE_BUFFER_DEPTH] = {0};
#define LCD_ERROR_MESSAGE_DELAY (4000)
#define LCD_SUCCESS_MESSAGE_DELAY (2000)
jsmn_parser parser;
jsmntok_t json_tokens[21];
boolean temperature_ready = false;
boolean humidity_ready = false;
boolean pressure_ready = false;
boolean init_sht25_ok = false;
boolean init_spi_flash_ok = false;
boolean init_esp8266_ok = false;
boolean init_sdcard_ok = false;
boolean init_rtc_ok = false;
boolean init_bmp280_ok = false;
typedef struct {
float temperature_degC; // starting at this temperature
float slope_volts_per_degC; // use a line with this slope
float intercept_volts; // and this intercept
// to calculate the baseline voltage
} baseline_voltage_t;
baseline_voltage_t baseline_voltage_struct; // scratch space for a single baseline_voltage_t entry
boolean valid_temperature_characterization_struct(baseline_voltage_t * temperature_characterization_struct_p) __attribute__((weak));
#define BACKLIGHT_OFF_AT_STARTUP (0)
#define BACKLIGHT_ON_AT_STARTUP (1)
#define BACKLIGHT_ALWAYS_ON (2)
#define BACKLIGHT_ALWAYS_OFF (3)
boolean g_backlight_turned_on = false;
// the software's operating mode
#define MODE_CONFIG (1)
#define MODE_OPERATIONAL (2)
// submodes of normal behavior
#define SUBMODE_NORMAL (3)
// #define SUBMODE_ZEROING (4) // deprecated for SUBMODE_OFFLINE
#define SUBMODE_OFFLINE (5)
uint8_t mode = MODE_OPERATIONAL;
// the config mode state machine's return values
#define CONFIG_MODE_NOTHING_SPECIAL (0)
#define CONFIG_MODE_GOT_INIT (1)
#define CONFIG_MODE_GOT_EXIT (2)
#define EEPROM_CONFIG_MEMORY_SIZE (1024)
#define EEPROM_MAC_ADDRESS (E2END + 1 - 6) // MAC address, i.e. the last 6-bytes of EEPROM
// more parameters follow, address relative to each other so they don't overlap
#define EEPROM_CONNECT_METHOD (EEPROM_MAC_ADDRESS - 1) // connection method encoded as a single byte value
#define EEPROM_SSID (EEPROM_CONNECT_METHOD - 32) // ssid string, up to 32 characters (one of which is a null terminator)
#define EEPROM_NETWORK_PWD (EEPROM_SSID - 32) // network password, up to 32 characters (one of which is a null terminator)
#define EEPROM_SECURITY_MODE (EEPROM_NETWORK_PWD - 1) // security mode encoded as a single byte value
#define EEPROM_STATIC_IP_ADDRESS (EEPROM_SECURITY_MODE - 4) // static ipv4 address, 4 bytes - 0.0.0.0 indicates use DHCP
#define EEPROM_STATIC_NETMASK (EEPROM_STATIC_IP_ADDRESS - 4) // static netmask, 4 bytes
#define EEPROM_STATIC_GATEWAY (EEPROM_STATIC_NETMASK - 4) // static default gateway ip address, 4 bytes
#define EEPROM_STATIC_DNS (EEPROM_STATIC_GATEWAY - 4) // static dns server ip address, 4 bytes
#define EEPROM_MQTT_PASSWORD (EEPROM_STATIC_DNS - 32) // password for mqtt server, up to 32 characters (one of which is a null terminator)
#define EEPROM_NO2_SENSITIVITY (EEPROM_MQTT_PASSWORD - 4) // float value, 4-bytes, the sensitivity from the sticker
#define EEPROM_NO2_CAL_SLOPE (EEPROM_NO2_SENSITIVITY - 4) // float value, 4-bytes, the slope applied to the sensor
#define EEPROM_NO2_CAL_OFFSET (EEPROM_NO2_CAL_SLOPE - 4) // float value, 4-btyes, the offset applied to the sensor
#define EEPROM_CO_SENSITIVITY (EEPROM_NO2_CAL_OFFSET - 4) // float value, 4-bytes, the sensitivity from the sticker
#define EEPROM_CO_CAL_SLOPE (EEPROM_CO_SENSITIVITY - 4) // float value, 4-bytes, the slope applied to the sensor
#define EEPROM_CO_CAL_OFFSET (EEPROM_CO_CAL_SLOPE - 4) // float value, 4-bytes, the offset applied to the sensor
#define EEPROM_PRIVATE_KEY (EEPROM_CO_CAL_OFFSET - 32) // 32-bytes of Random Data (256-bits)
#define EEPROM_MQTT_SERVER_NAME (EEPROM_PRIVATE_KEY - 32) // string, the DNS name of the MQTT server (default mqtt.wickeddevice.com), up to 32 characters (one of which is a null terminator)
#define EEPROM_MQTT_USERNAME (EEPROM_MQTT_SERVER_NAME - 32) // string, the user name for the MQTT server (default wickeddevice), up to 32 characters (one of which is a null terminator)
#define EEPROM_MQTT_CLIENT_ID (EEPROM_MQTT_USERNAME - 32) // string, the client identifier for the MQTT server (default SHT25 identifier), between 1 and 23 characters long
#define EEPROM_MQTT_AUTH (EEPROM_MQTT_CLIENT_ID - 1) // MQTT authentication enabled, single byte value 0 = disabled or 1 = enabled
#define EEPROM_MQTT_PORT (EEPROM_MQTT_AUTH - 4) // MQTT authentication enabled, reserve four bytes, even though you only need two for a port
#define EEPROM_UPDATE_SERVER_NAME (EEPROM_MQTT_PORT - 32) // string, the DNS name of the Firmware Update server (default update.wickeddevice.com), up to 32 characters (one of which is a null terminator)
#define EEPROM_OPERATIONAL_MODE (EEPROM_UPDATE_SERVER_NAME - 1) // operational mode encoded as a single byte value (e.g. NORMAL, OFFLINE, etc.)
#define EEPROM_TEMPERATURE_UNITS (EEPROM_OPERATIONAL_MODE - 1) // temperature units 'F' for Fahrenheit and 'C' for Celsius
#define EEPROM_UPDATE_FILENAME (EEPROM_TEMPERATURE_UNITS - 32) // 32-bytes for the update server filename (excluding the implied extension)
#define EEPROM_TEMPERATURE_OFFSET (EEPROM_UPDATE_FILENAME - 4) // float value, 4-bytes, the offset applied to the sensor for reporting
#define EEPROM_HUMIDITY_OFFSET (EEPROM_TEMPERATURE_OFFSET - 4) // float value, 4-bytes, the offset applied to the sensor for reporting
#define EEPROM_BACKLIGHT_DURATION (EEPROM_HUMIDITY_OFFSET - 2) // integer value, 2-bytes, how long, in seconds the backlight should stay on when it turns on
#define EEPROM_BACKLIGHT_STARTUP (EEPROM_BACKLIGHT_DURATION - 1) // boolean value, whether or not the backlight should turn on at startup
#define EEPROM_SAMPLING_INTERVAL (EEPROM_BACKLIGHT_STARTUP - 2) // integer value, number of seconds between sensor samplings
#define EEPROM_REPORTING_INTERVAL (EEPROM_SAMPLING_INTERVAL - 2) // integer value, number of seconds between sensor reports
#define EEPROM_AVERAGING_INTERVAL (EEPROM_REPORTING_INTERVAL - 2) // integer value, number of seconds of samples averaged
#define EEPROM_ALTITUDE_METERS (EEPROM_AVERAGING_INTERVAL - 2) // signed integer value, 2-bytes, the altitude in meters above sea level, where the Egg is located
#define EEPROM_MQTT_TOPIC_PREFIX (EEPROM_ALTITUDE_METERS - 64) // up to 64-character string, prefix prepended to logical sensor topics
#define EEPROM_USE_NTP (EEPROM_MQTT_TOPIC_PREFIX - 1) // 1 means use NTP, anything else means don't use NTP
#define EEPROM_NTP_SERVER_NAME (EEPROM_USE_NTP - 32) // 32-bytes for the NTP server to use
#define EEPROM_NTP_TZ_OFFSET_HRS (EEPROM_NTP_SERVER_NAME - 4) // timezone offset as a floating point value
#define EEPROM_NO2_BASELINE_VOLTAGE_TABLE (EEPROM_NTP_TZ_OFFSET_HRS - (5*sizeof(baseline_voltage_t))) // array of (up to) five structures for baseline offset characterization over temperature
#define EEPROM_CO_BASELINE_VOLTAGE_TABLE (EEPROM_NO2_BASELINE_VOLTAGE_TABLE - (5*sizeof(baseline_voltage_t))) // array of (up to) five structures for baseline offset characterization over temperature
#define EEPROM_MQTT_TOPIC_SUFFIX_ENABLED (EEPROM_CO_BASELINE_VOLTAGE_TABLE - 1) // a simple flag to indicate whether or not the topic suffix is enabled
#define EEPROM_USER_LATITUDE_DEG (EEPROM_MQTT_TOPIC_SUFFIX_ENABLED - 4) // float value, 4-bytes, user specified latitude in degrees
#define EEPROM_USER_LONGITUDE_DEG (EEPROM_USER_LATITUDE_DEG - 4) // float value, 4-bytes, user specified longitude in degrees
#define EEPROM_USER_LOCATION_EN (EEPROM_USER_LONGITUDE_DEG - 1) // 1 means user location supercedes GPS location, anything else means GPS or bust
#define EEPROM_2_2_0_SAMPLING_UPD (EEPROM_USER_LOCATION_EN - 1) // 1 means to sampling parameter default changes have been applied
#define EEPROM_DISABLE_SOFTAP (EEPROM_2_2_0_SAMPLING_UPD - 1) // 1 means to disable softap behavior
#define EEPROM_NO2_ZERO_NEGATIVE_RESULTS (EEPROM_DISABLE_SOFTAP - 1)
#define EEPROM_CO_ZERO_NEGATIVE_RESULTS (EEPROM_NO2_ZERO_NEGATIVE_RESULTS - 1)
#define EEPROM_TEMPERATURE_OFFLINE_OFFSET (EEPROM_CO_ZERO_NEGATIVE_RESULTS - 4)
#define EEPROM_HUMIDITY_OFFLINE_OFFSET (EEPROM_TEMPERATURE_OFFLINE_OFFSET - 4)
#define EEPROM_MQTT_STAY_CONNECTED (EEPROM_HUMIDITY_OFFLINE_OFFSET - 1)
// /\
// L Add values up here by subtracting offsets to previously added values
// * ... and make sure the addresses don't collide and start overlapping!
// T Add values down here by adding offsets to previously added values
// \/
#define EEPROM_BACKUP_HUMIDITY_OFFLINE_OFFSET (EEPROM_BACKUP_TEMPERATURE_OFFLINE_OFFSET + 4)
#define EEPROM_BACKUP_TEMPERATURE_OFFLINE_OFFSET (EEPROM_BACKUP_NTP_TZ_OFFSET_HRS + 4)
#define EEPROM_BACKUP_NTP_TZ_OFFSET_HRS (EEPROM_BACKUP_HUMIDITY_OFFSET + 4)
#define EEPROM_BACKUP_HUMIDITY_OFFSET (EEPROM_BACKUP_TEMPERATURE_OFFSET + 4)
#define EEPROM_BACKUP_TEMPERATURE_OFFSET (EEPROM_BACKUP_PRIVATE_KEY + 32)
#define EEPROM_BACKUP_PRIVATE_KEY (EEPROM_BACKUP_CO_CAL_OFFSET + 4)
#define EEPROM_BACKUP_CO_CAL_OFFSET (EEPROM_BACKUP_CO_CAL_SLOPE + 4)
#define EEPROM_BACKUP_CO_CAL_SLOPE (EEPROM_BACKUP_CO_SENSITIVITY + 4)
#define EEPROM_BACKUP_CO_SENSITIVITY (EEPROM_BACKUP_NO2_CAL_OFFSET + 4)
#define EEPROM_BACKUP_NO2_CAL_OFFSET (EEPROM_BACKUP_NO2_CAL_SLOPE + 4)
#define EEPROM_BACKUP_NO2_CAL_SLOPE (EEPROM_BACKUP_NO2_SENSITIVITY + 4)
#define EEPROM_BACKUP_NO2_SENSITIVITY (EEPROM_BACKUP_MQTT_PASSWORD + 32)
#define EEPROM_BACKUP_MQTT_PASSWORD (EEPROM_BACKUP_MAC_ADDRESS + 6)
#define EEPROM_BACKUP_MAC_ADDRESS (EEPROM_BACKUP_CHECK + 2) // backup parameters are added here offset from the EEPROM_CRC_CHECKSUM
#define EEPROM_BACKUP_CHECK (EEPROM_CRC_CHECKSUM + 2) // 2-byte value with various bits set if backup has ever happened
#define EEPROM_CRC_CHECKSUM (E2END + 1 - EEPROM_CONFIG_MEMORY_SIZE) // reserve the last 1kB for config
// backup status bits
#define BACKUP_STATUS_MAC_ADDRESS_BIT (7)
#define BACKUP_STATUS_MQTT_PASSSWORD_BIT (6)
#define BACKUP_STATUS_NO2_CALIBRATION_BIT (5)
#define BACKUP_STATUS_CO_CALIBRATION_BIT (4)
#define BACKUP_STATUS_PRIVATE_KEY_BIT (3)
#define BACKUP_STATUS_TEMPERATURE_CALIBRATION_BIT (2)
#define BACKUP_STATUS_HUMIDITY_CALIBRATION_BIT (1)
#define BACKUP_STATUS_TIMEZONE_CALIBRATION_BIT (0)
// valid connection methods
// only DIRECT is supported initially
#define CONNECT_METHOD_DIRECT (0)
#define BIT_IS_CLEARED(val, b) (!(val & (1UL << b)))
#define CLEAR_BIT(val, b) \
do { \
val &= ~(1UL << b); \
} while(0)
boolean mirrored_config_restore_and_validate(void);
boolean mirrored_config_matches_eeprom_config(void);
void setLCD_P(const char * str PROGMEM);
void suspendGpsProcessing(void);
void collectTouch(void);
boolean processTouchQuietly(void);
void petWatchdog(void);
void updateCornerDot(void);
void backlightOn(void);
boolean mode_requires_wifi(uint8_t opmode);
void resumeGpsProcessing(void);
void delayForWatchdog(void);
void displayRSSI(void);
boolean restartWifi(void);
void watchdogForceReset(void);
void commitConfigToMirroredConfig(void);
void checkForFirmwareUpdates(void);
void checkForESPFirmwareUpdates(void);
void getNetworkTime(void);
boolean mqttReconnect(void);
boolean mqttDisconnect(void);
void lcdFrownie(uint8_t pos_x, uint8_t pos_y);
void backlightOff(void);
void clearLCD(void);
void clearLCD(boolean repaint);
void repaintLCD(void);
void updateLCD(const char * str, uint8_t pos_x, uint8_t pos_y, uint8_t num_chars);
void updateLCD(const char * str, uint8_t pos_x, uint8_t pos_y, uint8_t num_chars, boolean repaint);
void updateLCD(float value, uint8_t pos_x, uint8_t pos_y, uint8_t field_width);
void updateLCD(float value, uint8_t pos_x, uint8_t pos_y, uint8_t field_width, boolean repaint);
void updateLCD(uint32_t ip, uint8_t line_number);
void updateLCD(const char * str, uint8_t line_number);
void updateLCD(int32_t value, uint8_t pos_x, uint8_t pos_y, uint8_t num_chars);
void updateLCD(char value, uint8_t pos_x, uint8_t pos_y, uint8_t num_chars);
float toFahrenheit(float degC);
void processTouchBetweenGpsMessages(char c);
void collectTemperature(void);
void collectHumidity(void);
void collectPressure(void);
void updateGpsStrings(void);
void advanceSampleBufferIndex(void);
void loop_wifi_mqtt_mode(void);
void loop_offline_mode(void);
void watchdogInitialize(void);
void selectNoSlot(void);
void getCurrentFirmwareSignature(void);
void selectSlot1(void);
void selectSlot2(void);
void selectSlot3(void);
time_t AQE_now(void);
void clearTempBuffers(void);
void recomputeAndStoreConfigChecksum(void);
uint16_t computeEepromChecksum(void);
uint16_t getStoredEepromChecksum(void);
void recomputeAndStoreConfigChecksum(void);
void help_menu(char * arg);
void print_eeprom_value(char * arg);
void initialize_eeprom_value(char * arg);
void restore(char * arg);
void set_mac_address(char * arg);
void set_connection_method(char * arg);
void set_ssid(char * arg);
void set_network_password(char * arg);
void set_network_security_mode(char * arg);
void set_static_ip_address(char * arg);
void use_command(char * arg);
void set_mqtt_password(char * arg);
void set_mqtt_server(char * arg);
void set_mqtt_port(char * arg);
void set_mqtt_username(char * arg);
void set_mqtt_client_id(char * arg);
void set_mqtt_authentication(char * arg);
void set_mqtt_topic_prefix(char * arg);
void backup(char * arg);
void set_reported_temperature_offset(char * arg);
void set_reported_humidity_offset(char * arg);
void set_reported_temperature_offline_offset(char * arg);
void set_reported_humidity_offline_offset(char * arg);
void set_private_key(char * arg);
void set_operational_mode(char * arg);
void set_temperature_units(char * arg);
void set_update_filename(char * arg);
void force_command(char * arg);
void set_backlight_behavior(char * arg);
void AQE_set_datetime(char * arg);
void list_command(char * arg);
void download_command(char * arg);
void delete_command(char * arg);
void sampling_command(char * arg);
void altitude_command(char * arg);
void set_ntp_server(char * arg);
void set_ntp_timezone_offset(char * arg);
void set_update_server_name(char * arg);
void topic_suffix_config(char * arg);
void set_user_latitude(char * arg);
void set_user_longitude(char * arg);
void set_user_location_enable(char * arg);
void verifyProgmemWithSpiFlash(void);
// Note to self:
// When implementing a new parameter, ask yourself:
// should there be a command for the user to set its value directly
// should 'get' support it (almost certainly the answer is yes)
// should 'init' support it (is there a way to set it without user intervention)
// should 'restore' support it directly
// should 'restore defaults' support it
// ... and remember, anything that changes the config EEPROM
// needs to call recomputeAndStoreConfigChecksum after doing so
// the order of the command keywords in this array
// must be kept in index-correspondence with the associated
// function pointers in the command_functions array
//
// these keywords are padded with spaces
// in order to ease printing as a table
// string comparisons should use strncmp rather than strcmp
const char cmd_string_get[] PROGMEM = "get ";
const char cmd_string_init[] PROGMEM = "init ";
const char cmd_string_restore[] PROGMEM = "restore ";
const char cmd_string_mac[] PROGMEM = "mac ";
const char cmd_string_method[] PROGMEM = "method ";
const char cmd_string_ssid[] PROGMEM = "ssid ";
const char cmd_string_pwd[] PROGMEM = "pwd ";
const char cmd_string_security[] PROGMEM = "security ";
const char cmd_string_staticip[] PROGMEM = "staticip ";
const char cmd_string_use[] PROGMEM = "use ";
const char cmd_string_mqttsrv[] PROGMEM = "mqttsrv ";
const char cmd_string_mqttport[] PROGMEM = "mqttport ";
const char cmd_string_mqttuser[] PROGMEM = "mqttuser ";
const char cmd_string_mqttpwd[] PROGMEM = "mqttpwd ";
const char cmd_string_mqttid[] PROGMEM = "mqttid ";
const char cmd_string_mqttauth[] PROGMEM = "mqttauth ";
const char cmd_string_mqttprefix[] PROGMEM = "mqttprefix ";
const char cmd_string_mqttsuffix[] PROGMEM = "mqttsuffix ";
const char cmd_string_updatesrv[] PROGMEM = "updatesrv ";
const char cmd_string_backup[] PROGMEM = "backup ";
const char cmd_string_temp_off[] PROGMEM = "temp_off ";
const char cmd_string_temp_sdoff[] PROGMEM = "temp_sdoff ";
const char cmd_string_hum_off[] PROGMEM = "hum_off ";
const char cmd_string_hum_sdoff[] PROGMEM = "hum_sdoff ";
const char cmd_string_key[] PROGMEM = "key ";
const char cmd_string_opmode[] PROGMEM = "opmode ";
const char cmd_string_tempunit[] PROGMEM = "tempunit ";
const char cmd_string_updatefile[] PROGMEM = "updatefile ";
const char cmd_string_force[] PROGMEM = "force ";
const char cmd_string_backlight[] PROGMEM = "backlight ";
const char cmd_string_datetime[] PROGMEM = "datetime ";
const char cmd_string_list[] PROGMEM = "list ";
const char cmd_string_download[] PROGMEM = "download ";
const char cmd_string_delete[] PROGMEM = "delete ";
const char cmd_string_sampling[] PROGMEM = "sampling ";
const char cmd_string_altitude[] PROGMEM = "altitude ";
const char cmd_string_ntpsrv[] PROGMEM = "ntpsrv ";
const char cmd_string_tz_off[] PROGMEM = "tz_off ";
const char cmd_string_usr_lat[] PROGMEM = "latitude ";
const char cmd_string_usr_lng[] PROGMEM = "longitude ";
const char cmd_string_usr_loc_en[] PROGMEM = "location ";
const char cmd_string_null[] PROGMEM = "";
PGM_P const commands[] PROGMEM = {
cmd_string_get,
cmd_string_init,
cmd_string_restore,
cmd_string_mac,
cmd_string_method,
cmd_string_ssid,
cmd_string_pwd,
cmd_string_security,
cmd_string_staticip,
cmd_string_use,
cmd_string_mqttsrv,
cmd_string_mqttport,
cmd_string_mqttuser,
cmd_string_mqttpwd,
cmd_string_mqttid,
cmd_string_mqttauth,
cmd_string_mqttprefix,
cmd_string_mqttsuffix,
cmd_string_updatesrv,
cmd_string_backup,
cmd_string_temp_off,
cmd_string_hum_off,
cmd_string_temp_sdoff,
cmd_string_hum_sdoff,
cmd_string_key,
cmd_string_opmode,
cmd_string_tempunit,
cmd_string_updatefile,
cmd_string_force,
cmd_string_backlight,
cmd_string_datetime,
cmd_string_list,
cmd_string_download,
cmd_string_delete,
cmd_string_sampling,
cmd_string_altitude,
cmd_string_ntpsrv,
cmd_string_tz_off,
cmd_string_usr_lat,
cmd_string_usr_lng,
cmd_string_usr_loc_en,
cmd_string_no2_sen,
cmd_string_no2_slope,
cmd_string_no2_off,
cmd_string_no2_blv,
cmd_string_no2_negz,
cmd_string_co_sen,
cmd_string_co_slope,
cmd_string_co_off,
cmd_string_co_blv,
cmd_string_co_negz,
cmd_string_null
};
void (*command_functions[])(char * arg) = {
print_eeprom_value,
initialize_eeprom_value,
restore,
set_mac_address,
set_connection_method,
set_ssid,
set_network_password,
set_network_security_mode,
set_static_ip_address,
use_command,
set_mqtt_server,
set_mqtt_port,
set_mqtt_username,
set_mqtt_password,
set_mqtt_client_id,
set_mqtt_authentication,
set_mqtt_topic_prefix,
topic_suffix_config,
set_update_server_name,
backup,
set_reported_temperature_offset,
set_reported_humidity_offset,
set_reported_temperature_offline_offset,
set_reported_humidity_offline_offset,
set_private_key,
set_operational_mode,
set_temperature_units,
set_update_filename,
force_command,
set_backlight_behavior,
AQE_set_datetime,
list_command,
download_command,
delete_command,
sampling_command,
altitude_command,
set_ntp_server,
set_ntp_timezone_offset,
set_user_latitude,
set_user_longitude,
set_user_location_enable,
set_no2_sensitivity,
set_no2_slope,
set_no2_offset,
no2_baseline_voltage_characterization_command,
no2_negz,
set_co_sensitivity,
set_co_slope,
set_co_offset,
co_baseline_voltage_characterization_command,
co_negz,
0
};
// tiny watchdog timer intervals
unsigned long previous_tinywdt_millis = 0;
const long tinywdt_interval = 1000;
// sensor sampling timer intervals
unsigned long previous_sensor_sampling_millis = 0;
// touch sampling timer intervals
unsigned long previous_touch_sampling_millis = 0;
const long touch_sampling_interval = 200;
// progress dots timer intervals
unsigned long previous_progress_dots_millis = 0;
const long progress_dots_interval = 1000;
#define NUM_HEARTBEAT_WAVEFORM_SAMPLES (84)
const uint8_t heartbeat_waveform[NUM_HEARTBEAT_WAVEFORM_SAMPLES] PROGMEM = {
95, 94, 95, 96, 95, 94, 95, 96, 95, 94,
95, 96, 95, 94, 95, 96, 95, 97, 105, 112,
117, 119, 120, 117, 111, 103, 95, 94, 95, 96,
95, 94, 100, 131, 162, 193, 224, 255, 244, 214,
183, 152, 121, 95, 88, 80, 71, 74, 82, 90,
96, 95, 94, 95, 96, 97, 106, 113, 120, 125,
129, 132, 133, 131, 128, 124, 118, 111, 103, 96,
95, 96, 95, 94, 95, 96, 95, 94, 95, 99,
105, 106, 101, 96
};
uint8_t heartbeat_waveform_index = 0;
#define SCRATCH_BUFFER_SIZE (1024)
char scratch[SCRATCH_BUFFER_SIZE] = { 0 }; // scratch buffer, for general use
uint16_t scratch_idx = 0;
#define ESP8266_INPUT_BUFFER_SIZE (1000)
uint8_t esp8266_input_buffer[ESP8266_INPUT_BUFFER_SIZE] = {0}; // sketch must instantiate a buffer to hold incoming data
// 1500 bytes is way overkill for MQTT, but if you have it, may as well
// make space for a whole TCP packet
char converted_value_string[64] = {0};
char compensated_value_string[64] = {0};
char raw_value_string[64] = {0};
char raw_instant_value_string[64] = {0};
char response_body[256] = {0};
char MQTT_TOPIC_STRING[128] = {0};
char MQTT_TOPIC_PREFIX[64] = "/orgs/wd/aqe/";
uint8_t mqtt_suffix_enabled = 0;
const char * header_row = "Timestamp,"
"Temperature[degC],"
"Humidity[percent],"
"NO2[ppb],"
"CO[ppm],"
"NO2[V],"
"CO[V],"
"Pressure[Pa],"
"Latitude[deg],"
"Longitude[deg],"
"Altitude[m]";
static boolean wdt_reset_pending = false;
void setup() {
boolean integrity_check_passed = false;
boolean mirrored_config_mismatch = false;
boolean valid_ssid_passed = false;
// turn off backlight
pinMode(A6, OUTPUT);
digitalWrite(A6, LOW);
// allow for power stabilization
delay(500);
// initialize hardware
initializeHardware();
// uint8_t tmp[EEPROM_CONFIG_MEMORY_SIZE] = {0};
// get_eeprom_config(tmp);
// Serial.println(F("EEPROM Config:"));
// dump_config(tmp);
// Serial.println();
// Serial.println(F("Mirrored Config:"));
// get_mirrored_config(tmp);
// dump_config(tmp);
// Serial.println();
integrity_check_passed = checkConfigIntegrity();
// if the integrity check failed, try and undo the damage using the mirror config, if it's valid
if(!integrity_check_passed) {
Serial.println(F("Info: Startup config integrity check failed, attempting to restore from mirrored configuration."));
allowed_to_write_config_eeprom = true;
mirrored_config_restore_and_validate();
integrity_check_passed = checkConfigIntegrity();
mirrored_config_mismatch = !mirrored_config_matches_eeprom_config();
allowed_to_write_config_eeprom = false;
}
else if(!mirrored_config_matches_eeprom_config()) {
// probably the reason you got into this case is because you changed settings
// in CLI mode but failed to connect to a network afterwards
// so this will revert the Egg to use its last settings that *did* connect to a network
Serial.println(F("Info: Startup config integrity check passed, but mirrored config differs, attempting to restore from mirrored configuration."));
allowed_to_write_config_eeprom = true;
mirrored_config_restore_and_validate();
integrity_check_passed = checkConfigIntegrity();
mirrored_config_mismatch = !mirrored_config_matches_eeprom_config();
allowed_to_write_config_eeprom = false;
}
// possible outcomes of the above code
// integrity_check_passed && mirrored_config_mismatch
// means... mirror configuration is not yet valid
// integrity_check_passed && !mirrored_config_mismatch
// means... everything is great, normal behavior
// !integrity_check_passed && mirrored_config_mismatch
// means... all attempts to attain a valid configuration faile
// and the mirrored config is *different* from the eeprom config
// !integrity_check_passed && !mirrored_config_mismatch
// means... all attempts to attain a valid configuration faile
// and the mirrored config is *identical* to the eeprom config
valid_ssid_passed = valid_ssid_config();
boolean ok_to_exit_config_mode = true;
// if a software update introduced new settings
// they should be populated with defaults as necessary
initializeNewConfigSettings();
user_location_override = (eeprom_read_byte((const uint8_t *) EEPROM_USER_LOCATION_EN) == 1) ? true : false;
uint8_t target_mode = eeprom_read_byte((const uint8_t *) EEPROM_OPERATIONAL_MODE);
// get the temperature units
temperature_units = eeprom_read_byte((const uint8_t *) EEPROM_TEMPERATURE_UNITS);
if((temperature_units != 'C') && (temperature_units != 'F')) {
temperature_units = 'C';
}
// get the sampling, reporting, and averaging parameters
sampling_interval = eeprom_read_word((uint16_t * ) EEPROM_SAMPLING_INTERVAL) * 1000L;
reporting_interval = eeprom_read_word((uint16_t * ) EEPROM_REPORTING_INTERVAL) * 1000L;
sample_buffer_depth = (uint16_t) ((((uint32_t) eeprom_read_word((uint16_t * ) EEPROM_AVERAGING_INTERVAL)) * 1000L) / sampling_interval);
// config mode processing loop
do {
// if the appropriate escape sequence is received within 8 seconds
// go into config mode
const long startup_time_period = 12000;
long start = millis();
long min_over = 100;
boolean got_serial_input = false;
Serial.println(F("Enter 'aqe' for CONFIG mode."));
Serial.print(F("OPERATIONAL mode automatically begins after "));
Serial.print(startup_time_period / 1000);
Serial.println(F(" secs of no input."));
setLCD_P(PSTR(" TOUCH TO SETUP "
" OR CONNECT USB "));
boolean touch_detected = false;
current_millis = millis();
suspendGpsProcessing();
while (current_millis < start + startup_time_period) { // can get away with this sort of thing at start up
current_millis = millis();
if(current_millis - previous_touch_sampling_millis >= touch_sampling_interval) {
static uint8_t num_touch_intervals = 0;
previous_touch_sampling_millis = current_millis;
collectTouch();
touch_detected = processTouchQuietly();
num_touch_intervals++;
if(num_touch_intervals == 5) {
petWatchdog();
num_touch_intervals = 0;
}
}
if (Serial.available()) {
if (got_serial_input == false) {
Serial.println();
}
got_serial_input = true;
start = millis(); // reset the timeout
if (CONFIG_MODE_GOT_INIT == configModeStateMachine(Serial.read(), false)) {
mode = MODE_CONFIG;
allowed_to_write_config_eeprom = true;
break;
}
}
// output a countdown to the Serial Monitor
if (millis() - start >= min_over) {
uint8_t countdown_value_display = (startup_time_period - 500 - min_over) / 1000;
if (got_serial_input == false) {
Serial.print(countdown_value_display);
Serial.print(F("..."));
}
updateCornerDot();
min_over += 1000;
}
}
Serial.println();
backlightOn();
if(true) {
valid_ssid_passed = valid_ssid_config();
// check for initial integrity of configuration in eeprom
if((mode != MODE_CONFIG) && mode_requires_wifi(target_mode) && !valid_ssid_passed) {
Serial.println(F("Info: No valid SSID configured, automatically falling back to CONFIG mode."));
configInject(F("aqe\r"));
Serial.println();
if(true) {
// if you're not already in config mode, and if softap is NOT allowed,
// and if your operational mode requires wifi
// and if you don't have a viable ssid configured
// then coerce terminal-based config mode
setLCD_P(PSTR("PLEASE CONFIGURE"
"NETWORK SETTINGS"));
delay(LCD_ERROR_MESSAGE_DELAY);
configInject(F("aqe\r"));
Serial.println();
mode = MODE_CONFIG;
}
}
else if(!integrity_check_passed) {
// we have no choice but to offer config mode to the user
Serial.println(F("Info: Config memory integrity check failed, automatically falling back to CONFIG mode."));
configInject(F("aqe\r"));
Serial.println();
setLCD_P(PSTR("CONFIG INTEGRITY"
" CHECK FAILED "));
mode = MODE_CONFIG;
}
}
resumeGpsProcessing();
Serial.println();
delayForWatchdog();
// check to determine if we have a GPS
uint32_t gps_wait = millis() + 1500;
while(!gps_installed && (gpsSerial.available() || (millis() < gps_wait))) {
char c = gpsSerial.read();
if(c == '$') {
gps_installed = true;
}
}
suspendGpsProcessing();
while((mode == MODE_CONFIG) || ((mode_requires_wifi(target_mode) && !valid_ssid_passed))) {
mode = MODE_CONFIG; // fix for invalid ssid in normal mode and typing exist causing spin state
allowed_to_write_config_eeprom = true;
const uint32_t idle_timeout_period_ms = 1000UL * 60UL * 5UL; // 5 minutes
uint32_t idle_time_ms = 0;
Serial.println(F("-~=* In CONFIG Mode *=~-"));
if(integrity_check_passed) {
setLCD_P(PSTR(" CONFIG MODE"));
}
Serial.print(F("OPERATIONAL mode begins automatically after "));
Serial.print((idle_timeout_period_ms / 1000UL) / 60UL);
Serial.println(F(" mins without input."));
Serial.println(F("Enter 'help' for a list of available commands, "));
configInject(F("get settings\r"));
Serial.println();
Serial.println(F(" @=============================================================@"));
Serial.println(F(" # GETTING STARTED #"));
Serial.println(F(" #-------------------------------------------------------------#"));
Serial.println(F(" # First type 'ssid your_ssid_here' and & press <enter> #"));
Serial.println(F(" # Then type 'pwd your_network_password' & press <enter> #"));
Serial.println(F(" # Then type 'get settings' & press <enter> to review config #"));
Serial.println(F(" # Finally, type 'exit' to go into OPERATIONAL mode, #"));
Serial.println(F(" # and verify that the Egg connects to your network! #"));
Serial.println(F(" @=============================================================@"));
prompt();
for (;;) {
current_millis = millis();
// check to determine if we have a GPS
while(!gps_installed && gpsSerial.available()) {
char c = gpsSerial.read();
if(c == '$') {
gps_installed = true;
}
}
// stuck in this loop until the command line receives an exit command
if(mode != MODE_CONFIG) {
break; // if a command changes mode, we're done with config
}
if (Serial.available()) {
idle_time_ms = 0;
// if you get serial traffic, pass it along to the configModeStateMachine for consumption
if (CONFIG_MODE_GOT_EXIT == configModeStateMachine(Serial.read(), false)) {
mode = MODE_OPERATIONAL;
break;
}
}
// pet the watchdog once a second
if (current_millis - previous_tinywdt_millis >= tinywdt_interval) {
idle_time_ms += tinywdt_interval;
petWatchdog();
previous_tinywdt_millis = current_millis;
}
if (idle_time_ms >= idle_timeout_period_ms) {
Serial.println(F("Info: Idle time expired, exiting CONFIG mode."));
break;
}
}
valid_ssid_passed = valid_ssid_config();
if(valid_ssid_passed) {
break;
}
}
integrity_check_passed = checkConfigIntegrity();
valid_ssid_passed = valid_ssid_config();
ok_to_exit_config_mode = true;
target_mode = eeprom_read_byte((const uint8_t *) EEPROM_OPERATIONAL_MODE);
if(!integrity_check_passed) {
ok_to_exit_config_mode = false;
mode = MODE_CONFIG;
}
else if(mode_requires_wifi(target_mode) && !valid_ssid_passed) {
ok_to_exit_config_mode = false;
mode = MODE_CONFIG;
}
}
while(!ok_to_exit_config_mode);
allowed_to_write_config_eeprom = false;
Serial.println(F("-~=* In OPERATIONAL Mode *=~-"));
setLCD_P(PSTR("OPERATIONAL MODE"));
SUCCESS_MESSAGE_DELAY();
// ... but *which* operational mode are we in?
mode = target_mode;
// ... and what is the temperature and humdidity offset we should use
if(mode_requires_wifi(mode)) {
reported_temperature_offset_degC = eeprom_read_float((float *) EEPROM_TEMPERATURE_OFFSET);
reported_humidity_offset_percent = eeprom_read_float((float *) EEPROM_HUMIDITY_OFFSET);
} else {
reported_temperature_offset_degC = eeprom_read_float((float *) EEPROM_TEMPERATURE_OFFLINE_OFFSET);
reported_humidity_offset_percent = eeprom_read_float((float *) EEPROM_HUMIDITY_OFFLINE_OFFSET);
if(isnan(reported_temperature_offset_degC)) {
reported_temperature_offset_degC = eeprom_read_float((float *) EEPROM_TEMPERATURE_OFFSET);
}
if(isnan(reported_humidity_offset_percent)) {
reported_humidity_offset_percent = eeprom_read_float((float *) EEPROM_HUMIDITY_OFFSET);
}
}
if(isnan(reported_temperature_offset_degC)) {
reported_temperature_offset_degC = 0;
}
if(isnan(reported_humidity_offset_percent)) {
reported_humidity_offset_percent = 0;
}
boolean use_ntp = eeprom_read_byte((uint8_t *) EEPROM_USE_NTP);
boolean shutdown_wifi = !mode_requires_wifi(mode);
mqtt_stay_connected = (eeprom_read_byte((const uint8_t *) EEPROM_MQTT_STAY_CONNECTED) == 1) ? true : false;
if(mode_requires_wifi(mode) || use_ntp) {
shutdown_wifi = false;
// Scan Networks to show RSSI
uint8_t connect_method = eeprom_read_byte((const uint8_t *) EEPROM_CONNECT_METHOD);
displayRSSI();
delayForWatchdog();
petWatchdog();
// Try and Connect to the Configured Network
if(!restartWifi()) {
// technically this code should be unreachable
// because error conditions internal to the restartWifi function
// should restart the unit at a finer granularity
// but this additional report should be harmless at any rate
Serial.println(F("Error: Failed to connect to configured network. Rebooting."));