Skip to content

Commit 67a1eba

Browse files
committed
If in lora slave mode, only try to connect to wifi and read configuration the first state loop after reset.
Following esp8266/Arduino#460 to try to reduce power consumption when not using wifi by calling forceSleepBegin, forceSleepWake
1 parent 5d17f9b commit 67a1eba

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

src/EspIdiot.ino

+41-35
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ volatile char lora_message_from[10];
7979
#include <elapsedMillis.h>
8080
elapsedMillis state_ms; // milliseconds elapsed since state switch
8181
elapsedMillis button_pressed_ms; // milliseconds elapsed since button press
82+
elapsedMillis loop_ms; // milliseconds elapsed since last loop started
8283

8384
// constants
8485

@@ -147,12 +148,8 @@ Led led(2); // built-in led
147148
Ticker tickerOSWatch;
148149
#define OSWATCH_RESET_TIME 10
149150

150-
static unsigned long last_loop;
151-
152151
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)) {
156153
Serial.println("!!! osWatch: reset");
157154
ESP.reset(); // hard reset
158155
}
@@ -254,22 +251,26 @@ void hardReset() {
254251
EspControl.restart();
255252
}
256253

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+
257258
void setup(void)
258259
{
259-
last_loop = millis();
260+
loop_ms = 0;
260261
tickerOSWatch.attach_ms(((OSWATCH_RESET_TIME / 3) * 1000), osWatch);
261262

262263
Serial.begin(115200);
263264
Serial.setDebugOutput(true);
264265
Serial.println();
265266

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());
270273
}
271-
272-
Serial.printf("? reset reason: %s\n? reset info: %s\n", ESP.getResetReason().c_str(), ESP.getResetInfo().c_str());
273274

274275
Serial.println("[setup]");
275276

@@ -284,7 +285,6 @@ void setup(void)
284285

285286
display.begin();
286287

287-
WiFi.mode(WIFI_STA);
288288

289289
ensureGpio(PIN_A, 0);
290290
ensureGpio(PIN_B, 0);
@@ -374,7 +374,7 @@ void ICACHE_RAM_ATTR onLoraReceive(int packetSize) {
374374

375375
void loop(void)
376376
{
377-
last_loop = millis();
377+
loop_ms = 0;
378378
display.refresh(state);
379379
idiotWifiServer.handleClient();
380380
led.loop();
@@ -409,7 +409,7 @@ void loop(void)
409409
toState(start_server);
410410
}
411411
else {
412-
toState(connect_to_wifi);
412+
toState(load_config);
413413
}
414414

415415
return;
@@ -418,20 +418,33 @@ void loop(void)
418418
WiFi.disconnect();
419419
WiFi.mode(WIFI_AP);
420420
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);
424422
toState(serve_locally);
425423
}
426424
else if (state == serve_locally) {
427425
if (state_ms > SERVE_LOCALLY_SECONDS * 1000) {
428426
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);
429442
}
430443
else {
431-
Serial.print(".");
432-
delay(10);
444+
WiFi.mode(WIFI_OFF);
445+
WiFi.forceSleepBegin();
446+
toState(read_senses);
433447
}
434-
return;
435448
}
436449
else if (state == connect_to_wifi) {
437450
WiFi.printDiag(Serial);
@@ -440,6 +453,8 @@ void loop(void)
440453
toState(connect_to_internet);
441454
return;
442455
}
456+
WiFi.forceSleepWake();
457+
WiFi.mode(WIFI_STA);
443458
char wifiName[WIFI_NAME_MAX_SIZE];
444459
PersistentStore.readWifiName(wifiName);
445460
char wifiPassword[WIFI_PASS_MAX_SIZE];
@@ -453,21 +468,19 @@ void loop(void)
453468
}
454469

455470
toState(wifi_wait);
456-
return;
457471
}
458472
else if (state == wifi_wait) {
459473
if (WiFi.status() == WL_CONNECTED) {
460474
toState(connect_to_internet);
461475
}
462476
else if (state_ms > WIFI_WAIT_SECONDS * 1000){
463477
Serial.println("\n? waited for wifi enough, continue without.");
464-
toState(load_config);
478+
toState(read_senses);
465479
}
466480
else {
467481
Serial.print('.');
468482
delay(50);
469483
}
470-
return;
471484
}
472485
else if (state == connect_to_internet) {
473486
const char* googleGenerate204 = "http://clients3.google.com/generate_204";
@@ -496,7 +509,7 @@ void loop(void)
496509
Serial.printf("? redirection detected. GET code: %d\n", httpCode);
497510
Serial.println("!!! WiFi connected but no access to internet - maybe stuck behind a login page.");
498511
WiFi.disconnect(); // so that next connect_wifi we reconnect
499-
toState(load_config);
512+
toState(read_senses);
500513
}
501514
else {
502515
Serial.println("? successfully passed the login page.");
@@ -510,14 +523,6 @@ void loop(void)
510523
}
511524
else if (state == connect_to_mqtt) {
512525
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);
521526
if (mqttClient.connected()) {
522527
toState(update_config);
523528
}
@@ -537,7 +542,7 @@ void loop(void)
537542
return;
538543
}
539544
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
541546
mqttClient.disconnect();
542547
}
543548
if (configChanged) {
@@ -600,7 +605,7 @@ void loop(void)
600605
}
601606
}
602607
else if (state == send_lora) {
603-
char message[100] = "hello gw!";
608+
char message[10] = "42";
604609

605610
sendLoraMessage(GATEWAY_ADDR, String(message));
606611
led.blink_fast(3);
@@ -630,6 +635,7 @@ void loop(void)
630635
toState(cool_off);
631636
}
632637
else if (state == cool_off) {
638+
first_state_loop = false;
633639
if (sleep_seconds > 0) {
634640
detachInterrupt(DISPLAY_CONTROL_PIN);
635641
LoRa.sleep();

src/State.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33

44
#define FOREACH_STATE(STATE) \
55
STATE(boot) \
6+
STATE(start_server) \
7+
STATE(serve_locally) \
8+
STATE(load_config) \
69
STATE(connect_to_wifi) \
710
STATE(wifi_wait) \
811
STATE(connect_to_internet) \
912
STATE(connect_to_mqtt) \
10-
STATE(start_server) \
11-
STATE(serve_locally) \
12-
STATE(load_config) \
1313
STATE(update_config) \
1414
STATE(wait_for_config) \
15+
STATE(read_senses) \
1516
STATE(setup_lora) \
1617
STATE(send_lora) \
17-
STATE(read_senses) \
1818
STATE(publish) \
1919
STATE(ota_update) \
2020
STATE(cool_off) \

0 commit comments

Comments
 (0)