forked from olehs/PZEM004T
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPZEM004T.h
63 lines (46 loc) · 1.39 KB
/
PZEM004T.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
#ifndef PZEM004T_H
#define PZEM004T_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))
#define PZEM004_SOFTSERIAL
#endif
#if defined(PZEM004_SOFTSERIAL)
#include <ESPeasySerial.h>
#endif
#include <IPAddress.h>
#define PZEM_DEFAULT_READ_TIMEOUT 1000
#define PZEM_ERROR_VALUE -1.0
struct PZEMCommand {
uint8_t command;
uint8_t addr[4];
uint8_t data;
uint8_t crc;
};
class PZEM004T
{
public:
PZEM004T(uint8_t receivePin, uint8_t transmitPin);
PZEM004T(HardwareSerial *port);
~PZEM004T();
void setReadTimeout(unsigned long msec);
unsigned long readTimeout() {return _readTimeOut;}
float voltage(const IPAddress &addr);
float current(const IPAddress &addr);
float power(const IPAddress &addr);
float energy(const IPAddress &addr);
bool setAddress(const IPAddress &newAddr);
bool setPowerAlarm(const IPAddress &addr, uint8_t threshold);
private:
Stream *serial;
bool _isSoft;
unsigned long _readTimeOut = PZEM_DEFAULT_READ_TIMEOUT;
void send(const IPAddress &addr, uint8_t cmd, uint8_t data = 0);
bool recieve(uint8_t resp, uint8_t *data = 0);
uint8_t crc(uint8_t *data, uint8_t sz);
};
#endif // PZEM004T_H