Explicitly set active mode for PM sensor upon initialization

This commit is contained in:
nick-4711
2024-09-15 08:26:38 +07:00
parent 90f336dee7
commit 520550037d
6 changed files with 35 additions and 21 deletions

View File

@ -697,7 +697,7 @@ static void oneIndoorInit(void) {
ledBarEnabledUpdate(); ledBarEnabledUpdate();
/** Show message init sensor */ /** Show message init sensor */
oledDisplay.setText("Sensor", "initializing...", ""); oledDisplay.setText("Monitor", "initializing...", "");
/** Init sensor SGP41 */ /** Init sensor SGP41 */
if (sgp41Init() == false) { 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) { if (fwMode == FW_MODE_O_1PST) {
bool pmInitSuccess = false; bool pmInitSuccess = false;
if (serial0Available) { if (serial0Available) {
if (ag->pms5003t_1.begin(Serial0) == false) { if (ag->pms5003t_1.begin(Serial0) == false) {
configuration.hasSensorPMS1 = false; configuration.hasSensorPMS1 = false;
Serial.println("PMS1 sensor not found"); Serial.println("No PM sensor detected on Serial0");
} else { } else {
serial0Available = false; serial0Available = false;
pmInitSuccess = true; pmInitSuccess = true;
Serial.println("Found PMS 1 on Serial0"); Serial.println("Detected PM 1 on Serial0");
} }
} }
if (pmInitSuccess == false) { if (pmInitSuccess == false) {
if (serial1Available) { if (serial1Available) {
if (ag->pms5003t_1.begin(Serial1) == false) { if (ag->pms5003t_1.begin(Serial1) == false) {
configuration.hasSensorPMS1 = false; configuration.hasSensorPMS1 = false;
Serial.println("PMS1 sensor not found"); Serial.println("No PM sensor detected on Serial1");
} else { } else {
serial1Available = false; 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 { } else {
if (ag->pms5003t_1.begin(Serial0) == false) { if (ag->pms5003t_1.begin(Serial0) == false) {
configuration.hasSensorPMS1 = false; configuration.hasSensorPMS1 = false;
Serial.println("PMS1 sensor not found"); Serial.println("No PM sensor detected on Serial0");
} else { } else {
Serial.println("Found PMS 1 on Serial0"); Serial.println("Detected PM 1 on Serial0");
} }
if (ag->pms5003t_2.begin(Serial1) == false) { if (ag->pms5003t_2.begin(Serial1) == false) {
configuration.hasSensorPMS2 = false; configuration.hasSensorPMS2 = false;
Serial.println("PMS2 sensor not found"); Serial.println("No PM sensor detected on Serial1");
} else { } else {
Serial.println("Found PMS 2 on Serial1"); Serial.println("Detected PM 2 on Serial1");
} }
if (fwMode == FW_MODE_O_1PP) { if (fwMode == FW_MODE_O_1PP) {

View File

@ -519,7 +519,7 @@ void OledDisplay::showRebooting(void) {
do { do {
DISP()->setFont(u8g2_font_t0_16_tf); DISP()->setFont(u8g2_font_t0_16_tf);
// setCentralText(20, "Firmware Update"); // setCentralText(20, "Firmware Update");
setCentralText(40, "Reboot..."); setCentralText(40, "Rebooting...");
// setCentralText(60, String("Retry after 24h")); // setCentralText(60, String("Retry after 24h"));
} while (DISP()->nextPage()); } while (DISP()->nextPage());
} else if (ag->isBasic()) { } else if (ag->isBasic()) {

View File

@ -72,7 +72,7 @@ bool WifiConnector::connect(void) {
WIFI()->setSaveParamsCallback([this]() { _wifiSaveParamCallback(); }); WIFI()->setSaveParamsCallback([this]() { _wifiSaveParamCallback(); });
WIFI()->setConfigPortalTimeoutCallback([this]() {_wifiTimeoutCallback();}); WIFI()->setConfigPortalTimeoutCallback([this]() {_wifiTimeoutCallback();});
if (ag->isOne() || (ag->isPro4_2()) || ag->isPro3_3() || ag->isBasic()) { if (ag->isOne() || (ag->isPro4_2()) || ag->isPro3_3() || ag->isBasic()) {
disp.setText("Connect to", "WiFi", "..."); disp.setText("Connecting to", "WiFi", "...");
} else { } else {
logInfo("Connecting to WiFi..."); logInfo("Connecting to WiFi...");
} }

View File

@ -2,34 +2,48 @@
#include "../Main/BoardDef.h" #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 * @param stream UART stream
* @return true Sucecss * @return true Sucecss
* @return false Failure * @return false Failure
*/ */
bool PMSBase::begin(Stream *stream) { bool PMSBase::begin(Stream *stream) {
Serial.printf("initializing PM sensor\n");
this->stream = stream; this->stream = stream;
failed = true; failed = true;
failCount = 0; failCount = 0;
lastRead = 0; // To read buffer on handle without wait after 1.5sec 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 // Run and check sensor data for 4sec
while (1) { while (1) {
handle(); handle();
if (failed == false) { if (failed == false) {
return true; Serial.printf("PM sensor initialized\n");
return true;
} }
delay(1); delay(10);
uint32_t ms = (uint32_t)(millis() - lastRead); uint32_t ms = (uint32_t)(millis() - lastRead);
if (ms >= 4000) { if (ms >= 4000) {
break; break;
} }
} }
Serial.printf("PM sensor initialization failed\n");
return false; return false;
} }

View File

@ -5,6 +5,9 @@
#define PMS_FAIL_COUNT_SET_INVALID 3 #define PMS_FAIL_COUNT_SET_INVALID 3
/**
* Known to work with these sensors: Plantower PMS5003, Plantower PMS5003, Cubic PM2009X
*/
class PMSBase { class PMSBase {
public: public:
bool begin(Stream *stream); bool begin(Stream *stream);

View File

@ -38,14 +38,11 @@ bool PMS5003::begin(HardwareSerial &serial) {
PMS5003::PMS5003(BoardType def) : _boardDef(def) {} PMS5003::PMS5003(BoardType def) : _boardDef(def) {}
/** /**
* @brief Init sensor * Initializes the sensor.
*
* @return true Success
* @return false Failure
*/ */
bool PMS5003::begin(void) { bool PMS5003::begin(void) {
if (this->_isBegin) { if (this->_isBegin) {
AgLog("Initialized, call end() then try again"); AgLog("Already initialized, call end() then try again");
return true; return true;
} }