This repository has been archived by the owner on Apr 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MiThermometer2MQTT.ino
371 lines (303 loc) · 11.3 KB
/
MiThermometer2MQTT.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
/*
MiThermometer2MQTT
==================
Picks up the sensor data of Mi Thermometers with custom firmware and sends it to a MQTT broker.
See README.md for more information.
*/
// Define your configurations and credentials in the following files. Changes are ignored by git.
#include "configs.h"
#include "secrets.h"
#include "MyUtils.h"
#if !defined(WIFI_SSID) || !defined(WIFI_PSK) || !defined(MQTT_USER) || !defined(MQTT_PASS)
#error "Don't forget to fill your credentials in 'secrets.h'!"
#endif // WIFI_SSID
#ifdef USE_BME280_SENSOR
#include "Module_BME280.h"
#endif // USE_BME280_SENSOR
#ifdef USE_SSD1306_DISPLAY
#include "SSD1306_SPI.h"
#include "time.h"
#ifndef USE_BME280_SENSOR
#error "To use the dispaly module it is required to enable BME280 sensor module as well."
#endif // USE_BME280_SENSOR
#endif // USE_SSD1306_DISPLAY
#ifdef USE_HOME_ASSISTANT
#include "Module_HomeAssistant.h"
#endif // USE_HOME_ASSISTANT
// Bluetooth
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
BLEScan* pBLEScan;
// Wifi
#include <WiFi.h>
// JSON
#include <ArduinoJson.h>
// https://arduinojson.org/v6/how-to/determine-the-capacity-of-the-jsondocument/
const int JSON_CAPACITY = 256;
// MQTT
#include <ArduinoMqttClient.h>
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char mqtt_broker[] = MQTT_SERVER;
int mqtt_port = MQTT_PORT;
const char mqtt_topic[] = MQTT_TOPIC;
// -----------------------------------------------------------------------------
void formatSensorData(JsonDocument& target, BLEAdvertisedDevice advertisedDevice, std::string mac_addr) {
std::string sensorData = advertisedDevice.getServiceData();
const char *sensorData_ptr = sensorData.c_str();
std::string id = hexStr((unsigned char*)(sensorData_ptr), 6);
int16_t temp = FLIP_ENDIAN ? __builtin_bswap16(*(int16_t *)(sensorData_ptr + 6)) : *(int16_t *)(sensorData_ptr + 6);
double temperature = 1.0 * temp / 10;
byte *humidity = (byte *) (sensorData_ptr + 8);
byte *battery_level = (byte *) (sensorData_ptr + 9);
int16_t voltage = FLIP_ENDIAN ? __builtin_bswap16(*(int16_t *)(sensorData_ptr + 10)) : *(int16_t *)(sensorData_ptr + 10);
target["id"] = id;
#ifdef USE_MAC_NAME_MAPPINGS
std::map<std::string, std::string>::const_iterator name_it = MAC_NAME_MAPPING.find(mac_addr);
if (name_it != MAC_NAME_MAPPING.end()) {
target["name"] = name_it->second;
}
#endif // USE_MAC_NAME_MAPPINGS
if (!target.containsKey("name") && advertisedDevice.haveName()) {
std::string name = advertisedDevice.getName();
target["name"] = name.c_str();
}
target["temperature"] = temperature;
target["humidity"] = *humidity;
target["battery"] = *battery_level;
target["voltage"] = voltage;
if (advertisedDevice.haveRSSI()) {
// RSSI (Received Signal Strength Indicator) is a negative dBm value.
// Lower values mean lesser signal strengths (e.g. -20 is good, -120 is bad).
target["signal_strength"] = advertisedDevice.getRSSI();
}
target["humidity"] = *humidity;
}
// -----------------------------------------------------------------------------
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
std::string mac = advertisedDevice.getAddress().toString();
if (mac.rfind(VENDOR_PREFIX, 0) != 0) {
return;
}
#ifdef ONLY_FORWARD_KNOWN_DEVICES
if (MAC_NAME_MAPPING.find(mac.c_str()) == MAC_NAME_MAPPING.end()) {
return;
}
#endif // ONLY_FORWARD_KNOWN_DEVICES
BLEUUID uid = advertisedDevice.getServiceDataUUID();
if (!advertisedDevice.haveServiceData() || uid.toString().rfind(SERVICE_DATA_UID, 0) != 0) {
return;
}
StaticJsonDocument<JSON_CAPACITY> sensorData;
formatSensorData(sensorData, advertisedDevice, mac);
String sensorDataString;
serializeJson(sensorData, sensorDataString);
const char *payload = sensorDataString.c_str();
String id = sensorData["id"];
String topic = mqtt_topic + String("/") + id;
mqttClient.beginMessage(topic,
sensorDataString.length(), // size
false, // retain
0, // qos
false); // dup
mqttClient.print(payload);
mqttClient.endMessage();
Serial.println(payload);
}
};
// -----------------------------------------------------------------------------
void publishState(bool online = true) {
char topic[] = MQTT_TOPIC_STATE;
mqttClient.beginMessage(topic,
true, // retain
1); // qos
mqttClient.print(online ? "online" : "offline");
mqttClient.endMessage();
}
// ---------------------------------------------------------
void publishWill() {
char topic[] = MQTT_TOPIC_STATE;
mqttClient.beginWill(topic,
true, // retain
1); // qos
mqttClient.print("offline");
mqttClient.endWill();
}
std::string BOARD_UID;
bool online_state = true; // Consciously initialized `true`
unsigned long previousMillisSensorInterval = 0;
#ifdef MODULE_SSD1306_DISPLAY
unsigned long previousMillisDisplayInterval = 0;
#endif // MODULE_SSD1306_DISPLAY
// -----------------------------------------------------------------------------
void setup() {
Serial.begin(SERIAL_BAUD);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println();
String mac = WiFi.macAddress();
BOARD_UID = stripColonsFromMac(mac.c_str());
// BME280 setup
#ifdef MODULE_BME280_SENSOR
Wire.begin();
digitalWrite(LED_BUILTIN, 0);
setup_bcm280_module();
digitalWrite(LED_BUILTIN, 1);
#endif // MODULE_BME280_SENSOR
#ifdef MODULE_SSD1306_DISPLAY
setup_ssd1306_module();
#endif // MODULE_SSD1306_DISPLAY
// Wifi config
WiFi.mode(WIFI_STA);
WiFi.setHostname(WIFI_HOSTNAME) && Serial.printf("Wifi hostname set to %s.\n", WIFI_HOSTNAME);
// Synchronize real time via NTP
#ifdef MODULE_SSD1306_DISPLAY
configTime(UTC_OFFSET, DAYLIGHT_SAVING_OFFSET, NTP_SERVER);
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Error: Failed to initialize RTC!");
drawText("Failed \n"
"to sync\n"
"clock!",
2);
delay(500);
}
Serial.println(&timeinfo, "RTC initialized at %A, %B %d %Y %H:%M:%S");
#endif // MODULE_SSD1306_DISPLAY
// Bluetooth setup
#ifdef MODULE_SSD1306_DISPLAY
drawText("Initia-\n"
"lizing\n"
"Bluetooth",
2);
delay(500);
#endif // MODULE_SSD1306_DISPLAY
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(false); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
Serial.println("Entering main loop...");
}
// -----------------------------------------------------------------------------
void loop() {
checkWifi();
if (!checkMqtt()) {
// Delay, so we won't bombard the MQTT server with connection attempts.
delay(30000);
return;
}
// call poll() regularly to allow the library to send MQTT keep alives which
// avoids being disconnected by the broker
mqttClient.poll();
// avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay
// see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info
unsigned long currentMillis = millis();
#ifdef MODULE_SSD1306_DISPLAY
if (currentMillis - previousMillisDisplayInterval >= DISPLAY_UPDATE_INTERVAL) {
previousMillisDisplayInterval += DISPLAY_UPDATE_INTERVAL;
printBME280Data(&drawSensorData);
}
#endif // MODULE_SSD1306_DISPLAY
if (currentMillis - previousMillisSensorInterval >= SCAN_SESSION_INTERVAL * 1000) {
previousMillisSensorInterval = currentMillis;
#ifdef MODULE_BME280_SENSOR
// Process attached BME280 sensor data
StaticJsonDocument<JSON_CAPACITY> sensorData;
formatBmeSensorData(sensorData, BOARD_UID.c_str(), WiFi.RSSI());
String sensorDataString;
serializeJson(sensorData, sensorDataString);
const char *payload = sensorDataString.c_str();
char topic[strlen(mqtt_topic) + 1 + strlen(BOARD_UID.c_str()) + 1];
sprintf(topic, "%s/%.12s", mqtt_topic, BOARD_UID.c_str());
mqttClient.beginMessage(topic,
sensorDataString.length(), // size
false, // retain
0, // qos
false); // dup
mqttClient.print(payload);
mqttClient.endMessage();
Serial.println(payload);
#endif // MODULE_BME280_SENSOR
// Bluetooth scan
BLEScanResults foundDevices = pBLEScan->start(SCAN_INTERVAL, false);
pBLEScan->clearResults(); // delete results from BLEScan buffer to release memory
}
}
// -----------------------------------------------------------------------------
void checkWifi () {
if (WiFi.status() == WL_CONNECTED) return;
Serial.print("No Wifi connection, (re)connecting");
#ifdef MODULE_SSD1306_DISPLAY
drawText("Connecting\n"
"to WiFi...",
2);
delay(500);
#endif // MODULE_SSD1306_DISPLAY
WiFi.begin(WIFI_SSID, WIFI_PSK);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(LED_BUILTIN, 0);
delay(500);
digitalWrite(LED_BUILTIN, 1);
delay(500);
Serial.print(".");
}
Serial.printf("\nConnected to Wifi network %s (%s).\n", WIFI_SSID, WiFi.localIP().toString().c_str());
#ifdef MODULE_SSD1306_DISPLAY
drawText("Connected\n"
"to WiFi\n"
WIFI_SSID,
2);
delay(1000);
#endif // MODULE_SSD1306_DISPLAY
return;
}
// ---------------------------------------------------------
bool checkMqtt () {
if (mqttClient.connected()) return true;
Serial.printf("MQTT client not connected, (re)connecting to broker at %s:%i.\n", mqtt_broker, mqtt_port);
#ifdef MODULE_SSD1306_DISPLAY
drawText("Connecting\n"
"to MQTT\n"
"broker...",
2);
delay(500);
#endif // MODULE_SSD1306_DISPLAY
// Create new MQTT client
// FIXME: I was not able to reconnect existing client (ArduinoMqttClient version 0.1.5 BETA).
mqttClient = MqttClient(wifiClient);
mqttClient.setId(MQTT_CLIENTID);
mqttClient.setUsernamePassword(MQTT_USER, MQTT_PASS);
if (!mqttClient.connect(mqtt_broker, mqtt_port)) {
Serial.printf("MQTT connection failed! Error code = %i\n", mqttClient.connectError());
#ifdef MODULE_SSD1306_DISPLAY
drawText("ERROR\n"
"connecting\n"
"to MQTT\n"
"broker!",
2);
#endif // MODULE_SSD1306_DISPLAY
return false;
}
Serial.println("Connected to the MQTT broker.");
#ifdef MODULE_SSD1306_DISPLAY
drawText("Connected\n"
"to MQTT\n"
"broker!",
2);
delay(1000);
#endif // MODULE_SSD1306_DISPLAY
// Online state will be deposited with MQTT broker
Serial.println("Publishing retained online state and will...");
publishState();
publishWill();
// Publish Home Assistant configurations via MQTT
#ifdef MODULE_HOME_ASSISTANT
Serial.println("Publishing Home Assistant configs for configured sensors...");
publishHomeAssistantConfigs(mqttClient, BOARD_UID.c_str());
#endif // MODULE_HOME_ASSISTANT
return true;
}