Skip to content

Commit 3bcd1af

Browse files
committed
MCU8MASS-943 Add MqttClient.end() at the end of the MQTT examples
1 parent c8da653 commit 3bcd1af

File tree

3 files changed

+74
-57
lines changed

3 files changed

+74
-57
lines changed

examples/mqtt_password_authentication/mqtt_password_authentication.ino

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void setup() {
3030
LedCtrl.begin();
3131
LedCtrl.startupCycle();
3232

33-
Log.info("Starting initialization of MQTT with username and password");
33+
Log.info("Starting MQTT with username and password example");
3434

3535
// Establish LTE connection
3636
if (!Lte.begin()) {
@@ -74,32 +74,38 @@ void setup() {
7474
// Halt here
7575
while (1) {}
7676
}
77-
}
7877

79-
void loop() {
78+
// Test MQTT publish and receive
79+
for (uint8_t i = 0; i < 3; i++) {
8080

81-
String message_to_publish = String("Hello world: " + String(counter));
81+
String message_to_publish = String("Hello world: " + String(counter));
8282

83-
bool publishedSuccessfully = MqttClient.publish(MQTT_PUB_TOPIC,
84-
message_to_publish.c_str());
83+
bool publishedSuccessfully =
84+
MqttClient.publish(MQTT_PUB_TOPIC, message_to_publish.c_str());
8585

86-
if (publishedSuccessfully) {
87-
Log.infof("Published message: %s\r\n", message_to_publish.c_str());
88-
counter++;
89-
} else {
90-
Log.error("Failed to publish");
91-
}
86+
if (publishedSuccessfully) {
87+
Log.infof("Published message: %s\r\n", message_to_publish.c_str());
88+
counter++;
89+
} else {
90+
Log.error("Failed to publish");
91+
}
92+
93+
delay(2000);
9294

93-
delay(2000);
95+
String message = MqttClient.readMessage(MQTT_SUB_TOPIC);
9496

95-
String message = MqttClient.readMessage(MQTT_SUB_TOPIC);
97+
// Read message will return an empty string if there were no new
98+
// messages, so anything other than that means that there was a new
99+
// message
100+
if (message != "") {
101+
Log.infof("Got new message: %s\r\n", message.c_str());
102+
}
96103

97-
// Read message will return an empty string if there were no new
98-
// messages, so anything other than that means that there was a new
99-
// message
100-
if (message != "") {
101-
Log.infof("Got new message: %s\r\n", message.c_str());
104+
delay(2000);
102105
}
103106

104-
delay(2000);
107+
Log.infof("Closing MQTT connection");
108+
MqttClient.end();
105109
}
110+
111+
void loop() {}

examples/mqtt_polling/mqtt_polling.ino

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void setup() {
2828
LedCtrl.begin();
2929
LedCtrl.startupCycle();
3030

31-
Log.info("Starting initialization of MQTT Polling");
31+
Log.info("Starting MQTT Polling example");
3232

3333
// Establish LTE connection
3434
if (!Lte.begin()) {
@@ -62,29 +62,35 @@ void setup() {
6262
// Halt here
6363
while (1) {}
6464
}
65-
}
6665

67-
void loop() {
68-
String message_to_publish = String("Hello world: " + String(counter));
66+
// Test MQTT publish and receive
67+
for (uint8_t i = 0; i < 3; i++) {
68+
String message_to_publish = String("Hello world: " + String(counter));
6969

70-
bool publishedSuccessfully = MqttClient.publish(MQTT_PUB_TOPIC,
71-
message_to_publish.c_str());
70+
bool publishedSuccessfully =
71+
MqttClient.publish(MQTT_PUB_TOPIC, message_to_publish.c_str());
7272

73-
if (publishedSuccessfully) {
74-
Log.infof("Published message: %s\r\n", message_to_publish.c_str());
75-
counter++;
76-
} else {
77-
Log.error("Failed to publish");
78-
}
73+
if (publishedSuccessfully) {
74+
Log.infof("Published message: %s\r\n", message_to_publish.c_str());
75+
counter++;
76+
} else {
77+
Log.error("Failed to publish");
78+
}
7979

80-
delay(3000);
80+
delay(3000);
8181

82-
String message = MqttClient.readMessage(MQTT_SUB_TOPIC);
82+
String message = MqttClient.readMessage(MQTT_SUB_TOPIC);
8383

84-
// Read message will return an empty string if there were no new
85-
// messages, so anything other than that means that there was a new
86-
// message
87-
if (message != "") {
88-
Log.infof("Got new message: %s\r\n", message.c_str());
84+
// Read message will return an empty string if there were no new
85+
// messages, so anything other than that means that there was a new
86+
// message
87+
if (message != "") {
88+
Log.infof("Got new message: %s\r\n", message.c_str());
89+
}
8990
}
91+
92+
Log.infof("Closing MQTT connection");
93+
MqttClient.end();
9094
}
95+
96+
void loop() {}

examples/mqtt_polling_aws/mqtt_polling_aws.ino

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void setup() {
4747
LedCtrl.begin();
4848
LedCtrl.startupCycle();
4949

50-
Log.info("Starting initialization of MQTT Polling for AWS\r\n");
50+
Log.info("Starting MQTT Polling for AWS example\r\n");
5151

5252
if (initMQTTTopics() == false) {
5353
Log.error("Unable to initialize the MQTT topics. Stopping...");
@@ -81,29 +81,34 @@ void setup() {
8181
// Halt here
8282
while (1) {}
8383
}
84-
}
8584

86-
void loop() {
85+
// Test MQTT publish and receive
86+
for (uint8_t i = 0; i < 3; i++) {
87+
bool published_successfully =
88+
MqttClient.publish(mqtt_pub_topic, "{\"light\": 9, \"temp\": 9}");
8789

88-
bool published_successfully =
89-
MqttClient.publish(mqtt_pub_topic, "{\"light\": 9, \"temp\": 9}");
90+
if (published_successfully) {
91+
Log.info("Published message");
92+
} else {
93+
Log.error("Failed to publish\r\n");
94+
}
9095

91-
if (published_successfully) {
92-
Log.info("Published message");
93-
} else {
94-
Log.error("Failed to publish\r\n");
95-
}
96+
delay(2000);
9697

97-
delay(2000);
98+
String message = MqttClient.readMessage(mqtt_sub_topic);
9899

99-
String message = MqttClient.readMessage(mqtt_sub_topic);
100+
// Read message will return an empty string if there were no new
101+
// messages, so anything other than that means that there were a
102+
// new message
103+
if (message != "") {
104+
Log.infof("Got new message: %s\r\n", message.c_str());
105+
}
100106

101-
// Read message will return an empty string if there were no new
102-
// messages, so anything other than that means that there were a
103-
// new message
104-
if (message != "") {
105-
Log.infof("Got new message: %s\r\n", message.c_str());
107+
delay(2000);
106108
}
107109

108-
delay(2000);
110+
Log.infof("Closing MQTT connection");
111+
MqttClient.end();
109112
}
113+
114+
void loop() {}

0 commit comments

Comments
 (0)