forked from OpenSprinkler/OpenSprinkler-Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.cpp
391 lines (318 loc) · 12.9 KB
/
mqtt.cpp
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* OpenSprinkler Unified (AVR/RPI/BBB/LINUX/ESP8266) Firmware
* Copyright (C) 2015 by Ray Wang ([email protected])
*
* OpenSprinkler library
* Feb 2015 @ OpenSprinkler.com
*
* This file is part of the OpenSprinkler library
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#if defined(ARDUINO)
#include <Arduino.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <UIPEthernet.h>
#include <PubSubClient.h>
struct PubSubClient *mqtt_client = NULL;
#else
#include <time.h>
#include <stdio.h>
#include <mosquitto.h>
struct mosquitto *mqtt_client = NULL;
#endif
#include "OpenSprinkler.h"
#include "mqtt.h"
// Debug routines to help identify any blocking of the event loop for an extended period
#if defined(ENABLE_DEBUG)
#if defined(ARDUINO)
#include "TimeLib.h"
#define DEBUG_PRINTF(msg, ...) {Serial.printf(msg, ##__VA_ARGS__);}
#define DEBUG_TIMESTAMP(msg, ...) {time_t t = os.now_tz(); Serial.printf(buffer, "%02d-%02d-%02d %02d:%02d:%02d - ", year(t), month(t), day(t), hour(t), minute(t), second(t));}
#else
#include <sys/time.h>
#define DEBUG_PRINTF(msg, ...) {printf(msg, ##__VA_ARGS__);}
#define DEBUG_TIMESTAMP() {char tstr[21]; time_t t = time(NULL); struct tm *tm = localtime(&t); strftime(tstr, 21, "%y-%m-%d %H:%M:%S - ", tm);printf("%s", tstr);}
#endif
#define DEBUG_LOGF(msg, ...) {DEBUG_TIMESTAMP(); DEBUG_PRINTF(msg, ##__VA_ARGS__);}
static unsigned long _lastMillis = 0; // Holds the timestamp associated with the last call to DEBUG_DURATION()
inline unsigned long DEBUG_DURATION() {unsigned long dur = millis() - _lastMillis; _lastMillis = millis(); return dur;}
#else
#define DEBUG_PRINTF(msg, ...) {}
#define DEBUG_LOGF(msg, ...) {}
#define DEBUG_DURATION() {}
#endif
#define str(s) #s
#define xstr(s) str(s)
extern OpenSprinkler os;
extern char tmp_buffer[];
#define MQTT_KEEPALIVE 60
#define MQTT_DEFAULT_PORT 1883 // Default port for MQTT. Can be overwritten through App config
#define MQTT_MAX_HOST_LEN 50 // Note: App is set to max 50 chars for broker name
#define MQTT_MAX_USERNAME_LEN 32 // Note: App is set to max 32 chars for username
#define MQTT_MAX_PASSWORD_LEN 32 // Note: App is set to max 32 chars for password
#define MQTT_MAX_ID_LEN 16 // MQTT Client Id to uniquely reference this unit
#define MQTT_RECONNECT_DELAY 120 // Minumum of 60 seconds between reconnect attempts
#define MQTT_ROOT_TOPIC "opensprinkler"
#define MQTT_AVAILABILITY_TOPIC MQTT_ROOT_TOPIC "/availability"
#define MQTT_ONLINE_PAYLOAD "online"
#define MQTT_OFFLINE_PAYLOAD "offline"
#define MQTT_SUCCESS 0 // Returned when function operated successfully
#define MQTT_ERROR 1 // Returned whan function failed
char OSMqtt::_id[MQTT_MAX_ID_LEN + 1] = {0}; // Id to identify the client to the broker
char OSMqtt::_host[MQTT_MAX_HOST_LEN + 1] = {0}; // IP or host name of the broker
char OSMqtt::_username[MQTT_MAX_USERNAME_LEN + 1] = {0}; // username to connect to the broker
char OSMqtt::_password[MQTT_MAX_PASSWORD_LEN + 1] = {0}; // password to connect to the broker
int OSMqtt::_port = MQTT_DEFAULT_PORT; // Port of the broker (default 1883)
bool OSMqtt::_enabled = false; // Flag indicating whether MQTT is enabled
// Initialise the client libraries and event handlers.
void OSMqtt::init(void) {
DEBUG_LOGF("MQTT Init\n");
char id[MQTT_MAX_ID_LEN + 1] = {0};
#if defined(ARDUINO)
uint8_t mac[6] = {0};
os.load_hardware_mac(mac, m_server!=NULL);
snprintf(id, MQTT_MAX_ID_LEN, "OS-%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
#endif
init(id);
};
// Initialise the client libraries and event handlers.
void OSMqtt::init(const char * clientId) {
DEBUG_LOGF("MQTT Init: ClientId %s\n", clientId);
strncpy(_id, clientId, MQTT_MAX_ID_LEN);
_id[MQTT_MAX_ID_LEN] = 0;
_init();
};
// Start the MQTT service and connect to the MQTT broker using the stored configuration.
void OSMqtt::begin(void) {
DEBUG_LOGF("MQTT Begin\n");
char host[MQTT_MAX_HOST_LEN + 1] = {0};
char username[MQTT_MAX_USERNAME_LEN + 1] = {0};
char password[MQTT_MAX_PASSWORD_LEN + 1] = {0};
int port = MQTT_DEFAULT_PORT;
int enabled = 0;
// JSON configuration settings in the form of {"en":0|1,"host":"server_name|IP address","port":1883,user:"",pass:""}
char *config = tmp_buffer;
os.sopt_load(SOPT_MQTT_OPTS, config);
if (*config != 0) {
sscanf(
config,
"\"en\":%d,\"host\":\"%" xstr(MQTT_MAX_HOST_LEN) "[^\"]\",\"port\":%d,\"user\":\"%" xstr(MQTT_MAX_USERNAME_LEN) "[^\"]\",\"pass\":\"%" xstr(MQTT_MAX_PASSWORD_LEN) "[^\"]\"",
&enabled, host, &port, username, password
);
}
begin(host, port, username, password, (bool)enabled);
}
// Start the MQTT service and connect to the MQTT broker.
void OSMqtt::begin( const char * host, int port, const char * username, const char * password, bool enabled ) {
DEBUG_LOGF("MQTT Begin: Config (%s:%d %s) %s\n", host, port, username, enabled ? "Enabled" : "Disabled");
strncpy(_host, host, MQTT_MAX_HOST_LEN);
_host[MQTT_MAX_HOST_LEN] = 0;
_port = port;
strncpy(_username, username, MQTT_MAX_USERNAME_LEN);
_username[MQTT_MAX_USERNAME_LEN] = 0;
strncpy(_password, password, MQTT_MAX_PASSWORD_LEN);
_username[MQTT_MAX_PASSWORD_LEN] = 0;
_enabled = enabled;
if (mqtt_client == NULL || os.status.network_fails > 0) return;
if (_connected()) {
_disconnect();
}
if (_enabled) {
_connect();
}
}
// Publish an MQTT message to a specific topic
void OSMqtt::publish(const char *topic, const char *payload) {
DEBUG_LOGF("MQTT Publish: %s %s\n", topic, payload);
if (mqtt_client == NULL || !_enabled || os.status.network_fails > 0) return;
if (!_connected()) {
DEBUG_LOGF("MQTT Publish: Not connected\n");
return;
}
_publish(topic, payload);
}
// Regularly call the loop function to ensure "keep alive" messages are sent to the broker and to reconnect if needed.
void OSMqtt::loop(void) {
static unsigned long last_reconnect_attempt = 0;
if (mqtt_client == NULL || !_enabled || os.status.network_fails > 0) return;
// Only attemp to reconnect every MQTT_RECONNECT_DELAY seconds to avoid blocking the main loop
if (!_connected() && (millis() - last_reconnect_attempt >= MQTT_RECONNECT_DELAY * 1000UL)) {
DEBUG_LOGF("MQTT Loop: Reconnecting\n");
_connect();
last_reconnect_attempt = millis();
}
int state = _loop();
#if defined(ENABLE_DEBUG)
// Print a diagnostic message whenever the MQTT state changes
bool network = os.network_connected(), mqtt = _connected();
static bool last_network = 0, last_mqtt = 0;
static int last_state = 999;
if (last_state != state || last_network != network || last_mqtt != mqtt) {
DEBUG_LOGF("MQTT Loop: Network %s, MQTT %s, State - %s\n",
network ? "UP" : "DOWN",
mqtt ? "UP" : "DOWN",
_state_string(state));
last_state = state; last_network = network; last_mqtt = mqtt;
}
#endif
}
/**************************** ARDUINO ********************************************/
#if defined(ARDUINO)
#if defined(ESP8266)
WiFiClient wifiClient;
#endif
EthernetClient ethClient;
int OSMqtt::_init(void) {
Client * client = NULL;
if (mqtt_client) { delete mqtt_client; mqtt_client = 0; }
#if defined(ESP8266)
if (m_server) client = ðClient;
else client = &wifiClient;
#else
client = ðClient;
#endif
mqtt_client = new PubSubClient(*client);
mqtt_client->setKeepAlive(MQTT_KEEPALIVE);
if (mqtt_client == NULL) {
DEBUG_LOGF("MQTT Init: Failed to initialise client\n");
return MQTT_ERROR;
}
return MQTT_SUCCESS;
}
int OSMqtt::_connect(void) {
mqtt_client->setServer(_host, _port);
boolean state;
if (_username[0])
state = mqtt_client->connect(_id, _username, _password, MQTT_AVAILABILITY_TOPIC, 0, true, MQTT_OFFLINE_PAYLOAD);
else
state = mqtt_client->connect(_id, NULL, NULL, MQTT_AVAILABILITY_TOPIC, 0, true, MQTT_OFFLINE_PAYLOAD);
if (state) {
mqtt_client->publish(MQTT_AVAILABILITY_TOPIC, MQTT_ONLINE_PAYLOAD, true);
} else {
DEBUG_LOGF("MQTT Connect: Failed (%d)\n", mqtt_client->state());
return MQTT_ERROR;
}
return MQTT_SUCCESS;
}
int OSMqtt::_disconnect(void) {
mqtt_client->disconnect();
return MQTT_SUCCESS;
}
bool OSMqtt::_connected(void) { return mqtt_client->connected(); }
int OSMqtt::_publish(const char *topic, const char *payload) {
if (!mqtt_client->publish(topic, payload)) {
DEBUG_LOGF("MQTT Publish: Failed (%d)\n", mqtt_client->state());
return MQTT_ERROR;
}
return MQTT_SUCCESS;
}
int OSMqtt::_loop(void) {
mqtt_client->loop();
return mqtt_client->state();
}
const char * OSMqtt::_state_string(int rc) {
switch (rc) {
case MQTT_CONNECTION_TIMEOUT: return "The server didn't respond within the keepalive time";
case MQTT_CONNECTION_LOST: return "The network connection was lost";
case MQTT_CONNECT_FAILED: return "The network connection failed";
case MQTT_DISCONNECTED: return "The client has cleanly disconnected";
case MQTT_CONNECTED: return "The client is connected";
case MQTT_CONNECT_BAD_PROTOCOL: return "The server doesn't support the requested version of MQTT";
case MQTT_CONNECT_BAD_CLIENT_ID: return "The server rejected the client identifier";
case MQTT_CONNECT_UNAVAILABLE: return "The server was unavailable to accept the connection";
case MQTT_CONNECT_BAD_CREDENTIALS: return "The username/password were rejected";
case MQTT_CONNECT_UNAUTHORIZED: return "The client was not authorized to connect";
default: return "Unrecognised state";
}
}
#else
/************************** RASPBERRY PI / BBB / DEMO ****************************************/
static bool _connected = false;
static void _mqtt_connection_cb(struct mosquitto *mqtt_client, void *obj, int reason) {
DEBUG_LOGF("MQTT Connnection Callback: %s (%d)\n", mosquitto_strerror(reason), reason);
::_connected = true;
if (reason == 0) {
int rc = mosquitto_publish(mqtt_client, NULL, MQTT_AVAILABILITY_TOPIC, strlen(MQTT_ONLINE_PAYLOAD), MQTT_ONLINE_PAYLOAD, 0, true);
if (rc != MOSQ_ERR_SUCCESS) {
DEBUG_LOGF("MQTT Publish: Failed (%s)\n", mosquitto_strerror(rc));
}
}
}
static void _mqtt_disconnection_cb(struct mosquitto *mqtt_client, void *obj, int reason) {
DEBUG_LOGF("MQTT Disconnnection Callback: %s (%d)\n", mosquitto_strerror(reason), reason);
::_connected = false;
}
static void _mqtt_log_cb(struct mosquitto *mqtt_client, void *obj, int level, const char *message){
if (level != MOSQ_LOG_DEBUG )
DEBUG_LOGF("MQTT Log Callback: %s (%d)\n", message, level);
}
int OSMqtt::_init(void) {
int major, minor, revision;
mosquitto_lib_init();
mosquitto_lib_version(&major, &minor, &revision);
DEBUG_LOGF("MQTT Init: Mosquitto Library v%d.%d.%d\n", major, minor, revision);
if (mqtt_client) { mosquitto_destroy(mqtt_client); mqtt_client = NULL; };
mqtt_client = mosquitto_new("OS", true, NULL);
if (mqtt_client == NULL) {
DEBUG_PRINTF("MQTT Init: Failed to initialise client\n");
return MQTT_ERROR;
}
mosquitto_connect_callback_set(mqtt_client, _mqtt_connection_cb);
mosquitto_disconnect_callback_set(mqtt_client, _mqtt_disconnection_cb);
mosquitto_log_callback_set(mqtt_client, _mqtt_log_cb);
mosquitto_will_set(mqtt_client, MQTT_AVAILABILITY_TOPIC, strlen(MQTT_OFFLINE_PAYLOAD), MQTT_OFFLINE_PAYLOAD, 0, true);
return MQTT_SUCCESS;
}
int OSMqtt::_connect(void) {
int rc;
if (_username[0]) {
rc = mosquitto_username_pw_set(mqtt_client, _username, _password);
if (rc != MOSQ_ERR_SUCCESS) {
DEBUG_LOGF("MQTT Connect: Connection Failed (%s)\n", mosquitto_strerror(rc));
return MQTT_ERROR;
}
}
rc = mosquitto_connect(mqtt_client, _host, _port, MQTT_KEEPALIVE);
if (rc != MOSQ_ERR_SUCCESS) {
DEBUG_LOGF("MQTT Connect: Connection Failed (%s)\n", mosquitto_strerror(rc));
return MQTT_ERROR;
}
// Allow 10ms for the Broker's ack to be received. We need this on start-up so that the
// connection is registered before we attempt to send our first NOTIFY_REBOOT notification.
usleep(10000);
return MQTT_SUCCESS;
}
int OSMqtt::_disconnect(void) {
int rc = mosquitto_disconnect(mqtt_client);
return rc == MOSQ_ERR_SUCCESS ? MQTT_SUCCESS : MQTT_ERROR;
}
bool OSMqtt::_connected(void) { return ::_connected; }
int OSMqtt::_publish(const char *topic, const char *payload) {
int rc = mosquitto_publish(mqtt_client, NULL, topic, strlen(payload), payload, 0, false);
if (rc != MOSQ_ERR_SUCCESS) {
DEBUG_LOGF("MQTT Publish: Failed (%s)\n", mosquitto_strerror(rc));
return MQTT_ERROR;
}
return MQTT_SUCCESS;
}
int OSMqtt::_loop(void) {
return mosquitto_loop(mqtt_client, 0 , 1);
}
const char * OSMqtt::_state_string(int error) {
return mosquitto_strerror(error);
}
#endif