MQTTHelpers.cpp
9.89 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
#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;
// ESP32 Chip ID is cca4f003f784 => 0x84, 0xf7, 0x03, 0xf0, 0xa4, 0xcc, 0x00, 0x00
// ESP8266 Chip ID is 00c2005a3cc2 => 0xc2, 0x3c, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x00
// So for the ESP32 MAC[4] and MAC[5] are the least significant bytes, and for the ESP8266 MAC[1] and MAC[0]
// Serial.print("Serial number bytes: "); for(size_t index = 0; index < 8; index++) Serial.printf("0x%02x, ", MAC[index]); Serial.println();
// sprintf(shortNumber, "%02X%02X", MAC[4], MAC[5]);
#if defined(ESP32)
sprintf(shortNumber, "%02X%02X", MAC[4], MAC[5]);
#else
sprintf(shortNumber, "%02X%02X", MAC[1], MAC[0]);
#endif
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);
Serial.printf("Deserialize error %s for JSON '%s'\n", 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);
Serial.printf("Key '%s' not found in JSON '%s'\n", key, discoveryMsgJson);
return nullptr;
}
if(*topic == '~' && baseTopic == nullptr) {
ESP_LOGE(TAG, "Base topic '~' not found in JSON '%s'", discoveryMsgJson);
Serial.printf("Base topic '~' not found in JSON '%s'\n", 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");
Serial.printf("topicByReference out of memory\n");
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(PubSubClient & client,
const char * clientID,
const char * user,
const char * password) {
// Start new MQTT connection
ESP_LOGI(TAG, "Connecting...");
// Connect
bool connectionResult = client.connect(clientID, user, password);
if(!connectionResult) {
ESP_LOGE(TAG, "Connection error: %d", client.state());
return false;
}
return true;
}
bool publish(PubSubClient & client,
const char * topic,
const char * payload,
size_t payloadLength,
bool retained) {
if(payloadLength == 0) return true;
if(!client.beginPublish(topic, payloadLength, retained)) return false;
size_t bytesWritten = client.write((const unsigned char *) payload, payloadLength);
return client.endPublish() && bytesWritten == payloadLength;
}
bool publish(PubSubClient & client,
const char * topic,
const char * payload,
bool retained) {
size_t payloadLength = strlen(payload);
return publish(client, topic, payload, payloadLength, retained);
}
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(PubSubClient & client,
const char * component,
const char * object,
JsonDocument & discoveryMsgDoc) {
if(!client.connected()) return false;
size_t jsonSize = measureJson(discoveryMsgDoc);
char discoveryMsgJson[jsonSize + 1];
serializeJson(discoveryMsgDoc, discoveryMsgJson, sizeof discoveryMsgJson);
size_t topicSize = sizeof("homeassistant/") - 1 + strlen(component) + sizeof("/") - 1 + strlen(chipID()) + sizeof("/") - 1 + strlen(object) + sizeof("/config") - 1 + 1;
char topic[topicSize];
sprintf(topic, "homeassistant/%s/%s/%s/config", component, chipID(), object);
ESP_LOGI(TAG, "Discovery topic is '%s'\n", topic);
ESP_LOGI(TAG, "Discovery message is '%s'", discoveryMsgJson);
return publish(client, topic, discoveryMsgJson);
}
// addEntity()
// Same as above but providing the discovery message as a const char * JSON
bool addEntity(PubSubClient & 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(PubSubClient & client,
const char * discoveryMessage,
const char * key) {
const char * topic = topicByReference(key, discoveryMessage);
if(topic == nullptr) return false;
ESP_LOGI(TAG, "Subscribe to '%s'", topic);
Serial.printf("Subscribe to '%s'\n", topic);
return client.subscribe(topic);
}