forked from mandulaj/PZEM-004T-v30
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PZEMHardSerial.ino
110 lines (91 loc) · 3.28 KB
/
PZEMHardSerial.ino
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
/*
Copyright (c) 2021 Jakub Mandula
Example of using one PZEM module with Hardware Serial interface.
================================================================
If desired, a HardwareSerial handle can be passed to the constructor
which will then be used for the communication with the module.
Note that ESP32 HardwareSerial must also be provided with the RX and TX
pins.
*/
#include <PZEM004Tv30.h>
#if !defined(PZEM_RX_PIN) && !defined(PZEM_TX_PIN)
#define PZEM_RX_PIN 16
#define PZEM_TX_PIN 17
#endif
#if !defined(PZEM_SERIAL)
#define PZEM_SERIAL Serial2
#endif
#if defined(ESP32)
/*************************
* ESP32 initialization
* ---------------------
*
* The ESP32 HW Serial interface can be routed to any GPIO pin
* Here we initialize the PZEM on Serial2 with RX/TX pins 16 and 17
*/
PZEM004Tv30 pzem(PZEM_SERIAL, PZEM_RX_PIN, PZEM_TX_PIN);
#elif defined(ESP8266)
/*************************
* ESP8266 initialization
* ---------------------
*
* Not all Arduino boards come with multiple HW Serial ports.
* Serial2 is for example available on the Arduino MEGA 2560 but not Arduino Uno!
* The ESP32 HW Serial interface can be routed to any GPIO pin
* Here we initialize the PZEM on Serial2 with default pins
*/
//PZEM004Tv30 pzem(Serial1);
#else
/*************************
* Arduino initialization
* ---------------------
*
* Not all Arduino boards come with multiple HW Serial ports.
* Serial2 is for example available on the Arduino MEGA 2560 but not Arduino Uno!
* The ESP32 HW Serial interface can be routed to any GPIO pin
* Here we initialize the PZEM on Serial2 with default pins
*/
PZEM004Tv30 pzem(PZEM_SERIAL);
#endif
void setup() {
// Debugging Serial port
Serial.begin(115200);
// Uncomment in order to reset the internal energy counter
// pzem.resetEnergy()
}
void loop() {
// Print the custom address of the PZEM
Serial.print("Custom Address:");
Serial.println(pzem.readAddress(), HEX);
// Read the data from the sensor
float voltage = pzem.voltage();
float current = pzem.current();
float power = pzem.power();
float energy = pzem.energy();
float frequency = pzem.frequency();
float pf = pzem.pf();
// Check if the data is valid
if(isnan(voltage)){
Serial.println("Error reading voltage");
} else if (isnan(current)) {
Serial.println("Error reading current");
} else if (isnan(power)) {
Serial.println("Error reading power");
} else if (isnan(energy)) {
Serial.println("Error reading energy");
} else if (isnan(frequency)) {
Serial.println("Error reading frequency");
} else if (isnan(pf)) {
Serial.println("Error reading power factor");
} else {
// Print the values to the Serial console
Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
Serial.print("Current: "); Serial.print(current); Serial.println("A");
Serial.print("Power: "); Serial.print(power); Serial.println("W");
Serial.print("Energy: "); Serial.print(energy,3); Serial.println("kWh");
Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz");
Serial.print("PF: "); Serial.println(pf);
}
Serial.println();
delay(2000);
}