diff --git a/src/PMS/PMS.cpp b/src/PMS/PMS.cpp index 59e525c..4bf4da6 100644 --- a/src/PMS/PMS.cpp +++ b/src/PMS/PMS.cpp @@ -4,32 +4,33 @@ /** * @brief Initializes the sensor and attempts to read data. * - * @param HardwareSerial UART stream + * @param stream UART stream * @return true Sucecss * @return false Failure */ -bool PMSBase::begin(HardwareSerial &serial) { +bool PMSBase::begin(Stream *stream) { + Serial.printf("initializing PM sensor\n"); + failCount = 0; _connected = false; - // Run and check sensor data for 4sec - int bytesCleared = serial.available(); - serial.flush(); - if (bytesCleared) { - Serial.printf("cleared %d byte(s)\n", bytesCleared); + // empty first + int bytesCleared = 0; + while (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 = - serial.write(activeModeCommand, sizeof(activeModeCommand)); + // 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 = stream->write(activeModeCommand, sizeof(activeModeCommand)); Serial.printf("%d byte(s) written\n", bytesWritten); + // Run and check sensor data for 4sec unsigned long lastInit = millis(); while (true) { - readPackage(serial); + readPackage(stream); if (_connected) { break; } @@ -37,15 +38,9 @@ bool PMSBase::begin(HardwareSerial &serial) { delay(1); unsigned long ms = (unsigned long)(millis() - lastInit); if (ms >= 4000) { - Serial.println("Initialize PMS sensor: Timeout"); break; } } - - if (_connected) { - Serial.println("Initialize PMS sensor"); - } - return _connected; } @@ -54,7 +49,7 @@ bool PMSBase::begin(HardwareSerial &serial) { * * @param serial */ -void PMSBase::readPackage(HardwareSerial &serial) { +void PMSBase::readPackage(Stream *serial) { /** If readPackage has process as period larger than READ_PACKAGE_TIMEOUT, * should be clear the lastPackage and readBufferIndex */ if (lastReadPackage) { @@ -83,9 +78,9 @@ void PMSBase::readPackage(HardwareSerial &serial) { /** Count to call delay() to release the while loop MCU resource for avoid the * watchdog time reset */ uint8_t delayCount = 0; - while (serial.available()) { + while (serial->available()) { /** Get value */ - uint8_t value = (uint8_t)serial.read(); + uint8_t value = (uint8_t)serial->read(); /** Process receiving package... */ switch (readBufferIndex) { diff --git a/src/PMS/PMS.h b/src/PMS/PMS.h index 617cdf3..d381d69 100644 --- a/src/PMS/PMS.h +++ b/src/PMS/PMS.h @@ -10,8 +10,8 @@ */ class PMSBase { public: - bool begin(HardwareSerial& serial); - void readPackage(HardwareSerial& serial); + bool begin(Stream *stream); + void readPackage(Stream *stream); void updateFailCount(void); void resetFailCount(void); int getFailCount(void); diff --git a/src/PMS/PMS5003.cpp b/src/PMS/PMS5003.cpp index bf57689..7488bd6 100644 --- a/src/PMS/PMS5003.cpp +++ b/src/PMS/PMS5003.cpp @@ -3,7 +3,6 @@ #include "../Main/utils.h" #if defined(ESP8266) -#include /** * @brief Init sensor * @@ -60,17 +59,16 @@ bool PMS5003::begin(void) { } #if defined(ESP8266) - bsp->Pms5003.uart_tx_pin; - SoftwareSerial *uart = + this->_serial = new SoftwareSerial(bsp->Pms5003.uart_tx_pin, bsp->Pms5003.uart_rx_pin); - uart->begin(9600); - if (pms.begin(uart) == false) { + this->_serial->begin(9600); + if (pms.begin(this->_serial) == false) { AgLog("PMS failed"); return false; } #else this->_serial->begin(9600); - if (pms.begin(*this->_serial) == false) { + if (pms.begin(this->_serial) == false) { AgLog("PMS failed"); return false; } @@ -187,7 +185,7 @@ void PMS5003::end(void) { * @brief Check and read PMS sensor data. This method should be callack from * loop process to continoue check sensor data if it's available */ -void PMS5003::handle(void) { pms.readPackage(*this->_serial); } +void PMS5003::handle(void) { pms.readPackage(this->_serial); } void PMS5003::updateFailCount(void) { pms.updateFailCount(); diff --git a/src/PMS/PMS5003.h b/src/PMS/PMS5003.h index 66af3a7..5bfadde 100644 --- a/src/PMS/PMS5003.h +++ b/src/PMS/PMS5003.h @@ -4,6 +4,9 @@ #include "../Main/BoardDef.h" #include "PMS.h" #include "Stream.h" +#ifdef ESP8266 +#include +#endif /** * @brief The class define how to handle PMS5003 sensor bas on @ref PMS class @@ -41,6 +44,7 @@ private: #if defined(ESP8266) Stream *_debugStream; const char *TAG = "PMS5003"; + SoftwareSerial *_serial; #else HardwareSerial *_serial; #endif diff --git a/src/PMS/PMS5003T.cpp b/src/PMS/PMS5003T.cpp index 0e1a8bf..4294ec5 100644 --- a/src/PMS/PMS5003T.cpp +++ b/src/PMS/PMS5003T.cpp @@ -67,11 +67,10 @@ bool PMS5003T::begin(void) { } #if defined(ESP8266) - bsp->Pms5003.uart_tx_pin; - SoftwareSerial *uart = + this->_serial = new SoftwareSerial(bsp->Pms5003.uart_tx_pin, bsp->Pms5003.uart_rx_pin); - uart->begin(9600); - if (pms.begin(uart) == false) { + this->_serial->begin(9600); + if (pms.begin(this->_serial) == false) { AgLog("PMS failed"); return false; } @@ -98,7 +97,7 @@ bool PMS5003T::begin(void) { this->_serial->begin(9600, SERIAL_8N1, bsp->SenseAirS8.uart_rx_pin, bsp->SenseAirS8.uart_tx_pin); } - if (pms.begin(*this->_serial) == false) { + if (pms.begin(this->_serial) == false) { AgLog("PMS failed"); return false; } @@ -230,7 +229,7 @@ void PMS5003T::end(void) { * @brief Check and read PMS sensor data. This method should be callack from * loop process to continoue check sensor data if it's available */ -void PMS5003T::handle(void) { pms.readPackage(*this->_serial); } +void PMS5003T::handle(void) { pms.readPackage(this->_serial); } void PMS5003T::updateFailCount(void) { pms.updateFailCount(); diff --git a/src/PMS/PMS5003T.h b/src/PMS/PMS5003T.h index ea9cbed..7c3e0df 100644 --- a/src/PMS/PMS5003T.h +++ b/src/PMS/PMS5003T.h @@ -6,6 +6,9 @@ #include "PMS5003TBase.h" #include "Stream.h" #include +#ifdef ESP8266 +#include +#endif /** * @brief The class define how to handle PMS5003T sensor bas on @ref PMS class @@ -47,6 +50,7 @@ private: #if defined(ESP8266) Stream *_debugStream; const char *TAG = "PMS5003T"; + SoftwareSerial *_serial; #else HardwareSerial *_serial; #endif