Skip to content

Commit 8a1e861

Browse files
authored
Adds support for MQTT Last Will & Testament settings (#26)
Added a method for setting MQTT last will params. The new method is optional. Default values are used if not called which match the original params.
1 parent 78ec8ff commit 8a1e861

File tree

8 files changed

+46
-3
lines changed

8 files changed

+46
-3
lines changed

examples/ChangeName.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
void setup() {
2424

2525
Serial.begin(SERIAL_RATE);
26-
26+
// Optional - setup MQTT last will & testament
27+
bootstrapManager.setMQTTWill("path/for/last/will/message","last will payload",lastWillQOS,lastWillRetain,cleanSession);
2728
bootstrapManager.bootstrapSetup(manageDisconnections, manageHardwareButton, callback);
2829

2930
// ENTER YOUR CODE HERE

examples/ChangeName.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ BootstrapManager bootstrapManager;
3333
/************* MQTT TOPICS **************************/
3434
const char* CHANGE_ME_TOPIC = "tele/changeme/CHANGEME";
3535
const char* CHANGE_ME_JSON_TOPIC = "tele/changeme/CHANGEME_JSON";
36+
static int lastWillQOS = 1;
37+
boolean lastWillRetain = false;
38+
boolean cleanSession = false;
3639

3740

3841
/********************************** FUNCTION DECLARATION (NEEDED BY PLATFORMIO WHILE COMPILING CPP FILES) *****************************************/

src/BootstrapManager.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ void BootstrapManager::bootstrapLoop(void (*manageDisconnections)(), void (*mana
7070

7171
}
7272

73+
/********************************** SET LAST WILL PARAMETERS IN THE Q MANAGER **********************************/
74+
void BootstrapManager::setMQTTWill(const char *topic, const char *payload, const int qos, boolean retain, boolean cleanSession){
75+
queueManager.setMQTTWill(topic, payload, qos, retain, cleanSession);
76+
}
77+
7378
/********************************** SEND A SIMPLE MESSAGE ON THE QUEUE **********************************/
7479
void BootstrapManager::publish(const char *topic, const char *payload, boolean retained) {
7580

src/BootstrapManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class BootstrapManager {
5151
StaticJsonDocument<BUFFER_SIZE> parseHttpMsg(String payload, unsigned int length); // print the message arriving from HTTP
5252
void bootstrapSetup(void (*manageDisconnectionFunction)(), void (*manageHardwareButton)(), void (*callback)(char*, byte*, unsigned int)); // bootstrap setup()
5353
void bootstrapLoop(void (*manageDisconnectionFunction)(), void (*manageQueueSubscription)(), void (*manageHardwareButton)()); // bootstrap loop()
54+
void setMQTTWill(const char *topic, const char *payload, const int qos, boolean retain, boolean cleanSession); // set the last will parameters for mqtt
5455
void publish(const char *topic, const char *payload, boolean retained); // send a message on the queue
5556
void publish(const char *topic, JsonObject objectToSend, boolean retained); // send a message on the queue
5657
void unsubscribe(const char *topic); // unsubscribe to a queue topic

src/Helpers.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ String mqttIP = "XXX";
4242
String mqttPort = "XXX";
4343
String mqttuser = "XXX";
4444
String mqttpass = "XXX";
45+
String mqttWillTopic = "0";
46+
String mqttWillPayload = "0";
47+
int mqttWillQOS = 1;
48+
bool mqttWillRetain = 0;
49+
bool mqttCleanSession = 1;
4550
String additionalParam = "XXX";
4651

4752
long previousMillis = 0;

src/Helpers.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ extern String mqttIP;
5151
extern String mqttPort;
5252
extern String mqttuser;
5353
extern String mqttpass;
54+
extern String mqttWillTopic;
55+
extern String mqttWillPayload;
56+
extern int mqttWillQOS;
57+
extern bool mqttWillRetain;
58+
extern bool mqttCleanSession;
5459
extern String additionalParam;
5560

5661
// Blink LED vars

src/QueueManager.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ void QueueManager::setupMQTTQueue(void (*callback)(char*, byte*, unsigned int))
3535

3636
}
3737

38+
/********************************** SET LAST WILL PARAMETERS **********************************/
39+
void QueueManager::setMQTTWill(const char *topic, const char *payload, const int qos, boolean retain, boolean cleanSession){
40+
mqttWillTopic = topic;
41+
mqttWillPayload = payload;
42+
mqttWillQOS = qos;
43+
mqttWillRetain = retain;
44+
mqttCleanSession = cleanSession;
45+
}
46+
3847
/********************************** MQTT RECONNECT **********************************/
3948
void QueueManager::mqttReconnect(void (*manageDisconnections)(), void (*manageQueueSubscription)(), void (*manageHardwareButton)()) {
4049

@@ -60,10 +69,23 @@ void QueueManager::mqttReconnect(void (*manageDisconnections)(), void (*manageQu
6069

6170
// Attempt to connect to MQTT server with QoS = 1 (pubsubclient supports QoS 1 for subscribe only, published msg have QoS 0 this is why I implemented a custom solution)
6271
boolean mqttSuccess;
72+
73+
Serial.println("MQTT Last Will Params: ");
74+
Serial.print("willTopic: ");
75+
Serial.println(mqttWillTopic);
76+
Serial.print("willPayload: ");
77+
Serial.println(mqttWillPayload);
78+
Serial.print("qos: ");
79+
Serial.println(mqttWillQOS);
80+
Serial.print("retain: ");
81+
Serial.println(mqttWillRetain);
82+
Serial.print("clean session: ");
83+
Serial.println(mqttCleanSession);
84+
6385
if (mqttuser.isEmpty() || mqttpass.isEmpty()) {
64-
mqttSuccess = mqttClient.connect(helper.string2char(deviceName), 0, 1, 0, 0);
86+
mqttSuccess = mqttClient.connect(helper.string2char(deviceName), helper.string2char(mqttWillTopic), mqttWillQOS, mqttWillRetain, helper.string2char(mqttWillPayload));
6587
} else {
66-
mqttSuccess = mqttClient.connect(helper.string2char(deviceName), helper.string2char(mqttuser), helper.string2char(mqttpass), 0, 1, 0, 0, 1);
88+
mqttSuccess = mqttClient.connect(helper.string2char(deviceName), helper.string2char(mqttuser), helper.string2char(mqttpass), helper.string2char(mqttWillTopic), mqttWillQOS, mqttWillRetain, helper.string2char(mqttWillPayload), mqttCleanSession);
6789
}
6890
if (mqttSuccess) {
6991

src/QueueManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class QueueManager {
3131

3232
public:
3333
void setupMQTTQueue(void (*callback)(char*, byte*, unsigned int)); // setup the queue
34+
void setMQTTWill(const char *topic, const char *payload, const int qos, boolean retain, boolean cleanSession); // set the last will parameters for mqtt
3435
void mqttReconnect(void (*manageDisconnections)(), void (*manageQueueSubscription)(), void (*manageHardwareButton)()); // manage reconnection on the queue
3536
void queueLoop(void (*manageDisconnections)(), void (*manageQueueSubscription)(), void (*manageHardwareButton)()); // manage queue loop
3637
void publish(const char *topic, const char *payload, boolean retained); // send a message on the queue

0 commit comments

Comments
 (0)