supla-device
Loading...
Searching...
No Matches
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_PZEMV3_H_
20#define SRC_SUPLA_SENSOR_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
27#define PZEM_0_DEFAULT_ADDR 0xF8
28
29#if defined(PZEM004_SOFTSERIAL)
30#include <SoftwareSerial.h>
31#endif
32#include <supla/time.h>
33
34#include "one_phase_electricity_meter.h"
35
36namespace Supla {
37namespace Sensor {
38
39class PZEMv3 : public OnePhaseElectricityMeter {
40 public:
41#if defined(PZEM004_SOFTSERIAL)
42 PZEMv3(uint8_t pinRX1,
43 uint8_t pinTX1,
44 uint8_t pzem_addr = PZEM_0_DEFAULT_ADDR)
45 : pzem{PZEM004Tv30(pinRX1, pinTX1, pzem_addr)} {
46 }
47#endif
48
49#if defined(ESP32)
50 PZEMv3(HardwareSerial *serial,
51 uint8_t pinRx1,
52 uint8_t pinTx1,
53 uint8_t pzem_addr = PZEM_0_DEFAULT_ADDR)
54 : pzem{PZEM004Tv30(serial, pinRx1, pinTx1, pzem_addr)} {
55 }
56#else
57 explicit PZEMv3(HardwareSerial *serial,
58 uint8_t pzem_addr = PZEM_0_DEFAULT_ADDR)
59 : pzem{PZEM004Tv30(serial, pzem_addr)} {
60 }
61#endif
62
63 void onInit() {
64 lastReadTime = millis();
65 readValuesFromDevice();
66 updateChannelValues();
67 }
68
69 virtual void readValuesFromDevice() {
70 float current = pzem.current();
71 // If current reading is NAN, we assume that PZEM there is no valid
72 // communication with PZEM. Sensor shouldn't show any data
73 if (isnan(current)) {
74 resetReadParameters();
75 return;
76 }
77
78 float voltage = pzem.voltage();
79 float active = pzem.power();
80 float apparent = (voltage * current);
81 float reactive = 0;
82 if (apparent > active) {
83 reactive = sqrt(apparent * apparent - active * active);
84 } else {
85 reactive = 0;
86 }
87
88 setVoltage(0, voltage * 100);
89 setCurrent(0, current * 1000);
90 setPowerActive(0, active * 100000);
91 setFwdActEnergy(0, pzem.energy() * 100000);
92 setFreq(pzem.frequency() * 100);
93 setPowerFactor(0, pzem.pf() * 1000);
94 setPowerApparent(0, apparent * 100000);
95 setPowerReactive(0, reactive * 100000);
96 }
97
98 void resetStorage() {
99 pzem.resetEnergy();
100 }
101
102 protected:
103 PZEM004Tv30 pzem;
104};
105
106}; // namespace Sensor
107}; // namespace Supla
108
109#endif // SRC_SUPLA_SENSOR_PZEMV3_H_
void onInit()
Third method called on element in SuplaDevice.begin()
Definition PzemV3.h:63