-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyMeter.ino
616 lines (525 loc) · 16.3 KB
/
MyMeter.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/***************************************************************************
MyMeter energy or gas tracking with Wemos D1 mini and a
MicroWakeupper battery shield with reed switch or proximity sensor
(see README.md for setup/installation)
(c) 2022, 2023 @tstoegi, Tobias Stöger , MIT license
***************************************************************************/
#include <Arduino.h>
#include <Credentials.h> // located (or create a new one) in folder "Arduino/libraries/Credentials/"
/*** example of a Credentials.h file
// your wifi
#define CR_WIFI_SSID "wifi_ssid"
#define CR_WIFI_PASSWORD "wifi_password"
// ota - over the air firmware updates - userid and password of your choise
#define CR_OTA_MYMETER_CLIENT_PASSWORD "0123456789"
// your mqtt server cert fingerprint -> use "" if you want to disable cert checking
#define CR_MQTT_BROKER_CERT_FINGERPRINT "AA F0 DE 66 E7 22 98 02 12 1D 59 08 4B 32 23 24 C9 F4 D1 00"
// your mqtt user and password as created on the server side
#define CR_MQTT_BROKER_MYMETER_USER "mymeter"
#define CR_MQTT_BROKER_MYMETER_PASSWORD "0123456789"
elpmaxe ***/
#define versionString "0.6.20230201.1"
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <PubSubClient.h>
#include <ArduinoOTA.h>
#include "config.h" // located in the sketch folder - edit the file and define your settings
const char *pubTopic = CO_MQTT_TOPIC_MAIN_FOLDER_PUB CO_MYMETER_NAME;
const char *subTopic = CO_MQTT_TOPIC_MAIN_FOLDER_PUB CO_MYMETER_NAME CO_MQTT_TOPIC_SUB_FOLDER_SUB;
#if debug == true
#define Log(str) \
Serial.print(__LINE__); \
Serial.print(' '); \
Serial.print(millis()); \
Serial.print(": "); \
Serial.print(__PRETTY_FUNCTION__); \
Serial.print(' '); \
Serial.println(str);
#define Logf(str, param) \
Serial.print(__LINE__); \
Serial.print(' '); \
Serial.print(millis()); \
Serial.print(": "); \
Serial.print(__PRETTY_FUNCTION__); \
Serial.print(' '); \
Serial.printf(str, param);
#else
#define Log(str)
#define Logf(str, param)
#endif
#include <MicroWakeupper.h>
MicroWakeupper microWakeupper; //MicroWakeupper instance (only one is supported!)
bool launchedByMicroWakeupperEvent = false;
WiFiClientSecure espClient;
PubSubClient mqttClient(espClient);
char msg[50];
const long one_turnaround = 10; // For gas: one complete round 10 liter of gas (or 0.01 m3)
struct MyCounter {
long total;
};
MyCounter myCounter = { 0 }; // initial value can be set via mqtt retain message - see readme
// we store the new value in EEPROM always at currentEEPROMAddress+1 to spread max write-life-cycles of the whole EEPROM - at the end we start from 0 again
int sizeofMyCounterLong = 10;
int currentEEPROMAddress = 0; // first address we try to read a valid value
#define MAGIC_BYTE '#' // start mark of our value in EEPROM "#1234567890" or "# 14567800" (max 10 digits)
#define EEPROM_SIZE_BYTES_MAX 512
long rssi = 0; // wifi signal strength
bool turningOff = true;
float voltageCalibration = 0.0;
enum State {
state_startup = 0,
state_setupWifi = 1,
state_setupMqtt = 3,
state_receiveMqtt = 4,
state_checkSensorData = 5,
state_sendMqtt = 6,
state_idle = 7,
state_turningOff = 8,
state_turnedOff = 9
} nextState;
bool otaEnabled = false;
bool mqttAvailable = false;
int nextStateDelaySeconds = 0;
unsigned long startTime;
void mqttPublish(const char *mainTopic, const char *subTopic, String msg) {
String topicString = String(mainTopic) + "/" + String(subTopic);
mqttClient.publish(topicString.c_str(), msg.c_str(), true);
Log(">> Published message: ");
Log(topicString);
Log(" ");
Log(msg);
mqttClient.loop();
}
void setup() {
startTime = millis();
Serial.begin(115200);
EEPROM.begin(EEPROM_SIZE_BYTES_MAX); // mandator for esp8266 (because EEPROM is emulated for FLASH)
microWakeupper.begin(); // For correct initialisation
delay(100);
Serial.println("\n*** MyMeter Monitoring ***");
Serial.println(versionString);
nextState = state_startup;
nextStateDelaySeconds = 0;
}
void loop() {
if (otaEnabled) {
ArduinoOTA.handle();
}
static unsigned long prevMillis = 0;
if (millis() - prevMillis >= nextStateDelaySeconds * 1000) {
prevMillis = millis();
doNextState(nextState);
}
delay(100);
}
void setNextState(State aNextState, int aNextStateDelaySeconds = 0) {
nextState = aNextState;
nextStateDelaySeconds = aNextStateDelaySeconds;
}
void doNextState(State aNewState) {
Log("___ doNextState() ___: ");
Log(aNewState);
switch (aNewState) {
case state_startup:
{
Log("state_startup");
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, true); // turn led off
findLastEEPROMAddress();
loadFromEEPROM();
if (microWakeupper.resetedBySwitch() && microWakeupper.isActive()) {
Log("Launched by a MicroWakeupperEvent");
launchedByMicroWakeupperEvent = true;
microWakeupper.disable(); // Preventing new triggering/resets
}
setNextState(state_setupWifi);
break;
}
case state_setupWifi:
{
Log("state_setupWifi");
if (!setupWifi()) {
setNextState(state_checkSensorData);
break;
}
setNextState(state_setupMqtt);
break;
}
case state_setupMqtt:
{
Log("state_setupMqtt");
setupMqtt();
if (!mqttReconnect()) {
setNextState(state_checkSensorData);
break;
}
setNextState(state_receiveMqtt);
break;
}
case state_receiveMqtt:
{
Log("state_receiveMqtt");
// we process all retained mqtt messages (in callback)
for (int i = 0; i < 50; i++) { // todo Hack - if more incoming messages are queued
mqttClient.loop();
delay(10);
yield();
}
setNextState(state_checkSensorData);
}
case state_checkSensorData:
{
Log("state_checkSensorData");
if (launchedByMicroWakeupperEvent) {
Log(myCounter.total);
myCounter.total = myCounter.total + one_turnaround;
Log(one_turnaround);
Log(myCounter.total);
setNextState(state_sendMqtt);
} else {
setNextState(state_turningOff);
}
}
case state_sendMqtt:
{
Log("state_sendMqtt");
if (mqttAvailable) {
mqttPublish(pubTopic, "total", String(myCounter.total / 1000.0f));
mqttPublish(pubTopic, "wifi_rssi", String(rssi));
mqttPublish(pubTopic, "batteryVoltage", String(microWakeupper.readVBatt() + voltageCalibration));
#if debug == true
mqttPublish(pubTopic, "version", versionString);
mqttPublish(pubTopic, "localIP", WiFi.localIP().toString());
#endif
}
setNextState(state_turningOff);
break;
}
case state_idle:
{
Log("state_idle (until next manual restart or external reset)"); // can be used for OTA updates
digitalWrite(LED_BUILTIN, false);
delay(10);
digitalWrite(LED_BUILTIN, true);
delay(1000);
static unsigned long prevMillis = millis();
if (millis() - prevMillis >= 60 * 1000) {
Log("OTA timed out!");
turningOff = true;
setNextState(state_turningOff);
}
break;
}
case state_turningOff:
{
if (!turningOff) {
setNextState(state_idle);
break;
}
Log("state_turningOff");
digitalWrite(LED_BUILTIN, true); // turn led off
increaseEEPROMAddress();
storeToEEPROM();
EEPROM.end();
Serial.println();
Serial.println(millis() - startTime);
Serial.println();
Log("Waiting for turning device off (only if jumper J1 on MicroWakeupperShield is cutted!)");
microWakeupper.reenable();
delay(5000); // time for the MicroWakeupper to power off the wemos
deepSleep(); // if the MicroWakeupper Switch is still in an ON state, we try to sleep
delay(1000);
setNextState(state_turnedOff);
break;
}
case state_turnedOff:
{
Log("state_turnedOff");
delay(1000);
break;
}
default:
{
Log("!!! unknown status !!!");
}
}
if (nextState > state_setupMqtt && nextState < state_turningOff) {
if (!mqttClient.loop()) { //needs to be called regularly
mqttReconnect();
}
}
}
// int32_t getWiFiChannel(const char* ssid) {
// if (int32_t n = WiFi.scanNetworks()) {
// for (uint8_t i = 0; i < n; i++) {
// if (!strcmp(ssid, WiFi.SSID(i).c_str())) {
// return WiFi.channel(i);
// }
// }
// }
// return 0;
// }
bool setupWifi() {
WiFi.forceSleepWake();
WiFi.persistent(false);
WiFi.mode(WIFI_STA); // <<< Station
//WiFi.setPhyMode(WIFI_PHY_MODE_11G);
//WiFi.setOutputPower(20.5); // max
delay(100);
#ifdef STATIC_IP
WiFi.config(ip, gateway, subnet, dns);
#endif
// Wait for connection
int retries = 2;
while (WiFi.status() != WL_CONNECTED && retries > 0) {
#ifdef STATIC_WIFI
Log("Trying WiFi channel id: ");
Log(channel);
Log("Trying WiFi BSSID: ");
Log(bssid);
WiFi.begin(CR_WIFI_SSID, CR_WIFI_PASSWORD, channel, bssid);
#else
WiFi.begin(CR_WIFI_SSID, CR_WIFI_PASSWORD);
#endif
Log(retries);
int secondsTimeout = 10;
while (WiFi.status() != WL_CONNECTED && secondsTimeout > 0) {
yield();
delay(1000);
Log(".");
secondsTimeout--;
}
retries--;
Log();
}
if (retries == 0) {
Log(CR_WIFI_SSID);
Log("Connection failed!");
return false;
}
Log("");
Log("Connected to ");
Log(CR_WIFI_SSID);
Log("IP address: ");
Log(WiFi.localIP());
Log("BSSID: ");
Log(WiFi.BSSIDstr());
rssi = WiFi.RSSI();
return true;
}
void setupOTA() {
String localIPWithoutDots = WiFi.localIP().toString();
localIPWithoutDots.replace(".", "_");
String ota_client_id = CO_MYMETER_NAME + localIPWithoutDots;
Log("OTA_CLIENT_ID: ");
Log(ota_client_id);
ArduinoOTA.setHostname(ota_client_id.c_str());
ArduinoOTA.setPassword(CR_OTA_MYMETER_CLIENT_PASSWORD);
ArduinoOTA.onStart([]() {
Log("Start");
});
ArduinoOTA.onEnd([]() {
Log("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Logf("Progress: %u%%\r", (progress / (total / 100)))
});
ArduinoOTA.onError([](ota_error_t error) {
Logf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Log("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Log("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Log("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Log("Receive Failed");
} else if (error == OTA_END_ERROR) {
Log("End Failed");
}
});
ArduinoOTA.begin();
Log("OTA ready");
}
void setupMqtt() {
if (strlen(CR_MQTT_BROKER_CERT_FINGERPRINT) > 0) {
Log("Setting mqtt server fingerprint");
espClient.setFingerprint(CR_MQTT_BROKER_CERT_FINGERPRINT);
} else {
Log("No fingerprint verification for mqtt");
espClient.setInsecure();
}
mqttClient.setBufferSize(512);
mqttClient.setServer(CO_MQTT_BROKER_IP, CO_MQTT_BROKER_PORT);
mqttClient.setCallback(mqttCallback);
}
bool mqttReconnect() {
// Loop until we're reconnected
int maxRetries = 2;
int retries = 0;
while (!mqttClient.connected() && retries < maxRetries) {
Log("Attempting MQTT connection...");
// Create a random and unique client ID for mqtt
String clientId = CO_MYMETER_NAME + WiFi.localIP().toString();
Log("MQTT_CLIENT_ID: ");
Log(clientId);
// Attempt to connect
if (mqttClient.connect(clientId.c_str(), CR_MQTT_BROKER_MYMETER_USER, CR_MQTT_BROKER_MYMETER_PASSWORD)) {
Log("connected");
String subTopicStr = String(subTopic) + "/#";
mqttClient.subscribe(subTopicStr.c_str());
Log("mqtt subscription topic: ");
Log(subTopicStr.c_str());
mqttClient.loop();
mqttAvailable = true;
return true;
} else {
Log("failed, rc=");
Log(mqttClient.state());
Log(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
retries++;
}
}
mqttAvailable = false;
return false;
}
void mqttCallback(char *topic, byte *payload, unsigned int length) {
Log();
Log("<< Received message: ");
Log(topic);
Log(" ");
char buff_p[length];
for (int i = 0; i < length; i++) {
buff_p[i] = (char)payload[i];
}
buff_p[length] = '\0';
String str_payload = String(buff_p);
Log(str_payload);
String subTopicKey = "total";
if (String(topic).endsWith(subTopicKey)) {
float newValue = str_payload.toFloat();
if (newValue > 0) {
Log("Override value total: ");
Log(newValue);
myCounter.total = newValue * 1000.0f;
formatEEPROM();
currentEEPROMAddress = 0;
storeToEEPROM();
Log(">>>");
Log(String(myCounter.total / 1000.0f));
mqttPublish(pubTopic, subTopicKey.c_str(), String(myCounter.total / 1000.0f));
mqttPublish(subTopic, subTopicKey.c_str(), ""); // delete the retained mqtt message
}
return;
}
subTopicKey = "waitForOTA";
if (String(topic).endsWith(subTopicKey)) {
if (str_payload.startsWith("true")
|| str_payload.startsWith("yes")) {
Log("Disabling temporarly turningOff/deepSleep");
turningOff = false;
setupOTA();
otaEnabled = true;
mqttPublish(subTopic, subTopicKey.c_str(), ""); // delete the retained mqtt message
}
return;
}
subTopicKey = "voltageCalibration";
if (String(topic).endsWith(subTopicKey)) {
float newValue = str_payload.toFloat();
Log("Calibrating voltage: ");
Log(newValue);
voltageCalibration = newValue;
if (newValue == 0.0) {
mqttPublish(subTopic, subTopicKey.c_str(), ""); // delete the retained mqtt message
}
return;
}
}
void deepSleep() {
mqttClient.disconnect();
delay(100);
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
delay(1);
WiFi.forceSleepBegin();
ESP.deepSleep(0, RF_DISABLED); // sleep "forever"
delay(100); // Seems recommended after calling deepSleep
}
void formatEEPROM() {
Log("formatEEPROM");
for (int i = 0; i < EEPROM_SIZE_BYTES_MAX; i++) {
EEPROM.write(i, 255);
char value = EEPROM.read(i);
if (value != 255) {
Log("EEPROM CHECK FAILED @");
Log(i);
}
}
}
void findLastEEPROMAddress() {
Log("### EEPROM DUMP ###");
for (; currentEEPROMAddress < EEPROM_SIZE_BYTES_MAX; currentEEPROMAddress++) {
// read a byte from the current address of the EEPROM
#if debug == true
char value = EEPROM[currentEEPROMAddress];
Log(currentEEPROMAddress);
Log("\t");
Log(value);
Log();
#endif
// read until we find something different as MAGIC_BYTE
// e.g. ########47110815#####...
// ^
if (EEPROM[currentEEPROMAddress] != MAGIC_BYTE) {
currentEEPROMAddress--; // skip one byte back for start write offset
break;
}
}
if (currentEEPROMAddress < 0 || currentEEPROMAddress >= EEPROM_SIZE_BYTES_MAX) { // empty/new EEPROM
currentEEPROMAddress = -1; // will be set to 0 in increaseEEPROMAddress
}
Log("Current EEPROM address (-1 -> nothing found): ");
Log(currentEEPROMAddress);
}
void increaseEEPROMAddress() {
currentEEPROMAddress++;
if (currentEEPROMAddress >= EEPROM_SIZE_BYTES_MAX - sizeofMyCounterLong) {
// we start from the beginning
currentEEPROMAddress = 0;
}
Log("New EEPROM address: ");
Log(currentEEPROMAddress);
}
void storeToEEPROM() {
Log("storeToEEPROM MyCounter: ");
Log(myCounter.total);
EEPROM.put(currentEEPROMAddress, MAGIC_BYTE);
char buffer[10];
sprintf(buffer, "%10lu", myCounter.total);
EEPROM.put(currentEEPROMAddress + 1, buffer);
EEPROM.commit();
}
void loadFromEEPROM() {
Log("Read EEPROM address: ");
Log(currentEEPROMAddress);
char magicByteRead = EEPROM.read(currentEEPROMAddress);
Log(magicByteRead);
if (magicByteRead != MAGIC_BYTE) {
Log("No MAGIC_BYTE found at ");
Log(currentEEPROMAddress);
} else {
char buffer[10];
EEPROM.get(currentEEPROMAddress + 1, buffer);
myCounter.total = String(buffer).toInt(); // returns long
Log("loadFromEEPROM MyCounter: ");
Log(myCounter.total);
}
if (myCounter.total < 0 || isnan(myCounter.total)) {
Log("!!! Resetting myCounter.total to 0");
myCounter.total = 0;
}
}