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.
subscribe_publish_sample.cpp
00001 /* 00002 * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"). 00005 * You may not use this file except in compliance with the License. 00006 * A copy of the License is located at 00007 * 00008 * http://aws.amazon.com/apache2.0 00009 * 00010 * or in the "license" file accompanying this file. This file is distributed 00011 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 00012 * express or implied. See the License for the specific language governing 00013 * permissions and limitations under the License. 00014 */ 00015 00016 /** 00017 * @file subscribe_publish_sample.c 00018 * @brief simple MQTT publish and subscribe on the same topic 00019 * 00020 * This example takes the parameters from the aws_iot_config.h file and establishes a connection to the AWS IoT MQTT Platform. 00021 * It subscribes and publishes to the same topic - "sdkTest/sub" 00022 * 00023 * If all the certs are correct, you should see the messages received by the application in a loop. 00024 * 00025 * The application takes in the certificate path, host name , port and the number of times the publish should happen. 00026 * 00027 */ 00028 #include <stdio.h> 00029 #include <stdlib.h> 00030 #include <ctype.h> 00031 #include <limits.h> 00032 #include <string.h> 00033 00034 #include "aws_iot_config.h" 00035 #include "aws_iot_log.h" 00036 #include "aws_iot_version.h" 00037 #include "aws_iot_mqtt_client_interface.h" 00038 00039 #include "easy-connect.h" 00040 #include "WNC14A2AInterface.h" 00041 00042 #define HOST_ADDRESS_SIZE 255 00043 00044 Thread aws_subscribe_publish(osPriorityNormal, 16*1024, NULL); 00045 void aws_subscribe_publish_task(void); 00046 00047 /** 00048 * @brief Default cert location 00049 */ 00050 char certDirectory[PATH_MAX + 1] = "../../../certs"; 00051 00052 /** 00053 * @brief Default MQTT HOST URL is pulled from the aws_iot_config.h 00054 */ 00055 char HostAddress[HOST_ADDRESS_SIZE] = AWS_IOT_MQTT_HOST; 00056 00057 /** 00058 * @brief Default MQTT port is pulled from the aws_iot_config.h 00059 */ 00060 uint32_t port = AWS_IOT_MQTT_PORT; 00061 00062 /** 00063 * @brief This parameter will avoid infinite loop of publish and exit the program after certain number of publishes 00064 */ 00065 uint32_t publishCount = 0; 00066 00067 void iot_subscribe_callback_handler(AWS_IoT_Client *pClient, char *topicName, uint16_t topicNameLen, 00068 IoT_Publish_Message_Params *params, void *pData) { 00069 IOT_UNUSED(pData); 00070 IOT_UNUSED(pClient); 00071 IOT_INFO("Subscribe callback"); 00072 IOT_INFO("%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, (char *) params->payload); 00073 } 00074 00075 void disconnectCallbackHandler(AWS_IoT_Client *pClient, void *data) { 00076 IOT_WARN("MQTT Disconnect"); 00077 IoT_Error_t rc = FAILURE; 00078 00079 if(NULL == pClient) { 00080 return; 00081 } 00082 00083 IOT_UNUSED(data); 00084 00085 if(aws_iot_is_autoreconnect_enabled(pClient)) { 00086 IOT_INFO("Auto Reconnect is enabled, Reconnecting attempt will start now"); 00087 } else { 00088 IOT_WARN("Auto Reconnect not enabled. Starting manual reconnect..."); 00089 rc = aws_iot_mqtt_attempt_reconnect(pClient); 00090 if(NETWORK_RECONNECTED == rc) { 00091 IOT_WARN("Manual Reconnect Successful"); 00092 } else { 00093 IOT_WARN("Manual Reconnect Failed - %d", rc); 00094 } 00095 } 00096 } 00097 00098 00099 int main() 00100 { 00101 printf("AWS %s Example.\n",__FILE__); 00102 IOT_INFO("\nAWS IoT SDK Version %d.%d.%d-%s\n", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG); 00103 00104 aws_subscribe_publish.start(aws_subscribe_publish_task); 00105 aws_subscribe_publish.join(); 00106 printf(" - - - - - - - ALL DONE - - - - - - - \n"); 00107 } 00108 00109 void aws_subscribe_publish_task() 00110 { 00111 bool infinitePublishFlag = true; 00112 00113 char rootCA[PATH_MAX + 1]; 00114 char clientCRT[PATH_MAX + 1]; 00115 char clientKey[PATH_MAX + 1]; 00116 char CurrentWD[PATH_MAX + 1]; 00117 char cPayload[100]; 00118 00119 int32_t i = 0; 00120 00121 IoT_Error_t rc = FAILURE; 00122 00123 AWS_IoT_Client client; 00124 IoT_Client_Init_Params mqttInitParams = iotClientInitParamsDefault; 00125 IoT_Client_Connect_Params connectParams = iotClientConnectParamsDefault; 00126 00127 IoT_Publish_Message_Params paramsQOS0; 00128 IoT_Publish_Message_Params paramsQOS1; 00129 00130 IOT_INFO("\nAWS IoT SDK Version %d.%d.%d-%s\n", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG); 00131 00132 snprintf(rootCA, PATH_MAX + 1, "%s/%s/%s", CurrentWD, certDirectory, AWS_IOT_ROOT_CA_FILENAME); 00133 snprintf(clientCRT, PATH_MAX + 1, "%s/%s/%s", CurrentWD, certDirectory, AWS_IOT_CERTIFICATE_FILENAME); 00134 snprintf(clientKey, PATH_MAX + 1, "%s/%s/%s", CurrentWD, certDirectory, AWS_IOT_PRIVATE_KEY_FILENAME); 00135 00136 IOT_DEBUG("rootCA %s", rootCA); 00137 IOT_DEBUG("clientCRT %s", clientCRT); 00138 IOT_DEBUG("clientKey %s", clientKey); 00139 mqttInitParams.enableAutoReconnect = false; // We enable this later below 00140 mqttInitParams.pHostURL = HostAddress; 00141 mqttInitParams.port = port; 00142 mqttInitParams.pRootCALocation = rootCA; 00143 mqttInitParams.pDeviceCertLocation = clientCRT; 00144 mqttInitParams.pDevicePrivateKeyLocation = clientKey; 00145 mqttInitParams.mqttCommandTimeout_ms = 20000; 00146 mqttInitParams.tlsHandshakeTimeout_ms = 5000; 00147 mqttInitParams.isSSLHostnameVerify = true; 00148 mqttInitParams.disconnectHandler = disconnectCallbackHandler; 00149 mqttInitParams.disconnectHandlerData = NULL; 00150 00151 rc = aws_iot_mqtt_init(&client, &mqttInitParams); 00152 if(SUCCESS != rc) { 00153 IOT_ERROR("aws_iot_mqtt_init returned error : %d ", rc); 00154 return; 00155 } 00156 00157 connectParams.keepAliveIntervalInSec = 600; 00158 connectParams.isCleanSession = true; 00159 connectParams.MQTTVersion = MQTT_3_1_1; 00160 connectParams.pClientID = AWS_IOT_MQTT_CLIENT_ID; 00161 connectParams.clientIDLen = (uint16_t) strlen(AWS_IOT_MQTT_CLIENT_ID); 00162 connectParams.isWillMsgPresent = false; 00163 00164 IOT_INFO("Connecting..."); 00165 rc = aws_iot_mqtt_connect(&client, &connectParams); 00166 if(SUCCESS != rc) { 00167 IOT_ERROR("Error(%d) connecting to %s:%d", rc, mqttInitParams.pHostURL, mqttInitParams.port); 00168 return; 00169 } 00170 /* 00171 * Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h 00172 * #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL 00173 * #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL 00174 */ 00175 rc = aws_iot_mqtt_autoreconnect_set_status(&client, true); 00176 if(SUCCESS != rc) { 00177 IOT_ERROR("Unable to set Auto Reconnect to true - %d", rc); 00178 return; 00179 } 00180 00181 IOT_INFO("Subscribing..."); 00182 rc = aws_iot_mqtt_subscribe(&client, "sdkTest/sub", 11, QOS0, iot_subscribe_callback_handler, NULL); 00183 if(SUCCESS != rc) { 00184 IOT_ERROR("Error subscribing : %d ", rc); 00185 return; 00186 } 00187 00188 sprintf(cPayload, "%s : %ld ", "hello from SDK", i); 00189 00190 paramsQOS0.qos = QOS0; 00191 paramsQOS0.payload = (void *) cPayload; 00192 paramsQOS0.isRetained = 0; 00193 00194 paramsQOS1.qos = QOS1; 00195 paramsQOS1.payload = (void *) cPayload; 00196 paramsQOS1.isRetained = 0; 00197 00198 if(publishCount != 0) { 00199 infinitePublishFlag = false; 00200 } 00201 00202 while((NETWORK_ATTEMPTING_RECONNECT == rc || NETWORK_RECONNECTED == rc || SUCCESS == rc) 00203 && (publishCount > 0 || infinitePublishFlag)) { 00204 00205 //Max time the yield function will wait for read messages 00206 rc = aws_iot_mqtt_yield(&client, 100); 00207 if(NETWORK_ATTEMPTING_RECONNECT == rc) { 00208 // If the client is attempting to reconnect we will skip the rest of the loop. 00209 continue; 00210 } 00211 00212 IOT_INFO("-->sleep"); 00213 wait(1); 00214 sprintf(cPayload, "%s : %ld ", "hello from SDK QOS0", i++); 00215 paramsQOS0.payloadLen = strlen(cPayload); 00216 rc = aws_iot_mqtt_publish(&client, "sdkTest/sub", 11, ¶msQOS0); 00217 if(publishCount > 0) { 00218 publishCount--; 00219 } 00220 00221 if(publishCount == 0 && !infinitePublishFlag) { 00222 break; 00223 } 00224 00225 sprintf(cPayload, "%s : %ld ", "hello from SDK QOS1", i++); 00226 paramsQOS1.payloadLen = strlen(cPayload); 00227 rc = aws_iot_mqtt_publish(&client, "sdkTest/sub", 11, ¶msQOS1); 00228 if (rc == MQTT_REQUEST_TIMEOUT_ERROR) { 00229 IOT_WARN("QOS1 publish ack not received.\n"); 00230 rc = SUCCESS; 00231 } 00232 if(publishCount > 0) { 00233 publishCount--; 00234 } 00235 } 00236 00237 // Wait for all the messages to be received 00238 aws_iot_mqtt_yield(&client, 100); 00239 00240 if(SUCCESS != rc) { 00241 IOT_ERROR("An error occurred in the loop.\n"); 00242 } else { 00243 IOT_INFO("Publish done\n"); 00244 } 00245 00246 return; 00247 } 00248 00249
Generated on Tue Jul 12 2022 19:02:38 by
1.7.2