forked from RoySpringer/esp32_p1meter
-
Notifications
You must be signed in to change notification settings - Fork 10
/
esp32_p1meter.ino
297 lines (260 loc) · 9.16 KB
/
esp32_p1meter.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include <WiFi.h>
#include "settings.h"
WiFiClient espClient;
PubSubClient mqttClient(espClient);
/***********************************
Main Setup
***********************************/
void setup()
{
// Initialize pins
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(BAUD_RATE);
Serial2.begin(BAUD_RATE, SERIAL_8N1, RXD2, TXD2, true);
#ifdef DEBUG
Serial.println("Booting - DEBUG mode on");
blinkLed(2, 500);
delay(500);
blinkLed(2, 2000);
// Blinking 2 times fast and two times slower to indicate DEBUG mode
#endif
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.waitForConnectResult() != WL_CONNECTED)
{
#ifdef DEBUG
Serial.println("Connection Failed! Rebooting...");
#endif
delay(5000);
ESP.restart();
}
delay(3000);
setupDataReadout();
setupOTA();
mqttClient.setServer(MQTT_HOST, atoi(MQTT_PORT));
blinkLed(5, 500); // Blink 5 times to indicate end of setup
#ifdef DEBUG
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
#endif
}
/***********************************
Main Loop
***********************************/
void loop()
{
long now = millis();
if (WiFi.status() != WL_CONNECTED)
{
blinkLed(20, 50); // Blink fast to indicate failed WiFi connection
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.waitForConnectResult() != WL_CONNECTED)
{
#ifdef DEBUG
Serial.println("Connection Failed! Rebooting...");
#endif
delay(5000);
ESP.restart();
}
}
ArduinoOTA.handle();
if (!mqttClient.connected())
{
if (now - LAST_RECONNECT_ATTEMPT > 5000)
{
LAST_RECONNECT_ATTEMPT = now;
if (!mqttReconnect())
{
#ifdef DEBUG
Serial.println("Connection to MQTT Failed! Rebooting...");
#endif
delay(5000);
ESP.restart();
}
else
{
LAST_RECONNECT_ATTEMPT = 0;
}
}
}
else
{
mqttClient.loop();
}
// Check if we want a full update of all the data including the unchanged data.
if (now - LAST_FULL_UPDATE_SENT > UPDATE_FULL_INTERVAL)
{
for (int i = 0; i < NUMBER_OF_READOUTS; i++)
{
telegramObjects[i].sendData = true;
LAST_FULL_UPDATE_SENT = millis();
}
}
if (now - LAST_UPDATE_SENT > UPDATE_INTERVAL)
{
if (readP1Serial())
{
LAST_UPDATE_SENT = millis();
sendDataToBroker();
}
}
}
/***********************************
Setup Methods
***********************************/
/**
setupDataReadout()
This method can be used to create more data readout to mqtt topic.
Use the name for the mqtt topic.
The code for finding this in the telegram see
https://www.netbeheernederland.nl/_upload/Files/Slimme_meter_15_a727fce1f1.pdf for the dutch codes pag. 19 -23
Use startChar and endChar for setting the boundies where the value is in between.
Default startChar and endChar is '(' and ')'
Note: Make sure when you add or remove telegramObject to update the NUMBER_OF_READOUTS accordingly.
*/
void setupDataReadout()
{
// 1-0:1.8.1(000992.992*kWh)
// 1-0:1.8.1 = Elektra verbruik laag tarief (DSMR v5.0)
telegramObjects[0].name = "consumption_tarif_1";
strcpy(telegramObjects[0].code, "1-0:1.8.1");
telegramObjects[0].endChar = '*';
// 1-0:1.8.2(000560.157*kWh)
// 1-0:1.8.2 = Elektra verbruik hoog tarief (DSMR v5.0)
telegramObjects[1].name = "consumption_tarif_2";
strcpy(telegramObjects[1].code, "1-0:1.8.2");
telegramObjects[1].endChar = '*';
telegramObjects[0].name = "received_tarif_1";
strcpy(telegramObjects[0].code, "1-0:2.8.1");
telegramObjects[0].endChar = '*';
// 1-0:1.8.2(000560.157*kWh)
// 1-0:1.8.2 = Elektra verbruik hoog tarief (DSMR v5.0)
telegramObjects[1].name = "received_tarif_2";
strcpy(telegramObjects[1].code, "1-0:2.8.2");
telegramObjects[1].endChar = '*';
// 1-0:1.7.0(00.424*kW) Actueel verbruik
// 1-0:1.7.x = Electricity consumption actual usage (DSMR v5.0)
telegramObjects[2].name = "actual_consumption";
strcpy(telegramObjects[2].code, "1-0:1.7.0");
telegramObjects[2].endChar = '*';
// 1-0:2.7.0(00.000*kW) Actuele teruglevering (-P) in 1 Watt resolution
telegramObjects[3].name = "actual_received";
strcpy(telegramObjects[3].code, "1-0:2.7.0");
telegramObjects[3].endChar = '*';
// 1-0:21.7.0(00.378*kW)
// 1-0:21.7.0 = Instantaan vermogen Elektriciteit levering L1
telegramObjects[4].name = "instant_power_usage_l1";
strcpy(telegramObjects[4].code, "1-0:21.7.0");
telegramObjects[4].endChar = '*';
// 1-0:41.7.0(00.378*kW)
// 1-0:41.7.0 = Instantaan vermogen Elektriciteit levering L2
telegramObjects[5].name = "instant_power_usage_l2";
strcpy(telegramObjects[5].code, "1-0:41.7.0");
telegramObjects[5].endChar = '*';
// 1-0:61.7.0(00.378*kW)
// 1-0:61.7.0 = Instantaan vermogen Elektriciteit levering L3
telegramObjects[6].name = "instant_power_usage_l3";
strcpy(telegramObjects[6].code, "1-0:61.7.0");
telegramObjects[6].endChar = '*';
// 1-0:22.7.0(00.378*kW)
// 1-0:22.7.0 = Instantaan vermogen Elektriciteit teruglevering L1
telegramObjects[4].name = "instant_power_return_l1";
strcpy(telegramObjects[4].code, "1-0:22.7.0");
telegramObjects[4].endChar = '*';
// 1-0:42.7.0(00.378*kW)
// 1-0:42.7.0 = Instantaan vermogen Elektriciteit teruglevering L2
telegramObjects[5].name = "instant_power_return_l2";
strcpy(telegramObjects[5].code, "1-0:42.7.0");
telegramObjects[5].endChar = '*';
// 1-0:62.7.0(00.378*kW)
// 1-0:62.7.0 = Instantaan vermogen Elektriciteit teruglevering L3
telegramObjects[6].name = "instant_power_return_l3";
strcpy(telegramObjects[6].code, "1-0:62.7.0");
telegramObjects[6].endChar = '*';
// 1-0:31.7.0(002*A)
// 1-0:31.7.0 = Instantane stroom Elektriciteit L1
telegramObjects[7].name = "instant_power_current_l1";
strcpy(telegramObjects[7].code, "1-0:31.7.0");
telegramObjects[7].endChar = '*';
// 1-0:51.7.0(002*A)
// 1-0:51.7.0 = Instantane stroom Elektriciteit L2
telegramObjects[8].name = "instant_power_current_l2";
strcpy(telegramObjects[8].code, "1-0:51.7.0");
telegramObjects[8].endChar = '*';
// 1-0:71.7.0(002*A)
// 1-0:71.7.0 = Instantane stroom Elektriciteit L3
telegramObjects[9].name = "instant_power_current_l3";
strcpy(telegramObjects[9].code, "1-0:71.7.0");
telegramObjects[9].endChar = '*';
// 1-0:32.7.0(232.0*V)
// 1-0:32.7.0 = Voltage L1
telegramObjects[10].name = "instant_voltage_l1";
strcpy(telegramObjects[10].code, "1-0:32.7.0");
telegramObjects[10].endChar = '*';
// 1-0:52.7.0(232.0*V)
// 1-0:52.7.0 = Voltage L2
telegramObjects[11].name = "instant_voltage_l2";
strcpy(telegramObjects[11].code, "1-0:52.7.0");
telegramObjects[11].endChar = '*';
// 1-0:72.7.0(232.0*V)
// 1-0:72.7.0 = Voltage L3
telegramObjects[12].name = "instant_voltage_l3";
strcpy(telegramObjects[12].code, "1-0:72.7.0");
telegramObjects[12].endChar = '*';
// 0-0:96.14.0(0001)
// 0-0:96.14.0 = Actual Tarif
telegramObjects[13].name = "actual_tarif_group";
strcpy(telegramObjects[13].code, "0-0:96.14.0");
// 0-1:24.2.3(150531200000S)(00811.923*m3)
// 0-1:24.2.3 = Gas (DSMR v5.0) on Belgian meters
telegramObjects[18].name = "gas_meter_m3";
strcpy(telegramObjects[18].code, "0-1:24.2.3");
telegramObjects[12].endChar = '*';
#ifdef DEBUG
Serial.println("MQTT Topics initialized:");
for (int i = 0; i < NUMBER_OF_READOUTS; i++)
{
Serial.println(String(MQTT_ROOT_TOPIC) + "/" + telegramObjects[i].name);
}
#endif
}
/**
Over the Air update setup
*/
void setupOTA()
{
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR)
Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR)
Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR)
Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR)
Serial.println("Receive Failed");
else if (error == OTA_END_ERROR)
Serial.println("End Failed");
});
ArduinoOTA.begin();
}