From 469d07a2d6c1c40a695b98a96d85bd7e201cc422 Mon Sep 17 00:00:00 2001 From: Phat Nguyen Date: Wed, 6 Mar 2024 17:20:55 +0700 Subject: [PATCH] fix: O-1PS not recognized --- examples/BASIC/BASIC.ino | 20 +++++-- examples/ONE/ONE.ino | 19 ++++-- examples/Open_Air/Open_Air.ino | 105 +++++++++++++++++++++++++-------- examples/TestCO2/TestCO2.ino | 2 +- src/PMS/PMS5003T.cpp | 6 +- src/S8/S8.cpp | 18 +++++- src/Sgp41/Sgp41.cpp | 4 +- 7 files changed, 133 insertions(+), 41 deletions(-) diff --git a/examples/BASIC/BASIC.ino b/examples/BASIC/BASIC.ino index 6dc0efb..383f158 100644 --- a/examples/BASIC/BASIC.ino +++ b/examples/BASIC/BASIC.ino @@ -382,7 +382,9 @@ bool hasSensorS8 = true; bool hasSensorPMS = true; bool hasSensorSHT = true; int pmFailCount = 0; -AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, updateServerConfiguration); +int getCO2FailCount = 0; +AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, + updateServerConfiguration); AgSchedule serverSchedule(SERVER_SYNC_INTERVAL, sendDataToServer); AgSchedule dispSchedule(DISP_UPDATE_INTERVAL, dispHandler); AgSchedule co2Schedule(SENSOR_CO2_UPDATE_INTERVAL, co2Update); @@ -591,7 +593,7 @@ static void updateServerConfiguration(void) { Serial.printf("abcDays config: %d days(%d hours)\r\n", agServer.getCo2AbcDaysConfig(), newHour); int curHour = ag.s8.getAbcPeriod(); - Serial.printf("Current config: %d (hours)\r\n", ag.s8.getAbcPeriod()); + Serial.printf("Current config: %d (hours)\r\n", curHour); if (curHour == newHour) { Serial.println("set 'abcDays' ignored"); } else { @@ -610,8 +612,18 @@ static void updateServerConfiguration(void) { } static void co2Update() { - co2Ppm = ag.s8.getCo2(); - Serial.printf("CO2 index: %d\r\n", co2Ppm); + int value = ag.s8.getCo2(); + if (value >= 0) { + co2Ppm = value; + getCO2FailCount = 0; + Serial.printf("CO2 index: %d\r\n", co2Ppm); + } else { + getCO2FailCount++; + Serial.printf("Get CO2 failed: %d\r\n", getCO2FailCount); + if (getCO2FailCount >= 3) { + co2Ppm = -1; + } + } } void pmUpdate() { diff --git a/examples/ONE/ONE.ino b/examples/ONE/ONE.ino index 6eeb557..5a24a9c 100644 --- a/examples/ONE/ONE.ino +++ b/examples/ONE/ONE.ino @@ -712,6 +712,7 @@ bool hasSensorSHT = true; int pmFailCount = 0; uint32_t factoryBtnPressTime = 0; String mdnsModelName = ""; +int getCO2FailCount = 0; AgSchedule dispLedSchedule(DISP_UPDATE_INTERVAL, displayAndLedBarUpdate); AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, updateServerConfiguration); @@ -910,8 +911,18 @@ static void ledTest2Min(void) { } static void co2Update(void) { - co2Ppm = ag.s8.getCo2(); - Serial.printf("CO2 index: %d\r\n", co2Ppm); + int value = ag.s8.getCo2(); + if (value >= 0) { + co2Ppm = value; + getCO2FailCount = 0; + Serial.printf("CO2 index: %d\r\n", co2Ppm); + } else { + getCO2FailCount++; + Serial.printf("Get CO2 failed: %d\r\n", getCO2FailCount); + if (getCO2FailCount >= 3) { + co2Ppm = 1; + } + } } static void showNr(void) { Serial.println("Serial nr: " + getDevId()); } @@ -1126,7 +1137,7 @@ static String getServerSyncData(bool localServer) { if (noxIndex >= 0) { root["nox_index"] = noxIndex; } - if(noxRawIndex >= 0) { + if (noxRawIndex >= 0) { root["nox_raw"] = noxRawIndex; } } @@ -1752,7 +1763,7 @@ static void updateServerConfiguration(void) { Serial.printf("abcDays config: %d days(%d hours)\r\n", agServer.getCo2AbcDaysConfig(), newHour); int curHour = ag.s8.getAbcPeriod(); - Serial.printf("Current config: %d (hours)\r\n", ag.s8.getAbcPeriod()); + Serial.printf("Current config: %d (hours)\r\n", curHour); if (curHour == newHour) { Serial.println("set 'abcDays' ignored"); } else { diff --git a/examples/Open_Air/Open_Air.ino b/examples/Open_Air/Open_Air.ino index 715b23d..854c100 100644 --- a/examples/Open_Air/Open_Air.ino +++ b/examples/Open_Air/Open_Air.ino @@ -77,9 +77,9 @@ enum { APP_SM_SERVER_LOST, /** Connected to WiFi network but the server cannot be reached through the internet, e.g. blocked by firewall */ - APP_SM_SENSOR_CONFIG_FAILED, /** Server is reachabFirmware nodele but there is some - configuration issue to be fixed on the server - side */ + APP_SM_SENSOR_CONFIG_FAILED, /** Server is reachabFirmware nodele but there is + some configuration issue to be fixed on the + server side */ APP_SM_NORMAL, }; @@ -667,7 +667,7 @@ int tvocIndex = -1; int tvocRawIndex = -1; int noxIndex = -1; int noxRawIndex = -1; -int co2Ppm = 0; +int co2Ppm = -1; int pm25_1 = -1; int pm01_1 = -1; @@ -728,6 +728,7 @@ bool hasSensorPMS2 = true; bool hasSensorSGP = true; uint32_t factoryBtnPressTime = 0; String mdnsModelName = ""; +int getCO2FailCount = 0; AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, updateServerConfiguration); AgSchedule serverSchedule(SERVER_SYNC_INTERVAL, sendDataToServer); @@ -907,23 +908,42 @@ void boardInit(void) { failedHandler("Init I2C failed"); } - Serial.println("Firmware Version: "+ag.getVersion()); + Serial.println("Firmware Version: " + ag.getVersion()); ag.watchdog.begin(); ag.button.begin(); ag.statusLed.begin(); /** detect sensor: PMS5003, PMS5003T, SGP41 and S8 */ + /** + * Serial1 and Serial0 is use for connect S8 and PM sensor or both PM + */ + bool serial1Available = true; + bool serial0Available = true; + if (ag.s8.begin(Serial1) == false) { - hasSensorS8 = false; - - Serial.println("CO2 S8 sensor not found"); - Serial.println("Can not detect S8 run mode 'PPT'"); - fw_mode = FW_MODE_PPT; - - /** De-initialize Serial1 */ Serial1.end(); + delay(200); + Serial.println("Can not detect S8 on Serial1, try on Serial0"); + /** Check on other port */ + if (ag.s8.begin(Serial0) == false) { + hasSensorS8 = false; + + Serial.println("CO2 S8 sensor not found"); + Serial.println("Can not detect S8 run mode 'PPT'"); + fw_mode = FW_MODE_PPT; + + Serial0.end(); + delay(200); + } else { + Serial.println("Found S8 on Serial0"); + serial0Available = false; + } + } else { + Serial.println("Found S8 on Serial1"); + serial1Available = false; } + if (ag.sgp41.begin(Wire) == false) { hasSensorSGP = false; Serial.println("SGP sensor not found"); @@ -932,20 +952,43 @@ void boardInit(void) { fw_mode = FW_MODE_PP; } - if (ag.pms5003t_1.begin(Serial0) == false) { - hasSensorPMS1 = false; - Serial.println("PMS1 sensor not found"); - - if (ag.pms5003t_2.begin(Serial1) == false) { - hasSensorPMS2 = false; - Serial.println("PMS2 sensor not found"); + /** Try to find the PMS on other difference port with S8 */ + if (fw_mode == FW_MODE_PST) { + bool pmInitSuccess = false; + if (serial0Available) { + if (ag.pms5003t_1.begin(Serial0) == false) { + hasSensorPMS1 = false; + Serial.println("PMS1 sensor not found"); + } else { + serial0Available = false; + pmInitSuccess = true; + Serial.println("Found PMS 1 on Serial0"); + } + } + if (pmInitSuccess == false) { + if (serial1Available) { + if (ag.pms5003t_1.begin(Serial1) == false) { + hasSensorPMS1 = false; + Serial.println("PMS1 sensor not found"); + } else { + serial1Available = false; + Serial.println("Found PMS 1 on Serial1"); + } + } + } + hasSensorPMS2 = false; // Disable PM2 + } else { + if (ag.pms5003t_1.begin(Serial0) == false) { + hasSensorPMS1 = false; + Serial.println("PMS1 sensor not found"); + } else { + Serial.println("Found PMS 1 on Serial0"); } - } - - if (fw_mode != FW_MODE_PST) { if (ag.pms5003t_2.begin(Serial1) == false) { hasSensorPMS2 = false; Serial.println("PMS2 sensor not found"); + } else { + Serial.println("Found PMS 2 on Serial1"); } } @@ -1142,8 +1185,18 @@ static void pmUpdate(void) { } static void co2Update(void) { - co2Ppm = ag.s8.getCo2(); - Serial.printf("CO2 index: %d\r\n", co2Ppm); + int value = ag.s8.getCo2(); + if (value >= 0) { + co2Ppm = value; + getCO2FailCount = 0; + Serial.printf("CO2 index: %d\r\n", co2Ppm); + } else { + getCO2FailCount++; + Serial.printf("Get CO2 failed: %d\r\n", getCO2FailCount); + if (getCO2FailCount >= 3) { + co2Ppm = -1; + } + } } static void updateServerConfiguration(void) { @@ -1164,7 +1217,7 @@ static void updateServerConfiguration(void) { Serial.printf("abcDays config: %d days(%d hours)\r\n", agServer.getCo2AbcDaysConfig(), newHour); int curHour = ag.s8.getAbcPeriod(); - Serial.printf("Current config: %d (hours)\r\n", ag.s8.getAbcPeriod()); + Serial.printf("Current config: %d (hours)\r\n", curHour); if (curHour == newHour) { Serial.println("set 'abcDays' ignored"); } else { @@ -1415,7 +1468,7 @@ static String getServerSyncData(bool localServer) { if (noxIndex >= 0) { root["nox_index"] = noxIndex; } - if(noxRawIndex >= 0) { + if (noxRawIndex >= 0) { root["nox_raw"] = noxRawIndex; } } diff --git a/examples/TestCO2/TestCO2.ino b/examples/TestCO2/TestCO2.ino index 9768e29..92cb4e6 100644 --- a/examples/TestCO2/TestCO2.ino +++ b/examples/TestCO2/TestCO2.ino @@ -25,7 +25,7 @@ void setup() if (ag.s8.begin(&Serial) == false) { #else - if (ag.s8.begin(Serial1) == false) + if (ag.s8.begin(Serial0) == false) { #endif failedHandler("SenseAir S8 init failed"); diff --git a/src/PMS/PMS5003T.cpp b/src/PMS/PMS5003T.cpp index 03b0fd2..6d44528 100644 --- a/src/PMS/PMS5003T.cpp +++ b/src/PMS/PMS5003T.cpp @@ -79,19 +79,21 @@ bool PMS5003T::begin(void) { #if ARDUINO_USB_CDC_ON_BOOT // Serial used for USB CDC if (this->_serial == &Serial0) { + AgLog("Init Serial0"); + _serial->begin(9600, SERIAL_8N1); #else if (this->_serial == &Serial) { -#endif AgLog("Init Serial"); this->_serial->begin(9600, SERIAL_8N1, bsp->Pms5003.uart_rx_pin, bsp->Pms5003.uart_tx_pin); +#endif } else { + /** Share with sensor air s8*/ if (bsp->SenseAirS8.supported == false) { AgLog("Board [%d] PMS5003T_2 not supported", this->_boardDef); return false; } - /** Share with sensor air s8*/ AgLog("Init Serialx"); this->_serial->begin(9600, SERIAL_8N1, bsp->SenseAirS8.uart_rx_pin, bsp->SenseAirS8.uart_tx_pin); diff --git a/src/S8/S8.cpp b/src/S8/S8.cpp index dc22155..c09f0a9 100644 --- a/src/S8/S8.cpp +++ b/src/S8/S8.cpp @@ -228,7 +228,7 @@ int16_t S8::getCo2(void) { return -1; } - int16_t co2 = 0; + int16_t co2 = -1; // Ask CO2 value sendCommand(MODBUS_FUNC_READ_INPUT_REGISTERS, MODBUS_IR4, 0x0001); @@ -649,11 +649,25 @@ bool S8::init(int txPin, int rxPin, uint32_t baud) { uart->begin(baud); this->_uartStream = uart; #else +#if ARDUINO_USB_CDC_ON_BOOT + /** The 'Serial0' can ont configure tx, rx pin, only use as default */ + if (_serial == &Serial0) { + AgLog("Init on 'Serial0'"); + _serial->begin(baud, SERIAL_8N1); + } else { + AgLog("Init on 'Serialx'"); + this->_serial->begin(baud, SERIAL_8N1, rxPin, txPin); + } + this->_uartStream = this->_serial; +#else + AgLog("Init on 'Serialx'"); this->_serial->begin(baud, SERIAL_8N1, rxPin, txPin); this->_uartStream = this->_serial; +#endif #endif /** Check communication by get firmware version */ + delay(100); char fwVers[11]; this->_isBegin = true; this->getFirmwareVersion(fwVers); @@ -712,7 +726,7 @@ uint8_t S8::uartReadBytes(uint8_t max_bytes, uint32_t timeout_ms) { #if defined(ESP32) // Relax 5ms to avoid watchdog reset - vTaskDelay(pdMS_TO_TICKS(5)); + vTaskDelay(pdMS_TO_TICKS(1)); #endif } return nb; diff --git a/src/Sgp41/Sgp41.cpp b/src/Sgp41/Sgp41.cpp index ae3e805..f477c94 100644 --- a/src/Sgp41/Sgp41.cpp +++ b/src/Sgp41/Sgp41.cpp @@ -91,7 +91,7 @@ void Sgp41::handle(void) { if (getRawSignal(srawVoc, srawNox)) { nox = noxAlgorithm()->process(srawNox); tvoc = vocAlgorithm()->process(srawVoc); - AgLog("Polling SGP41 success: tvoc: %d, nox: %d", tvoc, nox); + // AgLog("Polling SGP41 success: tvoc: %d, nox: %d", tvoc, nox); } } } @@ -124,7 +124,7 @@ void Sgp41::_handle(void) { noxRaw = srawNox; nox = noxAlgorithm()->process(srawNox); tvoc = vocAlgorithm()->process(srawVoc); - AgLog("Polling SGP41 success: tvoc: %d, nox: %d", tvoc, nox); + // AgLog("Polling SGP41 success: tvoc: %d, nox: %d", tvoc, nox); } } }