From 62ff3241396b197b5807d67afce1a951dc39ef13 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Tue, 27 Sep 2022 20:55:16 +0200 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BD=D0=B0=20=D0=BE=D1=82=D1=81=D1=83=D1=82=D1=81?= =?UTF-8?q?=D1=82=D0=B2=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_svelte/items.json | 2 +- data_svelte/widgets.json | 7 ---- lib/PZEMSensor/include/PZEMSensor.h | 5 +-- lib/PZEMSensor/src/PZEMSensor.cpp | 14 ++++--- platformio.ini | 1 + src/modules/sensors/Pzem004t/Pzem004t.cpp | 45 ++++++++++++++++++----- src/modules/sensors/Pzem004t/modinfo.json | 10 +++-- 7 files changed, 55 insertions(+), 29 deletions(-) diff --git a/data_svelte/items.json b/data_svelte/items.json index e3d64a57..3fb2570a 100644 --- a/data_svelte/items.json +++ b/data_svelte/items.json @@ -367,7 +367,7 @@ "page": "Сенсоры", "descr": "Частота", "int": 15, - "addr": "0x77", + "addr": "0xF8", "num": 28 }, { diff --git a/data_svelte/widgets.json b/data_svelte/widgets.json index 9480d4a2..c600b992 100644 --- a/data_svelte/widgets.json +++ b/data_svelte/widgets.json @@ -209,13 +209,6 @@ ], "status": 0 }, - { - "name": "anydataVlt", - "label": "Вольты", - "widget": "anydata", - "after": "Vlt", - "icon": "speedometer" - }, { "name": "nil", "label": "Без виджета" diff --git a/lib/PZEMSensor/include/PZEMSensor.h b/lib/PZEMSensor/include/PZEMSensor.h index e29362cc..25ddf175 100644 --- a/lib/PZEMSensor/include/PZEMSensor.h +++ b/lib/PZEMSensor/include/PZEMSensor.h @@ -1,7 +1,6 @@ #pragma once #include - #include #define PZEM_DEFAULT_ADDR 0xF8 @@ -22,13 +21,13 @@ class PZEMSensor { PZEMSensor(Stream *serial, uint16_t addr = PZEM_DEFAULT_ADDR); ~PZEMSensor(); - PZEM_Info* values(); + PZEM_Info *values(bool &online); bool setAddress(uint8_t addr); uint8_t getAddress(); bool setPowerAlarm(uint16_t watts); bool getPowerAlarm(); bool reset(); - void search(); + bool search(); // Get most up to date values from device registers and cache them bool refresh(); diff --git a/lib/PZEMSensor/src/PZEMSensor.cpp b/lib/PZEMSensor/src/PZEMSensor.cpp index cb9d297d..faadbe47 100644 --- a/lib/PZEMSensor/src/PZEMSensor.cpp +++ b/lib/PZEMSensor/src/PZEMSensor.cpp @@ -43,11 +43,13 @@ PZEMSensor::PZEMSensor(Stream *port, uint16_t addr) { init(); } -PZEM_Info *PZEMSensor::values() { +PZEM_Info *PZEMSensor::values(bool &online) { // Update vales if necessary if (!refresh()) { _values = PZEM_Info(); + online = false; } + online = true; return &_values; } @@ -62,7 +64,7 @@ PZEM_Info *PZEMSensor::values() { * @param[in] check - perform a simple read check after write * * @return success -*/ + */ bool PZEMSensor::sendCmd8(uint8_t cmd, uint16_t rAddr, uint16_t val, bool check, uint16_t slave_addr) { uint8_t sendBuffer[8]; // Send buffer uint8_t respBuffer[8]; // Response buffer (only used when check is true) @@ -295,7 +297,8 @@ uint16_t PZEMSensor::CRC16(const uint8_t *data, uint16_t len) { return crc; } -void PZEMSensor::search() { +bool PZEMSensor::search() { + bool ret = false; static uint8_t response[7]; for (uint16_t addr = 0x01; addr <= 0xF8; addr++) { sendCmd8(CMD_RIR, 0x00, 0x01, false, addr); @@ -303,8 +306,9 @@ void PZEMSensor::search() { // Something went wrong continue; } else { - Serial.print("Device on addr: "); - Serial.print(addr); + Serial.println("Pzem " + String(addr)); + ret = true; } } + return ret; } diff --git a/platformio.ini b/platformio.ini index b565b69f..f2f2f8a4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -61,6 +61,7 @@ lib_deps = https://github.com/JonasGMorsch/GY-21.git ClosedCube HDC1080 adafruit/MAX6675 library + mandulaj/PZEM-004T-v30 rc-switch @ ^2.6.4 robtillaart/SHT2x@^0.1.1 WEMOS SHT3x@1.0.0 diff --git a/src/modules/sensors/Pzem004t/Pzem004t.cpp b/src/modules/sensors/Pzem004t/Pzem004t.cpp index 66718d36..b274ac16 100644 --- a/src/modules/sensors/Pzem004t/Pzem004t.cpp +++ b/src/modules/sensors/Pzem004t/Pzem004t.cpp @@ -20,8 +20,13 @@ class Pzem004v : public IoTItem { void doByInterval() { if (pzem) { - value.valD = pzem->values()->voltage; - regEvent(value.valD, "Pzem Voltage"); + bool online = false; + value.valD = pzem->values(online)->voltage; + if (online) { + regEvent(value.valD, "Pzem V"); + } else { + SerialPrint("E", "Pzem", "V error"); + } } } @@ -43,8 +48,13 @@ class Pzem004a : public IoTItem { void doByInterval() { if (pzem) { - value.valD = pzem->values()->current; - regEvent(value.valD, "Pzem Ampere"); + bool online = false; + value.valD = pzem->values(online)->current; + if (online) { + regEvent(value.valD, "Pzem A"); + } else { + SerialPrint("E", "Pzem", "A error"); + } } } @@ -66,8 +76,13 @@ class Pzem004w : public IoTItem { void doByInterval() { if (pzem) { - value.valD = pzem->values()->power; - regEvent(value.valD, "Pzem Watt"); + bool online = false; + value.valD = pzem->values(online)->power; + if (online) { + regEvent(value.valD, "Pzem W"); + } else { + SerialPrint("E", "Pzem", "W error"); + } } } @@ -89,8 +104,13 @@ class Pzem004wh : public IoTItem { void doByInterval() { if (pzem) { - value.valD = pzem->values()->energy; - regEvent(value.valD, "Pzem Watt/hr"); + bool online = false; + value.valD = pzem->values(online)->energy; + if (online) { + regEvent(value.valD, "Pzem Wh"); + } else { + SerialPrint("E", "Pzem", "Wh error"); + } } } @@ -112,8 +132,13 @@ class Pzem004hz : public IoTItem { void doByInterval() { if (pzem) { - value.valD = pzem->values()->freq; - regEvent(value.valD, "Pzem Hz"); + bool online = false; + value.valD = pzem->values(online)->freq; + if (online) { + regEvent(value.valD, "Pzem Hz"); + } else { + SerialPrint("E", "Pzem", "Hz error"); + } } } diff --git a/src/modules/sensors/Pzem004t/modinfo.json b/src/modules/sensors/Pzem004t/modinfo.json index 45a92b3b..d89f3d71 100644 --- a/src/modules/sensors/Pzem004t/modinfo.json +++ b/src/modules/sensors/Pzem004t/modinfo.json @@ -54,7 +54,7 @@ "page": "Сенсоры", "descr": "Частота", "int": 15, - "addr": "0x77" + "addr": "0xF8" } ], "about": { @@ -81,7 +81,11 @@ }, "defActive": true, "devices": { - "esp32_4mb": [], - "esp8266_4mb": [] + "esp32_4mb": [ + "mandulaj/PZEM-004T-v30" + ], + "esp8266_4mb": [ + "mandulaj/PZEM-004T-v30" + ] } } \ No newline at end of file