set active mode on init

This commit is contained in:
Phat Nguyen
2024-09-24 10:39:17 +07:00
parent 2e0ba26c97
commit ebb3f01dcd

View File

@ -4,7 +4,7 @@
/** /**
* @brief Initializes the sensor and attempts to read data. * @brief Initializes the sensor and attempts to read data.
* *
* @param stream UART stream * @param HardwareSerial UART stream
* @return true Sucecss * @return true Sucecss
* @return false Failure * @return false Failure
*/ */
@ -13,7 +13,19 @@ bool PMSBase::begin(HardwareSerial &serial) {
_connected = false; _connected = false;
// Run and check sensor data for 4sec // Run and check sensor data for 4sec
int bytesCleared = serial.available();
serial.flush(); serial.flush();
if (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 =
serial.write(activeModeCommand, sizeof(activeModeCommand));
Serial.printf("%d byte(s) written\n", bytesWritten);
unsigned long lastInit = millis(); unsigned long lastInit = millis();
while (true) { while (true) {
@ -25,9 +37,15 @@ bool PMSBase::begin(HardwareSerial &serial) {
delay(1); delay(1);
unsigned long ms = (unsigned long)(millis() - lastInit); unsigned long ms = (unsigned long)(millis() - lastInit);
if (ms >= 4000) { if (ms >= 4000) {
Serial.println("Initialize PMS sensor: Timeout");
break; break;
} }
} }
if (_connected) {
Serial.println("Initialize PMS sensor");
}
return _connected; return _connected;
} }