From bdff41a4b0db1012c39d2999bfa207814a69b0b6 Mon Sep 17 00:00:00 2001 From: biver Date: Thu, 11 May 2023 15:51:41 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D0=BC=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20S8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/sensors/S8/S8.cpp | 95 +++++++++++++++++++++++++++++ src/modules/sensors/S8/modinfo.json | 43 +++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 src/modules/sensors/S8/S8.cpp create mode 100644 src/modules/sensors/S8/modinfo.json diff --git a/src/modules/sensors/S8/S8.cpp b/src/modules/sensors/S8/S8.cpp new file mode 100644 index 00000000..f8efa10e --- /dev/null +++ b/src/modules/sensors/S8/S8.cpp @@ -0,0 +1,95 @@ +#include "Global.h" +#include "Classes/IoTItem.h" + +#include + +#define R_LEN 7 +#define C_LEN 8 + +byte cmd_s8[8] = {0xFE, 0x04, 0x00, 0x03, 0x00, 0x01, 0xD5, 0xC5}; +//byte abc_s8[8] = {0xFE, 0x03, 0x00, 0x1F, 0x00, 0x01, 0xA1, 0xC3}; + + +class S8co : public IoTItem { + private: + SoftwareSerial* s8Serial; + + unsigned int _s8_co2; + int _s8_co2_mean = 0; + int _s8_co2_mean2 = 0; + + float smoothing_factor = 0.5; + float smoothing_factor2 = 0.15; + + byte _response_s8[7] = {0, 0, 0, 0, 0, 0, 0}; + + void s8Request(byte cmd[]) { + while(!s8Serial->available()) { + s8Serial->write(cmd, C_LEN); + delay(50); + } + + int timeout=0; + while(s8Serial->available() < R_LEN) { + timeout++; + if(timeout > 10) { + while(s8Serial->available()) { + s8Serial->read(); + break; + } + } + delay(50); + } + + for (int i=0; i < R_LEN; i++) { + _response_s8[i] = s8Serial->read(); + } + + s8Serial->end(); + } + + void co2_measure() { + s8Request(cmd_s8); // запрашиваем значение загрязнения + _s8_co2 = _response_s8[3] * 256 + _response_s8[4]; // полученный недобайт-ответ преобразуем в значение загрязнения + + if (!_s8_co2_mean) _s8_co2_mean = _s8_co2; + _s8_co2_mean = _s8_co2_mean - smoothing_factor*(_s8_co2_mean - _s8_co2); + + if (!_s8_co2_mean2) _s8_co2_mean2 = _s8_co2; + _s8_co2_mean2 = _s8_co2_mean2 - smoothing_factor2*(_s8_co2_mean2 - _s8_co2); + + Serial.printf("CO2 value: %d, M1Value: %d, M2Value: %d\n", _s8_co2, _s8_co2_mean, _s8_co2_mean2); + } + + public: + S8co(String parameters): IoTItem(parameters) { + int S8_RX_PIN, S8_TX_PIN; + jsonRead(parameters, "rxPin", S8_RX_PIN); + jsonRead(parameters, "txPin", S8_TX_PIN); + + s8Serial = new SoftwareSerial(S8_RX_PIN, S8_TX_PIN); + if (!s8Serial) return; + s8Serial->begin(9600); + } + + void doByInterval() { + co2_measure(); + value.valD = _s8_co2; + if (value.valD < 15000) + regEvent(value.valD, "S8co"); + else + SerialPrint("E", "Sensor S8_uart", "Error"); + } + + ~S8co() { + if (s8Serial) delete s8Serial; + }; +}; + +void* getAPI_S8(String subtype, String param) { + if (subtype == F("S8co")) { + return new S8co(param); + } else { + return nullptr; + } +} diff --git a/src/modules/sensors/S8/modinfo.json b/src/modules/sensors/S8/modinfo.json new file mode 100644 index 00000000..6f702b87 --- /dev/null +++ b/src/modules/sensors/S8/modinfo.json @@ -0,0 +1,43 @@ +{ + "menuSection": "Сенсоры", + "configItem": [ + { + "name": "(S8) Cенсор качества воздуха", + "num": 3, + "type": "Reading", + "subtype": "S8co", + "id": "s8co", + "widget": "anydataPpm", + "page": "Сенсоры", + "descr": "S8_CO2", + "int": 15, + "round": 1, + "rxPin": 13, + "txPin": 15 + } + ], + "about": { + "authorName": "Serghei Crasnicov", + "authorContact": "https://t.me/Serghei63", + "authorGit": "https://github.com/Serghei63", + "specialThanks": "", + "moduleName": "S8", + "moduleVersion": "2.1", + "usedRam": 15, + "subTypes": [ + "S8co" + ], + "title": "Датчик углекислого газа S8", + "moduleDesc": "Измеряет параметвы воздуха.", + "propInfo": { + "int": "Количество секунд между опросами датчика." + } + }, + "defActive": true, + "usedLibs": { + "esp32_4mb": [ + ], + "esp8266_4mb": [ + ] + } +} \ No newline at end of file