-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmqtt.h
354 lines (310 loc) · 9.68 KB
/
mqtt.h
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
#ifndef mqtt_h
#define mqtt_h
#undef nullptr
#define USEJSON
#ifdef USEJSON
#include <ArduinoJson.h> // bblanchon/ArduinoJson
// #define _JSONSIZE 4096
#ifndef _JSONSIZE
#define _JSONSIZE 1024
#endif
StaticJsonDocument<_JSONSIZE> payload;
StaticJsonDocument<_JSONSIZE> rootdoc;
// payload["sensor"] = "gps";
// payload["time"] = 1351824120;
// const size_t CAPACITY = JSON_ARRAY_SIZE(1);
// StaticJsonDocument<CAPACITY> docH;
// JsonArray arrayH = docH.to<JsonArray>();
DynamicJsonDocument pubjson(_JSONSIZE);
JsonArray jsondata = pubjson.to<JsonArray>();
#endif
#include <PubSubClient.h> // knolleary/pubsubclient
#define MQTT_MAX_PACKET_SIZE 2048
WiFiClient espClient;
PubSubClient client(espClient);
bool debug_mqtt = true;
bool debug_mqtt_json = false;
const char* clientID = "";
bool mqttconnected = false;
long lastReconnectAttempt = 0;
uint32_t mqttretry = 5000;
void MQTTGetErrorMessage(){
// Possible values for client.state()
// #define MQTT_CONNECTION_TIMEOUT -4
// #define MQTT_CONNECTION_LOST -3
// #define MQTT_CONNECT_FAILED -2
// #define MQTT_DISCONNECTED -1
// #define MQTT_CONNECTED 0
// #define MQTT_CONNECT_BAD_PROTOCOL 1
// #define MQTT_CONNECT_BAD_CLIENT_ID 2
// #define MQTT_CONNECT_UNAVAILABLE 3
// #define MQTT_CONNECT_BAD_CREDENTIALS 4
// #define MQTT_CONNECT_UNAUTHORIZED 5
}
void MQTTreconnect() {
if (!client.connected()) {
// while (!client.connected()) {
if(debug_mqtt) Logger.println("[MQTT] Connecting...");
// Attempt to connect
if (client.connect(clientID)) {
mqttconnected = true;
Logger.println("[MQTT] Connected");
// Once connected, publish an announcement...
client.publish("TESTOUT", "hello world");
// ... and resubscribe
client.subscribe("ESP_env_c/CMD");
} else {
Logger.print("[ERROR] [MQTT] failed, rc="); // @todo we get here but no actual reconnnect
Logger.println(client.state());
}
}
}
void MQTTcallback(char* topic, byte* payload, unsigned int length) {
Logger.print("[MQTT] IN Message arrived [");
Logger.print(topic);
Logger.print("] ");
Logger.print("payload: ");
for (int i = 0; i < length; i++) {
Logger.print((char)payload[i]);
}
Logger.println("");
// RESTART
if ((char)payload[0] == '1') {
Logger.println("[MQTT] payload: 1 TRIGGERED");
ESP.restart();
}
// WIFI off
if ((char)payload[0] == '2') {
Logger.println("[MQTT] payload: 2 TRIGGERED");
WiFi.disconnect();
}
}
boolean mqttWiFiReconnect() {
WiFi.reconnect();
return WiFi.status() == 'WL_CONNECTED';
}
void mqtt_checkconn(){
if (!client.connected()) {
long now = millis();
if (now - lastReconnectAttempt > mqttretry) {
Logger.println("[MQTT] try client re-connect");
lastReconnectAttempt = now;
MQTTreconnect();
}
} else {
// Client connected
client.loop();
}
}
bool process_MQTT_nb(){
if (!client.connected()) {
mqttconnected = false;
mqtt_checkconn();
}
client.loop(); // will wait loop reconnect to mqtt
return client.connected();
}
bool process_MQTT(){
if(!wifiIsConnected()){
// Logger.println("[MQTT] wifi not connected");
return false;
}
if(!client.connected()){
mqttconnected = false;
Logger.print("[MQTT] client not connected => ");
Logger.println(client.state());
MQTTreconnect(); // @todo throttle
return false;
}
client.loop(); // will wait loop reconnect to mqtt
return true;
}
void init_MQTT(){
if(!wifiIsConnected()) return;
if(clientID == ""){
Logger.println("[MQTT] clientID not set");
// clientID = getHostname();
}
client.setServer(mqtt_server_ip, mqtt_server_port);
// MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds. Override with setSocketTimeout()
client.setSocketTimeout(30); // #define MQTT_SOCKET_TIMEOUT 15
// MQTT_KEEPALIVE : keepAlive interval in Seconds. Override with setKeepAlive()
client.setKeepAlive(10); // #define MQTT_KEEPALIVE 15
client.setCallback(MQTTcallback);
if (client.connect(clientID)){
mqttconnected = true;
Logger.println("[MQTT] connected to " + (String)mqtt_server_ip + " as " + clientID);
}
else Logger.println("[MQTT] init failed to connect to " + (String)mqtt_server_ip);
client.setBufferSize(_JSONSIZE);
// client.subscribe("CMD");
// client.subscribe("ESP_env_c/CMD");
process_MQTT();
// jsondata = pubjson.to<JsonArray>();
// jsondata = pubjson.createNestedArray();
}
void init_MQTT(const char* clientid){
clientID = clientid; // global
init_MQTT();
}
void init_MQTT(const String& clientid){
clientID = clientid.c_str(); // global
init_MQTT();
}
#ifdef USEJSON
void MQTT_pub(String topic, String sensor, String value){
if(!wifiIsConnected()){
// Logger.print("[MQTT] OFFLINE: ");
// return;
}
if(!mqttconnected)return;
if(debug_mqtt){
Logger.print("[MQTT] Publish: ");
Logger.print(sensor);
Logger.print(" ");
Logger.println(value);
}
if(value == "") {
// Logger.println("[ERROR] MQTT value is empty");
return;
}
// JsonArray data = payload.createNestedArray(topic);
payload["topic"] = topic;
payload["clientid"] = clientID;
payload["type"] = sensor;
payload["value"] = value.toFloat();
// payload["unit"] = "";
// if(topic == "device")
jsondata.add(payload);
// serializeJson(jsondata, Serial);
// if(debug_mqtt) serializeJson(payload, Serial);
// serializeJsonPretty(payload, Serial);
// String output;
// serializeJson(payload,output);
// client.publish(topic.c_str(),("["+output+"\n]").c_str()); // must be object?
// close ?
}
void MQTT_pub(String topic, String sensor, String value, bool json){
if(!mqttconnected)return;
if(debug_mqtt){
Logger.print("[MQTT] Publish: ");
Logger.print(sensor);
Logger.print(" ");
Logger.println(value);
}
if(value == "") {
// Logger.println("[ERROR] MQTT value is empty");
return;
}
JsonArray data = payload.createNestedArray(topic);
payload["topic"] = data; // tag key = tag value
payload["clientid"] = clientID;
payload["type"] = sensor; // field key = field value
payload["value"] = value.toFloat();
// payload["unit"] = "";
// if(debug_mqtt_json) serializeJson(payload, Logger);
// serializeJsonPretty(payload, Serial);
String output;
serializeJson(payload,output);
client.publish(topic.c_str(),("["+output+"\n]").c_str()); // must be object?
// close ?
// mqttClient.beginMessage(topic);
// serializeJson(doc, mqttClient);
// mqttClient.endMessage();
// client.print
// client.endPublish();
// client.beginPublish("greenBottles/lyrics", msgLen, false);
// const size_t capacity = JSON_ARRAY_SIZE(2) + JSON_OBJECT_SIZE(3);
// DynamicJsonBuffer jsonBuffer(capacity);
// JsonObject& root = jsonBuffer.createObject();
// root["sensor"] = "gps";
// root["time"] = 1351824120;
// JsonArray& data = root.createNestedArray("data");
// data.add(48.75608);
// data.add(2.302038);
// root.printTo(Serial);
}
void MQTT_pub_send(String topic){
if(!mqttconnected)return;
if(debug_mqtt){
Logger.println("[MQTT] sending json for topic: " + topic);
}
// serializeJson(jsondata, Logger);
// rootdoc.createNestedObject();
// rootdoc.add(pubjson);
// String output;
// serializeJson(pubjson,output);
// Serial.println(output);
// serializeJson(rootdoc,output);
// Serial.println(output);
// Serial.flush();
char message[_JSONSIZE];
serializeJson(pubjson, message);
if(debug_mqtt_json){
Logger.println((String)message);
Logger.flush();
}
client.publish(topic.c_str(),message);
delay(500);
// pubjson.clear();
rootdoc.clear();
// jsondata.clear();
// rootdoc.createNestedObject(jsondata);
jsondata = pubjson.to<JsonArray>();
// payload.clear();
// pubjson.garbageCollect();
}
#else
void MQTT_pub(String topic, String msg){
if(!mqttconnected)return;
Logger.print("[MQTT] Publish message: ");
Logger.print("topic: ");
Logger.print(topic);
Logger.print(" mesg: ");
Logger.println(msg);
client.publish(topic.c_str(), msg.c_str());
}
void MQTT_pub(String topic, String sensor, String value){
if(!mqttconnected)return;
Logger.print("[MQTT] Publish message: ");
Logger.print("topic: ");
Logger.print(topic+"/"+clientID+"/"+sensor);
Logger.print("\t\tvalue: ");
Logger.println(value);
if(value == "") {
Logger.println("[ERROR] MQTT value is empty");
return;
}
client.publish((topic+"/"+clientID+"/"+sensor).c_str(), value.c_str());
}
#endif
#ifdef ESP8266
#define ESP_getChipId() ESP.getChipId()
#elif defined(ESP32)
#define ESP_getChipId() (uint32_t)ESP.getEfuseMac()
#endif
void MQTT_pub_device(bool verbose){
if(!mqttconnected)return;
if(debug_mqtt) Logger.println("[MQTT] Publish Device");
MQTT_pub("device","uptime_s",(String)(millis()/1000));
MQTT_pub("device","rssi",(String)getRSSIasQuality());
MQTT_pub("device","heap",(String)ESP.getFreeHeap());
if(verbose){
// MQTT_pub("device","ip",(String)ESP.getFreeHeap());
// MQTT_pub("device","hostname",(String)ESP.getFreeHeap());
MQTT_pub("device","ChipType",(String)ESP.getChipModel());
MQTT_pub("device","chipID",(String)ESP_getChipId());
MQTT_pub("device","ESPver",(String)ESP.getSdkVersion());
MQTT_pub("device","chipCores",(String)ESP.getChipCores());
MQTT_pub("device","chipRev",(String)ESP.getChipRevision());
}
// MQTT_pub("device","hall",(String)hallRead());
#ifdef ESP32
// MQTT_pub("device","temp",(String)temperatureRead());
#endif
MQTT_pub_send("device");
}
void MQTT_pub_device(){
MQTT_pub_device(false);
}
#endif