supla-device
Loading...
Searching...
No Matches
three_phase_PzemV3_ADDR.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_ADDR_H_
20#define SRC_SUPLA_SENSOR_THREE_PHASE_PZEMV3_ADDR_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
33#define PZEM_1_DEFAULT_ADDR 0x01
34#define PZEM_2_DEFAULT_ADDR 0x02
35#define PZEM_3_DEFAULT_ADDR 0x03
36
37namespace Supla {
38namespace Sensor {
39
40class ThreePhasePZEMv3_ADDR : public ElectricityMeter {
41 public:
42#if defined(PZEM004_SOFTSERIAL)
43 ThreePhasePZEMv3_ADDR(uint8_t pinRX1,
44 uint8_t pinTX1,
45 uint8_t pzem_1_addr = PZEM_1_DEFAULT_ADDR,
46 uint8_t pzem_2_addr = PZEM_2_DEFAULT_ADDR,
47 uint8_t pzem_3_addr = PZEM_3_DEFAULT_ADDR)
48 : pzem{PZEM004Tv30(pinRX1, pinTX1, pzem_1_addr),
49 PZEM004Tv30(pinRX1, pinTX1, pzem_2_addr),
50 PZEM004Tv30(pinRX1, pinTX1, pzem_3_addr)} {
51 }
52#endif
53
54#if defined(ESP32)
55 ThreePhasePZEMv3_ADDR(HardwareSerial *serial,
56 uint8_t pinRx1,
57 uint8_t pinTx1,
58 uint8_t pzem_1_addr = PZEM_1_DEFAULT_ADDR,
59 uint8_t pzem_2_addr = PZEM_2_DEFAULT_ADDR,
60 uint8_t pzem_3_addr = PZEM_3_DEFAULT_ADDR)
61 : pzem{PZEM004Tv30(serial, pinRx1, pinTx1, pzem_1_addr),
62 PZEM004Tv30(serial, pinRx1, pinTx1, pzem_2_addr),
63 PZEM004Tv30(serial, pinRx1, pinTx1, pzem_3_addr)} {
64 }
65#else
66 ThreePhasePZEMv3_ADDR(HardwareSerial *serial,
67 uint8_t pzem_1_addr = PZEM_1_DEFAULT_ADDR,
68 uint8_t pzem_2_addr = PZEM_2_DEFAULT_ADDR,
69 uint8_t pzem_3_addr = PZEM_3_DEFAULT_ADDR)
70 : pzem{PZEM004Tv30(serial, pzem_1_addr),
71 PZEM004Tv30(serial, pzem_2_addr),
72 PZEM004Tv30(serial, pzem_3_addr)} {
73 }
74#endif
75
76 void onInit() {
77 lastReadTime = millis();
78 readValuesFromDevice();
79 updateChannelValues();
80 }
81
82 virtual void readValuesFromDevice() {
83 bool atLeatOnePzemWasRead = false;
84 for (int i = 0; i < 3; i++) {
85 float current = pzem[i].current();
86 // If current reading is NAN, we assume that PZEM there is no valid
87 // communication with PZEM. Sensor shouldn't show any data
88 if (isnan(current)) {
89 resetReadParametersForPhase(i);
90 continue;
91 }
92
93 atLeatOnePzemWasRead = true;
94
95 float voltage = pzem[i].voltage();
96 float active = pzem[i].power();
97 float apparent = (voltage * current);
98 float reactive = 0;
99 if (apparent > active) {
100 reactive = sqrt(apparent * apparent - active * active);
101 } else {
102 reactive = 0;
103 }
104
105 setVoltage(i, voltage * 100);
106 setCurrent(i, current * 1000);
107 setPowerActive(i, active * 100000);
108 setFwdActEnergy(i, pzem[i].energy() * 100000);
109 setPowerFactor(i, pzem[i].pf() * 1000);
110 setPowerApparent(i, apparent * 100000);
111 setPowerReactive(i, reactive * 100000);
112
113 setFreq(pzem[i].frequency() * 100);
114 }
115
116 if (!atLeatOnePzemWasRead) {
117 resetReadParameters();
118 }
119 }
120
121 void resetStorage() {
122 for (int i = 0; i < 3; i++) {
123 pzem[i].resetEnergy();
124 }
125 }
126
127 protected:
128 PZEM004Tv30 pzem[3];
129};
130
131}; // namespace Sensor
132}; // namespace Supla
133
134#endif // SRC_SUPLA_SENSOR_THREE_PHASE_PZEMV3_ADDR_H_
void onInit()
Third method called on element in SuplaDevice.begin()
Definition three_phase_PzemV3_ADDR.h:76