supla-device
Loading...
Searching...
No Matches
three_phase_PzemV3.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_SENSOR_THREE_PHASE_PZEMV3_H_
5#define SRC_SUPLA_SENSOR_THREE_PHASE_PZEMV3_H_
6
7#include <Arduino.h>
8// dependence: Arduino library for the Updated PZEM-004T v3.0 Power and Energy
9// meter https://github.com/mandulaj/PZEM-004T-v30
10#include <PZEM004Tv30.h>
11#if defined(PZEM004_SOFTSERIAL)
12#include <SoftwareSerial.h>
13#endif
14#include <supla/time.h>
15
16#include "electricity_meter.h"
17
18namespace Supla {
19namespace Sensor {
20
22 public:
23#if defined(PZEM004_SOFTSERIAL)
24 ThreePhasePZEMv3(int8_t pinRX1,
25 int8_t pinTX1,
26 int8_t pinRX2,
27 int8_t pinTX2,
28 int8_t pinRX3,
29 int8_t pinTX3)
30 : pzem{PZEM004Tv30(pinRX1, pinTX1),
31 PZEM004Tv30(pinRX2, pinTX2),
32 PZEM004Tv30(pinRX3, pinTX3)} {
33 }
34#endif
35
36#if defined(ESP32)
37 ThreePhasePZEMv3(HardwareSerial *serial1,
38 int8_t pinRx1,
39 int8_t pinTx1,
40 HardwareSerial *serial2,
41 int8_t pinRx2,
42 int8_t pinTx2,
43 HardwareSerial *serial3,
44 int8_t pinRx3,
45 int8_t pinTx3)
46 : pzem{PZEM004Tv30(serial1, pinRx1, pinTx1),
47 PZEM004Tv30(serial2, pinRx2, pinTx2),
48 PZEM004Tv30(serial3, pinRx3, pinTx3)} {
49 }
50#else
51 ThreePhasePZEMv3(HardwareSerial *serial1,
52 HardwareSerial *serial2,
53 HardwareSerial *serial3)
54 : pzem{PZEM004Tv30(serial1), PZEM004Tv30(serial2), PZEM004Tv30(serial3)} {
55 }
56#endif
57
58 void onInit() {
59 lastReadTime = millis();
60 readValuesFromDevice();
61 updateChannelValues();
62 }
63
64 virtual void readValuesFromDevice() {
65 bool atLeatOnePzemWasRead = false;
66 for (int i = 0; i < 3; i++) {
67 float current = pzem[i].current();
68 // If current reading is NAN, we assume that PZEM there is no valid
69 // communication with PZEM. Sensor shouldn't show any data
70 if (isnan(current)) {
71 resetReadParametersForPhase(i);
72 continue;
73 }
74
75 atLeatOnePzemWasRead = true;
76
77 float voltage = pzem[i].voltage();
78 float active = pzem[i].power();
79 float apparent = (voltage * current);
80 float reactive = 0;
81 if (apparent > active) {
82 reactive = sqrt(apparent * apparent - active * active);
83 } else {
84 reactive = 0;
85 }
86
87 setVoltage(i, voltage * 100);
88 setCurrent(i, current * 1000);
89 setPowerActive(i, active * 100000);
90 setFwdActEnergy(i, pzem[i].energy() * 100000);
91 setPowerFactor(i, pzem[i].pf() * 1000);
92 setPowerApparent(i, apparent * 100000);
93 setPowerReactive(i, reactive * 10000);
94
95 setFreq(pzem[i].frequency() * 100);
96 }
97
98 if (!atLeatOnePzemWasRead) {
99 resetReadParameters();
100 }
101 }
102
103 void resetStorage() {
104 for (int i = 0; i < 3; i++) {
105 pzem[i].resetEnergy();
106 }
107 }
108
109 protected:
110 PZEM004Tv30 pzem[3];
111};
112
113}; // namespace Sensor
114}; // namespace Supla
115
116#endif // SRC_SUPLA_SENSOR_THREE_PHASE_PZEMV3_H_
Definition electricity_meter.h:52
Definition three_phase_PzemV3.h:21
void onInit()
Third method called on element in SuplaDevice.begin()
Definition three_phase_PzemV3.h:58