/**\r
* @brief The callback invoked by the MQTT library when the MQTT connection gets\r
* disconnected.\r
- * \r
+ *\r
* @param[in] pvCallbackContext Callback context as provided at the time of\r
* connect.\r
* @param[in] pxCallbackParams Contains the reason why the MQTT connection was\r
* disconnected.\r
*/\r
-static void prvExample_DisconnectCallback( void * pvCallbackContext,\r
- IotMqttCallbackParam_t * pxCallbackParams );\r
+static void prvExample_OnDisconnect( void * pvCallbackContext,\r
+ IotMqttCallbackParam_t * pxCallbackParams );\r
\r
/**\r
* @brief The callback invoked by the MQTT library when a message is received on\r
* a subscribed topic from the MQTT broker.\r
- * \r
+ *\r
* @param[in] pvCallbackContext Callback context as provided at the time of\r
* subscribe.\r
- * @param[in] pxCallbackParams Contain the details about the received message - \r
+ * @param[in] pxCallbackParams Contain the details about the received message -\r
* topic on which the message was received, the received message.\r
*/\r
-static void prvExample_PublishCallback( void * pvCallbackContext,\r
- IotMqttCallbackParam_t * pxCallbackParams );\r
+static void prvExample_OnMessageReceived( void * pvCallbackContext,\r
+ IotMqttCallbackParam_t * pxCallbackParams );\r
\r
/**\r
* @brief Connects to the MQTT broker as specified in mqttexampleMQTT_BROKER_ENDPOINT\r
* and mqttexampleMQTT_BROKER_PORT.\r
- * \r
+ *\r
* @note This example does not use TLS and therefore will not work with MQTT.\r
*/\r
static void prvMQTTConnect( void );\r
static void prvMQTTDisconnect( void );\r
/*-----------------------------------------------------------*/\r
\r
-static void prvExample_DisconnectCallback( void * pvCallbackContext,\r
+static void prvExample_OnDisconnect( void * pvCallbackContext,\r
IotMqttCallbackParam_t * pxCallbackParams )\r
{\r
TaskHandle_t xDemoTaskHandle = ( TaskHandle_t ) pvCallbackContext;\r
}\r
/*-----------------------------------------------------------*/\r
\r
-static void prvExample_PublishCallback( void * pvCallbackContext,\r
+static void prvExample_OnMessageReceived( void * pvCallbackContext,\r
IotMqttCallbackParam_t * pxCallbackParams )\r
{\r
TaskHandle_t xDemoTaskHandle = ( TaskHandle_t ) pvCallbackContext;\r
/* Ensure that the message QoS is as expected. */\r
configASSERT( pxCallbackParams->u.message.info.qos == IOT_MQTT_QOS_1 );\r
\r
+ /* Although this print uses the constants rather than the data from the\r
+ message payload the asserts above have already checked the message payload\r
+ equals the constants, and it is more efficient not to have to worry about\r
+ lengths in the print. */\r
+ configPRINTF( ( "Received %s from topic %s\r\n", mqttexampleMESSAGE, mqttexampleTOPIC ) );\r
+\r
/* Inform the demo task about the message received from the MQTT broker. */\r
xTaskNotify( xDemoTaskHandle,\r
mqttexampleMESSAGE_RECEIVED_BIT,\r
static void prvMQTTDemoTask( void *pvParameters )\r
{\r
IotMqttError_t xResult;\r
-uint32_t ulNotificationValue = 0;\r
+uint32_t ulNotificationValue = 0, ulIterations;\r
+const uint32_t ulMaxIterations = 5UL;\r
const TickType_t xNoDelay = ( TickType_t ) 0;\r
\r
/* Remove compiler warnings about unused parameters. */\r
/* Don't expect any notifications to be pending yet. */\r
configASSERT( ulTaskNotifyTake( pdTRUE, xNoDelay ) == 0 );\r
\r
+\r
+ /******************** CONNECT ****************************************/\r
+\r
/* Establish a connection to the MQTT broker. This example connects to\r
* the MQTT broker as specified in mqttexampleMQTT_BROKER_ENDPOINT and\r
* mqttexampleMQTT_BROKER_PORT at the top of this file. Please change\r
* it to the MQTT broker you want to connect to. Note that this example\r
* does not use TLS and therefore will not work with AWS IoT. */\r
prvMQTTConnect();\r
+ configPRINTF( ( "Connected to %s\r\n", mqttexampleMQTT_BROKER_ENDPOINT ) );\r
+\r
\r
- /* Subscribe to the topic as specified in mqttexampleTOPIC at the top\r
- * of this file. */\r
+ /******************* SUBSCRIBE ***************************************/\r
+\r
+ /* The client is now connected to the broker. Subscribe to the topic\r
+ as specified in mqttexampleTOPIC at the top of this file. This client\r
+ will then publish to the same topic it subscribed to, so will expect\r
+ all the messages it sends to the broker to be sent back to it from the\r
+ broker. */\r
prvMQTTSubscribe();\r
+ configPRINTF( ( "Subscribed to %s\r\n", mqttexampleTOPIC ) );\r
\r
- /* Publish a message on the mqttexampleTOPIC topic as specified at the\r
- * top of this file. */\r
- prvMQTTPublish();\r
\r
- /* Since we are subscribed on the same topic, we will get the same\r
- * message back from the MQTT broker. Wait for the message to be\r
- * received which is informed to us by the publish callback\r
- * (prvExample_PublishCallback) by setting the mqttexampleMESSAGE_RECEIVED_BIT\r
- * in this task's notification value. */\r
- xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */\r
- 0UL, /* Don't clear any bits on exit. */\r
- &( ulNotificationValue ), /* Obtain the notification value. */\r
- pdMS_TO_TICKS( mqttexampleMQTT_TIMEOUT_MS ) );\r
- configASSERT( ( ulNotificationValue & mqttexampleMESSAGE_RECEIVED_BIT ) == mqttexampleMESSAGE_RECEIVED_BIT );\r
+ /******************* PUBLISH 5 TIMES *********************************/\r
\r
- /* Unsubscribe from the topic mqttexampleTOPIC. */\r
- prvMQTTUnsubscribe();\r
+ /* Publish a few messages while connected. */\r
+ for( ulIterations = 0; ulIterations < ulMaxIterations; ulIterations++ )\r
+ {\r
+ /* Publish a message on the mqttexampleTOPIC topic as specified at the\r
+ * top of this file. */\r
+ prvMQTTPublish();\r
+ configPRINTF( ( "Published %s to %s\r\n", mqttexampleMESSAGE, mqttexampleTOPIC ) );\r
+\r
+ /* Since we are subscribed on the same topic, we will get the same\r
+ * message back from the MQTT broker. Wait for the message to be\r
+ * received which is informed to us by the publish callback\r
+ * (prvExample_OnMessageReceived) by setting the\r
+ * mqttexampleMESSAGE_RECEIVED_BIT in this task's notification\r
+ value. */\r
+ xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */\r
+ mqttexampleMESSAGE_RECEIVED_BIT, /* Clear bit on exit. */\r
+ &( ulNotificationValue ), /* Obtain the notification value. */\r
+ pdMS_TO_TICKS( mqttexampleMQTT_TIMEOUT_MS ) );\r
+ configASSERT( ( ulNotificationValue & mqttexampleMESSAGE_RECEIVED_BIT ) == mqttexampleMESSAGE_RECEIVED_BIT );\r
+ }\r
\r
- /* Gracefully disconnect from the MQTT broker by sending an MQTT\r
- * DISCONNECT message. */\r
+\r
+ /******************* UNSUBSCRIBE AND DISCONNECT **********************/\r
+\r
+ /* Unsubscribe from the topic mqttexampleTOPIC the disconnect\r
+ gracefully. */\r
+ prvMQTTUnsubscribe();\r
prvMQTTDisconnect();\r
+ configPRINTF( ( "Disconnected from from %s\r\n\r\n", mqttexampleMQTT_BROKER_ENDPOINT ) );\r
\r
/* Wait for the disconnect operation to complete which is informed to us\r
- * by the disconnect callback (prvExample_DisconnectCallback)by setting\r
+ * by the disconnect callback (prvExample_OnDisconnect)by setting\r
* the mqttexampleDISCONNECTED_BIT in this task's notification value.\r
* Note that all bits are cleared in the task's notification value to\r
* ensure that it is ready for the next run. */\r
xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */\r
- portMAX_DELAY, /* Clear all bits on exit - portMAX_DELAY is used as it is a portable way of having all bits set. */\r
+ mqttexampleDISCONNECTED_BIT, /* Clear bit again on exit. */\r
&( ulNotificationValue ), /* Obtain the notification value. */\r
pdMS_TO_TICKS( mqttexampleMQTT_TIMEOUT_MS ) );\r
configASSERT( ( ulNotificationValue & mqttexampleDISCONNECTED_BIT ) == mqttexampleDISCONNECTED_BIT );\r
\r
- printf( "prvMQTTDemoTask() completed an iteration without hitting an assert.\r\n" );\r
- fflush( stdout );\r
+\r
\r
/* Wait for some time between two iterations to ensure that we do not\r
* bombard the public test mosquitto broker. */\r
+ configPRINTF( ( "prvMQTTDemoTask() completed an iteration without hitting an assert. Total free heap is %u\r\n\r\n", xPortGetFreeHeapSize() ) );\r
vTaskDelay( pdMS_TO_TICKS( 5000 ) );\r
}\r
}\r
xNetworkInfo.pNetworkInterface = IOT_NETWORK_INTERFACE_FREERTOS;\r
\r
/* Setup the callback which is called when the MQTT connection is disconnected. */\r
- xNetworkInfo.disconnectCallback.pCallbackContext = ( void * ) xTaskGetCurrentTaskHandle();\r
- xNetworkInfo.disconnectCallback.function = prvExample_DisconnectCallback;\r
+ xNetworkInfo.disconnectCallback.pCallbackContext = ( void * ) xTaskGetCurrentTaskHandle();//_RB_ Why the task handle?\r
+ xNetworkInfo.disconnectCallback.function = prvExample_OnDisconnect;\r
\r
/****************** MQTT Connection information setup. ********************/\r
- /* This example does not use TLS and therefore won't work with AWS IoT. */\r
+ /* Set this flag to true if connecting to the AWS IoT MQTT broker. This\r
+ example does not use TLS and therefore won't work with AWS IoT. */\r
xConnectInfo.awsIotMqttMode = false;\r
\r
/* Start with a clean session i.e. direct the MQTT broker to discard any\r
* client gets disconnected. */\r
xConnectInfo.pWillInfo = NULL;\r
\r
- /* Send an MQTT PING request every minute. */\r
+ /* Send an MQTT PING request every minute to keep the connection open if\r
+ there is no other MQTT trafic. */\r
xConnectInfo.keepAliveSeconds = mqttexampleKEEP_ALIVE_SECONDS;\r
\r
/* The client identifier is used to uniquely identify this MQTT client to\r
- * the MQTT broker. */\r
+ * the MQTT broker. In a production device the identifier can be something\r
+ unique, such as a device serial number. */\r
xConnectInfo.pClientIdentifier = mqttexampleCLIENT_IDENTIFIER;\r
xConnectInfo.clientIdentifierLength = ( uint16_t ) strlen( mqttexampleCLIENT_IDENTIFIER );\r
\r
xConnectInfo.passwordLength = 0;\r
\r
/* Establish the connection to the MQTT broker - It is a blocking call and\r
- will return only when connection is complete. */\r
+ will return only when connection is complete or a timeout occurrs. */\r
xResult = IotMqtt_Connect( &( xNetworkInfo ),\r
&( xConnectInfo ),\r
mqttexampleMQTT_TIMEOUT_MS,\r
xMQTTSubscription.pTopicFilter = mqttexampleTOPIC;\r
xMQTTSubscription.topicFilterLength = ( uint16_t ) strlen( mqttexampleTOPIC );\r
xMQTTSubscription.callback.pCallbackContext = ( void * ) xTaskGetCurrentTaskHandle();\r
- xMQTTSubscription.callback.function = prvExample_PublishCallback;\r
+ xMQTTSubscription.callback.function = prvExample_OnMessageReceived;\r
\r
/* Use the synchronous API to subscribe - It is a blocking call and only\r
* returns when the subscribe operation is complete. */\r