supla-device
Loading...
Searching...
No Matches
three_phase_PzemV3.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#ifndef SRC_SUPLA_SENSOR_THREE_PHASE_PZEMV3_H_
20#define SRC_SUPLA_SENSOR_THREE_PHASE_PZEMV3_H_
21
22#include <Arduino.h>
23// dependence: Arduino library for the Updated PZEM-004T v3.0 Power and Energy
24// meter https://github.com/mandulaj/PZEM-004T-v30
25#include <PZEM004Tv30.h>
26#if defined(PZEM004_SOFTSERIAL)
27#include <SoftwareSerial.h>
28#endif
29#include <supla/time.h>
30
31#include "electricity_meter.h"
32
33namespace Supla {
34namespace Sensor {
35
36class ThreePhasePZEMv3 : public ElectricityMeter {
37 public:
38#if defined(PZEM004_SOFTSERIAL)
39 ThreePhasePZEMv3(int8_t pinRX1,
40 int8_t pinTX1,
41 int8_t pinRX2,
42 int8_t pinTX2,
43 int8_t pinRX3,
44 int8_t pinTX3)
45 : pzem{PZEM004Tv30(pinRX1, pinTX1),
46 PZEM004Tv30(pinRX2, pinTX2),
47 PZEM004Tv30(pinRX3, pinTX3)} {
48 }
49#endif
50
51#if defined(ESP32)
52 ThreePhasePZEMv3(HardwareSerial *serial1,
53 int8_t pinRx1,
54 int8_t pinTx1,
55 HardwareSerial *serial2,
56 int8_t pinRx2,
57 int8_t pinTx2,
58 HardwareSerial *serial3,
59 int8_t pinRx3,
60 int8_t pinTx3)
61 : pzem{PZEM004Tv30(serial1, pinRx1, pinTx1),
62 PZEM004Tv30(serial2, pinRx2, pinTx2),
63 PZEM004Tv30(serial3, pinRx3, pinTx3)} {
64 }
65#else
66 ThreePhasePZEMv3(HardwareSerial *serial1,
67 HardwareSerial *serial2,
68 HardwareSerial *serial3)
69 : pzem{PZEM004Tv30(serial1), PZEM004Tv30(serial2), PZEM004Tv30(serial3)} {
70 }
71#endif
72
73 void onInit() {
74 lastReadTime = millis();
75 readValuesFromDevice();
76 updateChannelValues();
77 }
78
79 virtual void readValuesFromDevice() {
80 bool atLeatOnePzemWasRead = false;
81 for (int i = 0; i < 3; i++) {
82 float current = pzem[i].current();
83 // If current reading is NAN, we assume that PZEM there is no valid
84 // communication with PZEM. Sensor shouldn't show any data
85 if (isnan(current)) {
86 resetReadParametersForPhase(i);
87 continue;
88 }
89
90 atLeatOnePzemWasRead = true;
91
92 float voltage = pzem[i].voltage();
93 float active = pzem[i].power();
94 float apparent = (voltage * current);
95 float reactive = 0;
96 if (apparent > active) {
97 reactive = sqrt(apparent * apparent - active * active);
98 } else {
99 reactive = 0;
100 }
101
102 setVoltage(i, voltage * 100);
103 setCurrent(i, current * 1000);
104 setPowerActive(i, active * 100000);
105 setFwdActEnergy(i, pzem[i].energy() * 100000);
106 setPowerFactor(i, pzem[i].pf() * 1000);
107 setPowerApparent(i, apparent * 100000);
108 setPowerReactive(i, reactive * 10000);
109
110 setFreq(pzem[i].frequency() * 100);
111 }
112
113 if (!atLeatOnePzemWasRead) {
114 resetReadParameters();
115 }
116 }
117
118 void resetStorage() {
119 for (int i = 0; i < 3; i++) {
120 pzem[i].resetEnergy();
121 }
122 }
123
124 protected:
125 PZEM004Tv30 pzem[3];
126};
127
128}; // namespace Sensor
129}; // namespace Supla
130
131#endif // SRC_SUPLA_SENSOR_THREE_PHASE_PZEMV3_H_
void onInit()
Third method called on element in SuplaDevice.begin()
Definition three_phase_PzemV3.h:73