A simple IoTHub sample using MQTT as transport

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed wolfSSL azure_c_shared_utility iothub_client azure_umqtt_c iothub_mqtt_transport serializer

Committer:
AzureIoTClient
Date:
Thu Oct 04 09:19:48 2018 -0700
Revision:
64:e57100ee63ad
Parent:
63:3bbdcd21171b
1.2.10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 2:eef448cf9eb7 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 2:eef448cf9eb7 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 2:eef448cf9eb7 3
AzureIoTClient 63:3bbdcd21171b 4 //
AzureIoTClient 63:3bbdcd21171b 5 // IMPORTANT: Please read and understand serializer limitations and alternatives as
AzureIoTClient 63:3bbdcd21171b 6 // described ../../readme.md before beginning to use the serializer.
AzureIoTClient 63:3bbdcd21171b 7 //
AzureIoTClient 63:3bbdcd21171b 8
Azure.IoT Build 2:eef448cf9eb7 9 #include <stdlib.h>
Azure.IoT Build 2:eef448cf9eb7 10
Azure.IoT Build 2:eef448cf9eb7 11 #include <stdio.h>
Azure.IoT Build 2:eef448cf9eb7 12 #include <stdint.h>
Azure.IoT Build 2:eef448cf9eb7 13
Azure.IoT Build 2:eef448cf9eb7 14 /* This sample uses the _LL APIs of iothub_client for example purposes.
Azure.IoT Build 2:eef448cf9eb7 15 That does not mean that MQTT only works with the _LL APIs.
Azure.IoT Build 2:eef448cf9eb7 16 Simply changing the using the convenience layer (functions not having _LL)
Azure.IoT Build 2:eef448cf9eb7 17 and removing calls to _DoWork will yield the same results. */
Azure.IoT Build 2:eef448cf9eb7 18
Azure.IoT Build 16:15f02cc1f940 19 #include "azure_c_shared_utility/threadapi.h"
Azure.IoT Build 16:15f02cc1f940 20 #include "azure_c_shared_utility/platform.h"
Azure.IoT Build 2:eef448cf9eb7 21 #include "serializer.h"
Azure.IoT Build 2:eef448cf9eb7 22 #include "iothub_client_ll.h"
Azure.IoT Build 2:eef448cf9eb7 23 #include "iothubtransportmqtt.h"
Azure.IoT.Build 26:3126b9d4187b 24
AzureIoTClient 6:55659b146374 25 #ifdef MBED_BUILD_TIMESTAMP
AzureIoTClient 47:5a11f794a6ae 26 #define SET_TRUSTED_CERT_IN_SAMPLES
AzureIoTClient 47:5a11f794a6ae 27 #endif // MBED_BUILD_TIMESTAMP
AzureIoTClient 47:5a11f794a6ae 28
AzureIoTClient 47:5a11f794a6ae 29 #ifdef SET_TRUSTED_CERT_IN_SAMPLES
AzureIoTClient 6:55659b146374 30 #include "certs.h"
AzureIoTClient 47:5a11f794a6ae 31 #endif // SET_TRUSTED_CERT_IN_SAMPLES
AzureIoTClient 6:55659b146374 32
Azure.IoT Build 2:eef448cf9eb7 33
Azure.IoT Build 2:eef448cf9eb7 34 /*String containing Hostname, Device Id & Device Key in the format: */
Azure.IoT Build 2:eef448cf9eb7 35 /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
Azure.IoT Build 2:eef448cf9eb7 36 static const char* connectionString = "[device connection string]";
Azure.IoT Build 2:eef448cf9eb7 37
Azure.IoT Build 2:eef448cf9eb7 38 // Define the Model
Azure.IoT Build 2:eef448cf9eb7 39 BEGIN_NAMESPACE(WeatherStation);
Azure.IoT Build 2:eef448cf9eb7 40
Azure.IoT Build 2:eef448cf9eb7 41 DECLARE_MODEL(ContosoAnemometer,
Azure.IoT Build 2:eef448cf9eb7 42 WITH_DATA(ascii_char_ptr, DeviceId),
Azure.IoT Build 2:eef448cf9eb7 43 WITH_DATA(int, WindSpeed),
AzureIoTClient 38:6661a95af557 44 WITH_DATA(float, Temperature),
AzureIoTClient 38:6661a95af557 45 WITH_DATA(float, Humidity),
Azure.IoT Build 2:eef448cf9eb7 46 WITH_ACTION(TurnFanOn),
Azure.IoT Build 2:eef448cf9eb7 47 WITH_ACTION(TurnFanOff),
Azure.IoT Build 2:eef448cf9eb7 48 WITH_ACTION(SetAirResistance, int, Position)
Azure.IoT Build 2:eef448cf9eb7 49 );
Azure.IoT Build 2:eef448cf9eb7 50
Azure.IoT Build 2:eef448cf9eb7 51 END_NAMESPACE(WeatherStation);
Azure.IoT Build 2:eef448cf9eb7 52
AzureIoTClient 38:6661a95af557 53 static char propText[1024];
Azure.IoT Build 2:eef448cf9eb7 54
Azure.IoT Build 2:eef448cf9eb7 55 EXECUTE_COMMAND_RESULT TurnFanOn(ContosoAnemometer* device)
Azure.IoT Build 2:eef448cf9eb7 56 {
Azure.IoT Build 2:eef448cf9eb7 57 (void)device;
Azure.IoT Build 2:eef448cf9eb7 58 (void)printf("Turning fan on.\r\n");
Azure.IoT Build 2:eef448cf9eb7 59 return EXECUTE_COMMAND_SUCCESS;
Azure.IoT Build 2:eef448cf9eb7 60 }
Azure.IoT Build 2:eef448cf9eb7 61
Azure.IoT Build 2:eef448cf9eb7 62 EXECUTE_COMMAND_RESULT TurnFanOff(ContosoAnemometer* device)
Azure.IoT Build 2:eef448cf9eb7 63 {
Azure.IoT Build 2:eef448cf9eb7 64 (void)device;
Azure.IoT Build 2:eef448cf9eb7 65 (void)printf("Turning fan off.\r\n");
Azure.IoT Build 2:eef448cf9eb7 66 return EXECUTE_COMMAND_SUCCESS;
Azure.IoT Build 2:eef448cf9eb7 67 }
Azure.IoT Build 2:eef448cf9eb7 68
Azure.IoT Build 2:eef448cf9eb7 69 EXECUTE_COMMAND_RESULT SetAirResistance(ContosoAnemometer* device, int Position)
Azure.IoT Build 2:eef448cf9eb7 70 {
Azure.IoT Build 2:eef448cf9eb7 71 (void)device;
Azure.IoT Build 2:eef448cf9eb7 72 (void)printf("Setting Air Resistance Position to %d.\r\n", Position);
Azure.IoT Build 2:eef448cf9eb7 73 return EXECUTE_COMMAND_SUCCESS;
Azure.IoT Build 2:eef448cf9eb7 74 }
Azure.IoT Build 2:eef448cf9eb7 75
Azure.IoT Build 2:eef448cf9eb7 76 void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
Azure.IoT Build 2:eef448cf9eb7 77 {
AzureIoTClient 17:a6f7340182f9 78 unsigned int messageTrackingId = (unsigned int)(uintptr_t)userContextCallback;
Azure.IoT Build 2:eef448cf9eb7 79
AzureIoTClient 17:a6f7340182f9 80 (void)printf("Message Id: %u Received.\r\n", messageTrackingId);
Azure.IoT Build 2:eef448cf9eb7 81
Azure.IoT Build 2:eef448cf9eb7 82 (void)printf("Result Call Back Called! Result is: %s \r\n", ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
Azure.IoT Build 2:eef448cf9eb7 83 }
Azure.IoT Build 2:eef448cf9eb7 84
AzureIoTClient 38:6661a95af557 85 static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size, ContosoAnemometer *myWeather)
Azure.IoT Build 2:eef448cf9eb7 86 {
Azure.IoT Build 2:eef448cf9eb7 87 static unsigned int messageTrackingId;
Azure.IoT Build 2:eef448cf9eb7 88 IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
Azure.IoT Build 2:eef448cf9eb7 89 if (messageHandle == NULL)
Azure.IoT Build 2:eef448cf9eb7 90 {
Azure.IoT Build 2:eef448cf9eb7 91 printf("unable to create a new IoTHubMessage\r\n");
Azure.IoT Build 2:eef448cf9eb7 92 }
Azure.IoT Build 2:eef448cf9eb7 93 else
Azure.IoT Build 2:eef448cf9eb7 94 {
AzureIoTClient 38:6661a95af557 95 MAP_HANDLE propMap = IoTHubMessage_Properties(messageHandle);
AzureIoTClient 38:6661a95af557 96 (void)sprintf_s(propText, sizeof(propText), myWeather->Temperature > 28 ? "true" : "false");
AzureIoTClient 38:6661a95af557 97 if (Map_AddOrUpdate(propMap, "temperatureAlert", propText) != MAP_OK)
AzureIoTClient 38:6661a95af557 98 {
AzureIoTClient 38:6661a95af557 99 (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
AzureIoTClient 38:6661a95af557 100 }
AzureIoTClient 38:6661a95af557 101
Azure.IoT Build 2:eef448cf9eb7 102 if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)(uintptr_t)messageTrackingId) != IOTHUB_CLIENT_OK)
Azure.IoT Build 2:eef448cf9eb7 103 {
Azure.IoT Build 2:eef448cf9eb7 104 printf("failed to hand over the message to IoTHubClient");
Azure.IoT Build 2:eef448cf9eb7 105 }
Azure.IoT Build 2:eef448cf9eb7 106 else
Azure.IoT Build 2:eef448cf9eb7 107 {
Azure.IoT Build 2:eef448cf9eb7 108 printf("IoTHubClient accepted the message for delivery\r\n");
Azure.IoT Build 2:eef448cf9eb7 109 }
Azure.IoT Build 2:eef448cf9eb7 110 IoTHubMessage_Destroy(messageHandle);
Azure.IoT Build 2:eef448cf9eb7 111 }
Azure.IoT Build 2:eef448cf9eb7 112 messageTrackingId++;
Azure.IoT Build 2:eef448cf9eb7 113 }
Azure.IoT Build 2:eef448cf9eb7 114
Azure.IoT Build 2:eef448cf9eb7 115 /*this function "links" IoTHub to the serialization library*/
Azure.IoT Build 2:eef448cf9eb7 116 static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
Azure.IoT Build 2:eef448cf9eb7 117 {
Azure.IoT Build 2:eef448cf9eb7 118 const unsigned char* buffer;
Azure.IoT Build 2:eef448cf9eb7 119 size_t size;
Azure.IoT Build 2:eef448cf9eb7 120 if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK)
Azure.IoT Build 2:eef448cf9eb7 121 {
AzureIoTClient 51:90c6d50af45f 122 (void)printf("unable to IoTHubMessage_GetByteArray\r\n");
Azure.IoT Build 2:eef448cf9eb7 123 }
Azure.IoT Build 2:eef448cf9eb7 124 else
Azure.IoT Build 2:eef448cf9eb7 125 {
Azure.IoT Build 2:eef448cf9eb7 126 /*buffer is not zero terminated*/
Azure.IoT Build 2:eef448cf9eb7 127 char* temp = malloc(size + 1);
Azure.IoT Build 2:eef448cf9eb7 128 if (temp == NULL)
Azure.IoT Build 2:eef448cf9eb7 129 {
AzureIoTClient 51:90c6d50af45f 130 (void)printf("failed to malloc\r\n");
Azure.IoT Build 2:eef448cf9eb7 131 }
Azure.IoT Build 2:eef448cf9eb7 132 else
Azure.IoT Build 2:eef448cf9eb7 133 {
AzureIoTClient 30:7bb030702a93 134 (void)memcpy(temp, buffer, size);
Azure.IoT Build 2:eef448cf9eb7 135 temp[size] = '\0';
Azure.IoT Build 2:eef448cf9eb7 136 EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp);
AzureIoTClient 51:90c6d50af45f 137 if (executeCommandResult != EXECUTE_COMMAND_SUCCESS)
AzureIoTClient 51:90c6d50af45f 138 {
AzureIoTClient 51:90c6d50af45f 139 (void)printf("Execute command failed\r\n");
AzureIoTClient 51:90c6d50af45f 140 }
Azure.IoT Build 2:eef448cf9eb7 141 free(temp);
Azure.IoT Build 2:eef448cf9eb7 142 }
Azure.IoT Build 2:eef448cf9eb7 143 }
AzureIoTClient 51:90c6d50af45f 144 // MQTT can only accept messages
AzureIoTClient 51:90c6d50af45f 145 return IOTHUBMESSAGE_ACCEPTED;
Azure.IoT Build 2:eef448cf9eb7 146 }
Azure.IoT Build 2:eef448cf9eb7 147
Azure.IoT Build 2:eef448cf9eb7 148 void simplesample_mqtt_run(void)
Azure.IoT Build 2:eef448cf9eb7 149 {
Azure.IoT Build 2:eef448cf9eb7 150 if (platform_init() != 0)
Azure.IoT Build 2:eef448cf9eb7 151 {
Azure.IoT Build 2:eef448cf9eb7 152 (void)printf("Failed to initialize platform.\r\n");
Azure.IoT Build 2:eef448cf9eb7 153 }
Azure.IoT Build 2:eef448cf9eb7 154 else
Azure.IoT Build 2:eef448cf9eb7 155 {
Azure.IoT Build 2:eef448cf9eb7 156 if (serializer_init(NULL) != SERIALIZER_OK)
Azure.IoT Build 2:eef448cf9eb7 157 {
Azure.IoT Build 2:eef448cf9eb7 158 (void)printf("Failed on serializer_init\r\n");
Azure.IoT Build 2:eef448cf9eb7 159 }
Azure.IoT Build 2:eef448cf9eb7 160 else
Azure.IoT Build 2:eef448cf9eb7 161 {
Azure.IoT Build 2:eef448cf9eb7 162 IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol);
Azure.IoT Build 2:eef448cf9eb7 163 srand((unsigned int)time(NULL));
Azure.IoT Build 2:eef448cf9eb7 164 int avgWindSpeed = 10;
AzureIoTClient 38:6661a95af557 165 float minTemperature = 20.0;
AzureIoTClient 38:6661a95af557 166 float minHumidity = 60.0;
Azure.IoT Build 2:eef448cf9eb7 167
Azure.IoT Build 2:eef448cf9eb7 168 if (iotHubClientHandle == NULL)
Azure.IoT Build 2:eef448cf9eb7 169 {
Azure.IoT Build 2:eef448cf9eb7 170 (void)printf("Failed on IoTHubClient_LL_Create\r\n");
Azure.IoT Build 2:eef448cf9eb7 171 }
Azure.IoT Build 2:eef448cf9eb7 172 else
Azure.IoT Build 2:eef448cf9eb7 173 {
AzureIoTClient 47:5a11f794a6ae 174 #ifdef SET_TRUSTED_CERT_IN_SAMPLES
AzureIoTClient 6:55659b146374 175 // For mbed add the certificate information
AzureIoTClient 6:55659b146374 176 if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
AzureIoTClient 6:55659b146374 177 {
AzureIoTClient 6:55659b146374 178 (void)printf("failure to set option \"TrustedCerts\"\r\n");
AzureIoTClient 6:55659b146374 179 }
AzureIoTClient 47:5a11f794a6ae 180 #endif // SET_TRUSTED_CERT_IN_SAMPLES
AzureIoTClient 63:3bbdcd21171b 181
Azure.IoT Build 2:eef448cf9eb7 182
Azure.IoT Build 2:eef448cf9eb7 183 ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);
Azure.IoT Build 2:eef448cf9eb7 184 if (myWeather == NULL)
Azure.IoT Build 2:eef448cf9eb7 185 {
Azure.IoT Build 2:eef448cf9eb7 186 (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
Azure.IoT Build 2:eef448cf9eb7 187 }
Azure.IoT Build 2:eef448cf9eb7 188 else
Azure.IoT Build 2:eef448cf9eb7 189 {
Azure.IoT Build 2:eef448cf9eb7 190 if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK)
Azure.IoT Build 2:eef448cf9eb7 191 {
Azure.IoT Build 2:eef448cf9eb7 192 printf("unable to IoTHubClient_SetMessageCallback\r\n");
Azure.IoT Build 2:eef448cf9eb7 193 }
Azure.IoT Build 2:eef448cf9eb7 194 else
Azure.IoT Build 2:eef448cf9eb7 195 {
Azure.IoT Build 2:eef448cf9eb7 196 myWeather->DeviceId = "myFirstDevice";
Azure.IoT Build 2:eef448cf9eb7 197 myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2);
AzureIoTClient 38:6661a95af557 198 myWeather->Temperature = minTemperature + (rand() % 10);
AzureIoTClient 38:6661a95af557 199 myWeather->Humidity = minHumidity + (rand() % 20);
Azure.IoT Build 2:eef448cf9eb7 200 {
Azure.IoT Build 2:eef448cf9eb7 201 unsigned char* destination;
Azure.IoT Build 2:eef448cf9eb7 202 size_t destinationSize;
AzureIoTClient 38:6661a95af557 203 if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed, myWeather->Temperature, myWeather->Humidity) != CODEFIRST_OK)
Azure.IoT Build 2:eef448cf9eb7 204 {
Azure.IoT Build 2:eef448cf9eb7 205 (void)printf("Failed to serialize\r\n");
Azure.IoT Build 2:eef448cf9eb7 206 }
Azure.IoT Build 2:eef448cf9eb7 207 else
Azure.IoT Build 2:eef448cf9eb7 208 {
AzureIoTClient 38:6661a95af557 209 sendMessage(iotHubClientHandle, destination, destinationSize, myWeather);
Azure.IoT Build 2:eef448cf9eb7 210 free(destination);
Azure.IoT Build 2:eef448cf9eb7 211 }
Azure.IoT Build 2:eef448cf9eb7 212 }
Azure.IoT Build 2:eef448cf9eb7 213
Azure.IoT Build 2:eef448cf9eb7 214 /* wait for commands */
Azure.IoT Build 2:eef448cf9eb7 215 while (1)
Azure.IoT Build 2:eef448cf9eb7 216 {
Azure.IoT Build 2:eef448cf9eb7 217 IoTHubClient_LL_DoWork(iotHubClientHandle);
Azure.IoT Build 2:eef448cf9eb7 218 ThreadAPI_Sleep(100);
Azure.IoT Build 2:eef448cf9eb7 219 }
Azure.IoT Build 2:eef448cf9eb7 220 }
Azure.IoT Build 2:eef448cf9eb7 221
Azure.IoT Build 2:eef448cf9eb7 222 DESTROY_MODEL_INSTANCE(myWeather);
Azure.IoT Build 2:eef448cf9eb7 223 }
Azure.IoT Build 2:eef448cf9eb7 224 IoTHubClient_LL_Destroy(iotHubClientHandle);
Azure.IoT Build 2:eef448cf9eb7 225 }
Azure.IoT Build 2:eef448cf9eb7 226 serializer_deinit();
Azure.IoT Build 2:eef448cf9eb7 227 }
Azure.IoT Build 2:eef448cf9eb7 228 platform_deinit();
Azure.IoT Build 2:eef448cf9eb7 229 }
Azure.IoT Build 2:eef448cf9eb7 230 }