| 1 |
raymond |
1 |
#include <FS.h>
|
|
|
2 |
#include <EEPROM.h>
|
|
|
3 |
#include <DNSServer.h>
|
|
|
4 |
#include <ESP8266WiFi.h>
|
|
|
5 |
#include <Ticker.h>
|
|
|
6 |
#include <WiFiManager.h>
|
|
|
7 |
#include <ESP8266mDNS.h>
|
|
|
8 |
#include <WiFiUdp.h>
|
|
|
9 |
#include <ArduinoOTA.h>
|
|
|
10 |
#include <PubSubClient.h>
|
|
|
11 |
|
|
|
12 |
// * Include settings
|
|
|
13 |
#include "settings.h"
|
|
|
14 |
|
|
|
15 |
// * Initiate led blinker library
|
|
|
16 |
Ticker ticker;
|
|
|
17 |
|
|
|
18 |
// * Initiate WIFI client
|
|
|
19 |
WiFiClient espClient;
|
|
|
20 |
|
|
|
21 |
// * Initiate MQTT client
|
|
|
22 |
PubSubClient mqtt_client(espClient);
|
|
|
23 |
|
|
|
24 |
// **********************************
|
|
|
25 |
// * WIFI *
|
|
|
26 |
// **********************************
|
|
|
27 |
|
|
|
28 |
// * Gets called when WiFiManager enters configuration mode
|
|
|
29 |
void configModeCallback(WiFiManager *myWiFiManager)
|
|
|
30 |
{
|
|
|
31 |
Serial.println(F("Entered config mode"));
|
|
|
32 |
Serial.println(WiFi.softAPIP());
|
|
|
33 |
|
|
|
34 |
// * If you used auto generated SSID, print it
|
|
|
35 |
Serial.println(myWiFiManager->getConfigPortalSSID());
|
|
|
36 |
|
|
|
37 |
// * Entered config mode, make led toggle faster
|
|
|
38 |
ticker.attach(0.2, tick);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
// **********************************
|
|
|
42 |
// * Ticker (System LED Blinker) *
|
|
|
43 |
// **********************************
|
|
|
44 |
|
|
|
45 |
// * Blink on-board Led
|
|
|
46 |
void tick()
|
|
|
47 |
{
|
|
|
48 |
// * Toggle state
|
|
|
49 |
int state = digitalRead(LED_BUILTIN); // * Get the current state of GPIO1 pin
|
|
|
50 |
digitalWrite(LED_BUILTIN, !state); // * Set pin to the opposite state
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
// **********************************
|
|
|
54 |
// * MQTT *
|
|
|
55 |
// **********************************
|
|
|
56 |
|
|
|
57 |
// * Send a message to a broker topic
|
|
|
58 |
void send_mqtt_message(const char *topic, char *payload)
|
|
|
59 |
{
|
|
|
60 |
Serial.printf("MQTT Outgoing on %s: ", topic);
|
|
|
61 |
Serial.println(payload);
|
|
|
62 |
|
|
|
63 |
bool result = mqtt_client.publish(topic, payload, false);
|
|
|
64 |
|
|
|
65 |
if (!result)
|
|
|
66 |
{
|
|
|
67 |
Serial.printf("MQTT publish to topic %s failed\n", topic);
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
// * Reconnect to MQTT server and subscribe to in and out topics
|
|
|
72 |
bool mqtt_reconnect()
|
|
|
73 |
{
|
|
|
74 |
// * Loop until we're reconnected
|
|
|
75 |
int MQTT_RECONNECT_RETRIES = 0;
|
|
|
76 |
|
|
|
77 |
while (!mqtt_client.connected() && MQTT_RECONNECT_RETRIES < MQTT_MAX_RECONNECT_TRIES)
|
|
|
78 |
{
|
|
|
79 |
MQTT_RECONNECT_RETRIES++;
|
|
|
80 |
Serial.printf("MQTT connection attempt %d / %d ...\n", MQTT_RECONNECT_RETRIES, MQTT_MAX_RECONNECT_TRIES);
|
|
|
81 |
|
|
|
82 |
// * Attempt to connect
|
|
|
83 |
if (mqtt_client.connect(HOSTNAME, MQTT_USER, MQTT_PASS))
|
|
|
84 |
{
|
|
|
85 |
Serial.println(F("MQTT connected!"));
|
|
|
86 |
|
|
|
87 |
// * Once connected, publish an announcement...
|
|
|
88 |
char *message = new char[16 + strlen(HOSTNAME) + 1];
|
|
|
89 |
strcpy(message, "p1 meter alive: ");
|
|
|
90 |
strcat(message, HOSTNAME);
|
|
|
91 |
mqtt_client.publish("hass/status", message);
|
|
|
92 |
|
|
|
93 |
Serial.printf("MQTT root topic: %s\n", MQTT_ROOT_TOPIC);
|
|
|
94 |
}
|
|
|
95 |
else
|
|
|
96 |
{
|
|
|
97 |
Serial.print(F("MQTT Connection failed: rc="));
|
|
|
98 |
Serial.println(mqtt_client.state());
|
|
|
99 |
Serial.println(F(" Retrying in 5 seconds"));
|
|
|
100 |
Serial.println("");
|
|
|
101 |
|
|
|
102 |
// * Wait 5 seconds before retrying
|
|
|
103 |
delay(5000);
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
if (MQTT_RECONNECT_RETRIES >= MQTT_MAX_RECONNECT_TRIES)
|
|
|
108 |
{
|
|
|
109 |
Serial.printf("*** MQTT connection failed, giving up after %d tries ...\n", MQTT_RECONNECT_RETRIES);
|
|
|
110 |
return false;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
return true;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
void send_metric(String name, long metric)
|
|
|
117 |
{
|
|
|
118 |
Serial.print(F("Sending metric to broker: "));
|
|
|
119 |
Serial.print(name);
|
|
|
120 |
Serial.print(F("="));
|
|
|
121 |
Serial.println(metric);
|
|
|
122 |
|
|
|
123 |
char output[10];
|
|
|
124 |
ltoa(metric, output, sizeof(output));
|
|
|
125 |
|
|
|
126 |
String topic = String(MQTT_ROOT_TOPIC) + "/" + name;
|
|
|
127 |
send_mqtt_message(topic.c_str(), output);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
void send_data_to_broker()
|
|
|
131 |
{
|
|
|
132 |
send_metric("consumption_low_tarif", CONSUMPTION_LOW_TARIF);
|
|
|
133 |
send_metric("consumption_high_tarif", CONSUMPTION_HIGH_TARIF);
|
|
|
134 |
send_metric("returndelivery_low_tarif", RETURNDELIVERY_LOW_TARIF);
|
|
|
135 |
send_metric("returndelivery_high_tarif", RETURNDELIVERY_HIGH_TARIF);
|
|
|
136 |
send_metric("actual_consumption", ACTUAL_CONSUMPTION);
|
|
|
137 |
send_metric("actual_returndelivery", ACTUAL_RETURNDELIVERY);
|
|
|
138 |
|
|
|
139 |
send_metric("l1_instant_power_usage", L1_INSTANT_POWER_USAGE);
|
|
|
140 |
send_metric("l2_instant_power_usage", L2_INSTANT_POWER_USAGE);
|
|
|
141 |
send_metric("l3_instant_power_usage", L3_INSTANT_POWER_USAGE);
|
|
|
142 |
send_metric("l1_instant_power_current", L1_INSTANT_POWER_CURRENT);
|
|
|
143 |
send_metric("l2_instant_power_current", L2_INSTANT_POWER_CURRENT);
|
|
|
144 |
send_metric("l3_instant_power_current", L3_INSTANT_POWER_CURRENT);
|
|
|
145 |
send_metric("l1_voltage", L1_VOLTAGE);
|
|
|
146 |
send_metric("l2_voltage", L2_VOLTAGE);
|
|
|
147 |
send_metric("l3_voltage", L3_VOLTAGE);
|
|
|
148 |
|
|
|
149 |
send_metric("gas_meter_m3", GAS_METER_M3);
|
|
|
150 |
|
|
|
151 |
send_metric("actual_tarif_group", ACTUAL_TARIF);
|
|
|
152 |
send_metric("short_power_outages", SHORT_POWER_OUTAGES);
|
|
|
153 |
send_metric("long_power_outages", LONG_POWER_OUTAGES);
|
|
|
154 |
send_metric("short_power_drops", SHORT_POWER_DROPS);
|
|
|
155 |
send_metric("short_power_peaks", SHORT_POWER_PEAKS);
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
// **********************************
|
|
|
159 |
// * P1 *
|
|
|
160 |
// **********************************
|
|
|
161 |
|
|
|
162 |
unsigned int CRC16(unsigned int crc, unsigned char *buf, int len)
|
|
|
163 |
{
|
|
|
164 |
for (int pos = 0; pos < len; pos++)
|
|
|
165 |
{
|
|
|
166 |
crc ^= (unsigned int)buf[pos]; // * XOR byte into least sig. byte of crc
|
|
|
167 |
// * Loop over each bit
|
|
|
168 |
for (int i = 8; i != 0; i--)
|
|
|
169 |
{
|
|
|
170 |
// * If the LSB is set
|
|
|
171 |
if ((crc & 0x0001) != 0)
|
|
|
172 |
{
|
|
|
173 |
// * Shift right and XOR 0xA001
|
|
|
174 |
crc >>= 1;
|
|
|
175 |
crc ^= 0xA001;
|
|
|
176 |
}
|
|
|
177 |
// * Else LSB is not set
|
|
|
178 |
else
|
|
|
179 |
// * Just shift right
|
|
|
180 |
crc >>= 1;
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
return crc;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
bool isNumber(char *res, int len)
|
|
|
187 |
{
|
|
|
188 |
for (int i = 0; i < len; i++)
|
|
|
189 |
{
|
|
|
190 |
if (((res[i] < '0') || (res[i] > '9')) && (res[i] != '.' && res[i] != 0))
|
|
|
191 |
return false;
|
|
|
192 |
}
|
|
|
193 |
return true;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
int FindCharInArrayRev(char array[], char c, int len)
|
|
|
197 |
{
|
|
|
198 |
for (int i = len - 1; i >= 0; i--)
|
|
|
199 |
{
|
|
|
200 |
if (array[i] == c)
|
|
|
201 |
return i;
|
|
|
202 |
}
|
|
|
203 |
return -1;
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
long getValue(char *buffer, int maxlen, char startchar, char endchar)
|
|
|
207 |
{
|
|
|
208 |
int s = FindCharInArrayRev(buffer, startchar, maxlen - 2);
|
|
|
209 |
int l = FindCharInArrayRev(buffer, endchar, maxlen - 2) - s - 1;
|
|
|
210 |
|
|
|
211 |
char res[16];
|
|
|
212 |
memset(res, 0, sizeof(res));
|
|
|
213 |
|
|
|
214 |
if (strncpy(res, buffer + s + 1, l))
|
|
|
215 |
{
|
|
|
216 |
if (endchar == '*')
|
|
|
217 |
{
|
|
|
218 |
if (isNumber(res, l))
|
|
|
219 |
// * Lazy convert float to long
|
|
|
220 |
return (1000 * atof(res));
|
|
|
221 |
}
|
|
|
222 |
else if (endchar == ')')
|
|
|
223 |
{
|
|
|
224 |
if (isNumber(res, l))
|
|
|
225 |
return atof(res);
|
|
|
226 |
}
|
|
|
227 |
}
|
|
|
228 |
return 0;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
bool decode_telegram(int len)
|
|
|
232 |
{
|
|
|
233 |
int startChar = FindCharInArrayRev(telegram, '/', len);
|
|
|
234 |
int endChar = FindCharInArrayRev(telegram, '!', len);
|
|
|
235 |
bool validCRCFound = false;
|
|
|
236 |
|
|
|
237 |
for (int cnt = 0; cnt < len; cnt++) {
|
|
|
238 |
Serial.print(telegram[cnt]);
|
|
|
239 |
}
|
|
|
240 |
Serial.print("\n");
|
|
|
241 |
|
|
|
242 |
if (startChar >= 0)
|
|
|
243 |
{
|
|
|
244 |
// * Start found. Reset CRC calculation
|
|
|
245 |
currentCRC = CRC16(0x0000,(unsigned char *) telegram+startChar, len-startChar);
|
|
|
246 |
}
|
|
|
247 |
else if (endChar >= 0)
|
|
|
248 |
{
|
|
|
249 |
// * Add to crc calc
|
|
|
250 |
currentCRC = CRC16(currentCRC,(unsigned char*)telegram+endChar, 1);
|
|
|
251 |
|
|
|
252 |
char messageCRC[5];
|
|
|
253 |
strncpy(messageCRC, telegram + endChar + 1, 4);
|
|
|
254 |
|
|
|
255 |
messageCRC[4] = 0; // * Thanks to HarmOtten (issue 5)
|
|
|
256 |
validCRCFound = (strtol(messageCRC, NULL, 16) == currentCRC);
|
|
|
257 |
|
|
|
258 |
if (validCRCFound)
|
|
|
259 |
Serial.println(F("CRC Valid!"));
|
|
|
260 |
else
|
|
|
261 |
Serial.println(F("CRC Invalid!"));
|
|
|
262 |
|
|
|
263 |
currentCRC = 0;
|
|
|
264 |
}
|
|
|
265 |
else
|
|
|
266 |
{
|
|
|
267 |
currentCRC = CRC16(currentCRC, (unsigned char*) telegram, len);
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
// 1-0:1.8.1(000992.992*kWh)
|
|
|
271 |
// 1-0:1.8.1 = Elektra verbruik laag tarief (DSMR v4.0)
|
|
|
272 |
if (strncmp(telegram, "1-0:1.8.1", strlen("1-0:1.8.1")) == 0)
|
|
|
273 |
{
|
|
|
274 |
CONSUMPTION_LOW_TARIF = getValue(telegram, len, '(', '*');
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
// 1-0:1.8.2(000560.157*kWh)
|
|
|
278 |
// 1-0:1.8.2 = Elektra verbruik hoog tarief (DSMR v4.0)
|
|
|
279 |
if (strncmp(telegram, "1-0:1.8.2", strlen("1-0:1.8.2")) == 0)
|
|
|
280 |
{
|
|
|
281 |
CONSUMPTION_HIGH_TARIF = getValue(telegram, len, '(', '*');
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
// 1-0:2.8.1(000560.157*kWh)
|
|
|
285 |
// 1-0:2.8.1 = Elektra teruglevering laag tarief (DSMR v4.0)
|
|
|
286 |
if (strncmp(telegram, "1-0:2.8.1", strlen("1-0:2.8.1")) == 0)
|
|
|
287 |
{
|
|
|
288 |
RETURNDELIVERY_LOW_TARIF = getValue(telegram, len, '(', '*');
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
// 1-0:2.8.2(000560.157*kWh)
|
|
|
292 |
// 1-0:2.8.2 = Elektra teruglevering hoog tarief (DSMR v4.0)
|
|
|
293 |
if (strncmp(telegram, "1-0:2.8.2", strlen("1-0:2.8.2")) == 0)
|
|
|
294 |
{
|
|
|
295 |
RETURNDELIVERY_HIGH_TARIF = getValue(telegram, len, '(', '*');
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
// 1-0:1.7.0(00.424*kW) Actueel verbruik
|
|
|
299 |
// 1-0:1.7.x = Electricity consumption actual usage (DSMR v4.0)
|
|
|
300 |
if (strncmp(telegram, "1-0:1.7.0", strlen("1-0:1.7.0")) == 0)
|
|
|
301 |
{
|
|
|
302 |
ACTUAL_CONSUMPTION = getValue(telegram, len, '(', '*');
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
// 1-0:2.7.0(00.000*kW) Actuele teruglevering (-P) in 1 Watt resolution
|
|
|
306 |
if (strncmp(telegram, "1-0:2.7.0", strlen("1-0:2.7.0")) == 0)
|
|
|
307 |
{
|
|
|
308 |
ACTUAL_RETURNDELIVERY = getValue(telegram, len, '(', '*');
|
|
|
309 |
}
|
|
|
310 |
|
|
|
311 |
// 1-0:21.7.0(00.378*kW)
|
|
|
312 |
// 1-0:21.7.0 = Instantaan vermogen Elektriciteit levering L1
|
|
|
313 |
if (strncmp(telegram, "1-0:21.7.0", strlen("1-0:21.7.0")) == 0)
|
|
|
314 |
{
|
|
|
315 |
L1_INSTANT_POWER_USAGE = getValue(telegram, len, '(', '*');
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
// 1-0:41.7.0(00.378*kW)
|
|
|
319 |
// 1-0:41.7.0 = Instantaan vermogen Elektriciteit levering L2
|
|
|
320 |
if (strncmp(telegram, "1-0:41.7.0", strlen("1-0:41.7.0")) == 0)
|
|
|
321 |
{
|
|
|
322 |
L2_INSTANT_POWER_USAGE = getValue(telegram, len, '(', '*');
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
// 1-0:61.7.0(00.378*kW)
|
|
|
326 |
// 1-0:61.7.0 = Instantaan vermogen Elektriciteit levering L3
|
|
|
327 |
if (strncmp(telegram, "1-0:61.7.0", strlen("1-0:61.7.0")) == 0)
|
|
|
328 |
{
|
|
|
329 |
L3_INSTANT_POWER_USAGE = getValue(telegram, len, '(', '*');
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
// 1-0:31.7.0(002*A)
|
|
|
333 |
// 1-0:31.7.0 = Instantane stroom Elektriciteit L1
|
|
|
334 |
if (strncmp(telegram, "1-0:31.7.0", strlen("1-0:31.7.0")) == 0)
|
|
|
335 |
{
|
|
|
336 |
L1_INSTANT_POWER_CURRENT = getValue(telegram, len, '(', '*');
|
|
|
337 |
}
|
|
|
338 |
// 1-0:51.7.0(002*A)
|
|
|
339 |
// 1-0:51.7.0 = Instantane stroom Elektriciteit L2
|
|
|
340 |
if (strncmp(telegram, "1-0:51.7.0", strlen("1-0:51.7.0")) == 0)
|
|
|
341 |
{
|
|
|
342 |
L2_INSTANT_POWER_CURRENT = getValue(telegram, len, '(', '*');
|
|
|
343 |
}
|
|
|
344 |
// 1-0:71.7.0(002*A)
|
|
|
345 |
// 1-0:71.7.0 = Instantane stroom Elektriciteit L3
|
|
|
346 |
if (strncmp(telegram, "1-0:71.7.0", strlen("1-0:71.7.0")) == 0)
|
|
|
347 |
{
|
|
|
348 |
L3_INSTANT_POWER_CURRENT = getValue(telegram, len, '(', '*');
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
// 1-0:32.7.0(232.0*V)
|
|
|
352 |
// 1-0:32.7.0 = Voltage L1
|
|
|
353 |
if (strncmp(telegram, "1-0:32.7.0", strlen("1-0:32.7.0")) == 0)
|
|
|
354 |
{
|
|
|
355 |
L1_VOLTAGE = getValue(telegram, len, '(', '*');
|
|
|
356 |
}
|
|
|
357 |
// 1-0:52.7.0(232.0*V)
|
|
|
358 |
// 1-0:52.7.0 = Voltage L2
|
|
|
359 |
if (strncmp(telegram, "1-0:52.7.0", strlen("1-0:52.7.0")) == 0)
|
|
|
360 |
{
|
|
|
361 |
L2_VOLTAGE = getValue(telegram, len, '(', '*');
|
|
|
362 |
}
|
|
|
363 |
// 1-0:72.7.0(232.0*V)
|
|
|
364 |
// 1-0:72.7.0 = Voltage L3
|
|
|
365 |
if (strncmp(telegram, "1-0:72.7.0", strlen("1-0:72.7.0")) == 0)
|
|
|
366 |
{
|
|
|
367 |
L3_VOLTAGE = getValue(telegram, len, '(', '*');
|
|
|
368 |
}
|
|
|
369 |
|
|
|
370 |
// 0-1:24.2.1(150531200000S)(00811.923*m3)
|
|
|
371 |
// 0-1:24.2.1 = Gas (DSMR v4.0) on Kaifa MA105 meter
|
|
|
372 |
if (strncmp(telegram, "0-1:24.2.1", strlen("0-1:24.2.1")) == 0)
|
|
|
373 |
{
|
|
|
374 |
GAS_METER_M3 = getValue(telegram, len, '(', '*');
|
|
|
375 |
}
|
|
|
376 |
|
|
|
377 |
// 0-0:96.14.0(0001)
|
|
|
378 |
// 0-0:96.14.0 = Actual Tarif
|
|
|
379 |
if (strncmp(telegram, "0-0:96.14.0", strlen("0-0:96.14.0")) == 0)
|
|
|
380 |
{
|
|
|
381 |
ACTUAL_TARIF = getValue(telegram, len, '(', ')');
|
|
|
382 |
}
|
|
|
383 |
|
|
|
384 |
// 0-0:96.7.21(00003)
|
|
|
385 |
// 0-0:96.7.21 = Aantal onderbrekingen Elektriciteit
|
|
|
386 |
if (strncmp(telegram, "0-0:96.7.21", strlen("0-0:96.7.21")) == 0)
|
|
|
387 |
{
|
|
|
388 |
SHORT_POWER_OUTAGES = getValue(telegram, len, '(', ')');
|
|
|
389 |
}
|
|
|
390 |
|
|
|
391 |
// 0-0:96.7.9(00001)
|
|
|
392 |
// 0-0:96.7.9 = Aantal lange onderbrekingen Elektriciteit
|
|
|
393 |
if (strncmp(telegram, "0-0:96.7.9", strlen("0-0:96.7.9")) == 0)
|
|
|
394 |
{
|
|
|
395 |
LONG_POWER_OUTAGES = getValue(telegram, len, '(', ')');
|
|
|
396 |
}
|
|
|
397 |
|
|
|
398 |
// 1-0:32.32.0(00000)
|
|
|
399 |
// 1-0:32.32.0 = Aantal korte spanningsdalingen Elektriciteit in fase 1
|
|
|
400 |
if (strncmp(telegram, "1-0:32.32.0", strlen("1-0:32.32.0")) == 0)
|
|
|
401 |
{
|
|
|
402 |
SHORT_POWER_DROPS = getValue(telegram, len, '(', ')');
|
|
|
403 |
}
|
|
|
404 |
|
|
|
405 |
// 1-0:32.36.0(00000)
|
|
|
406 |
// 1-0:32.36.0 = Aantal korte spanningsstijgingen Elektriciteit in fase 1
|
|
|
407 |
if (strncmp(telegram, "1-0:32.36.0", strlen("1-0:32.36.0")) == 0)
|
|
|
408 |
{
|
|
|
409 |
SHORT_POWER_PEAKS = getValue(telegram, len, '(', ')');
|
|
|
410 |
}
|
|
|
411 |
|
|
|
412 |
return validCRCFound;
|
|
|
413 |
}
|
|
|
414 |
|
|
|
415 |
void read_p1_hardwareserial()
|
|
|
416 |
{
|
|
|
417 |
if (Serial.available())
|
|
|
418 |
{
|
|
|
419 |
memset(telegram, 0, sizeof(telegram));
|
|
|
420 |
|
|
|
421 |
while (Serial.available())
|
|
|
422 |
{
|
|
|
423 |
ESP.wdtDisable();
|
|
|
424 |
int len = Serial.readBytesUntil('\n', telegram, P1_MAXLINELENGTH);
|
|
|
425 |
ESP.wdtEnable(1);
|
|
|
426 |
|
|
|
427 |
processLine(len);
|
|
|
428 |
}
|
|
|
429 |
}
|
|
|
430 |
}
|
|
|
431 |
|
|
|
432 |
void processLine(int len) {
|
|
|
433 |
telegram[len] = '\n';
|
|
|
434 |
telegram[len + 1] = 0;
|
|
|
435 |
yield();
|
|
|
436 |
|
|
|
437 |
bool result = decode_telegram(len + 1);
|
|
|
438 |
if (result) {
|
|
|
439 |
send_data_to_broker();
|
|
|
440 |
LAST_UPDATE_SENT = millis();
|
|
|
441 |
}
|
|
|
442 |
}
|
|
|
443 |
|
|
|
444 |
// **********************************
|
|
|
445 |
// * EEPROM helpers *
|
|
|
446 |
// **********************************
|
|
|
447 |
|
|
|
448 |
String read_eeprom(int offset, int len)
|
|
|
449 |
{
|
|
|
450 |
Serial.print(F("read_eeprom()"));
|
|
|
451 |
|
|
|
452 |
String res = "";
|
|
|
453 |
for (int i = 0; i < len; ++i)
|
|
|
454 |
{
|
|
|
455 |
res += char(EEPROM.read(i + offset));
|
|
|
456 |
}
|
|
|
457 |
return res;
|
|
|
458 |
}
|
|
|
459 |
|
|
|
460 |
void write_eeprom(int offset, int len, String value)
|
|
|
461 |
{
|
|
|
462 |
Serial.println(F("write_eeprom()"));
|
|
|
463 |
for (int i = 0; i < len; ++i)
|
|
|
464 |
{
|
|
|
465 |
if ((unsigned)i < value.length())
|
|
|
466 |
{
|
|
|
467 |
EEPROM.write(i + offset, value[i]);
|
|
|
468 |
}
|
|
|
469 |
else
|
|
|
470 |
{
|
|
|
471 |
EEPROM.write(i + offset, 0);
|
|
|
472 |
}
|
|
|
473 |
}
|
|
|
474 |
}
|
|
|
475 |
|
|
|
476 |
// ******************************************
|
|
|
477 |
// * Callback for saving WIFI config *
|
|
|
478 |
// ******************************************
|
|
|
479 |
|
|
|
480 |
bool shouldSaveConfig = false;
|
|
|
481 |
|
|
|
482 |
// * Callback notifying us of the need to save config
|
|
|
483 |
void save_wifi_config_callback ()
|
|
|
484 |
{
|
|
|
485 |
Serial.println(F("Should save config"));
|
|
|
486 |
shouldSaveConfig = true;
|
|
|
487 |
}
|
|
|
488 |
|
|
|
489 |
// **********************************
|
|
|
490 |
// * Setup OTA *
|
|
|
491 |
// **********************************
|
|
|
492 |
|
|
|
493 |
void setup_ota()
|
|
|
494 |
{
|
|
|
495 |
Serial.println(F("Arduino OTA activated."));
|
|
|
496 |
|
|
|
497 |
// * Port defaults to 8266
|
|
|
498 |
ArduinoOTA.setPort(8266);
|
|
|
499 |
|
|
|
500 |
// * Set hostname for OTA
|
|
|
501 |
ArduinoOTA.setHostname(HOSTNAME);
|
|
|
502 |
ArduinoOTA.setPassword(OTA_PASSWORD);
|
|
|
503 |
|
|
|
504 |
ArduinoOTA.onStart([]()
|
|
|
505 |
{
|
|
|
506 |
Serial.println(F("Arduino OTA: Start"));
|
|
|
507 |
});
|
|
|
508 |
|
|
|
509 |
ArduinoOTA.onEnd([]()
|
|
|
510 |
{
|
|
|
511 |
Serial.println(F("Arduino OTA: End (Running reboot)"));
|
|
|
512 |
});
|
|
|
513 |
|
|
|
514 |
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total)
|
|
|
515 |
{
|
|
|
516 |
Serial.printf("Arduino OTA Progress: %u%%\r", (progress / (total / 100)));
|
|
|
517 |
});
|
|
|
518 |
|
|
|
519 |
ArduinoOTA.onError([](ota_error_t error)
|
|
|
520 |
{
|
|
|
521 |
Serial.printf("Arduino OTA Error[%u]: ", error);
|
|
|
522 |
if (error == OTA_AUTH_ERROR)
|
|
|
523 |
Serial.println(F("Arduino OTA: Auth Failed"));
|
|
|
524 |
else if (error == OTA_BEGIN_ERROR)
|
|
|
525 |
Serial.println(F("Arduino OTA: Begin Failed"));
|
|
|
526 |
else if (error == OTA_CONNECT_ERROR)
|
|
|
527 |
Serial.println(F("Arduino OTA: Connect Failed"));
|
|
|
528 |
else if (error == OTA_RECEIVE_ERROR)
|
|
|
529 |
Serial.println(F("Arduino OTA: Receive Failed"));
|
|
|
530 |
else if (error == OTA_END_ERROR)
|
|
|
531 |
Serial.println(F("Arduino OTA: End Failed"));
|
|
|
532 |
});
|
|
|
533 |
|
|
|
534 |
ArduinoOTA.begin();
|
|
|
535 |
Serial.println(F("Arduino OTA finished"));
|
|
|
536 |
}
|
|
|
537 |
|
|
|
538 |
// **********************************
|
|
|
539 |
// * Setup MDNS discovery service *
|
|
|
540 |
// **********************************
|
|
|
541 |
|
|
|
542 |
void setup_mdns()
|
|
|
543 |
{
|
|
|
544 |
Serial.println(F("Starting MDNS responder service"));
|
|
|
545 |
|
|
|
546 |
bool mdns_result = MDNS.begin(HOSTNAME);
|
|
|
547 |
if (mdns_result)
|
|
|
548 |
{
|
|
|
549 |
MDNS.addService("http", "tcp", 80);
|
|
|
550 |
}
|
|
|
551 |
}
|
|
|
552 |
|
|
|
553 |
// **********************************
|
|
|
554 |
// * Setup Main *
|
|
|
555 |
// **********************************
|
|
|
556 |
|
|
|
557 |
void setup()
|
|
|
558 |
{
|
|
|
559 |
// * Configure EEPROM
|
|
|
560 |
EEPROM.begin(512);
|
|
|
561 |
|
|
|
562 |
// Setup a hw serial connection for communication with the P1 meter and logging (not using inversion)
|
|
|
563 |
Serial.begin(BAUD_RATE, SERIAL_8N1, SERIAL_FULL);
|
|
|
564 |
Serial.setRxBufferSize(1024);
|
|
|
565 |
Serial.println("");
|
|
|
566 |
Serial.println("Swapping UART0 RX to inverted");
|
|
|
567 |
Serial.flush();
|
|
|
568 |
|
|
|
569 |
// Invert the RX serialport by setting a register value, this way the TX might continue normally allowing the serial monitor to read println's
|
|
|
570 |
USC0(UART0) = USC0(UART0) | BIT(UCRXI);
|
|
|
571 |
Serial.println("Serial port is ready to recieve.");
|
|
|
572 |
|
|
|
573 |
// * Set led pin as output
|
|
|
574 |
pinMode(LED_BUILTIN, OUTPUT);
|
|
|
575 |
|
|
|
576 |
// * Start ticker with 0.5 because we start in AP mode and try to connect
|
|
|
577 |
ticker.attach(0.6, tick);
|
|
|
578 |
|
|
|
579 |
// * Get MQTT Server settings
|
|
|
580 |
String settings_available = read_eeprom(134, 1);
|
|
|
581 |
|
|
|
582 |
if (settings_available == "1")
|
|
|
583 |
{
|
|
|
584 |
read_eeprom(0, 64).toCharArray(MQTT_HOST, 64); // * 0-63
|
|
|
585 |
read_eeprom(64, 6).toCharArray(MQTT_PORT, 6); // * 64-69
|
|
|
586 |
read_eeprom(70, 32).toCharArray(MQTT_USER, 32); // * 70-101
|
|
|
587 |
read_eeprom(102, 32).toCharArray(MQTT_PASS, 32); // * 102-133
|
|
|
588 |
}
|
|
|
589 |
|
|
|
590 |
WiFiManagerParameter CUSTOM_MQTT_HOST("host", "MQTT hostname", MQTT_HOST, 64);
|
|
|
591 |
WiFiManagerParameter CUSTOM_MQTT_PORT("port", "MQTT port", MQTT_PORT, 6);
|
|
|
592 |
WiFiManagerParameter CUSTOM_MQTT_USER("user", "MQTT user", MQTT_USER, 32);
|
|
|
593 |
WiFiManagerParameter CUSTOM_MQTT_PASS("pass", "MQTT pass", MQTT_PASS, 32);
|
|
|
594 |
|
|
|
595 |
// * WiFiManager local initialization. Once its business is done, there is no need to keep it around
|
|
|
596 |
WiFiManager wifiManager;
|
|
|
597 |
|
|
|
598 |
// * Reset settings - uncomment for testing
|
|
|
599 |
// wifiManager.resetSettings();
|
|
|
600 |
|
|
|
601 |
// * Set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
|
|
|
602 |
wifiManager.setAPCallback(configModeCallback);
|
|
|
603 |
|
|
|
604 |
// * Set timeout
|
|
|
605 |
wifiManager.setConfigPortalTimeout(WIFI_TIMEOUT);
|
|
|
606 |
|
|
|
607 |
// * Set save config callback
|
|
|
608 |
wifiManager.setSaveConfigCallback(save_wifi_config_callback);
|
|
|
609 |
|
|
|
610 |
// * Add all your parameters here
|
|
|
611 |
wifiManager.addParameter(&CUSTOM_MQTT_HOST);
|
|
|
612 |
wifiManager.addParameter(&CUSTOM_MQTT_PORT);
|
|
|
613 |
wifiManager.addParameter(&CUSTOM_MQTT_USER);
|
|
|
614 |
wifiManager.addParameter(&CUSTOM_MQTT_PASS);
|
|
|
615 |
|
|
|
616 |
// * Fetches SSID and pass and tries to connect
|
|
|
617 |
// * Reset when no connection after 10 seconds
|
|
|
618 |
if (!wifiManager.autoConnect())
|
|
|
619 |
{
|
|
|
620 |
Serial.println(F("Failed to connect to WIFI and hit timeout"));
|
|
|
621 |
|
|
|
622 |
// * Reset and try again, or maybe put it to deep sleep
|
|
|
623 |
ESP.reset();
|
|
|
624 |
delay(WIFI_TIMEOUT);
|
|
|
625 |
}
|
|
|
626 |
|
|
|
627 |
// * Read updated parameters
|
|
|
628 |
strcpy(MQTT_HOST, CUSTOM_MQTT_HOST.getValue());
|
|
|
629 |
strcpy(MQTT_PORT, CUSTOM_MQTT_PORT.getValue());
|
|
|
630 |
strcpy(MQTT_USER, CUSTOM_MQTT_USER.getValue());
|
|
|
631 |
strcpy(MQTT_PASS, CUSTOM_MQTT_PASS.getValue());
|
|
|
632 |
|
|
|
633 |
// * Save the custom parameters to FS
|
|
|
634 |
if (shouldSaveConfig)
|
|
|
635 |
{
|
|
|
636 |
Serial.println(F("Saving WiFiManager config"));
|
|
|
637 |
|
|
|
638 |
write_eeprom(0, 64, MQTT_HOST); // * 0-63
|
|
|
639 |
write_eeprom(64, 6, MQTT_PORT); // * 64-69
|
|
|
640 |
write_eeprom(70, 32, MQTT_USER); // * 70-101
|
|
|
641 |
write_eeprom(102, 32, MQTT_PASS); // * 102-133
|
|
|
642 |
write_eeprom(134, 1, "1"); // * 134 --> always "1"
|
|
|
643 |
EEPROM.commit();
|
|
|
644 |
}
|
|
|
645 |
|
|
|
646 |
// * If you get here you have connected to the WiFi
|
|
|
647 |
Serial.println(F("Connected to WIFI..."));
|
|
|
648 |
|
|
|
649 |
// * Keep LED on
|
|
|
650 |
ticker.detach();
|
|
|
651 |
digitalWrite(LED_BUILTIN, LOW);
|
|
|
652 |
|
|
|
653 |
// * Configure OTA
|
|
|
654 |
setup_ota();
|
|
|
655 |
|
|
|
656 |
// * Startup MDNS Service
|
|
|
657 |
setup_mdns();
|
|
|
658 |
|
|
|
659 |
// * Setup MQTT
|
|
|
660 |
Serial.printf("MQTT connecting to: %s:%s\n", MQTT_HOST, MQTT_PORT);
|
|
|
661 |
|
|
|
662 |
mqtt_client.setServer(MQTT_HOST, atoi(MQTT_PORT));
|
|
|
663 |
|
|
|
664 |
}
|
|
|
665 |
|
|
|
666 |
// **********************************
|
|
|
667 |
// * Loop *
|
|
|
668 |
// **********************************
|
|
|
669 |
|
|
|
670 |
void loop()
|
|
|
671 |
{
|
|
|
672 |
ArduinoOTA.handle();
|
|
|
673 |
long now = millis();
|
|
|
674 |
|
|
|
675 |
if (!mqtt_client.connected())
|
|
|
676 |
{
|
|
|
677 |
if (now - LAST_RECONNECT_ATTEMPT > 5000)
|
|
|
678 |
{
|
|
|
679 |
LAST_RECONNECT_ATTEMPT = now;
|
|
|
680 |
|
|
|
681 |
if (mqtt_reconnect())
|
|
|
682 |
{
|
|
|
683 |
LAST_RECONNECT_ATTEMPT = 0;
|
|
|
684 |
}
|
|
|
685 |
}
|
|
|
686 |
}
|
|
|
687 |
else
|
|
|
688 |
{
|
|
|
689 |
mqtt_client.loop();
|
|
|
690 |
}
|
|
|
691 |
|
|
|
692 |
if (now - LAST_UPDATE_SENT > UPDATE_INTERVAL) {
|
|
|
693 |
read_p1_hardwareserial();
|
|
|
694 |
}
|
|
|
695 |
}
|