|
| 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