From 0ddb47f2193cd49c83f5c8d73a50d8f2f1e0a519 Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 23 Jan 2025 18:26:14 +0000 Subject: [PATCH] UART PM2.5 retry data read --- platformio.ini | 2 +- src/Wippersnapper.h | 2 +- .../uart/drivers/ws_uart_drv_pm25aqi.h | 64 ++++++++++--------- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/platformio.ini b/platformio.ini index 0382d0bf1..478ae2f9e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -220,7 +220,7 @@ extends = common:esp32 board = adafruit_feather_esp32s3 build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3 -DBOARD_HAS_PSRAM ;set partition to tinyuf2-partitions-4MB.csv as of idf 5.1 -board_build.partitions = tinyuf2-partitions-4MB.csv +board_build.partitions = tinyuf2-partitions-4MB-noota.csv extra_scripts = pre:rename_usb_config.py ; Adafruit Feather ESP32-S3 NO PSRAM diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index e58353b5b..3527c5302 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -95,7 +95,7 @@ { \ unsigned long startTime = millis(); \ while (millis() - startTime < timeout) { \ - result_type result_var = func(__VA_ARGS__); \ + result_var = func(__VA_ARGS__); \ if (condition(result_var)) { \ break; \ } \ diff --git a/src/components/uart/drivers/ws_uart_drv_pm25aqi.h b/src/components/uart/drivers/ws_uart_drv_pm25aqi.h index 4eb4c0c92..be044c89d 100644 --- a/src/components/uart/drivers/ws_uart_drv_pm25aqi.h +++ b/src/components/uart/drivers/ws_uart_drv_pm25aqi.h @@ -102,35 +102,37 @@ class ws_uart_drv_pm25aqi : public ws_uart_drv { */ /*******************************************************************************/ bool read_data() override { - Serial.println("[UART, PM25] Reading data..."); - // Attempt to read the PM2.5 Sensor - if (!_aqi->read(&_data)) { - Serial.println("[UART, PM25] Data not available."); - delay(500); - return false; + // Attempt to read the PM2.5 Sensor, can be flaky see Adafruit_PM25AQI#14 + bool result = false; + RETRY_FUNCTION_UNTIL_TIMEOUT(_aqi->read, bool, result, + [](bool res) -> bool { return res==true; }, + 500, 100, &_data); + + if (!result) { + WS_DEBUG_PRINTLN("[UART, PM25] Data not available."); + return result; } - Serial.println("[UART, PM25] Read data OK"); - Serial.println(); - Serial.println(F("---------------------------------------")); - Serial.println(F("Concentration Units (standard)")); - Serial.println(F("---------------------------------------")); - Serial.print(F("PM 1.0: ")); - Serial.print(_data.pm10_standard); - Serial.print(F("\t\tPM 2.5: ")); - Serial.print(_data.pm25_standard); - Serial.print(F("\t\tPM 10: ")); - Serial.println(_data.pm100_standard); - Serial.println(F("Concentration Units (environmental)")); - Serial.println(F("---------------------------------------")); - Serial.print(F("PM 1.0: ")); - Serial.print(_data.pm10_env); - Serial.print(F("\t\tPM 2.5: ")); - Serial.print(_data.pm25_env); - Serial.print(F("\t\tPM 10: ")); - Serial.println(_data.pm100_env); - Serial.println(F("---------------------------------------")); - - return true; + WS_DEBUG_PRINTLN("[UART, PM25] Read data OK"); + WS_DEBUG_PRINTLN(); + WS_DEBUG_PRINTLN(F("---------------------------------------")); + WS_DEBUG_PRINTLN(F("Concentration Units (standard)")); + WS_DEBUG_PRINTLN(F("---------------------------------------")); + WS_DEBUG_PRINT(F("PM 1.0: ")); + WS_DEBUG_PRINT(_data.pm10_standard); + WS_DEBUG_PRINT(F("\t\tPM 2.5: ")); + WS_DEBUG_PRINT(_data.pm25_standard); + WS_DEBUG_PRINT(F("\t\tPM 10: ")); + WS_DEBUG_PRINTLN(_data.pm100_standard); + WS_DEBUG_PRINTLN(F("Concentration Units (environmental)")); + WS_DEBUG_PRINTLN(F("---------------------------------------")); + WS_DEBUG_PRINT(F("PM 1.0: ")); + WS_DEBUG_PRINT(_data.pm10_env); + WS_DEBUG_PRINT(F("\t\tPM 2.5: ")); + WS_DEBUG_PRINT(_data.pm25_env); + WS_DEBUG_PRINT(F("\t\tPM 10: ")); + WS_DEBUG_PRINTLN(_data.pm100_env); + WS_DEBUG_PRINTLN(F("---------------------------------------")); + return result; } /*******************************************************************************/ @@ -187,7 +189,7 @@ class ws_uart_drv_pm25aqi : public ws_uart_drv { pb_ostream_from_buffer(mqttBuffer, sizeof(mqttBuffer)); if (!ws_pb_encode(&ostream, wippersnapper_signal_v1_UARTResponse_fields, &msgUARTResponse)) { - Serial.println("[ERROR, UART]: Unable to encode device response!"); + WS_DEBUG_PRINTLN("[ERROR, UART]: Unable to encode device response!"); return; } @@ -195,9 +197,9 @@ class ws_uart_drv_pm25aqi : public ws_uart_drv { size_t msgSz; pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_UARTResponse_fields, &msgUARTResponse); - Serial.print("[UART] Publishing event to IO.."); + WS_DEBUG_PRINT("[UART] Publishing event to IO.."); mqttClient->publish(uartTopic, mqttBuffer, msgSz, 1); - Serial.println("Published!"); + WS_DEBUG_PRINTLN("Published!"); setPrvPollTime(millis()); }