1414
1515#define HEIGHT_ABOVE_SEA_LEVEL 50 // Berlin
1616#define TZ_DATA " CET-1CEST,M3.5.0,M10.5.0/3" // Europe/Berlin time zone from https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
17+ #define LIGHT_SLEEP_TIME 500
18+ #define DEEP_SLEEP_TIME 29500
1719
1820/* Includes display */
1921#include " DEV_Config.h"
2022#include " epd_abstraction.h"
21- #define DISPLAY_POWER 45
23+ #define DISPLAY_POWER GPIO_NUM_45
2224#define LED_POWER GPIO_NUM_9
2325#define USB_PRESENT GPIO_NUM_4
2426#define BATTERY_VOLTAGE GPIO_NUM_5
@@ -324,7 +326,7 @@ void goto_deep_sleep(int ms) {
324326 /* Wakeup by IO0 button */
325327 rtc_gpio_pullup_en (BUTTON);
326328 rtc_gpio_pulldown_dis (BUTTON);
327- esp_sleep_enable_ext1_wakeup (0x1 ,ESP_EXT1_WAKEUP_ANY_LOW ); // 2^0 = GPIO_NUM_0 = BUTTON
329+ esp_sleep_enable_ext1_wakeup (0x1 ,ESP_EXT1_WAKEUP_ALL_LOW ); // 2^0 = GPIO_NUM_0 = BUTTON
328330
329331 /* Keep LED enabled */
330332 if (LEDonBattery) gpio_hold_en (LED_POWER);
@@ -553,10 +555,11 @@ void toggleWiFi() {
553555 bool ip_shown = false ;
554556 while (digitalRead (BUTTON) != 0 ) { // wait for button press
555557 delay (100 );
558+ wifiManager.process ();
556559 if (!ip_shown && WiFi.status () == WL_CONNECTED) {
560+ delay (100 );
557561 ip_shown = true ;
558562 displayWiFi (useWiFi); // to update displayed IP
559- wifiManager.process ();
560563 }
561564 if (BatteryMode && (digitalRead (USB_PRESENT) == HIGH)) { // power got connected
562565 BatteryMode = false ;
@@ -667,19 +670,18 @@ void loop() {
667670 updateBatteryMode (); // check again in USB Power mode
668671 measureESP32temperature ();
669672
670- if (useWiFi && !BatteryMode) wifiManager.process ();
673+ if (useWiFi && !BatteryMode) {
674+ if (WiFi.status () != WL_CONNECTED) wifiManager.process ();
675+ #ifdef airgradient
676+ if (WiFi.status () == WL_CONNECTED) server.handleClient ();
677+ #endif /* airgradient */
678+ }
671679
672680 bool isDataReady = false ;
673681 uint16_t ready_error = scd4x.getDataReadyFlag (isDataReady);
674682 if (ready_error || !isDataReady) {
675- // needed to overwrite displayed Menu
676- displayWriteMeasuerments (co2, temperature, humidity);
677- if (BatteryMode) displayBattery (calcBatteryPercentage (readBatteryVoltage ()));
678- else if (useWiFi) displayWiFiStrengh ();
679- updateDisplay ();
680-
681- if (BatteryMode) goto_deep_sleep (29000 );
682- else goto_light_sleep (4000 );
683+ if (BatteryMode) goto_deep_sleep (DEEP_SLEEP_TIME);
684+ else goto_light_sleep (LIGHT_SLEEP_TIME);
683685 return ; // otherwise continues running!
684686 }
685687
@@ -696,7 +698,7 @@ void loop() {
696698 /* don't update in Battery mode, unless CO2 has changed by 3% or temperature by 0.5°C */
697699 if (!TEST_MODE && BatteryMode && comingFromDeepSleep) {
698700 if ((abs (new_co2 - co2) < (0.03 *co2)) && (fabs (new_temperature - temperature) < 0.5 )) {
699- goto_deep_sleep (30000 );
701+ goto_deep_sleep (DEEP_SLEEP_TIME );
700702 }
701703 }
702704
@@ -706,31 +708,19 @@ void loop() {
706708 displayWriteMeasuerments (co2, temperature, humidity);
707709 }
708710
709- if (useWiFi) {
710711#ifdef MQTT
711- if (!error && !BatteryMode) {
712- if (WiFi.status () == WL_CONNECTED) {
713- mqttClient.beginMessage (" co2_ppm" );
714- mqttClient.print (co2);
715- mqttClient.endMessage ();
716- mqttClient.beginMessage (" temperature" );
717- mqttClient.print (temperature);
718- mqttClient.endMessage ();
719- mqttClient.beginMessage (" humidity" );
720- mqttClient.print (humidity);
721- mqttClient.endMessage ();
722- }
723- }
724- #endif /* MQTT */
725-
726- #ifdef airgradient
727- if (!error && !BatteryMode) {
728- if (WiFi.status () == WL_CONNECTED) {
729- server.handleClient ();
730- }
731- }
732- #endif /* airgradient */
712+ if (!error && !BatteryMode && useWiFi && WiFi.status () == WL_CONNECTED) {
713+ mqttClient.beginMessage (" co2_ppm" );
714+ mqttClient.print (co2);
715+ mqttClient.endMessage ();
716+ mqttClient.beginMessage (" temperature" );
717+ mqttClient.print (temperature);
718+ mqttClient.endMessage ();
719+ mqttClient.beginMessage (" humidity" );
720+ mqttClient.print (humidity);
721+ mqttClient.endMessage ();
733722 }
723+ #endif /* MQTT */
734724
735725 if (TEST_MODE) {
736726#if ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE
@@ -761,8 +751,8 @@ void loop() {
761751 scd4x.setTemperatureOffset (0.8 );
762752 scd4x.startLowPowerPeriodicMeasurement ();
763753 }
764- goto_deep_sleep (29500 );
754+ goto_deep_sleep (DEEP_SLEEP_TIME );
765755 }
766756
767- goto_light_sleep (4000 );
757+ goto_light_sleep (LIGHT_SLEEP_TIME );
768758}
0 commit comments