Skip to content

Commit 18af1b6

Browse files
authored
Merge pull request #216 from ben-willmore/retain-flag
Add support for retain flag when publishing
2 parents 1e33ece + 33b62c7 commit 18af1b6

File tree

7 files changed

+200
-11
lines changed

7 files changed

+200
-11
lines changed

Adafruit_MQTT.cpp

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,25 @@ bool Adafruit_MQTT::disconnect() {
366366
}
367367

368368
bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos) {
369-
return publish(topic, (uint8_t *)(data), strlen(data), qos);
369+
return publish(topic, (uint8_t *)(data), strlen(data), false, qos);
370+
}
371+
372+
bool Adafruit_MQTT::publish(const char *topic, const char *data, bool retain,
373+
uint8_t qos) {
374+
return publish(topic, (uint8_t *)(data), strlen(data), retain, qos);
370375
}
371376

372377
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen,
373378
uint8_t qos) {
379+
return publish(topic, data, bLen, false, qos);
380+
}
381+
382+
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen,
383+
bool retain, uint8_t qos) {
374384
// Construct and send publish packet.
375-
uint16_t len =
376-
publishPacket(buffer, topic, data, bLen, qos, (uint16_t)sizeof(buffer));
385+
uint16_t len = publishPacket(buffer, topic, data, bLen, retain, qos,
386+
(uint16_t)sizeof(buffer));
387+
377388
if (!sendPacket(buffer, len))
378389
return false;
379390

@@ -753,6 +764,12 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
753764
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
754765
uint8_t *data, uint16_t bLen, uint8_t qos,
755766
uint16_t maxPacketLen) {
767+
return publishPacket(packet, topic, data, bLen, false, qos, maxPacketLen);
768+
}
769+
770+
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
771+
uint8_t *data, uint16_t bLen, bool retain,
772+
uint8_t qos, uint16_t maxPacketLen) {
756773
uint8_t *p = packet;
757774
uint16_t len = 0;
758775

@@ -782,7 +799,7 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
782799
len += bLen; // remaining len excludes header byte & length field
783800

784801
// Now you can start generating the packet!
785-
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1;
802+
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1 | (retain ? 1 : 0);
786803
p++;
787804

788805
// fill in packet[1] last
@@ -907,33 +924,51 @@ Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver,
907924
topic = feed;
908925
qos = q;
909926
}
910-
bool Adafruit_MQTT_Publish::publish(int32_t i) {
927+
928+
bool Adafruit_MQTT_Publish::publish(int32_t i) { return publish(i, false); }
929+
930+
bool Adafruit_MQTT_Publish::publish(int32_t i, bool retain) {
911931
char payload[12];
912932
ltoa(i, payload, 10);
913-
return mqtt->publish(topic, payload, qos);
933+
return mqtt->publish(topic, payload, retain, qos);
914934
}
915935

916-
bool Adafruit_MQTT_Publish::publish(uint32_t i) {
936+
bool Adafruit_MQTT_Publish::publish(uint32_t i) { return publish(i, false); }
937+
938+
bool Adafruit_MQTT_Publish::publish(uint32_t i, bool retain) {
917939
char payload[11];
918940
ultoa(i, payload, 10);
919-
return mqtt->publish(topic, payload, qos);
941+
return mqtt->publish(topic, payload, retain, qos);
920942
}
921943

922944
bool Adafruit_MQTT_Publish::publish(double f, uint8_t precision) {
945+
return publish(f, false, precision);
946+
}
947+
948+
bool Adafruit_MQTT_Publish::publish(double f, bool retain, uint8_t precision) {
923949
char payload[41]; // Need to technically hold float max, 39 digits and minus
924950
// sign.
925951
dtostrf(f, 0, precision, payload);
926-
return mqtt->publish(topic, payload, qos);
952+
return mqtt->publish(topic, payload, retain, qos);
927953
}
928954

929955
bool Adafruit_MQTT_Publish::publish(const char *payload) {
930-
return mqtt->publish(topic, payload, qos);
956+
return publish(payload, false);
957+
}
958+
959+
bool Adafruit_MQTT_Publish::publish(const char *payload, bool retain) {
960+
return mqtt->publish(topic, payload, retain, qos);
931961
}
932962

933963
// publish buffer of arbitrary length
934964
bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t bLen) {
965+
return publish(payload, bLen, false);
966+
}
967+
968+
bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t bLen,
969+
bool retain) {
935970

936-
return mqtt->publish(topic, payload, bLen, qos);
971+
return mqtt->publish(topic, payload, bLen, retain, qos);
937972
}
938973

939974
// Adafruit_MQTT_Subscribe Definition //////////////////////////////////////////

Adafruit_MQTT.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,12 @@ class Adafruit_MQTT {
190190
// Publish a message to a topic using the specified QoS level. Returns true
191191
// if the message was published, false otherwise.
192192
bool publish(const char *topic, const char *payload, uint8_t qos = 0);
193+
bool publish(const char *topic, const char *payload, bool retain,
194+
uint8_t qos = 0);
193195
bool publish(const char *topic, uint8_t *payload, uint16_t bLen,
194196
uint8_t qos = 0);
197+
bool publish(const char *topic, uint8_t *payload, uint16_t bLen, bool retain,
198+
uint8_t qos = 0);
195199

196200
// Add a subscription to receive messages for a topic. Returns true if the
197201
// subscription could be added or was already present, false otherwise.
@@ -269,6 +273,10 @@ class Adafruit_MQTT {
269273
uint8_t disconnectPacket(uint8_t *packet);
270274
uint16_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload,
271275
uint16_t bLen, uint8_t qos, uint16_t maxPacketLen = 0);
276+
uint16_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload,
277+
uint16_t bLen, bool retain, uint8_t qos,
278+
uint16_t maxPacketLen = 0);
279+
272280
uint8_t subscribePacket(uint8_t *packet, const char *topic, uint8_t qos);
273281
uint8_t unsubscribePacket(uint8_t *packet, const char *topic);
274282
uint8_t pingPacket(uint8_t *packet);
@@ -281,14 +289,19 @@ class Adafruit_MQTT_Publish {
281289
uint8_t qos = 0);
282290

283291
bool publish(const char *s);
292+
bool publish(const char *s, bool retain);
284293
bool publish(
285294
double f,
286295
uint8_t precision =
287296
2); // Precision controls the minimum number of digits after decimal.
288297
// This might be ignored and a higher precision value sent.
298+
bool publish(double f, bool retain, uint8_t precision = 2);
289299
bool publish(int32_t i);
300+
bool publish(int32_t i, bool retain);
290301
bool publish(uint32_t i);
302+
bool publish(uint32_t i, bool retain);
291303
bool publish(uint8_t *b, uint16_t bLen);
304+
bool publish(uint8_t *b, uint16_t bLen, bool retain);
292305

293306
private:
294307
Adafruit_MQTT *mqtt;

examples/mqtt_retain/.due.test.skip

Whitespace-only changes.

examples/mqtt_retain/.leonardo.test.skip

Whitespace-only changes.

examples/mqtt_retain/.uno.test.skip

Whitespace-only changes.

examples/mqtt_retain/.zero.test.skip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

examples/mqtt_retain/mqtt_retain.ino

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/***************************************************
2+
Adafruit MQTT Library Retain Flag Example
3+
4+
This example demonstrates use of the retain flag when publishing messages.
5+
If retain is set, the MQTT broker will store the message. When a new
6+
client subscribes to the topic, the retained message will be republished
7+
to that client. This is useful for configuration messages and 'last known
8+
good' values.
9+
10+
Written by Ben Willmore.
11+
MIT license, all text above must be included in any redistribution
12+
****************************************************/
13+
14+
#include <ESP8266WiFi.h> // use <WiFi.h> for ESP32
15+
#include "Adafruit_MQTT.h"
16+
#include "Adafruit_MQTT_Client.h"
17+
18+
/************************* WiFi Access Point *********************************/
19+
20+
#define WLAN_SSID "...your SSID..."
21+
#define WLAN_PASS "...your password..."
22+
23+
/************************* Adafruit.io Setup *********************************/
24+
25+
#define MQTT_SERVER "...your MQTT server..."
26+
#define MQTT_SERVERPORT 1883 // use 8883 for SSL
27+
#define MQTT_USERNAME "MQTT username"
28+
#define MQTT_KEY "MQTT key"
29+
#define DEVICE_ID "mqtt-retain-example"
30+
31+
/************ Global State (you don't need to change this!) ******************/
32+
33+
// Create a WiFiClient class to connect to the MQTT server.
34+
WiFiClient client;
35+
// or... use WiFiClientSecure for SSL
36+
//WiFiClientSecure client;
37+
38+
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
39+
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_KEY);
40+
41+
/****************************** Feeds ***************************************/
42+
43+
// Set up for publishing and subscribing to the same feed, for demonstration only
44+
Adafruit_MQTT_Publish publish_feed = Adafruit_MQTT_Publish(&mqtt, DEVICE_ID "/temp");
45+
Adafruit_MQTT_Subscribe subscribe_feed = Adafruit_MQTT_Subscribe(&mqtt, DEVICE_ID "/temp");
46+
47+
/*************************** Sketch Code ************************************/
48+
49+
void setup() {
50+
Serial.begin(115200);
51+
delay(10);
52+
53+
Serial.println(F("MQTT retain flag demo"));
54+
55+
// Connect to WiFi access point.
56+
Serial.println(); Serial.println();
57+
Serial.print("Connecting to ");
58+
Serial.println(WLAN_SSID);
59+
60+
WiFi.begin(WLAN_SSID, WLAN_PASS);
61+
while (WiFi.status() != WL_CONNECTED) {
62+
delay(500);
63+
Serial.print(".");
64+
}
65+
Serial.println();
66+
67+
Serial.println("WiFi connected");
68+
Serial.println("IP address: ");
69+
Serial.println(WiFi.localIP());
70+
71+
// Connect to MQTT broker, then publish a retained message and a
72+
// non-retained message.
73+
Serial.print("\nConnecting to MQTT broker...");
74+
MQTT_connect();
75+
Serial.println("connected");
76+
77+
Serial.println("Publishing messages while not subscribed");
78+
publish_feed.publish("This message should be retained", true);
79+
publish_feed.publish("This message should not be retained");
80+
81+
Serial.println("Disconnecting from MQTT broker\n");
82+
mqtt.disconnect();
83+
84+
subscribe_feed.setCallback(subscribe_callback);
85+
mqtt.subscribe(&subscribe_feed);
86+
}
87+
88+
void subscribe_callback(char *data, uint16_t len) {
89+
Serial.print("--> Message received: \"");
90+
Serial.print(data);
91+
Serial.println("\"\n");
92+
}
93+
94+
void loop() {
95+
96+
// Connect to MQTT broker. We should receive the retained message only.
97+
Serial.println("Connecting to broker. Expect to receive retained message:");
98+
MQTT_connect();
99+
100+
mqtt.processPackets(1000);
101+
102+
Serial.println("Publishing non-retained message. Expect to receive it immediately:");
103+
publish_feed.publish("This message should be received immediately but not retained");
104+
105+
mqtt.processPackets(1000);
106+
107+
Serial.println("Publishing retained message. Expect to receive it immediately and on re-subscribing:");
108+
publish_feed.publish("This message should be received immediately AND retained", true);
109+
110+
mqtt.processPackets(10000);
111+
112+
Serial.println("Disconnecting from broker\n");
113+
mqtt.disconnect();
114+
115+
delay(15000);
116+
}
117+
118+
// Function to connect and reconnect as necessary to the MQTT server.
119+
// Should be called in the loop function and it will take care if connecting.
120+
void MQTT_connect() {
121+
int8_t ret;
122+
123+
// Stop if already connected.
124+
if (mqtt.connected()) {
125+
return;
126+
}
127+
128+
uint8_t retries = 3;
129+
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
130+
Serial.println(mqtt.connectErrorString(ret));
131+
Serial.println("Retrying MQTT connection in 5 seconds...");
132+
mqtt.disconnect();
133+
delay(5000); // wait 5 seconds
134+
retries--;
135+
if (retries == 0) {
136+
// basically die and wait for WDT to reset me
137+
while (1);
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)