-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathESP32-autoconnect-PZEM-004T.ino
251 lines (207 loc) · 5.91 KB
/
ESP32-autoconnect-PZEM-004T.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Simple.ino, Example for the AutoConnect library.
Copyright (c) 2018, Hieromon Ikasamo
https://github.com/Hieromon/AutoConnect
This software is released under the MIT License.
https://opensource.org/licenses/MIT
*/
String PROJECT_URL="https://github.com/mqu/esp32-pzem-004t";
// HardwareSerial PzemSerial2(2); // Use hwserial UART2 at pins IO-16 (RX2) and IO-17 (TX2)
#include <HardwareSerial.h>
#include <PZEM004T.h>
PZEM004T pzem(&Serial2);
IPAddress ip(192,168,1,1);
// serial2
#define RXD2 16
#define TXD2 17
#include <esp_int_wdt.h>
#include <esp_task_wdt.h>
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#include <WebServer.h>
#endif
#include <time.h>
#include <AutoConnect.h>
#if defined(ARDUINO_ARCH_ESP8266)
ESP8266WebServer Server;
#elif defined(ARDUINO_ARCH_ESP32)
WebServer Server;
#endif
AutoConnect Portal(Server);
AutoConnectConfig Config; // Enable autoReconnect supported on v0.9.4
#define TIMEZONE (3600 * 1) // Paris
#define NTPServer1 "ntp.nict.jp" // NICT japan.
#define NTPServer2 "time1.google.com"
/**
* An actual reset function dependent on the architecture
*/
#if defined(ARDUINO_ARCH_ESP8266)
#define SOFT_RESET() ESP.reset()
#elif defined(ARDUINO_ARCH_ESP32)
#define SOFT_RESET() ESP.restart()
#endif
// from ESP/wifi/autoconnect ; https://github.com/Hieromon/AutoConnect/blob/master/src/AutoConnect.cpp#L18
// invoque watchdog + infinite loop
void hard_restart() {
esp_task_wdt_init(1,true);
esp_task_wdt_add(NULL);
while(true);
}
void restart() {
Serial.println("going to reset");
sleep(1000);
// ESP.restart(); not working
hard_restart();
}
extern "C" int rom_phy_get_vdd33();
void vcc() {
char buffer[20];
sprintf(buffer, "%d\n", rom_phy_get_vdd33());
Server.send(200, "text/txt", buffer);
}
void uptimems() {
char buffer[20];
sprintf(buffer, "%d\n", millis());
Server.send(200, "text/txt", buffer);
}
void uptime() {
char buffer[20];
sprintf(buffer, "%d\n", millis() / 1000);
Server.send(200, "text/txt", buffer);
}
void power_all() {
char buffer[40];
float v = pzem.voltage(ip);
if (v < 0.0) v = 0.0;
float i = pzem.current(ip);
float p = pzem.power(ip);
float e = pzem.energy(ip);
sprintf(buffer, "%0.2fV;%0.3fA;%0.1fW;%0.1fWh\n", v, i, p, e);
Serial.println(buffer);
Server.send(200, "text/txt", buffer);
}
void voltage() {
char buffer[30];
float v = pzem.voltage(ip);
if (v < 0.0) v = 0.0;
sprintf(buffer, "%0.2fV\n", v);
Serial.println(buffer);
Server.send(200, "text/txt", buffer);
}
void current() {
char buffer[30];
float v = pzem.current(ip);
if (v < 0.0) v = 0.0;
sprintf(buffer, "%0.3fA\n", v);
Serial.println(buffer);
Server.send(200, "text/txt", buffer);
}
void _power() {
char buffer[30];
float v = pzem.power(ip);
if (v < 0.0) v = 0.0;
sprintf(buffer, "%0.2fW\n", v);
Serial.println(buffer);
Server.send(200, "text/txt", buffer);
}
void energy() {
char buffer[30];
float v = pzem.energy(ip);
if (v < 0.0) v = 0.0;
sprintf(buffer, "%0.1fWh\n", v);
Serial.println(buffer);
Server.send(200, "text/txt", buffer);
}
void rootPage() {
String content =
"<html>"
"<head>"
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
"</head>"
"<body>"
"<h2 align=\"center\" style=\"color:blue;margin:20px;\">Hello, world</h2>"
"<h3 align=\"center\" style=\"color:gray;margin:10px;\">{{DateTime}}</h3>"
"<p style=\"padding-top:10px;text-align:center\">" AUTOCONNECT_LINK(COG_32) "</p>"
"</body>"
"</html>";
static const char *wd[7] = { "Sun","Mon","Tue","Wed","Thr","Fri","Sat" };
struct tm *tm;
time_t t;
char dateTime[26];
t = time(NULL);
tm = localtime(&t);
sprintf(dateTime, "%04d/%02d/%02d(%s) %02d:%02d:%02d.",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
wd[tm->tm_wday],
tm->tm_hour, tm->tm_min, tm->tm_sec);
content.replace("{{DateTime}}", String(dateTime));
Server.send(200, "text/html", content);
}
void blink(int count, int _delay=180){
for(int i=0;i<count;i++){
digitalWrite(LED_BUILTIN, HIGH);
delay(_delay);
digitalWrite(LED_BUILTIN, LOW);
delay(_delay);
}
}
int count=0;
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.println("Energy monitoring system using ESP32 and PZEM-004T");
Serial.print("- project URL");
Serial.println(PROJECT_URL);
// to read VCC
// ADC_MODE(ADC_VCC); // not available for ESP32
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
// PZEM
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
while (true) {
Serial.println("Connecting to PZEM...");
blink(2,110);
if(pzem.setAddress(ip))
break;
delay(1000);
}
Serial.println("connected to PZEM");
// Behavior a root path of ESP8266WebServer.
Server.on("/", rootPage);
// restart ESP32 when needed
Server.on("/restart", restart);
// what it uptime (in seconds)
Server.on("/uptime", uptime);
Server.on("/uptimems", uptimems);
Server.on("/vcc", vcc);
Server.on("/power/all", power_all);
Server.on("/power", _power);
Server.on("/voltage", voltage);
Server.on("/current", current);
Server.on("/energy", energy);
Portal.config(Config);
// Establish a connection with an autoReconnect option.
if (Portal.begin()) {
Serial.println("WiFi connected: " + WiFi.localIP().toString());
configTime(TIMEZONE, 0, NTPServer1, NTPServer2);
}
// avoid ping slow RTT - https://github.com/espressif/arduino-esp32/issues/1484
// https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/generic-class.html#wifi-power-management-dtim
WiFi.setSleep(false);
}
void loop() {
Portal.handleClient();
yield();
count++;
if(count%500000==0)
blink(2,30);
if(count%600000==0) {
Serial.print(rom_phy_get_vdd33());
Serial.println("VCC");
}
yield();
}