From 520550037d9ab0a65f7dde789153cbef9b53d93a Mon Sep 17 00:00:00 2001 From: nick-4711 Date: Sun, 15 Sep 2024 08:26:38 +0700 Subject: [PATCH] Explicitly set active mode for PM sensor upon initialization --- examples/OneOpenAir/OneOpenAir.ino | 20 ++++++++++---------- src/AgOledDisplay.cpp | 2 +- src/AgWiFiConnector.cpp | 2 +- src/PMS/PMS.cpp | 22 ++++++++++++++++++---- src/PMS/PMS.h | 3 +++ src/PMS/PMS5003.cpp | 7 ++----- 6 files changed, 35 insertions(+), 21 deletions(-) diff --git a/examples/OneOpenAir/OneOpenAir.ino b/examples/OneOpenAir/OneOpenAir.ino index ee7314e..2ade349 100644 --- a/examples/OneOpenAir/OneOpenAir.ino +++ b/examples/OneOpenAir/OneOpenAir.ino @@ -697,7 +697,7 @@ static void oneIndoorInit(void) { ledBarEnabledUpdate(); /** Show message init sensor */ - oledDisplay.setText("Sensor", "initializing...", ""); + oledDisplay.setText("Monitor", "initializing...", ""); /** Init sensor SGP41 */ if (sgp41Init() == false) { @@ -778,27 +778,27 @@ static void openAirInit(void) { } } - /** Try to find the PMS on other difference port with S8 */ + /** Attempt to detect PM sensors */ if (fwMode == FW_MODE_O_1PST) { bool pmInitSuccess = false; if (serial0Available) { if (ag->pms5003t_1.begin(Serial0) == false) { configuration.hasSensorPMS1 = false; - Serial.println("PMS1 sensor not found"); + Serial.println("No PM sensor detected on Serial0"); } else { serial0Available = false; pmInitSuccess = true; - Serial.println("Found PMS 1 on Serial0"); + Serial.println("Detected PM 1 on Serial0"); } } if (pmInitSuccess == false) { if (serial1Available) { if (ag->pms5003t_1.begin(Serial1) == false) { configuration.hasSensorPMS1 = false; - Serial.println("PMS1 sensor not found"); + Serial.println("No PM sensor detected on Serial1"); } else { serial1Available = false; - Serial.println("Found PMS 1 on Serial1"); + Serial.println("Detected PM 1 on Serial1"); } } } @@ -806,15 +806,15 @@ static void openAirInit(void) { } else { if (ag->pms5003t_1.begin(Serial0) == false) { configuration.hasSensorPMS1 = false; - Serial.println("PMS1 sensor not found"); + Serial.println("No PM sensor detected on Serial0"); } else { - Serial.println("Found PMS 1 on Serial0"); + Serial.println("Detected PM 1 on Serial0"); } if (ag->pms5003t_2.begin(Serial1) == false) { configuration.hasSensorPMS2 = false; - Serial.println("PMS2 sensor not found"); + Serial.println("No PM sensor detected on Serial1"); } else { - Serial.println("Found PMS 2 on Serial1"); + Serial.println("Detected PM 2 on Serial1"); } if (fwMode == FW_MODE_O_1PP) { diff --git a/src/AgOledDisplay.cpp b/src/AgOledDisplay.cpp index 109be27..8250407 100644 --- a/src/AgOledDisplay.cpp +++ b/src/AgOledDisplay.cpp @@ -519,7 +519,7 @@ void OledDisplay::showRebooting(void) { do { DISP()->setFont(u8g2_font_t0_16_tf); // setCentralText(20, "Firmware Update"); - setCentralText(40, "Reboot..."); + setCentralText(40, "Rebooting..."); // setCentralText(60, String("Retry after 24h")); } while (DISP()->nextPage()); } else if (ag->isBasic()) { diff --git a/src/AgWiFiConnector.cpp b/src/AgWiFiConnector.cpp index ddf4736..66fa34d 100644 --- a/src/AgWiFiConnector.cpp +++ b/src/AgWiFiConnector.cpp @@ -72,7 +72,7 @@ bool WifiConnector::connect(void) { WIFI()->setSaveParamsCallback([this]() { _wifiSaveParamCallback(); }); WIFI()->setConfigPortalTimeoutCallback([this]() {_wifiTimeoutCallback();}); if (ag->isOne() || (ag->isPro4_2()) || ag->isPro3_3() || ag->isBasic()) { - disp.setText("Connect to", "WiFi", "..."); + disp.setText("Connecting to", "WiFi", "..."); } else { logInfo("Connecting to WiFi..."); } diff --git a/src/PMS/PMS.cpp b/src/PMS/PMS.cpp index 1107185..5b2ef27 100644 --- a/src/PMS/PMS.cpp +++ b/src/PMS/PMS.cpp @@ -2,34 +2,48 @@ #include "../Main/BoardDef.h" /** - * @brief Init and check that sensor has connected + * @brief Initializes the sensor and attempts to read data. * * @param stream UART stream * @return true Sucecss * @return false Failure */ bool PMSBase::begin(Stream *stream) { + Serial.printf("initializing PM sensor\n"); this->stream = stream; failed = true; failCount = 0; lastRead = 0; // To read buffer on handle without wait after 1.5sec - this->stream->flush(); + // empty first + int bytesCleared = 0; + while (this->stream->read() != -1) { + bytesCleared++; + } + Serial.printf("cleared %d byte(s)\n", bytesCleared); + + // explicitly put the sensor into active mode, this seems to be be needed for the Cubic PM2009X + Serial.printf("setting active mode\n"); + uint8_t activeModeCommand[] = { 0x42, 0x4D, 0xE1, 0x00, 0x01, 0x01, 0x71 }; + size_t bytesWritten = this->stream->write(activeModeCommand, sizeof(activeModeCommand)); + Serial.printf("%d byte(s) written\n", bytesWritten); // Run and check sensor data for 4sec while (1) { handle(); if (failed == false) { - return true; + Serial.printf("PM sensor initialized\n"); + return true; } - delay(1); + delay(10); uint32_t ms = (uint32_t)(millis() - lastRead); if (ms >= 4000) { break; } } + Serial.printf("PM sensor initialization failed\n"); return false; } diff --git a/src/PMS/PMS.h b/src/PMS/PMS.h index e79480e..3fd5108 100644 --- a/src/PMS/PMS.h +++ b/src/PMS/PMS.h @@ -5,6 +5,9 @@ #define PMS_FAIL_COUNT_SET_INVALID 3 +/** + * Known to work with these sensors: Plantower PMS5003, Plantower PMS5003, Cubic PM2009X + */ class PMSBase { public: bool begin(Stream *stream); diff --git a/src/PMS/PMS5003.cpp b/src/PMS/PMS5003.cpp index b632627..8daf3d8 100644 --- a/src/PMS/PMS5003.cpp +++ b/src/PMS/PMS5003.cpp @@ -38,14 +38,11 @@ bool PMS5003::begin(HardwareSerial &serial) { PMS5003::PMS5003(BoardType def) : _boardDef(def) {} /** - * @brief Init sensor - * - * @return true Success - * @return false Failure + * Initializes the sensor. */ bool PMS5003::begin(void) { if (this->_isBegin) { - AgLog("Initialized, call end() then try again"); + AgLog("Already initialized, call end() then try again"); return true; }