Skip to content

Commit 5d17f9b

Browse files
committed
Using elapsedMillis library to refacture code blocks with non-blocking waits
1 parent 700e0fd commit 5d17f9b

File tree

3 files changed

+46
-59
lines changed

3 files changed

+46
-59
lines changed

platformio.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ platform = espressif8266
99
framework = arduino
1010
board = esp12e
1111
upload_resetmethod = ck
12-
lib_deps = 1, 19, 64, 294, 1167
12+
lib_deps = 1, 19, 64, 294, 1167, 1002
1313
upload_port = /dev/ttyUSB0
1414
build_flags = -D ZELENIK2
1515

@@ -18,6 +18,6 @@ platform = espressif8266
1818
framework = arduino
1919
board = esp12e
2020
upload_resetmethod = ck
21-
lib_deps = 1, 19, 64, 294, 1167
21+
lib_deps = 1, 19, 64, 294, 1167, 1002
2222
upload_port = /dev/ttyUSB0
2323
build_flags = -D ZELENIK1

src/EspIdiot.ino

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ byte local_address = GATEWAY_ADDR;
7676
volatile char lora_message[LORA_MESSAGE_SIZE];
7777
volatile char lora_message_from[10];
7878

79+
#include <elapsedMillis.h>
80+
elapsedMillis state_ms; // milliseconds elapsed since state switch
81+
elapsedMillis button_pressed_ms; // milliseconds elapsed since button press
82+
7983
// constants
8084

8185
#define HARD_RESET_PIN 0
@@ -86,10 +90,10 @@ volatile char lora_message_from[10];
8690
#define DELTA_WAIT_SECONDS 2
8791
#define WIFI_WAIT_SECONDS 5
8892

89-
#define COOL_OFF_WAIT_SECONDS 0
93+
#define CHILL_WAIT_SECONDS 5
9094

9195
#define DEFAULT_PUBLISH_INTERVAL 60
92-
#define DEFAULT_SERVE_LOCALLY_SECONDS 2
96+
#define SERVE_LOCALLY_SECONDS 60
9397
#define GPIO_SENSE "gpio-sense"
9498
#define MAX_ACTIONS_SIZE 10
9599

@@ -126,14 +130,6 @@ char finalState[MAX_STATE_JSON_LENGTH];
126130
Action actions[MAX_ACTIONS_SIZE];
127131
int actionsSize;
128132

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-
137133
#define DISPLAY_CONTROL_PIN 0
138134
OLED oled(I2C_PIN_SDA, I2C_PIN_SCL);
139135
DisplayController display(oled);
@@ -149,7 +145,7 @@ Led led(2); // built-in led
149145
// source: https://github.com/esp8266/Arduino/issues/1532
150146
#include <Ticker.h>
151147
Ticker tickerOSWatch;
152-
#define OSWATCH_RESET_TIME 30
148+
#define OSWATCH_RESET_TIME 10
153149

154150
static unsigned long last_loop;
155151

@@ -169,7 +165,7 @@ void toState(state_enum newState) {
169165
return;
170166
}
171167
Serial.printf("\n[%s] -> [%s]\n", STATE_STRING[state], STATE_STRING[newState]);
172-
168+
state_ms = 0;
173169
state = newState;
174170
}
175171

@@ -316,9 +312,9 @@ volatile unsigned long lastInterruptTime = 0;
316312
volatile unsigned long debounceDelay = 300;
317313

318314
void ICACHE_RAM_ATTR interruptDisplayButtonPressed() {
319-
if (millis() - lastInterruptTime > debounceDelay) {
315+
if (button_pressed_ms > debounceDelay) {
320316
display.changePage();
321-
lastInterruptTime = millis();
317+
button_pressed_ms = 0;
322318
}
323319
}
324320

@@ -396,13 +392,6 @@ void loop(void)
396392
configReceived = false;
397393
gpioStateChanged = false;
398394

399-
coolOffStartTime = 0;
400-
i2cPowerStartTime = 0;
401-
updateConfigStartTime = 0;
402-
wifiWaitStartTime = 0;
403-
404-
serveLocallyStartMs = 0;
405-
serveLocallySeconds = DEFAULT_SERVE_LOCALLY_SECONDS;
406395
actionsSize = 0;
407396

408397
GpioState.clear();
@@ -417,29 +406,29 @@ void loop(void)
417406
local_address = lora_gateway ? GATEWAY_ADDR: SLAVE_ADDR;
418407

419408
if (!PersistentStore.wifiCredentialsStored()) {
420-
toState(serve_locally);
421-
serveLocallySeconds = 60;
409+
toState(start_server);
422410
}
423411
else {
424412
toState(connect_to_wifi);
425413
}
426414

427415
return;
428416
}
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+
}
429426
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) {
440428
toState(cool_off);
441429
}
442430
else {
431+
Serial.print(".");
443432
delay(10);
444433
}
445434
return;
@@ -463,15 +452,14 @@ void loop(void)
463452
WiFi.begin(wifiName, wifiPassword);
464453
}
465454

466-
wifiWaitStartTime = millis();
467455
toState(wifi_wait);
468456
return;
469457
}
470458
else if (state == wifi_wait) {
471459
if (WiFi.status() == WL_CONNECTED) {
472460
toState(connect_to_internet);
473461
}
474-
else if (millis() - wifiWaitStartTime > WIFI_WAIT_SECONDS * 1000){
462+
else if (state_ms > WIFI_WAIT_SECONDS * 1000){
475463
Serial.println("\n? waited for wifi enough, continue without.");
476464
toState(load_config);
477465
}
@@ -523,7 +511,6 @@ void loop(void)
523511
else if (state == connect_to_mqtt) {
524512
mqttConnect();
525513
toState(load_config);
526-
return;
527514
}
528515
else if (state == load_config) {
529516
char config[CONFIG_MAX_SIZE];
@@ -539,27 +526,24 @@ void loop(void)
539526
}
540527
}
541528
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) {
550536
Serial.print('.');
551537
return;
552538
}
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();
562545
}
546+
toState(read_senses);
563547
}
564548
else if (state == read_senses) {
565549
StaticJsonBuffer<MAX_READ_SENSES_RESULT_SIZE> jsonBuffer;
@@ -652,10 +636,10 @@ void loop(void)
652636
toState(deep_sleep);
653637
return;
654638
}
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) {
659643
Serial.print('.');
660644
delay(100);
661645
}

src/State.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
STATE(wifi_wait) \
88
STATE(connect_to_internet) \
99
STATE(connect_to_mqtt) \
10+
STATE(start_server) \
1011
STATE(serve_locally) \
1112
STATE(load_config) \
1213
STATE(update_config) \
14+
STATE(wait_for_config) \
1315
STATE(setup_lora) \
1416
STATE(send_lora) \
1517
STATE(read_senses) \
1618
STATE(publish) \
1719
STATE(ota_update) \
1820
STATE(cool_off) \
21+
STATE(chill) \
1922
STATE(deep_sleep) \
2023
STATE(hard_reset) \
2124

0 commit comments

Comments
 (0)