@@ -76,6 +76,10 @@ byte local_address = GATEWAY_ADDR;
76
76
volatile char lora_message[LORA_MESSAGE_SIZE];
77
77
volatile char lora_message_from[10 ];
78
78
79
+ #include < elapsedMillis.h>
80
+ elapsedMillis state_ms; // milliseconds elapsed since state switch
81
+ elapsedMillis button_pressed_ms; // milliseconds elapsed since button press
82
+
79
83
// constants
80
84
81
85
#define HARD_RESET_PIN 0
@@ -86,10 +90,10 @@ volatile char lora_message_from[10];
86
90
#define DELTA_WAIT_SECONDS 2
87
91
#define WIFI_WAIT_SECONDS 5
88
92
89
- #define COOL_OFF_WAIT_SECONDS 0
93
+ #define CHILL_WAIT_SECONDS 5
90
94
91
95
#define DEFAULT_PUBLISH_INTERVAL 60
92
- #define DEFAULT_SERVE_LOCALLY_SECONDS 2
96
+ #define SERVE_LOCALLY_SECONDS 60
93
97
#define GPIO_SENSE " gpio-sense"
94
98
#define MAX_ACTIONS_SIZE 10
95
99
@@ -126,14 +130,6 @@ char finalState[MAX_STATE_JSON_LENGTH];
126
130
Action actions[MAX_ACTIONS_SIZE];
127
131
int actionsSize;
128
132
129
-
130
- unsigned long coolOffStartTime;
131
- unsigned long i2cPowerStartTime;
132
- unsigned long wifiWaitStartTime;
133
- unsigned long updateConfigStartTime;
134
- unsigned long serveLocallyStartMs;
135
- float serveLocallySeconds;
136
-
137
133
#define DISPLAY_CONTROL_PIN 0
138
134
OLED oled (I2C_PIN_SDA, I2C_PIN_SCL);
139
135
DisplayController display (oled);
@@ -149,7 +145,7 @@ Led led(2); // built-in led
149
145
// source: https://github.com/esp8266/Arduino/issues/1532
150
146
#include < Ticker.h>
151
147
Ticker tickerOSWatch;
152
- #define OSWATCH_RESET_TIME 30
148
+ #define OSWATCH_RESET_TIME 10
153
149
154
150
static unsigned long last_loop;
155
151
@@ -169,7 +165,7 @@ void toState(state_enum newState) {
169
165
return ;
170
166
}
171
167
Serial.printf (" \n [%s] -> [%s]\n " , STATE_STRING[state], STATE_STRING[newState]);
172
-
168
+ state_ms = 0 ;
173
169
state = newState;
174
170
}
175
171
@@ -316,9 +312,9 @@ volatile unsigned long lastInterruptTime = 0;
316
312
volatile unsigned long debounceDelay = 300 ;
317
313
318
314
void ICACHE_RAM_ATTR interruptDisplayButtonPressed () {
319
- if (millis () - lastInterruptTime > debounceDelay) {
315
+ if (button_pressed_ms > debounceDelay) {
320
316
display.changePage ();
321
- lastInterruptTime = millis () ;
317
+ button_pressed_ms = 0 ;
322
318
}
323
319
}
324
320
@@ -396,13 +392,6 @@ void loop(void)
396
392
configReceived = false ;
397
393
gpioStateChanged = false ;
398
394
399
- coolOffStartTime = 0 ;
400
- i2cPowerStartTime = 0 ;
401
- updateConfigStartTime = 0 ;
402
- wifiWaitStartTime = 0 ;
403
-
404
- serveLocallyStartMs = 0 ;
405
- serveLocallySeconds = DEFAULT_SERVE_LOCALLY_SECONDS;
406
395
actionsSize = 0 ;
407
396
408
397
GpioState.clear ();
@@ -417,29 +406,29 @@ void loop(void)
417
406
local_address = lora_gateway ? GATEWAY_ADDR: SLAVE_ADDR;
418
407
419
408
if (!PersistentStore.wifiCredentialsStored ()) {
420
- toState (serve_locally);
421
- serveLocallySeconds = 60 ;
409
+ toState (start_server);
422
410
}
423
411
else {
424
412
toState (connect_to_wifi);
425
413
}
426
414
427
415
return ;
428
416
}
417
+ else if (state == start_server) {
418
+ WiFi.disconnect ();
419
+ WiFi.mode (WIFI_AP);
420
+ idiotWifiServer.start (uuid);
421
+ Serial.print (" Serving locally for " );
422
+ Serial.print (SERVE_LOCALLY_SECONDS);
423
+ Serial.println (" seconds" );
424
+ toState (serve_locally);
425
+ }
429
426
else if (state == serve_locally) {
430
- if (serveLocallyStartMs == 0 ) {
431
- WiFi.disconnect ();
432
- WiFi.mode (WIFI_AP);
433
- idiotWifiServer.start (uuid);
434
- serveLocallyStartMs = millis ();
435
- Serial.print (" Serving locally for " );
436
- Serial.print (serveLocallySeconds);
437
- Serial.println (" seconds" );
438
- }
439
- else if (millis () - serveLocallyStartMs > serveLocallySeconds * 1000 ) {
427
+ if (state_ms > SERVE_LOCALLY_SECONDS * 1000 ) {
440
428
toState (cool_off);
441
429
}
442
430
else {
431
+ Serial.print (" ." );
443
432
delay (10 );
444
433
}
445
434
return ;
@@ -463,15 +452,14 @@ void loop(void)
463
452
WiFi.begin (wifiName, wifiPassword);
464
453
}
465
454
466
- wifiWaitStartTime = millis ();
467
455
toState (wifi_wait);
468
456
return ;
469
457
}
470
458
else if (state == wifi_wait) {
471
459
if (WiFi.status () == WL_CONNECTED) {
472
460
toState (connect_to_internet);
473
461
}
474
- else if (millis () - wifiWaitStartTime > WIFI_WAIT_SECONDS * 1000 ){
462
+ else if (state_ms > WIFI_WAIT_SECONDS * 1000 ){
475
463
Serial.println (" \n ? waited for wifi enough, continue without." );
476
464
toState (load_config);
477
465
}
@@ -523,7 +511,6 @@ void loop(void)
523
511
else if (state == connect_to_mqtt) {
524
512
mqttConnect ();
525
513
toState (load_config);
526
- return ;
527
514
}
528
515
else if (state == load_config) {
529
516
char config[CONFIG_MAX_SIZE];
@@ -539,27 +526,24 @@ void loop(void)
539
526
}
540
527
}
541
528
else if (state == update_config) {
542
- if (updateConfigStartTime == 0 ) {
543
- requestState ();
544
- updateConfigStartTime = millis ();
545
- configChanged = false ;
546
- configReceived = false ;
547
- }
548
-
549
- if (!configReceived && millis () - updateConfigStartTime < DELTA_WAIT_SECONDS * 1000 ) {
529
+ requestState ();
530
+ configChanged = false ;
531
+ configReceived = false ;
532
+ toState (wait_for_config);
533
+ }
534
+ else if (state == wait_for_config) {
535
+ if (!configReceived && state_ms < DELTA_WAIT_SECONDS * 1000 ) {
550
536
Serial.print (' .' );
551
537
return ;
552
538
}
553
- else {
554
- if (!configReceived) {
555
- // maybe server has problems or our connection to it has problems, disconnect to be sure
556
- mqttClient.disconnect ();
557
- }
558
- if (configChanged) {
559
- saveConfig ();
560
- }
561
- toState (read_senses);
539
+ if (!configReceived) {
540
+ // maybe server has problems or our connection to it has problems, disconnect to be sure
541
+ mqttClient.disconnect ();
542
+ }
543
+ if (configChanged) {
544
+ saveConfig ();
562
545
}
546
+ toState (read_senses);
563
547
}
564
548
else if (state == read_senses) {
565
549
StaticJsonBuffer<MAX_READ_SENSES_RESULT_SIZE> jsonBuffer;
@@ -652,10 +636,10 @@ void loop(void)
652
636
toState (deep_sleep);
653
637
return ;
654
638
}
655
- if (coolOffStartTime == 0 ) {
656
- coolOffStartTime = millis ();
657
- }
658
- else if (millis () - coolOffStartTime < COOL_OFF_WAIT_SECONDS * 1000 ) {
639
+ toState (chill);
640
+ }
641
+ else if (state == chill) {
642
+ if (state_ms < CHILL_WAIT_SECONDS * 1000 ) {
659
643
Serial.print (' .' );
660
644
delay (100 );
661
645
}
0 commit comments