From 81945a358ec2fde508d4bb1c17181bdcf23cdfd5 Mon Sep 17 00:00:00 2001 From: samuelbles07 Date: Sat, 12 Apr 2025 02:22:55 +0700 Subject: [PATCH 1/4] SGP41 add method to pause and resume task handle --- src/Sgp41/Sgp41.cpp | 21 +++++++++++++++++++++ src/Sgp41/Sgp41.h | 5 +++++ 2 files changed, 26 insertions(+) diff --git a/src/Sgp41/Sgp41.cpp b/src/Sgp41/Sgp41.cpp index 3f957a17..2a0d34d4 100644 --- a/src/Sgp41/Sgp41.cpp +++ b/src/Sgp41/Sgp41.cpp @@ -131,6 +131,22 @@ void Sgp41::handle(void) { } #else + +void Sgp41::pauseHandle() { + onPause = true; + Serial.println("Pausing SGP41 handler task"); + // Set latest value to invalid + tvocRaw = utils::getInvalidVOC(); + tvoc = utils::getInvalidVOC(); + noxRaw = utils::getInvalidNOx(); + nox = utils::getInvalidNOx(); +} + +void Sgp41::resumeHandle() { + onPause = false; + Serial.println("Resume SGP41 handler task"); +} + /** * @brief Handle the sensor conditioning and run time udpate value, This method * must not call, it's called on private task @@ -152,6 +168,11 @@ void Sgp41::_handle(void) { uint16_t srawVoc, srawNox; for (;;) { vTaskDelay(pdMS_TO_TICKS(1000)); + + if (onPause) { + continue; + } + if (getRawSignal(srawVoc, srawNox)) { tvocRaw = srawVoc; noxRaw = srawNox; diff --git a/src/Sgp41/Sgp41.h b/src/Sgp41/Sgp41.h index 381af7f3..b8275e6f 100644 --- a/src/Sgp41/Sgp41.h +++ b/src/Sgp41/Sgp41.h @@ -18,6 +18,10 @@ class Sgp41 { bool begin(TwoWire &wire, Stream &stream); void handle(void); #else + /* pause _handle task to read sensor */ + void pauseHandle(); + /* resume _handle task to read sensor */ + void resumeHandle(); void _handle(void); #endif void end(void); @@ -32,6 +36,7 @@ class Sgp41 { int getTvocLearningOffset(void); private: + bool onPause = false; bool onConditioning = true; bool ready = false; bool _isBegin = false; From 9bba89722e2da71d808c2e332728ec9f7258e139 Mon Sep 17 00:00:00 2001 From: samuelbles07 Date: Sat, 12 Apr 2025 02:25:04 +0700 Subject: [PATCH 2/4] Fix sgp unreliable value by only pause task when performing ota --- examples/OneOpenAir/OneOpenAir.ino | 50 +++++++++++++++--------------- src/Libraries/airgradient-ota | 2 +- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/examples/OneOpenAir/OneOpenAir.ino b/examples/OneOpenAir/OneOpenAir.ino index 94ccac76..794fbb39 100644 --- a/examples/OneOpenAir/OneOpenAir.ino +++ b/examples/OneOpenAir/OneOpenAir.ino @@ -117,7 +117,7 @@ enum NetworkOption { }; NetworkOption networkOption; TaskHandle_t handleNetworkTask = NULL; -static bool otaInProgress = false; +static bool firmwareUpdateInProgress = false; static uint32_t factoryBtnPressTime = 0; static AgFirmwareMode fwMode = FW_MODE_I_9PSL; @@ -318,8 +318,8 @@ void loop() { // Schedule to feed external watchdog watchdogFeedSchedule.run(); - if (otaInProgress) { - // OTA currently in progress, temporarily disable running sensor schedules + if (firmwareUpdateInProgress) { + // Firmare update currently in progress, temporarily disable running sensor schedules delay(10000); return; } @@ -562,28 +562,17 @@ void checkForFirmwareUpdate(void) { agOta = new AirgradientOTACellular(cellularCard); } - // Indicate main task that ota is performing - Serial.println("Check for firmware update, disabling main task"); - otaInProgress = true; - if (configuration.hasSensorSGP && networkOption == UseCellular) { - // Only for cellular because it can disturb i2c line - Serial.println("Disable SGP41 task for cellular OTA"); - ag->sgp41.end(); - } + // Indicate main task that firmware update is in progress + firmwareUpdateInProgress = true; agOta->setHandlerCallback(otaHandlerCallback); agOta->updateIfAvailable(ag->deviceId().c_str(), GIT_VERSION); - // Only goes to this line if OTA is not success + // Only goes to this line if firmware update is not success // Handled by otaHandlerCallback - otaInProgress = false; - if (configuration.hasSensorSGP && networkOption == UseCellular) { - // Re-start SGP41 task - if (!sgp41Init()) { - Serial.println("Failed re-start SGP41 task"); - } - } + // Indicate main task that firmware update finish + firmwareUpdateInProgress = false; delete agOta; Serial.println(); @@ -591,9 +580,15 @@ void checkForFirmwareUpdate(void) { void otaHandlerCallback(AirgradientOTA::OtaResult result, const char *msg) { switch (result) { - case AirgradientOTA::Starting: + case AirgradientOTA::Starting: { + Serial.println("Firmware update starting..."); + if (configuration.hasSensorSGP && networkOption == UseCellular) { + // Temporary pause SGP41 task while cellular firmware update is in progress + ag->sgp41.pauseHandle(); + } displayExecuteOta(result, fwNewVersion, 0); break; + } case AirgradientOTA::InProgress: Serial.printf("OTA progress: %s\n", msg); displayExecuteOta(result, "", std::stoi(msg)); @@ -602,6 +597,10 @@ void otaHandlerCallback(AirgradientOTA::OtaResult result, const char *msg) { case AirgradientOTA::Skipped: case AirgradientOTA::AlreadyUpToDate: displayExecuteOta(result, "", 0); + if (configuration.hasSensorSGP && networkOption == UseCellular) { + // Cellular firmware update finish, resuming SGP41 task + ag->sgp41.resumeHandle(); + } break; case AirgradientOTA::Success: displayExecuteOta(result, "", 0); @@ -665,7 +664,11 @@ static void displayExecuteOta(AirgradientOTA::OtaResult result, String msg, int } delay(1000); } - oledDisplay.setAirGradient(0); + + if (ag->isOne()) { + oledDisplay.setAirGradient(0); + oledDisplay.setBrightness(0); + } break; } default: @@ -1548,10 +1551,7 @@ void restartIfCeClientIssueOverTwoHours() { void networkingTask(void *args) { // OTA check on boot -#ifdef ESP8266 - // ota not supported -#else - // because cellular it takes too long, watchdog triggered +#ifndef ESP8266 checkForFirmwareUpdate(); checkForUpdateSchedule.update(); #endif diff --git a/src/Libraries/airgradient-ota b/src/Libraries/airgradient-ota index fde43801..be4e839f 160000 --- a/src/Libraries/airgradient-ota +++ b/src/Libraries/airgradient-ota @@ -1 +1 @@ -Subproject commit fde4380164e2d21db51acae0d6b36c5bc5294192 +Subproject commit be4e839f2927decfa78e16fa49167d6728163ccd From e7603a76590bd0994e2cb24b6e7aba9113565013 Mon Sep 17 00:00:00 2001 From: samuelbles07 Date: Mon, 14 Apr 2025 15:24:53 +0700 Subject: [PATCH 3/4] Update feedback Change airgradient-ota submodule to latest main instead of branch --- examples/OneOpenAir/OneOpenAir.ino | 12 +++++++----- src/Libraries/airgradient-ota | 2 +- src/Sgp41/Sgp41.cpp | 6 +++--- src/Sgp41/Sgp41.h | 4 ++-- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/examples/OneOpenAir/OneOpenAir.ino b/examples/OneOpenAir/OneOpenAir.ino index 794fbb39..8efbd0fd 100644 --- a/examples/OneOpenAir/OneOpenAir.ino +++ b/examples/OneOpenAir/OneOpenAir.ino @@ -584,7 +584,7 @@ void otaHandlerCallback(AirgradientOTA::OtaResult result, const char *msg) { Serial.println("Firmware update starting..."); if (configuration.hasSensorSGP && networkOption == UseCellular) { // Temporary pause SGP41 task while cellular firmware update is in progress - ag->sgp41.pauseHandle(); + ag->sgp41.pause(); } displayExecuteOta(result, fwNewVersion, 0); break; @@ -594,13 +594,15 @@ void otaHandlerCallback(AirgradientOTA::OtaResult result, const char *msg) { displayExecuteOta(result, "", std::stoi(msg)); break; case AirgradientOTA::Failed: + displayExecuteOta(result, "", 0); + if (configuration.hasSensorSGP && networkOption == UseCellular) { + // Cellular firmware update finish, resuming SGP41 task + ag->sgp41.resume(); + } + break; case AirgradientOTA::Skipped: case AirgradientOTA::AlreadyUpToDate: displayExecuteOta(result, "", 0); - if (configuration.hasSensorSGP && networkOption == UseCellular) { - // Cellular firmware update finish, resuming SGP41 task - ag->sgp41.resumeHandle(); - } break; case AirgradientOTA::Success: displayExecuteOta(result, "", 0); diff --git a/src/Libraries/airgradient-ota b/src/Libraries/airgradient-ota index be4e839f..81a0189f 160000 --- a/src/Libraries/airgradient-ota +++ b/src/Libraries/airgradient-ota @@ -1 +1 @@ -Subproject commit be4e839f2927decfa78e16fa49167d6728163ccd +Subproject commit 81a0189f54c45485d4a58de29b394105466e5530 diff --git a/src/Sgp41/Sgp41.cpp b/src/Sgp41/Sgp41.cpp index 2a0d34d4..31707449 100644 --- a/src/Sgp41/Sgp41.cpp +++ b/src/Sgp41/Sgp41.cpp @@ -132,7 +132,7 @@ void Sgp41::handle(void) { #else -void Sgp41::pauseHandle() { +void Sgp41::pause() { onPause = true; Serial.println("Pausing SGP41 handler task"); // Set latest value to invalid @@ -142,9 +142,9 @@ void Sgp41::pauseHandle() { nox = utils::getInvalidNOx(); } -void Sgp41::resumeHandle() { +void Sgp41::resume() { onPause = false; - Serial.println("Resume SGP41 handler task"); + Serial.println("Resuming SGP41 handler task"); } /** diff --git a/src/Sgp41/Sgp41.h b/src/Sgp41/Sgp41.h index b8275e6f..57580480 100644 --- a/src/Sgp41/Sgp41.h +++ b/src/Sgp41/Sgp41.h @@ -19,9 +19,9 @@ class Sgp41 { void handle(void); #else /* pause _handle task to read sensor */ - void pauseHandle(); + void pause(); /* resume _handle task to read sensor */ - void resumeHandle(); + void resume(); void _handle(void); #endif void end(void); From 9475724d0c496bb729be9e97fe832e3b63e4bed8 Mon Sep 17 00:00:00 2001 From: samuelbles07 Date: Tue, 15 Apr 2025 12:20:26 +0700 Subject: [PATCH 4/4] Remove comment --- examples/OneOpenAir/OneOpenAir.ino | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/OneOpenAir/OneOpenAir.ino b/examples/OneOpenAir/OneOpenAir.ino index 8efbd0fd..3241e11f 100644 --- a/examples/OneOpenAir/OneOpenAir.ino +++ b/examples/OneOpenAir/OneOpenAir.ino @@ -596,7 +596,6 @@ void otaHandlerCallback(AirgradientOTA::OtaResult result, const char *msg) { case AirgradientOTA::Failed: displayExecuteOta(result, "", 0); if (configuration.hasSensorSGP && networkOption == UseCellular) { - // Cellular firmware update finish, resuming SGP41 task ag->sgp41.resume(); } break;