From cc23deac00908118000a7851df88c3edc695bf06 Mon Sep 17 00:00:00 2001 From: Nico Date: Mon, 10 Sep 2018 11:37:16 +0200 Subject: [PATCH 1/7] use SDS0X1 working period command --- sonoff/xsns_20_novasds.ino | 119 ++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 62 deletions(-) diff --git a/sonoff/xsns_20_novasds.ino b/sonoff/xsns_20_novasds.ino index 851326f3f..607bcc545 100644 --- a/sonoff/xsns_20_novasds.ino +++ b/sonoff/xsns_20_novasds.ino @@ -27,26 +27,65 @@ #include +#ifndef WORKING_PERIOD + #define WORKING_PERIOD 5 +#endif + TasmotaSerial *NovaSdsSerial; uint8_t novasds_type = 1; uint8_t novasds_valid = 0; -uint8_t novasds_running = 1; -uint8_t novasds_read_tick = 30; -uint8_t novasds_wakup_tick = 179; -uint8_t novasds_ticker = 0; + +uint8_t novasds_workperiod[19] = {0xAA, 0xB4, 0x08, 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x0C, 0xAB}; //5 minutes +uint8_t novasds_setquerymode[19] = {0xAA, 0xB4, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x02, 0xAB}; //query mode +uint8_t novasds_querydata[19] = {0xAA, 0xB4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x02, 0xAB}; //query DATA + struct sds011data { uint16_t pm100; uint16_t pm25; } novasds_data; +//Put sensor to sleep +void NovaSdsSetWorkPeriod() +{ -bool NovaSdsReadData(bool publish) + while (NovaSdsSerial->available() > 0) { + NovaSdsSerial->read(); + } + + novasds_workperiod[4] = WORKING_PERIOD; + novasds_workperiod[17] = ((novasds_workperiod[2] + novasds_workperiod[3] + novasds_workperiod[4] + novasds_workperiod[15] + novasds_workperiod[16]) & 0xFF); //checksum + + + NovaSdsSerial->write(novasds_workperiod, sizeof(novasds_workperiod)); + + NovaSdsSerial->flush(); + + while (NovaSdsSerial->available() > 0) { + NovaSdsSerial->read(); + } + + NovaSdsSerial->write(novasds_setquerymode, sizeof(novasds_setquerymode)); + + NovaSdsSerial->flush(); + + while (NovaSdsSerial->available() > 0) { + NovaSdsSerial->read(); + } +} + + + +bool NovaSdsReadData() { if (! NovaSdsSerial->available()) return false; - while ((NovaSdsSerial->peek() != 0xAA) && NovaSdsSerial->available()) { + NovaSdsSerial->write(novasds_querydata, sizeof(novasds_querydata)); + + NovaSdsSerial->flush(); + + while ((NovaSdsSerial->peek() != 0xAA) && NovaSdsSerial->available()) { NovaSdsSerial->read(); } @@ -56,9 +95,7 @@ bool NovaSdsReadData(bool publish) NovaSdsSerial->flush(); AddLogSerial(LOG_LEVEL_DEBUG_MORE, d, 8); - if (!publish){ - return false; - } + if (d[7] == ((d[1] + d[2] + d[3] + d[4] + d[5] + d[6]) & 0xFF)) { novasds_data.pm25 = (d[1] + 256 * d[2]); novasds_data.pm100 = (d[3] + 256 * d[4]); @@ -66,6 +103,9 @@ bool NovaSdsReadData(bool publish) AddLog_P(LOG_LEVEL_DEBUG, PSTR("SDS: " D_CHECKSUM_FAILURE)); return false; } + + novasds_valid = 10; + return true; } @@ -73,35 +113,12 @@ bool NovaSdsReadData(bool publish) void NovaSdsSecond() // Every second { - if (novasds_ticker < novasds_read_tick) { - // wake up the sensor and wait read ticks to stabalize the sensor - if (!novasds_running) { - NovaSdsStart(); - novasds_running = 1; - } - - // drain the serial without publishing data - NovaSdsReadData(false); - novasds_ticker++; - - } else if (novasds_ticker == novasds_read_tick) { - - // try to take a single stable reading and sleep the sensor - if (NovaSdsReadData(true)) { - novasds_valid = 1; - NovaSdsStop(); - novasds_running = 0; - novasds_ticker++; - } else { - novasds_valid = 0; - } - - } else if (novasds_ticker >= novasds_wakup_tick) { - // reset the counter - novasds_ticker = 0; + if (NovaSdsReadData()) { + novasds_valid = 10; } else { - // sensor is sleeping keep waiting - novasds_ticker++; + if (novasds_valid) { + novasds_valid--; + } } } @@ -110,37 +127,15 @@ void NovaSdsSecond() // Every second void NovaSdsInit() { novasds_type = 0; - if (pin[GPIO_SDS0X1_RX] < 99 && pin[GPIO_SDS0X1_TX] < 99) { + if ((pin[GPIO_SDS0X1_RX] < 99) && (pin[GPIO_SDS0X1_TX] < 99)) { NovaSdsSerial = new TasmotaSerial(pin[GPIO_SDS0X1_RX], pin[GPIO_SDS0X1_TX], 1); - if (NovaSdsSerial->begin(9600)) { - if (NovaSdsSerial->hardwareSerial()) { - ClaimSerial(); - } + if (NovaSdsSerial->hardwareSerial()) { ClaimSerial(); } novasds_type = 1; } - } -} -void NovaSdsStart() -{ - AddLog_P(LOG_LEVEL_DEBUG, "SDS: start"); - const uint8_t novasds_start_cmd[] = {0xAA, 0xB4, 0x06, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x06, 0xAB}; - NovaSdsSerial->write(novasds_start_cmd, sizeof(novasds_start_cmd)); - NovaSdsSerial->flush(); -} - -void NovaSdsStop() -{ - AddLog_P(LOG_LEVEL_DEBUG, "SDS: stop"); - const uint8_t novasds_stop_cmd[] = {0xAA, 0xB4, 0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x05, 0xAB}; - NovaSdsSerial->write(novasds_stop_cmd, sizeof(novasds_stop_cmd)); - NovaSdsSerial->flush(); - // drain any old data - while (NovaSdsSerial->available()) { - NovaSdsSerial->read(); - } + NovaSdsSetWorkPeriod(); } #ifdef USE_WEBSERVER From 0f25925357554efa51fe40346196fe8e46c2b2e8 Mon Sep 17 00:00:00 2001 From: edelstahlratte <17500289+edelstahlratte@users.noreply.github.com> Date: Mon, 10 Sep 2018 11:50:54 +0200 Subject: [PATCH 2/7] Add SDS0X1 working period --- sonoff/user_config.h | 1 + 1 file changed, 1 insertion(+) diff --git a/sonoff/user_config.h b/sonoff/user_config.h index d46a295d7..357fe5fab 100644 --- a/sonoff/user_config.h +++ b/sonoff/user_config.h @@ -335,6 +335,7 @@ #define CO2_HIGH 1200 // Above this CO2 value show red light (needs PWM or WS2812 RG(B) led and enable with SetOption18 1) #define USE_PMS5003 // Add support for PMS5003 and PMS7003 particle concentration sensor (+1k3 code) #define USE_NOVA_SDS // Add support for SDS011 and SDS021 particle concentration sensor (+0k7 code) + #define WORKING_PERIOD 5 // Working period of the SDS Sensor, Takes a reading every X Minutes #define USE_SERIAL_BRIDGE // Add support for software Serial Bridge (+0k8 code) //#define USE_SDM120 // Add support for Eastron SDM120-Modbus energy meter (+1k7 code) #define SDM120_SPEED 9600 // SDM120-Modbus RS485 serial speed (default: 2400 baud) From ef6d5bb340c6d26509414c450d4afce71b4a45b3 Mon Sep 17 00:00:00 2001 From: Nico Date: Mon, 10 Sep 2018 12:11:28 +0200 Subject: [PATCH 3/7] cosmetics --- sonoff/xsns_20_novasds.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sonoff/xsns_20_novasds.ino b/sonoff/xsns_20_novasds.ino index 607bcc545..02614c912 100644 --- a/sonoff/xsns_20_novasds.ino +++ b/sonoff/xsns_20_novasds.ino @@ -46,7 +46,6 @@ struct sds011data { uint16_t pm25; } novasds_data; -//Put sensor to sleep void NovaSdsSetWorkPeriod() { @@ -57,7 +56,6 @@ void NovaSdsSetWorkPeriod() novasds_workperiod[4] = WORKING_PERIOD; novasds_workperiod[17] = ((novasds_workperiod[2] + novasds_workperiod[3] + novasds_workperiod[4] + novasds_workperiod[15] + novasds_workperiod[16]) & 0xFF); //checksum - NovaSdsSerial->write(novasds_workperiod, sizeof(novasds_workperiod)); NovaSdsSerial->flush(); @@ -127,10 +125,12 @@ void NovaSdsSecond() // Every second void NovaSdsInit() { novasds_type = 0; - if ((pin[GPIO_SDS0X1_RX] < 99) && (pin[GPIO_SDS0X1_TX] < 99)) { + if (pin[GPIO_SDS0X1_RX] < 99 && pin[GPIO_SDS0X1_TX] < 99) { NovaSdsSerial = new TasmotaSerial(pin[GPIO_SDS0X1_RX], pin[GPIO_SDS0X1_TX], 1); if (NovaSdsSerial->begin(9600)) { - if (NovaSdsSerial->hardwareSerial()) { ClaimSerial(); } + if (NovaSdsSerial->hardwareSerial()) { + ClaimSerial(); + } novasds_type = 1; } } From 804e74d0de0322eac9a466db6a57f3578c99b351 Mon Sep 17 00:00:00 2001 From: Nico Date: Mon, 10 Sep 2018 12:22:00 +0200 Subject: [PATCH 4/7] cosmetics --- sonoff/xsns_20_novasds.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonoff/xsns_20_novasds.ino b/sonoff/xsns_20_novasds.ino index 02614c912..2bce13e2a 100644 --- a/sonoff/xsns_20_novasds.ino +++ b/sonoff/xsns_20_novasds.ino @@ -53,7 +53,7 @@ void NovaSdsSetWorkPeriod() NovaSdsSerial->read(); } - novasds_workperiod[4] = WORKING_PERIOD; + novasds_workperiod[4] = WORKING_PERIOD; novasds_workperiod[17] = ((novasds_workperiod[2] + novasds_workperiod[3] + novasds_workperiod[4] + novasds_workperiod[15] + novasds_workperiod[16]) & 0xFF); //checksum NovaSdsSerial->write(novasds_workperiod, sizeof(novasds_workperiod)); From dc3d699fd1436c245806bbe804e117a9c2ad7dba Mon Sep 17 00:00:00 2001 From: Nico Date: Mon, 10 Sep 2018 12:23:51 +0200 Subject: [PATCH 5/7] cosmetics --- sonoff/xsns_20_novasds.ino | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sonoff/xsns_20_novasds.ino b/sonoff/xsns_20_novasds.ino index 2bce13e2a..b6bd4a2c0 100644 --- a/sonoff/xsns_20_novasds.ino +++ b/sonoff/xsns_20_novasds.ino @@ -60,17 +60,17 @@ void NovaSdsSetWorkPeriod() NovaSdsSerial->flush(); - while (NovaSdsSerial->available() > 0) { - NovaSdsSerial->read(); - } + while (NovaSdsSerial->available() > 0) { + NovaSdsSerial->read(); + } NovaSdsSerial->write(novasds_setquerymode, sizeof(novasds_setquerymode)); NovaSdsSerial->flush(); - while (NovaSdsSerial->available() > 0) { - NovaSdsSerial->read(); - } + while (NovaSdsSerial->available() > 0) { + NovaSdsSerial->read(); + } } From 047932a191ccc38c43d4ffb262b5695aaae903ee Mon Sep 17 00:00:00 2001 From: Nico Date: Mon, 10 Sep 2018 12:25:57 +0200 Subject: [PATCH 6/7] cosmetics --- sonoff/xsns_20_novasds.ino | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/sonoff/xsns_20_novasds.ino b/sonoff/xsns_20_novasds.ino index b6bd4a2c0..46db2fb6a 100644 --- a/sonoff/xsns_20_novasds.ino +++ b/sonoff/xsns_20_novasds.ino @@ -57,15 +57,13 @@ void NovaSdsSetWorkPeriod() novasds_workperiod[17] = ((novasds_workperiod[2] + novasds_workperiod[3] + novasds_workperiod[4] + novasds_workperiod[15] + novasds_workperiod[16]) & 0xFF); //checksum NovaSdsSerial->write(novasds_workperiod, sizeof(novasds_workperiod)); - NovaSdsSerial->flush(); while (NovaSdsSerial->available() > 0) { NovaSdsSerial->read(); } - NovaSdsSerial->write(novasds_setquerymode, sizeof(novasds_setquerymode)); - + NovaSdsSerial->write(novasds_setquerymode, sizeof(novasds_setquerymode)); NovaSdsSerial->flush(); while (NovaSdsSerial->available() > 0) { @@ -80,10 +78,9 @@ bool NovaSdsReadData() if (! NovaSdsSerial->available()) return false; NovaSdsSerial->write(novasds_querydata, sizeof(novasds_querydata)); - NovaSdsSerial->flush(); - while ((NovaSdsSerial->peek() != 0xAA) && NovaSdsSerial->available()) { + while ((NovaSdsSerial->peek() != 0xAA) && NovaSdsSerial->available()) { NovaSdsSerial->read(); } From b9d1bb965ced0887af43467f747cef38da692209 Mon Sep 17 00:00:00 2001 From: Nico Date: Mon, 10 Sep 2018 14:06:31 +0200 Subject: [PATCH 7/7] correct error --- sonoff/xsns_20_novasds.ino | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sonoff/xsns_20_novasds.ino b/sonoff/xsns_20_novasds.ino index 46db2fb6a..815041cab 100644 --- a/sonoff/xsns_20_novasds.ino +++ b/sonoff/xsns_20_novasds.ino @@ -129,10 +129,9 @@ void NovaSdsInit() ClaimSerial(); } novasds_type = 1; + NovaSdsSetWorkPeriod(); } } - - NovaSdsSetWorkPeriod(); } #ifdef USE_WEBSERVER