Skip to content

Commit

Permalink
UART PM2.5 retry data read
Browse files Browse the repository at this point in the history
  • Loading branch information
tyeth committed Jan 23, 2025
1 parent bab3c65 commit 0ddb47f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Wippersnapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; \
} \
Expand Down
64 changes: 33 additions & 31 deletions src/components/uart/drivers/ws_uart_drv_pm25aqi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*******************************************************************************/
Expand Down Expand Up @@ -187,17 +189,17 @@ 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;
}

// Publish message to IO
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());
}
Expand Down

0 comments on commit 0ddb47f

Please sign in to comment.