forked from neutrinus/everblu-meters
-
Notifications
You must be signed in to change notification settings - Fork 17
/
everblu-meters-esp8266.ino
313 lines (254 loc) · 8.87 KB
/
everblu-meters-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
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
#include <ArduinoOTA.h>
#include "everblu_meters.h"
// Project source :
// http://www.lamaisonsimon.fr/wiki/doku.php?id=maison2:compteur_d_eau:compteur_d_eau
// Require EspMQTTClient library (by Patrick Lapointe) version 1.13.3
// Install from Arduino library manager (and its dependancies)
// https://github.com/plapointe6/EspMQTTClient/releases/tag/1.13.3
#include "EspMQTTClient.h"
// Edit "everblu_meters.h" file then change the define at the end of the file
#ifndef LED_BUILTIN
// Change this pin if needed
#define LED_BUILTIN 2
#endif
EspMQTTClient mqtt(
"MyESSID", // Your Wifi SSID
"MyWiFiKey", // Your WiFi key
"mqtt.server.com", // MQTT Broker server ip
"MQTTUsername", // Can be omitted if not needed
"MQTTPassword", // Can be omitted if not needed
"EverblueCyble", // Client name that uniquely identify your device
1883 // MQTT Broker server port
);
char *jsonTemplate =
"{ \
\"liters\": %d, \
\"counter\" : %d, \
\"battery\" : %d, \
\"timestamp\" : \"%s\"\
}";
int _retry = 0;
void onUpdateData()
{
struct tmeter_data meter_data;
meter_data = get_meter_data();
time_t tnow = time(nullptr);
struct tm *ptm = gmtime(&tnow);
Serial.printf("Current date (UTC) : %04d/%02d/%02d %02d:%02d:%02d - %s\n", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, String(tnow, DEC).c_str());
char iso8601[128];
strftime(iso8601, sizeof iso8601, "%FT%TZ", gmtime(&tnow));
if (meter_data.reads_counter == 0 || meter_data.liters == 0) {
Serial.println("Unable to retrieve data from meter. Retry later...");
// Call back this function in 10 sec (in miliseconds)
if (_retry++ < 10)
mqtt.executeDelayed(1000 * 10, onUpdateData);
return;
}
digitalWrite(LED_BUILTIN, LOW); // turned on
Serial.printf("Liters : %d\nBattery (in months) : %d\nCounter : %d\n\n", meter_data.liters, meter_data.battery_left, meter_data.reads_counter);
mqtt.publish("everblu/cyble/liters", String(meter_data.liters, DEC), true);
delay(50); // Do not remove
mqtt.publish("everblu/cyble/counter", String(meter_data.reads_counter, DEC), true);
delay(50); // Do not remove
mqtt.publish("everblu/cyble/battery", String(meter_data.battery_left, DEC), true);
delay(50); // Do not remove
mqtt.publish("everblu/cyble/timestamp", iso8601, true); // timestamp since epoch in UTC
delay(50); // Do not remove
char json[512];
sprintf(json, jsonTemplate, meter_data.liters, meter_data.reads_counter, meter_data.battery_left, iso8601);
mqtt.publish("everblu/cyble/json", json, true); // send all data as a json message
}
// This function calls onUpdateData() every days at 10:00am UTC
void onScheduled()
{
time_t tnow = time(nullptr);
struct tm *ptm = gmtime(&tnow);
// At 10:00:00am UTC
if (ptm->tm_hour == 10 && ptm->tm_min == 0 && ptm->tm_sec == 0) {
// Call back in 23 hours
mqtt.executeDelayed(1000 * 60 * 60 * 23, onScheduled);
Serial.println("It is time to update data from meter :)");
// Update data
_retry = 0;
onUpdateData();
return;
}
// Every 500 ms
mqtt.executeDelayed(500, onScheduled);
}
String jsonDiscoveryDevice1(
"{ \
\"name\": \"Compteur Eau Index\", \
\"unique_id\": \"water_meter_value\",\
\"object_id\": \"water_meter_value\",\
\"icon\": \"mdi:water\",\
\"state\": \"{{ states(sensor.water_meter_value)|float / 1 }}\",\
\"unit_of_measurement\": \"L\",\
\"device_class\": \"water\",\
\"state_class\": \"total_increasing\",\
\"qos\": \"0\",\
\"state_topic\": \"everblu/cyble/liters\",\
\"force_update\": \"true\",\
\"device\" : {\
\"identifiers\" : [\
\"14071984\" ],\
\"name\": \"Compteur Eau\",\
\"model\": \"Everblu Cyble ESP8266/ESP32\",\
\"manufacturer\": \"Psykokwak\",\
\"suggested_area\": \"Home\"}\
}");
String jsonDiscoveryDevice2(
"{ \
\"name\": \"Compteur Eau Batterie\", \
\"unique_id\": \"water_meter_battery\",\
\"object_id\": \"water_meter_battery\",\
\"device_class\": \"battery\",\
\"icon\": \"mdi:battery\",\
\"unit_of_measurement\": \"%\",\
\"qos\": \"0\",\
\"state_topic\": \"everblu/cyble/battery\",\
\"value_template\": \"{{ [(value|int), 100] | min }}\",\
\"force_update\": \"true\",\
\"device\" : {\
\"identifiers\" : [\
\"14071984\" ],\
\"name\": \"Compteur Eau\",\
\"model\": \"Everblu Cyble ESP8266/ESP32\",\
\"manufacturer\": \"Psykokwak\",\
\"suggested_area\": \"Home\"}\
}");
String jsonDiscoveryDevice3(
"{ \
\"name\": \"Compteur Eau Compteur\", \
\"unique_id\": \"water_meter_counter\",\
\"object_id\": \"water_meter_counter\",\
\"icon\": \"mdi:counter\",\
\"qos\": \"0\",\
\"state_topic\": \"everblu/cyble/counter\",\
\"force_update\": \"true\",\
\"device\" : {\
\"identifiers\" : [\
\"14071984\" ],\
\"name\": \"Compteur Eau\",\
\"model\": \"Everblu Cyble ESP8266/ESP32\",\
\"manufacturer\": \"Psykokwak\",\
\"suggested_area\": \"Home\"}\
}");
String jsonDiscoveryDevice4(
"{ \
\"name\": \"Compteur Eau Timestamp\", \
\"unique_id\": \"water_meter_timestamp\",\
\"object_id\": \"water_meter_timestamp\",\
\"device_class\": \"timestamp\",\
\"icon\": \"mdi:clock\",\
\"qos\": \"0\",\
\"state_topic\": \"everblu/cyble/timestamp\",\
\"force_update\": \"true\",\
\"device\" : {\
\"identifiers\" : [\
\"14071984\" ],\
\"name\": \"Compteur Eau\",\
\"model\": \"Everblu Cyble ESP8266/ESP32\",\
\"manufacturer\": \"Psykokwak\",\
\"suggested_area\": \"Home\"}\
}");
void onConnectionEstablished()
{
Serial.println("Connected to MQTT Broker :)");
Serial.println("> Configure time from NTP server.");
configTzTime("UTC0", "pool.ntp.org");
Serial.println("> Configure Arduino OTA flash.");
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
}
else { // U_FS
type = "filesystem";
}
// NOTE: if updating FS this would be the place to unmount FS using FS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd updating.");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("%u%%\r\n", (progress / (total / 100)));
});
ArduinoOTA.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.setHostname("EVERBLUREADER");
ArduinoOTA.begin();
mqtt.subscribe("everblu/cyble/trigger", [](const String& message) {
if (message.length() > 0) {
Serial.println("Update data from meter from MQTT trigger");
_retry = 0;
onUpdateData();
}
});
Serial.println("> Send MQTT config for HA.");
// Auto discovery
delay(50); // Do not remove
mqtt.publish("homeassistant/sensor/water_meter_value/config", jsonDiscoveryDevice1, true);
delay(50); // Do not remove
mqtt.publish("homeassistant/sensor/water_meter_battery/config", jsonDiscoveryDevice2, true);
delay(50); // Do not remove
mqtt.publish("homeassistant/sensor/water_meter_counter/config", jsonDiscoveryDevice3, true);
delay(50); // Do not remove
mqtt.publish("homeassistant/sensor/water_meter_timestamp/config", jsonDiscoveryDevice4, true);
delay(50); // Do not remove
onScheduled();
}
void setup()
{
Serial.begin(115200);
Serial.println("\n");
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // turned off
mqtt.setMaxPacketSize(1024);
//mqtt.enableDebuggingMessages(true);
/*
// Use this piece of code to find the right frequency.
for (float i = 433.76f; i < 433.890f; i += 0.0005f) {
Serial.printf("Test frequency : %f\n", i);
cc1101_init(i);
struct tmeter_data meter_data;
meter_data = get_meter_data();
if (meter_data.reads_counter != 0 || meter_data.liters != 0) {
Serial.printf("\n------------------------------\nGot frequency : %f\n------------------------------\n", i);
Serial.printf("Liters : %d\nBattery (in months) : %d\nCounter : %d\n\n", meter_data.liters, meter_data.battery_left, meter_data.reads_counter);
digitalWrite(LED_BUILTIN, LOW); // turned on
while (42);
}
}
*/
cc1101_init(FREQUENCY);
/*
// Use this piece of code to test
struct tmeter_data meter_data;
meter_data = get_meter_data();
Serial.printf("\nLiters : %d\nBattery (in months) : %d\nCounter : %d\nTime start : %d\nTime end : %d\n\n", meter_data.liters, meter_data.battery_left, meter_data.reads_counter, meter_data.time_start, meter_data.time_end);
while (42);
*/
}
void loop()
{
mqtt.loop();
ArduinoOTA.handle();
}