MQTTHelpers.cpp
9.77 KB
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
#include "MQTTHelpers.h"
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#else
#error Unsupported or unknown board
#endif
static const char * TAG = "EasyOpenTherm MQTTHelpers";
#if !defined(ESP32) && !defined(ESP_LOGE)
#define ESP_LOGE(...)
#define ESP_LOGI(...)
#define ESP_LOGV(...)
#endif
const size_t smallJsonDocSize = 256;
/*
* chipID()
* Store the ESP32 unique ID once. Return a pointer to the stored chip ID. The ID is the same as the MAC but in reversed byte order
*/
const char * chipID() {
static char serialNumber[13] = "";
if(*serialNumber == '\0') {
#if defined(ESP32)
sprintf(serialNumber, "%012llx", ESP.getEfuseMac());
#elif defined(ESP8266)
sprintf(serialNumber, "%012llx", ESP.getChipId());
#else
#error Unsupported or unknown board
#endif
ESP_LOGI(TAG, "Serial number is %s", serialNumber);
}
return serialNumber;
}
#include <machine/types.h>
/*
* shortID()
* Store the least significant two bytes of the ESP32 unique ID once. Return a pointer to the stored short ID. The ID is the same as the last two MAC bytes, in the same order
*/
const char * shortID() {
static char shortNumber[5] = "";
if(*shortNumber == '\0') {
#if defined(ESP32)
uint64_t mac = ESP.getEfuseMac();
#elif defined(ESP8266)
uint64_t mac = ESP.getChipId();
#else
#error Unsupported or unknown board
#endif
uint8_t * MAC = (uint8_t *) &mac;
sprintf(shortNumber, "%02X%02X", MAC[4], MAC[5]);
ESP_LOGI(TAG, "Short ID is %s", shortNumber);
}
return shortNumber;
}
/*
* replaceAll()
* Do an 'in place' replacement into 'destination' of all occurances of the 'search' by 'replace'
*/
bool replaceAll(char * destination,
const char * search,
const char * replace) {
size_t searchLength = strlen(search);
if(strlen(replace) != searchLength) {
ESP_LOGE(TAG, "Length of search string (%s) does not match replace string (%s)\n", search, replace);
return false;
}
size_t length = strlen(destination);
char * searchFrom = destination;
bool replaced = false;
for(;;) {
char * found = strstr(searchFrom, search);
if(found == nullptr) break;
memcpy(found, replace, searchLength);
replaced = true;
searchFrom = found + searchLength;
if(searchFrom >= destination + length) break;
}
return replaced;
}
/*
* discoveryMessageSetIDs()
* Replace all placeholders '112233445566' with the chip ID, all placeholders '11:22:33:44:55:66' with the MAC and all placeholder '####' with the short ID.
*/
void discoveryMessageSetIDs(char * discoveryMsgJson) {
replaceAll(discoveryMsgJson, "112233445566", chipID());
replaceAll(discoveryMsgJson, "11:22:33:44:55:66", WiFi.macAddress().c_str());
replaceAll(discoveryMsgJson, "####", shortID());
}
const char * topicByReference(const char * key,
const char * discoveryMsgJson) {
static char * topicBuffer = nullptr;
static size_t topicBufferSize = 0; // Includes the trailing `\0'
StaticJsonDocument<smallJsonDocSize> discoveryJsonPartDoc;
StaticJsonDocument<smallJsonDocSize> filter;
filter["~"] = true;
filter[key] = true;
DeserializationError error = deserializeJson(discoveryJsonPartDoc, discoveryMsgJson, DeserializationOption::Filter(filter));
if(error) {
ESP_LOGE(TAG, "Deserialize error %s for JSON '%s'", error.f_str(), discoveryMsgJson);
return nullptr;
}
const char * baseTopic = discoveryJsonPartDoc["~"];
const char * topic = discoveryJsonPartDoc[key];
if(topic == nullptr) {
ESP_LOGE(TAG, "Key '%s' not found in JSON '%s'", key, discoveryMsgJson);
return nullptr;
}
if(*topic == '~' && baseTopic == nullptr) {
ESP_LOGE(TAG, "Base topic '~' not found in JSON '%s'", discoveryMsgJson);
return nullptr;
}
size_t newSize = strlen(topic) + 1;
if(*topic == '~') newSize += strlen(baseTopic);
if(topicBufferSize < newSize) {
free(topicBuffer);
topicBuffer = (char *) malloc(newSize);
if(topicBuffer == nullptr) {
topicBufferSize = 0;
ESP_LOGE(TAG, "topicByReference out of memory");
return nullptr;
}
topicBufferSize = newSize;
}
if(*topic == '~') {
sprintf(topicBuffer, "%s%s", baseTopic, topic + 1);
} else {
sprintf(topicBuffer, "%s", topic);
}
return topicBuffer;
}
bool validJson(const char * discoveryMsgJson) {
StaticJsonDocument<0> emptyDoc;
StaticJsonDocument<0> filter;
DeserializationError error = deserializeJson(emptyDoc, discoveryMsgJson, DeserializationOption::Filter(filter));
if(error) {
ESP_LOGE(TAG, "Deserialize error %s for JSON '%s'", error.f_str(), discoveryMsgJson);
}
return error == DeserializationError::Ok;
}
bool connectMQTT(MqttClient & client,
const char * clientID,
const char * user,
const char * password) {
// Start new MQTT connection
ESP_LOGI(TAG, "Connecting");
MqttClient::ConnectResult connectResult;
// Connect
MQTTPacket_connectData options = MQTTPacket_connectData_initializer;
options.MQTTVersion = 4;
options.clientID.cstring = (char *)clientID;
options.username.cstring = (char *)user;
options.password.cstring = (char *)password;
options.cleansession = true;
options.keepAliveInterval = 15; // 15 seconds
MqttClient::Error::type rc = client.connect(options, connectResult);
if (rc != MqttClient::Error::SUCCESS) {
ESP_LOGE(TAG, "Connection error: %i", rc);
return false;
}
return true;
}
bool publish(MqttClient & client,
const char * topic,
const char * payload,
bool retained) {
if(!client.isConnected()) return false;
MqttClient::Message message;
message.qos = MqttClient::QOS0;
message.retained = retained;
message.dup = true;
message.payload = (void *)payload;
message.payloadLen = strlen(payload);
client.publish(topic, message);
Serial.printf("PUBLISH topic is '%s' message is '%s'\n", topic, message.payload);
return true;
}
bool discoveryMsgToJsonDoc(JsonDocument & discoveryMsgDoc,
const char * discoveryMsgJson) {
DeserializationError error = deserializeJson(discoveryMsgDoc, discoveryMsgJson);
if(error) {
ESP_LOGE(TAG, "Deserialize error %s for JSON '%s'", error.f_str(), discoveryMsgJson);
return false;
}
return true;
}
/*
* addEntity()
* Publish the prepared and deserialized discovery message to topic 'homeassistant/[component]/[chip ID]/[object]/config', with component being one of the
* Home Assistant MQTT components like 'climate', 'sensor' or 'binary_sensor' and object a unique ID to differentiate between e.g. two sensors in the same
* device.
*/
bool addEntity(MqttClient & client,
const char * component,
const char * object,
JsonDocument & discoveryMsgDoc) {
if(!client.isConnected()) return false;
size_t jsonSize = measureJson(discoveryMsgDoc);
char discoveryMsgJson[jsonSize + 1];
serializeJson(discoveryMsgDoc, discoveryMsgJson, sizeof discoveryMsgJson);
ESP_LOGI(TAG, "Discovery topic is '%s'\n", (String("homeassistant/") + String(component) + String("/") + String(chipID()) + String("/") + String(object) + String("/config")).c_str());
ESP_LOGI(TAG, "Discovery message is '%s'", discoveryMsgJson);
MqttClient::Message message;
message.qos = MqttClient::QOS0;
message.retained = false;
message.dup = false;
message.payload = (void*) discoveryMsgJson;
message.payloadLen = jsonSize;
client.publish((String("homeassistant/") + String(component) + String("/") + String(chipID()) + String("/") + String(object) + String("/config")).c_str(), message);
Serial.printf("PUBLISH topic is '%s' message is '%s'\n", (String("homeassistant/") + String(component) + String("/") + String(chipID()) + String("/") + String(object) + String("/config")).c_str(), message.payload);
return true;
}
// addEntity()
// Same as above but providing the discovery message as a const char * JSON
bool addEntity(MqttClient & client,
const char * component,
const char * object,
const char * discoveryMsgJson) {
DynamicJsonDocument discoveryMsgDoc(fullJsonDocSize);
if(!discoveryMsgToJsonDoc(discoveryMsgDoc, discoveryMsgJson)) return false;
return addEntity(client, component, object, discoveryMsgDoc);
}
/*
* subscribe()
* Subscribe to the topic at at the given key in the discovery message. Before subscription the topic in the discovery
* message is expanded to the full topic by prefixing the "~" path.
*/
bool subscribe(MqttClient & client,
MqttClient::MessageHandlerCbk cbk,
const char * discoveryMessage,
const char * key) {
const char * topic = topicByReference(key, discoveryMessage);
if(topic == nullptr) return false;
MqttClient::Error::type rc = client.subscribe(topic, MqttClient::QOS0, cbk);
if (rc != MqttClient::Error::SUCCESS) {
ESP_LOGE(TAG, "Subscribe error: %i for topic '%s'", rc, topic);
ESP_LOGE(TAG, "Drop connection");
client.disconnect();
return false;
}
ESP_LOGI(TAG, "Subscribed to '%s'", topic);
Serial.printf("Subscribed to '%s'\n", topic);
return true;
}