Skip to content

Commit 53c3332

Browse files
committed
Add publish and subscribe to same topic for mqtt polling to get instant feedback
1 parent a7613a2 commit 53c3332

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/examples/mqtt_polling/mqtt_polling.ino

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#include <lte.h>
1212
#include <mqtt_client.h>
1313

14-
#define MQTT_SUB_TOPIC "mchp_topic_sub"
15-
#define MQTT_PUB_TOPIC "mchp_topic_pub"
14+
#define MQTT_SUB_TOPIC "mchp_topic"
15+
#define MQTT_PUB_TOPIC "mchp_topic"
1616

1717
#define MQTT_THING_NAME "someuniquemchp"
1818
#define MQTT_BROKER "test.mosquitto.org"
@@ -76,13 +76,14 @@ void connectLTE() {
7676

7777
void setup() {
7878
Log.begin(115200);
79-
Log.setLogLevel(LogLevel::DEBUG);
8079
LedCtrl.begin();
8180
LedCtrl.startupCycle();
8281

8382
Log.info("Starting initialization of MQTT Polling");
8483
}
8584

85+
static uint32_t counter = 0;
86+
8687
void loop() {
8788

8889
if (mqttConnected) {
@@ -92,17 +93,19 @@ void loop() {
9293
// Read message will return an empty string if there were no new
9394
// messages, so anything other than that means that there were a new
9495
// message
95-
9696
if (message != "") {
97-
Log.info("Got new message: ");
98-
Log.info(message);
99-
Log.info("\r\n");
97+
Log.infof("Got new message: %s\r\n", message.c_str());
10098
}
10199

100+
String message_to_publish = String("Hello world: " + String(counter));
101+
102102
bool publishedSuccessfully =
103-
MqttClient.publish(MQTT_PUB_TOPIC, "hello world");
103+
MqttClient.publish(MQTT_PUB_TOPIC, message_to_publish.c_str());
104104

105-
if (!publishedSuccessfully) {
105+
if (publishedSuccessfully) {
106+
Log.infof("Published message: %s\r\n", message_to_publish.c_str());
107+
counter++;
108+
} else {
106109
Log.error("Failed to publish");
107110
}
108111
} else {

0 commit comments

Comments
 (0)