Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MQTT
Revision 35:36578356ed8a, committed 2020-03-18
- Comitter:
- ccli8
- Date:
- Wed Mar 18 15:18:19 2020 +0800
- Parent:
- 34:369cb3afc198
- Child:
- 36:1bec082ad582
- Commit message:
- Add timeout for MQTT receiving message with subscribed topic
This is to avoid trap in while-loop.
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Mar 17 20:14:37 2020 +0800
+++ b/main.cpp Wed Mar 18 15:18:19 2020 +0800
@@ -159,6 +159,9 @@
* MQTT lib doesn't tell enough error message. Try to enlarge it. */
const int MAX_MQTT_PACKET_SIZE = 1000;
+/* Timeout for receiving message with subscribed topic */
+const int MQTT_RECEIVE_MESSAGE_WITH_SUBSCRIBED_TOPIC_TIMEOUT_MS = 5000;
+
#endif // End of AWS_IOT_MQTT_TEST
#if AWS_IOT_HTTPS_TEST
@@ -379,6 +382,9 @@
printf("\rMQTT subscribes to %s OK\n", *topic_filter);
}
+ /* Clear count of received message with subscribed topic */
+ clear_message_arrive_count();
+
MQTT::Message message;
int _bpos;
@@ -407,10 +413,20 @@
printf("\rMQTT publishes message to %s OK\n", topic);
/* Receive message with subscribed topic */
+ printf("MQTT receives message with subscribed %s...\n", topic);
+ Timer timer;
+ timer.start();
while (! _message_arrive_count) {
+ if (timer.read_ms() >= MQTT_RECEIVE_MESSAGE_WITH_SUBSCRIBED_TOPIC_TIMEOUT_MS) {
+ printf("MQTT receives message with subscribed %s TIMEOUT\n", topic);
+ break;
+ }
+
_mqtt_client->yield(100);
}
- clear_message_arrive_count();
+ if (_message_arrive_count) {
+ printf("MQTT receives message with subscribed %s OK\n", topic);
+ }
printf("\n");
/* Unsubscribe