Skip to content

Commit 97bfa82

Browse files
committed
MQTT Last Will and Testament
Register a message with the MQTT broker to publish if the client loses connection. Also publish online/offline status - all to a `status` topic.
1 parent 16e5949 commit 97bfa82

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

Diff for: RX_FSK/src/conn-mqtt.cpp

+14-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ TimerHandle_t mqttReconnectTimer;
3636

3737
extern t_wifi_state wifi_state;
3838
char time_str[32];
39-
39+
char lwt[128];
4040

4141
/* Global initalization (on TTGO startup) */
4242
void MQTT::init() {
@@ -64,16 +64,20 @@ void MQTT::netsetup() {
6464

6565
mqttClient.setServer(ip, sonde.config.mqtt.port);
6666
snprintf(clientID, 20, "%s%04d", sonde.config.mqtt.id, (int)random(0, 1000));
67+
snprintf(lwt, sizeof(lwt), "%sstatus", sonde.config.mqtt.prefix);
6768
clientID[20] = 0;
6869
LOG_I(TAG, "pubsub client %s connecting to %s\n", clientID, sonde.config.mqtt.host);
6970
mqttClient.setClientId(clientID);
7071
if (strlen(sonde.config.mqtt.password) > 0) {
7172
mqttClient.setCredentials(sonde.config.mqtt.username, sonde.config.mqtt.password);
7273
}
74+
mqttClient.setWill(lwt, MQTT_QOS, MQTT_RETAIN_TRUE, "lost connection");
75+
7376
MQTT::connectToMqtt();
7477
}
7578

7679
void MQTT::netshutdown() {
80+
mqttClient.publish(lwt, MQTT_QOS, MQTT_RETAIN_TRUE, "offline");
7781
mqttClient.disconnect(false); // nice shutdown....
7882
delay(200);
7983
mqttClient.disconnect(true); // force
@@ -91,6 +95,7 @@ void MQTT::updateStation( PosInfo *pi ) {
9195
unsigned long now = millis();
9296
if ( (lastMqttUptime == 0) || (now - lastMqttUptime >= sonde.config.mqtt.report_interval) ) {
9397
MQTT::connectToMqtt();
98+
mqttClient.publish(lwt, MQTT_QOS, MQTT_RETAIN_TRUE, "online");
9499
publishUptime();
95100
publishPmuInfo();
96101
publishGps();
@@ -115,6 +120,7 @@ int MQTT::connectToMqtt() {
115120
return 0;
116121
LOG_D(TAG, "MQTT not connected, connecting....");
117122
mqttClient.connect();
123+
mqttClient.publish(lwt, MQTT_QOS, MQTT_RETAIN_TRUE, "online");
118124
return 1;
119125
}
120126

@@ -160,7 +166,7 @@ void MQTT::publishUptime()
160166
LOG_D(TAG, "publishUptime: sending %s\n", payload);
161167
char topic[128];
162168
snprintf(topic, 128, "%s%s", sonde.config.mqtt.prefix, "uptime");
163-
mqttClient.publish(topic, 1, 1, payload);
169+
mqttClient.publish(topic, MQTT_QOS, MQTT_RETAIN_TRUE, payload);
164170
}
165171

166172
void MQTT::publishPmuInfo()
@@ -187,7 +193,7 @@ void MQTT::publishPmuInfo()
187193

188194
char topic[128];
189195
snprintf(topic, sizeof(topic), "%s%s", sonde.config.mqtt.prefix, "pmu");
190-
mqttClient.publish(topic, 1, 1, payload);
196+
mqttClient.publish(topic, MQTT_QOS, MQTT_RETAIN_TRUE, payload);
191197
}
192198

193199

@@ -209,7 +215,7 @@ void MQTT::publishGps()
209215

210216
char topic[128];
211217
snprintf(topic, sizeof(topic), "%s%s", sonde.config.mqtt.prefix, "gps");
212-
mqttClient.publish(topic, 1, 1, payload);
218+
mqttClient.publish(topic, MQTT_QOS, MQTT_RETAIN_TRUE, payload);
213219
}
214220

215221

@@ -225,7 +231,7 @@ void MQTT::publishPeak(double pf, int rssi)
225231

226232
char topic[128];
227233
snprintf(topic, sizeof(topic), "%s%s", sonde.config.mqtt.prefix, "spectrum");
228-
mqttClient.publish(topic, 1, /* retain */ false, payload);
234+
mqttClient.publish(topic, MQTT_QOS_NONE, MQTT_RETAIN_FALSE, payload);
229235
}
230236

231237
// What's the scanner looking at?
@@ -243,7 +249,7 @@ void MQTT::publishQRG(int num, const char* type, char* launchsite, float mhz)
243249

244250
char topic[128];
245251
snprintf(topic, sizeof(topic), "%s%s", sonde.config.mqtt.prefix, "qrg");
246-
mqttClient.publish(topic, 1, /*retain*/ false, payload);
252+
mqttClient.publish(topic, MQTT_QOS_NONE, MQTT_RETAIN_FALSE, payload);
247253
}
248254

249255

@@ -257,7 +263,7 @@ void MQTT::publishDebug(char *debugmsg)
257263
snprintf(payload, 256, "{\"msg\": %s}", debugmsg);
258264
char topic[128];
259265
snprintf(topic, sizeof(topic), "%s%s", sonde.config.mqtt.prefix, "debug");
260-
mqttClient.publish(topic, 1, /*retain*/ false, payload);
266+
mqttClient.publish(topic, MQTT_QOS_NONE, MQTT_RETAIN_FALSE, payload);
261267
}
262268

263269
void MQTT::publishPacket(SondeInfo *si)
@@ -279,7 +285,7 @@ void MQTT::publishPacket(SondeInfo *si)
279285
char topic[128];
280286
snprintf(topic, 128, "%s%s", sonde.config.mqtt.prefix, "packet");
281287
LOG_D(TAG, "publishPacket: %s\n", payload);
282-
mqttClient.publish(topic, 1, 1, payload);
288+
mqttClient.publish(topic, MQTT_QOS, MQTT_RETAIN_TRUE, payload);
283289
}
284290

285291
String MQTT::getStatus() {

Diff for: RX_FSK/src/conn-mqtt.h

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
#define MQTT_SEND_DEBUG 0x80
1818
#define MQTT_SEND_ANY (MQTT_SEND_UPTIME|MQTT_SEND_SONDE|MQTT_SEND_PMU|MQTT_SEND_GPS|MQTT_SEND_RFINFO|MQTT_SEND_DEBUG)
1919

20+
#define MQTT_QOS_NONE 0
21+
#define MQTT_QOS 1
22+
#define MQTT_RETAIN_TRUE true
23+
#define MQTT_RETAIN_FALSE false
24+
2025
class MQTT : public Conn
2126
{
2227
public:

Diff for: RX_FSK/version.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
const char *version_name = "rdzTTGOsonde";
2-
const char *version_id = "dev20241229";
2+
const char *version_id = "dev20250102";
33
const int FS_MAJOR=3;
44
const int FS_MINOR=3;

0 commit comments

Comments
 (0)