From c091f1b4cc062d64b7192d2806ae3bf3adaad39b Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Mon, 26 Sep 2022 21:56:54 +0200 Subject: [PATCH] uart --- .vscode/settings.json | 13 +++ data_svelte/items.json | 8 +- data_svelte/widgets.json | 25 +++++- myProfile.json | 6 +- platformio.ini | 2 +- src/modules/API.cpp | 4 +- src/modules/sensors/Pzem004t/Pzem004t.cpp | 103 ++++------------------ src/modules/sensors/Pzem004t/modinfo.json | 60 +++++++++++-- src/modules/sensors/SoftUart/SoftUart.cpp | 66 ++++++++++++++ src/modules/sensors/SoftUart/modinfo.json | 42 +++++++++ 10 files changed, 226 insertions(+), 103 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 src/modules/sensors/SoftUart/SoftUart.cpp create mode 100644 src/modules/sensors/SoftUart/modinfo.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..89f6be4b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,13 @@ +{ + "files.associations": { + "array": "cpp", + "deque": "cpp", + "list": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "string_view": "cpp", + "initializer_list": "cpp", + "ranges": "cpp" + } +} \ No newline at end of file diff --git a/data_svelte/items.json b/data_svelte/items.json index d7d86242..42bbc731 100644 --- a/data_svelte/items.json +++ b/data_svelte/items.json @@ -454,18 +454,16 @@ "num": 34 }, { - "name": "35. PWM ESP32", + "name": "35. PWM ESP8266", "type": "Writing", - "subtype": "Pwm32", + "subtype": "Pwm8266", "id": "pwm", "widget": "range", "page": "Кнопки", "descr": "PWM", "int": 0, - "pin": 2, + "pin": 15, "freq": 5000, - "ledChannel": 2, - "PWM_resolution": 10, "val": 0, "apin": -1, "num": 35 diff --git a/data_svelte/widgets.json b/data_svelte/widgets.json index 43238fb5..5b852c3c 100644 --- a/data_svelte/widgets.json +++ b/data_svelte/widgets.json @@ -22,18 +22,39 @@ "after": "", "icon": "" }, + { + "name": "anydataVlt", + "label": "Вольты", + "widget": "anydata", + "after": "V", + "icon": "speedometer" + }, { "name": "anydataAmp", "label": "Амперы", "widget": "anydata", - "after": "Amp", + "after": "A", + "icon": "speedometer" + }, + { + "name": "anydataWt", + "label": "Ватты", + "widget": "anydata", + "after": "Wt", + "icon": "speedometer" + }, + { + "name": "anydataWth", + "label": "Энергия", + "widget": "anydata", + "after": "Wt/hr", "icon": "speedometer" }, { "name": "anydataHtz", "label": "Герцы", "widget": "anydata", - "after": "Htz", + "after": "Hz", "icon": "speedometer" }, { diff --git a/myProfile.json b/myProfile.json index 77db2a2b..f8fb97a1 100644 --- a/myProfile.json +++ b/myProfile.json @@ -21,7 +21,7 @@ }, "projectProp": { "platformio": { - "default_envs": "esp32_4mb", + "default_envs": "esp8266_4mb", "data_dir": "data_svelte" } }, @@ -117,6 +117,10 @@ "path": "src\\modules\\sensors\\Sht30", "active": true }, + { + "path": "src\\modules\\sensors\\SoftUart", + "active": false + }, { "path": "src\\modules\\sensors\\Sonar", "active": true diff --git a/platformio.ini b/platformio.ini index 72b9330b..ef5b920a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -41,7 +41,7 @@ build_src_filter = ${env:esp32_4mb_fromitems.build_src_filter} [platformio] -default_envs = esp32_4mb +default_envs = esp8266_4mb data_dir = data_svelte [common_env_data] diff --git a/src/modules/API.cpp b/src/modules/API.cpp index b2f9af1f..9ee02105 100644 --- a/src/modules/API.cpp +++ b/src/modules/API.cpp @@ -22,7 +22,7 @@ void* getAPI_ButtonOut(String subtype, String params); void* getAPI_IoTServo(String subtype, String params); void* getAPI_Mcp23017(String subtype, String params); void* getAPI_Mp3(String subtype, String params); -void* getAPI_Pwm32(String subtype, String params); +void* getAPI_Pwm8266(String subtype, String params); void* getAPI_TelegramLT(String subtype, String params); void* getAPI_Lcd2004(String subtype, String params); @@ -50,7 +50,7 @@ if ((tmpAPI = getAPI_ButtonOut(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_IoTServo(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Mcp23017(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Mp3(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Pwm32(subtype, params)) != nullptr) return tmpAPI; +if ((tmpAPI = getAPI_Pwm8266(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_TelegramLT(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Lcd2004(subtype, params)) != nullptr) return tmpAPI; return nullptr; diff --git a/src/modules/sensors/Pzem004t/Pzem004t.cpp b/src/modules/sensors/Pzem004t/Pzem004t.cpp index d61402d5..9eb06d50 100644 --- a/src/modules/sensors/Pzem004t/Pzem004t.cpp +++ b/src/modules/sensors/Pzem004t/Pzem004t.cpp @@ -3,91 +3,22 @@ #include "classes/IoTItem.h" #include "PZEMSensor.h" -#include "SoftUART.h" +//#include "SoftUART.h" -#include +PZEMSensor* pzem; -// std::map bmes; -// -// class Pzem004t : public IoTItem { -// private: -// Adafruit_Pzem004* _bme; -// -// public: -// Pzem004t(Adafruit_Pzem004* bme, String parameters) : IoTItem(parameters) { -// _bme = bme; -// } -// -// void doByInterval() { -// value.valD = _bme->readTemperature(); -// if (value.valD < 145) -// regEvent(value.valD, "Pzem004t"); -// else -// SerialPrint("E", "Sensor Pzem004t", "Error"); -// } -// -// ~Pzem004t(){}; -// }; -// -// class Pzem004h : public IoTItem { -// private: -// Adafruit_Pzem004* _bme; -// -// public: -// Pzem004h(Adafruit_Pzem004* bme, String parameters) : IoTItem(parameters) { -// _bme = bme; -// } -// -// void doByInterval() { -// value.valD = _bme->readHumidity(); -// if (value.valD < 100) -// regEvent(value.valD, "Pzem004h"); -// else -// SerialPrint("E", "Sensor Pzem004h", "Error"); -// } -// -// ~Pzem004h(){}; -// }; -// -// class Pzem004p : public IoTItem { -// private: -// Adafruit_Pzem004* _bme; -// -// public: -// Pzem004p(Adafruit_Pzem004* bme, String parameters) : IoTItem(parameters) { -// _bme = bme; -// } -// -// void doByInterval() { -// value.valD = _bme->readPressure(); -// if (value.valD > 0) { -// value.valD = value.valD / 1.333224 / 100; -// regEvent(value.valD, "Pzem004p"); -// } else -// SerialPrint("E", "Sensor Pzem004p", "Error"); -// } -// -// ~Pzem004p(){}; -// }; -// -// void* getAPI_Pzem004(String subtype, String param) { -// if (subtype == F("Pzem004t") || subtype == F("Pzem004h") || subtype == F("Pzem004p")) { -// String addr; -// jsonRead(param, "addr", addr); -// -// if (bmes.find(addr) == bmes.end()) { -// bmes[addr] = new Adafruit_Pzem004(); -// bmes[addr]->begin(hexStringToUint8(addr)); -// } -// -// if (subtype == F("Pzem004t")) { -// return new Pzem004t(bmes[addr], param); -// } else if (subtype == F("Pzem004h")) { -// return new Pzem004h(bmes[addr], param); -// } else if (subtype == F("Pzem004p")) { -// return new Pzem004p(bmes[addr], param); -// } -// } -// -// return nullptr; -// } +class Pzem004v : public IoTItem { + private: + String addr; + + public: + Pzem004v(String parameters) : IoTItem(parameters) { + addr = jsonReadStr(parameters, "addr"); + // pzem = new PZEMSensor(myUART, hexStringToUint8(addr)); + } + + ~Pzem004v(){}; +}; + +void* getAPI_Pzem004(String subtype, String param) { +} diff --git a/src/modules/sensors/Pzem004t/modinfo.json b/src/modules/sensors/Pzem004t/modinfo.json index 35d05a8c..1baf3a8c 100644 --- a/src/modules/sensors/Pzem004t/modinfo.json +++ b/src/modules/sensors/Pzem004t/modinfo.json @@ -4,11 +4,59 @@ { "name": "PZEM 004t Напряжение", "type": "Reading", - "subtype": "Bme280t", - "id": "tmp3", - "widget": "anydataTmp", + "subtype": "Pzem004v", + "id": "V", + "widget": "anydataVlt", "page": "Сенсоры", - "descr": "Температура", + "descr": "Напряжение", + "int": 15, + "addr": "0x77", + "round": 1 + }, + { + "name": "PZEM 004t Сила тока", + "type": "Reading", + "subtype": "Pzem004a", + "id": "A", + "widget": "anydataAmp", + "page": "Сенсоры", + "descr": "Сила тока", + "int": 15, + "addr": "0x77", + "round": 1 + }, + { + "name": "PZEM 004t Мощность", + "type": "Reading", + "subtype": "Pzem004w", + "id": "A", + "widget": "anydataWt", + "page": "Сенсоры", + "descr": "Мощность", + "int": 15, + "addr": "0x77", + "round": 1 + }, + { + "name": "PZEM 004t Энергия", + "type": "Reading", + "subtype": "Pzem004wh", + "id": "Wh", + "widget": "anydataWth", + "page": "Сенсоры", + "descr": "Энергия", + "int": 15, + "addr": "0x77", + "round": 1 + }, + { + "name": "PZEM 004t Частота", + "type": "Reading", + "subtype": "Pzem004hz", + "id": "Hz", + "widget": "anydataHtz", + "page": "Сенсоры", + "descr": "Энергия", "int": 15, "addr": "0x77", "round": 1 @@ -26,8 +74,8 @@ "Pzem004v", "Pzem004a", "Pzem004w", - "Pzem004hz", - "Pzem004wh" + "Pzem004wh", + "Pzem004hz" ], "title": "Счетчик электроэнергии PZEM 004t версии 3.0 (с модбасом)", "moduleDesc": "Считает потраченную электроэнергию, измеряет напряжение, частоту, силу тока и прочие параметры", diff --git a/src/modules/sensors/SoftUart/SoftUart.cpp b/src/modules/sensors/SoftUart/SoftUart.cpp new file mode 100644 index 00000000..eb9b50df --- /dev/null +++ b/src/modules/sensors/SoftUart/SoftUart.cpp @@ -0,0 +1,66 @@ + +#include "Global.h" +#include "classes/IoTItem.h" +#include +//#include "SoftUART.h" + +#ifdef ESP8266 +SoftwareSerial* myUART = nullptr; +#else +HardwareSerial* myUART = nullptr; +#endif + +class SoftUART : public IoTItem { + private: + int tx; + int rx; + + public: + SoftUART(String parameters) : IoTItem(parameters) { + tx = jsonReadInt(parameters, "tx"); + rx = jsonReadInt(parameters, "rx"); + speed = jsonReadInt(parameters, "speed"); + + if (!myUART) { +#ifdef ESP8266 + myUART = new SoftwareSerial(tx, rx); + myUART->begin(speed); +#endif +#ifdef ESP32 + myUART = new HardwareSerial(2); + myUART->begin(speed, SERIAL_8N1, rx, tx); +#endif + } + SerialPrint("i", F("UART"), F("UART Init")); + } + + void uartHandle() { + if (myUART) { + if (!jsonReadBool(configSetupJson, "uart")) { + return; + } + static String incStr; + if (myUART->available()) { + char inc; + inc = myUART->read(); + incStr += inc; + if (inc == '\n') { + parse(incStr); + incStr = ""; + } + } + } + } + + void parse(String& incStr) { + SerialPrint("I", "=>UART", incStr); + } +}; + +void* getAPI_SoftUART(String subtype, String param) { + if (subtype == F("SoftUART")) { + return new SoftUART(param); + } else { + return nullptr; + } +} diff --git a/src/modules/sensors/SoftUart/modinfo.json b/src/modules/sensors/SoftUart/modinfo.json new file mode 100644 index 00000000..36ab564d --- /dev/null +++ b/src/modules/sensors/SoftUart/modinfo.json @@ -0,0 +1,42 @@ +{ + "menuSection": "Сенсоры", + "configItem": [ + { + "name": "UART", + "type": "Reading", + "subtype": "SoftUART", + "id": "u", + "tx": 15, + "rx": 16, + "speed": 9600 + } + ], + "about": { + "authorName": "Dmitry Borisenko", + "authorContact": "https://t.me/Dmitry_Borisenko", + "authorGit": "https://github.com/DmitryBorisenko33", + "specialThanks": "Serghei Crasnicov @Serghei63", + "moduleName": "Bme280", + "moduleVersion": "1.0", + "usedRam": 15, + "subTypes": [ + "SoftUART" + ], + "title": "Софтовый uart для esp8266 или harware uart для esp32", + "moduleDesc": "Используется вместе с Pzem004t", + "propInfo": { + "tx": "TX пин", + "rx": "RX пин", + "speed": "Скорость UART" + } + }, + "defActive": false, + "devices": { + "esp32_4mb": [ + "plerup/espsoftwareserial" + ], + "esp8266_4mb": [ + "plerup/espsoftwareserial" + ] + } +} \ No newline at end of file