forked from olehs/PZEM004T
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPZEM004T.h
50 lines (38 loc) · 1.1 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
#ifndef PZEM004T_H
#define PZEM004T_H
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <SoftwareSerial.h>
#include <IPAddress.h>
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;
unsigned long _readTimeOut;
bool _isSoft;
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