-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPZem-004T_Esp8266.ino
115 lines (103 loc) · 2.85 KB
/
PZem-004T_Esp8266.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
111
112
113
114
115
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <InfluxDbClient.h>
#include <PZEM004Tv30.h>
#include "config.h"
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN);
Point sensor("PowerStatus");
/* Use software serial for the PZEM
* Pin 5 Rx (Connects to the Tx pin on the PZEM)
* Pin 4 Tx (Connects to the Rx pin on the PZEM)
*/
PZEM004Tv30 pzem(5, 4);
// Connecting to the WIFI network
bool Connect_WIFI() {
if (WiFi.status() == WL_CONNECTED)
return 0; //Meaningno action needed
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
return 1; //Connection or reconnection was needed!
}
WiFiClient espClient;
void ConnectToInflux() {
client.setInsecure();
// Check server connection
if (client.validateConnection()) {
Serial.print("Connected to InfluxDB: ");
Serial.println(client.getServerUrl());
} else {
Serial.print("InfluxDB connection failed: ");
Serial.println(client.getLastErrorMessage());
}
}
void setup() {
Serial.begin(115200);
Connect_WIFI();
// Add constant tags - only once
sensor.addTag("Device", "ESP8266_Main");
sensor.addTag("SSID", WiFi.SSID());
sensor.addTag("Power_Phase", "Geral");
ConnectToInflux();
}
double sens[4]; //Array of 4 Values
void loop() {
if (Connect_WIFI())
ConnectToInflux();
sensor.clearFields();
sensor.addField("wifi-rssi", WiFi.RSSI());
float voltage = pzem.voltage();
if(!isnan(voltage)){
sensor.addField("voltage", voltage);
} else {
Serial.println("Error reading voltage");
}
float current = pzem.current();
if(!isnan(current)){
sensor.addField("current", current);
} else {
Serial.println("Error reading current");
}
float power = pzem.power();
if(!isnan(power)){
sensor.addField("power", power);
} else {
Serial.println("Error reading power");
}
float energy = pzem.energy();
if(!isnan(energy)){
sensor.addField("energy", energy);
} else {
Serial.println("Error reading energy");
}
float frequency = pzem.frequency();
if(!isnan(frequency)){
sensor.addField("freq", frequency);
} else {
Serial.println("Error reading frequency");
}
float pf = pzem.pf();
if(!isnan(pf)){
sensor.addField("power-factor", pf);
} else {
Serial.println("Error reading power factor");
}
// Write point
if (!client.writePoint(sensor)) {
Serial.print("InfluxDB write failed: ");
Serial.println(client.getLastErrorMessage());
}
Serial.println("Sleep 10s");
delay(10000);
}