@@ -79,6 +79,7 @@ volatile char lora_message_from[10];
79
79
#include < elapsedMillis.h>
80
80
elapsedMillis state_ms; // milliseconds elapsed since state switch
81
81
elapsedMillis button_pressed_ms; // milliseconds elapsed since button press
82
+ elapsedMillis loop_ms; // milliseconds elapsed since last loop started
82
83
83
84
// constants
84
85
@@ -147,12 +148,8 @@ Led led(2); // built-in led
147
148
Ticker tickerOSWatch;
148
149
#define OSWATCH_RESET_TIME 10
149
150
150
- static unsigned long last_loop;
151
-
152
151
void ICACHE_RAM_ATTR osWatch (void ) {
153
- unsigned long t = millis ();
154
- unsigned long last_run = abs (t - last_loop);
155
- if (last_run >= (OSWATCH_RESET_TIME * 1000 )) {
152
+ if (loop_ms >= (OSWATCH_RESET_TIME * 1000 )) {
156
153
Serial.println (" !!! osWatch: reset" );
157
154
ESP.reset (); // hard reset
158
155
}
@@ -254,22 +251,26 @@ void hardReset() {
254
251
EspControl.restart ();
255
252
}
256
253
254
+ // first state loop from boot to cool_off since reset / restart.
255
+ // Sleeping does not count as reset / restart
256
+ bool first_state_loop = true ;
257
+
257
258
void setup (void )
258
259
{
259
- last_loop = millis () ;
260
+ loop_ms = 0 ;
260
261
tickerOSWatch.attach_ms (((OSWATCH_RESET_TIME / 3 ) * 1000 ), osWatch);
261
262
262
263
Serial.begin (115200 );
263
264
Serial.setDebugOutput (true );
264
265
Serial.println ();
265
266
266
- if (ESP.getResetReason ().equals (" Hardware Watchdog" )) {
267
- Serial.println (" Clearing state because Hardware Watchdog reset detected." );
268
- ESP.eraseConfig ();
269
- ESP.reset ();
267
+ if (ESP.getResetReason ().equals (" Deep-Sleep Wake" )) {
268
+ Serial.println (" ? woke up from sleep" );
269
+ first_state_loop = false ;
270
+ }
271
+ else {
272
+ Serial.printf (" ? reset reason: %s\n ? reset info: %s\n " , ESP.getResetReason ().c_str (), ESP.getResetInfo ().c_str ());
270
273
}
271
-
272
- Serial.printf (" ? reset reason: %s\n ? reset info: %s\n " , ESP.getResetReason ().c_str (), ESP.getResetInfo ().c_str ());
273
274
274
275
Serial.println (" [setup]" );
275
276
@@ -284,7 +285,6 @@ void setup(void)
284
285
285
286
display.begin ();
286
287
287
- WiFi.mode (WIFI_STA);
288
288
289
289
ensureGpio (PIN_A, 0 );
290
290
ensureGpio (PIN_B, 0 );
@@ -374,7 +374,7 @@ void ICACHE_RAM_ATTR onLoraReceive(int packetSize) {
374
374
375
375
void loop (void )
376
376
{
377
- last_loop = millis () ;
377
+ loop_ms = 0 ;
378
378
display.refresh (state);
379
379
idiotWifiServer.handleClient ();
380
380
led.loop ();
@@ -409,7 +409,7 @@ void loop(void)
409
409
toState (start_server);
410
410
}
411
411
else {
412
- toState (connect_to_wifi );
412
+ toState (load_config );
413
413
}
414
414
415
415
return ;
@@ -418,20 +418,33 @@ void loop(void)
418
418
WiFi.disconnect ();
419
419
WiFi.mode (WIFI_AP);
420
420
idiotWifiServer.start (uuid);
421
- Serial.print (" Serving locally for " );
422
- Serial.print (SERVE_LOCALLY_SECONDS);
423
- Serial.println (" seconds" );
421
+ Serial.printf (" ? serving locally for %d seconds" , SERVE_LOCALLY_SECONDS);
424
422
toState (serve_locally);
425
423
}
426
424
else if (state == serve_locally) {
427
425
if (state_ms > SERVE_LOCALLY_SECONDS * 1000 ) {
428
426
toState (cool_off);
427
+ return ;
428
+ }
429
+
430
+ Serial.print (" ." );
431
+ delay (10 );
432
+ }
433
+ else if (state == load_config) {
434
+ char config[CONFIG_MAX_SIZE];
435
+ PersistentStore.readConfig (config);
436
+ Serial.printf (" [stored config]\n %s\n " , config);
437
+ yield ();
438
+ loadConfig (config, false );
439
+
440
+ if (lora_gateway || first_state_loop) {
441
+ toState (connect_to_wifi);
429
442
}
430
443
else {
431
- Serial.print (" ." );
432
- delay (10 );
444
+ WiFi.mode (WIFI_OFF);
445
+ WiFi.forceSleepBegin ();
446
+ toState (read_senses);
433
447
}
434
- return ;
435
448
}
436
449
else if (state == connect_to_wifi) {
437
450
WiFi.printDiag (Serial);
@@ -440,6 +453,8 @@ void loop(void)
440
453
toState (connect_to_internet);
441
454
return ;
442
455
}
456
+ WiFi.forceSleepWake ();
457
+ WiFi.mode (WIFI_STA);
443
458
char wifiName[WIFI_NAME_MAX_SIZE];
444
459
PersistentStore.readWifiName (wifiName);
445
460
char wifiPassword[WIFI_PASS_MAX_SIZE];
@@ -453,21 +468,19 @@ void loop(void)
453
468
}
454
469
455
470
toState (wifi_wait);
456
- return ;
457
471
}
458
472
else if (state == wifi_wait) {
459
473
if (WiFi.status () == WL_CONNECTED) {
460
474
toState (connect_to_internet);
461
475
}
462
476
else if (state_ms > WIFI_WAIT_SECONDS * 1000 ){
463
477
Serial.println (" \n ? waited for wifi enough, continue without." );
464
- toState (load_config );
478
+ toState (read_senses );
465
479
}
466
480
else {
467
481
Serial.print (' .' );
468
482
delay (50 );
469
483
}
470
- return ;
471
484
}
472
485
else if (state == connect_to_internet) {
473
486
const char * googleGenerate204 = " http://clients3.google.com/generate_204" ;
@@ -496,7 +509,7 @@ void loop(void)
496
509
Serial.printf (" ? redirection detected. GET code: %d\n " , httpCode);
497
510
Serial.println (" !!! WiFi connected but no access to internet - maybe stuck behind a login page." );
498
511
WiFi.disconnect (); // so that next connect_wifi we reconnect
499
- toState (load_config );
512
+ toState (read_senses );
500
513
}
501
514
else {
502
515
Serial.println (" ? successfully passed the login page." );
@@ -510,14 +523,6 @@ void loop(void)
510
523
}
511
524
else if (state == connect_to_mqtt) {
512
525
mqttConnect ();
513
- toState (load_config);
514
- }
515
- else if (state == load_config) {
516
- char config[CONFIG_MAX_SIZE];
517
- PersistentStore.readConfig (config);
518
- Serial.printf (" [stored config]\n %s\n " , config);
519
- yield ();
520
- loadConfig (config, false );
521
526
if (mqttClient.connected ()) {
522
527
toState (update_config);
523
528
}
@@ -537,7 +542,7 @@ void loop(void)
537
542
return ;
538
543
}
539
544
if (!configReceived) {
540
- // maybe server has problems or our connection to it has problems, disconnect to be sure
545
+ // maybe server has problems or our connection to it has problems, disconnect to be sure so next state loop we reconnect
541
546
mqttClient.disconnect ();
542
547
}
543
548
if (configChanged) {
@@ -600,7 +605,7 @@ void loop(void)
600
605
}
601
606
}
602
607
else if (state == send_lora) {
603
- char message[100 ] = " hello gw! " ;
608
+ char message[10 ] = " 42 " ;
604
609
605
610
sendLoraMessage (GATEWAY_ADDR, String (message));
606
611
led.blink_fast (3 );
@@ -630,6 +635,7 @@ void loop(void)
630
635
toState (cool_off);
631
636
}
632
637
else if (state == cool_off) {
638
+ first_state_loop = false ;
633
639
if (sleep_seconds > 0 ) {
634
640
detachInterrupt (DISPLAY_CONTROL_PIN);
635
641
LoRa.sleep ();
0 commit comments