Skip to content

Commit 79bf981

Browse files
authored
Merge pull request #303 from airgradienthq/fix/ce-tvoc
Fix incorrect TVOC / NOx values when when network option is cellular
2 parents 3d26a54 + 9475724 commit 79bf981

File tree

4 files changed

+53
-26
lines changed

4 files changed

+53
-26
lines changed

examples/OneOpenAir/OneOpenAir.ino

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ enum NetworkOption {
117117
};
118118
NetworkOption networkOption;
119119
TaskHandle_t handleNetworkTask = NULL;
120-
static bool otaInProgress = false;
120+
static bool firmwareUpdateInProgress = false;
121121

122122
static uint32_t factoryBtnPressTime = 0;
123123
static AgFirmwareMode fwMode = FW_MODE_I_9PSL;
@@ -318,8 +318,8 @@ void loop() {
318318
// Schedule to feed external watchdog
319319
watchdogFeedSchedule.run();
320320

321-
if (otaInProgress) {
322-
// OTA currently in progress, temporarily disable running sensor schedules
321+
if (firmwareUpdateInProgress) {
322+
// Firmare update currently in progress, temporarily disable running sensor schedules
323323
delay(10000);
324324
return;
325325
}
@@ -562,43 +562,43 @@ void checkForFirmwareUpdate(void) {
562562
agOta = new AirgradientOTACellular(cellularCard);
563563
}
564564

565-
// Indicate main task that ota is performing
566-
Serial.println("Check for firmware update, disabling main task");
567-
otaInProgress = true;
568-
if (configuration.hasSensorSGP && networkOption == UseCellular) {
569-
// Only for cellular because it can disturb i2c line
570-
Serial.println("Disable SGP41 task for cellular OTA");
571-
ag->sgp41.end();
572-
}
565+
// Indicate main task that firmware update is in progress
566+
firmwareUpdateInProgress = true;
573567

574568
agOta->setHandlerCallback(otaHandlerCallback);
575569
agOta->updateIfAvailable(ag->deviceId().c_str(), GIT_VERSION);
576570

577-
// Only goes to this line if OTA is not success
571+
// Only goes to this line if firmware update is not success
578572
// Handled by otaHandlerCallback
579573

580-
otaInProgress = false;
581-
if (configuration.hasSensorSGP && networkOption == UseCellular) {
582-
// Re-start SGP41 task
583-
if (!sgp41Init()) {
584-
Serial.println("Failed re-start SGP41 task");
585-
}
586-
}
574+
// Indicate main task that firmware update finish
575+
firmwareUpdateInProgress = false;
587576

588577
delete agOta;
589578
Serial.println();
590579
}
591580

592581
void otaHandlerCallback(AirgradientOTA::OtaResult result, const char *msg) {
593582
switch (result) {
594-
case AirgradientOTA::Starting:
583+
case AirgradientOTA::Starting: {
584+
Serial.println("Firmware update starting...");
585+
if (configuration.hasSensorSGP && networkOption == UseCellular) {
586+
// Temporary pause SGP41 task while cellular firmware update is in progress
587+
ag->sgp41.pause();
588+
}
595589
displayExecuteOta(result, fwNewVersion, 0);
596590
break;
591+
}
597592
case AirgradientOTA::InProgress:
598593
Serial.printf("OTA progress: %s\n", msg);
599594
displayExecuteOta(result, "", std::stoi(msg));
600595
break;
601596
case AirgradientOTA::Failed:
597+
displayExecuteOta(result, "", 0);
598+
if (configuration.hasSensorSGP && networkOption == UseCellular) {
599+
ag->sgp41.resume();
600+
}
601+
break;
602602
case AirgradientOTA::Skipped:
603603
case AirgradientOTA::AlreadyUpToDate:
604604
displayExecuteOta(result, "", 0);
@@ -665,7 +665,11 @@ static void displayExecuteOta(AirgradientOTA::OtaResult result, String msg, int
665665
}
666666
delay(1000);
667667
}
668-
oledDisplay.setAirGradient(0);
668+
669+
if (ag->isOne()) {
670+
oledDisplay.setAirGradient(0);
671+
oledDisplay.setBrightness(0);
672+
}
669673
break;
670674
}
671675
default:
@@ -1548,10 +1552,7 @@ void restartIfCeClientIssueOverTwoHours() {
15481552

15491553
void networkingTask(void *args) {
15501554
// OTA check on boot
1551-
#ifdef ESP8266
1552-
// ota not supported
1553-
#else
1554-
// because cellular it takes too long, watchdog triggered
1555+
#ifndef ESP8266
15551556
checkForFirmwareUpdate();
15561557
checkForUpdateSchedule.update();
15571558
#endif

src/Sgp41/Sgp41.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ void Sgp41::handle(void) {
131131
}
132132

133133
#else
134+
135+
void Sgp41::pause() {
136+
onPause = true;
137+
Serial.println("Pausing SGP41 handler task");
138+
// Set latest value to invalid
139+
tvocRaw = utils::getInvalidVOC();
140+
tvoc = utils::getInvalidVOC();
141+
noxRaw = utils::getInvalidNOx();
142+
nox = utils::getInvalidNOx();
143+
}
144+
145+
void Sgp41::resume() {
146+
onPause = false;
147+
Serial.println("Resuming SGP41 handler task");
148+
}
149+
134150
/**
135151
* @brief Handle the sensor conditioning and run time udpate value, This method
136152
* must not call, it's called on private task
@@ -152,6 +168,11 @@ void Sgp41::_handle(void) {
152168
uint16_t srawVoc, srawNox;
153169
for (;;) {
154170
vTaskDelay(pdMS_TO_TICKS(1000));
171+
172+
if (onPause) {
173+
continue;
174+
}
175+
155176
if (getRawSignal(srawVoc, srawNox)) {
156177
tvocRaw = srawVoc;
157178
noxRaw = srawNox;

src/Sgp41/Sgp41.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class Sgp41 {
1818
bool begin(TwoWire &wire, Stream &stream);
1919
void handle(void);
2020
#else
21+
/* pause _handle task to read sensor */
22+
void pause();
23+
/* resume _handle task to read sensor */
24+
void resume();
2125
void _handle(void);
2226
#endif
2327
void end(void);
@@ -32,6 +36,7 @@ class Sgp41 {
3236
int getTvocLearningOffset(void);
3337

3438
private:
39+
bool onPause = false;
3540
bool onConditioning = true;
3641
bool ready = false;
3742
bool _isBegin = false;

0 commit comments

Comments
 (0)