-
Notifications
You must be signed in to change notification settings - Fork 1
/
PZEM004Tv30.h
121 lines (85 loc) · 3.26 KB
/
PZEM004Tv30.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Copyright (c) 2019 Jakub Mandula
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* PZEM-004Tv30.h
*
* Interface library for the upgraded version of PZEM-004T v3.0
* Based on the PZEM004T library by @olehs https://github.com/olehs/PZEM004T
*
* Author: Jakub Mandula https://github.com/mandulaj
*
*
*/
#ifndef PZEM004TV30_H
#define PZEM004TV30_H
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// #define PZEM004_NO_SWSERIAL
#if (not defined(PZEM004_NO_SWSERIAL)) && (defined(__AVR__) || defined(ESP8266) && (not defined(ESP32)))
#define PZEM004_SOFTSERIAL
#endif
#if defined(PZEM004_SOFTSERIAL)
#include <SoftwareSerial.h>
#endif
#define PZEM_DEFAULT_ADDR 0xF8
class PZEM004Tv30
{
public:
#if defined(PZEM004_SOFTSERIAL)
PZEM004Tv30(uint8_t receivePin, uint8_t transmitPin, uint8_t addr=PZEM_DEFAULT_ADDR);
#endif
PZEM004Tv30(HardwareSerial& serialPZEM, uint8_t addr=PZEM_DEFAULT_ADDR);
~PZEM004Tv30();
float voltage();
float current();
float power();
float energy();
float frequency();
float pf();
bool setAddress(uint8_t addr);
uint8_t getAddress();
bool setPowerAlarm(uint16_t watts);
bool getPowerAlarm();
bool resetEnergy();
void search();
private:
Stream& _serial; // Serial interface
bool _isSoft; // Is serial interface software
uint8_t _addr; // Device address
struct {
float voltage;
float current;
float power;
float energy;
float frequeny;
float pf;
uint16_t alarms;
} _currentValues; // Measured values
uint64_t _lastRead; // Last time values were updated
void init(uint8_t addr); // Init common to all constructors
bool updateValues(); // Get most up to date values from device registers and cache them
uint16_t recieve(uint8_t *resp, uint16_t len); // Receive len bytes into a buffer
bool sendCmd8(uint8_t cmd, uint16_t rAddr, uint16_t val, bool check=false, uint16_t slave_addr=0xFFFF); // Send 8 byte command
void setCRC(uint8_t *buf, uint16_t len); // Set the CRC for a buffer
bool checkCRC(const uint8_t *buf, uint16_t len); // Check CRC of buffer
uint16_t CRC16(const uint8_t *data, uint16_t len); // Calculate CRC of buffer
};
#endif // PZEM004T_H