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:
Azure.IoT Build
Date:
Fri Mar 25 16:10:49 2016 -0700
Revision:
2:eef448cf9eb7
Child:
4:79b01589ddcb
1.0.3

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
Azure.IoT Build 2:eef448cf9eb7 4 #include <stdlib.h>
Azure.IoT Build 2:eef448cf9eb7 5
Azure.IoT Build 2:eef448cf9eb7 6 #include <stdio.h>
Azure.IoT Build 2:eef448cf9eb7 7 #include <stdint.h>
Azure.IoT Build 2:eef448cf9eb7 8
Azure.IoT Build 2:eef448cf9eb7 9 /* This sample uses the _LL APIs of iothub_client for example purposes.
Azure.IoT Build 2:eef448cf9eb7 10 That does not mean that MQTT only works with the _LL APIs.
Azure.IoT Build 2:eef448cf9eb7 11 Simply changing the using the convenience layer (functions not having _LL)
Azure.IoT Build 2:eef448cf9eb7 12 and removing calls to _DoWork will yield the same results. */
Azure.IoT Build 2:eef448cf9eb7 13
Azure.IoT Build 2:eef448cf9eb7 14 #include "serializer.h"
Azure.IoT Build 2:eef448cf9eb7 15 #include "iothub_client_ll.h"
Azure.IoT Build 2:eef448cf9eb7 16 #include "iothubtransportmqtt.h"
Azure.IoT Build 2:eef448cf9eb7 17 #include "threadapi.h"
Azure.IoT Build 2:eef448cf9eb7 18 #include "platform.h"
Azure.IoT Build 2:eef448cf9eb7 19
Azure.IoT Build 2:eef448cf9eb7 20 /*String containing Hostname, Device Id & Device Key in the format: */
Azure.IoT Build 2:eef448cf9eb7 21 /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
Azure.IoT Build 2:eef448cf9eb7 22 static const char* connectionString = "[device connection string]";
Azure.IoT Build 2:eef448cf9eb7 23
Azure.IoT Build 2:eef448cf9eb7 24 // Define the Model
Azure.IoT Build 2:eef448cf9eb7 25 BEGIN_NAMESPACE(WeatherStation);
Azure.IoT Build 2:eef448cf9eb7 26
Azure.IoT Build 2:eef448cf9eb7 27 DECLARE_MODEL(ContosoAnemometer,
Azure.IoT Build 2:eef448cf9eb7 28 WITH_DATA(ascii_char_ptr, DeviceId),
Azure.IoT Build 2:eef448cf9eb7 29 WITH_DATA(int, WindSpeed),
Azure.IoT Build 2:eef448cf9eb7 30 WITH_ACTION(TurnFanOn),
Azure.IoT Build 2:eef448cf9eb7 31 WITH_ACTION(TurnFanOff),
Azure.IoT Build 2:eef448cf9eb7 32 WITH_ACTION(SetAirResistance, int, Position)
Azure.IoT Build 2:eef448cf9eb7 33 );
Azure.IoT Build 2:eef448cf9eb7 34
Azure.IoT Build 2:eef448cf9eb7 35 END_NAMESPACE(WeatherStation);
Azure.IoT Build 2:eef448cf9eb7 36
Azure.IoT Build 2:eef448cf9eb7 37 DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES)
Azure.IoT Build 2:eef448cf9eb7 38
Azure.IoT Build 2:eef448cf9eb7 39 EXECUTE_COMMAND_RESULT TurnFanOn(ContosoAnemometer* device)
Azure.IoT Build 2:eef448cf9eb7 40 {
Azure.IoT Build 2:eef448cf9eb7 41 (void)device;
Azure.IoT Build 2:eef448cf9eb7 42 (void)printf("Turning fan on.\r\n");
Azure.IoT Build 2:eef448cf9eb7 43 return EXECUTE_COMMAND_SUCCESS;
Azure.IoT Build 2:eef448cf9eb7 44 }
Azure.IoT Build 2:eef448cf9eb7 45
Azure.IoT Build 2:eef448cf9eb7 46 EXECUTE_COMMAND_RESULT TurnFanOff(ContosoAnemometer* device)
Azure.IoT Build 2:eef448cf9eb7 47 {
Azure.IoT Build 2:eef448cf9eb7 48 (void)device;
Azure.IoT Build 2:eef448cf9eb7 49 (void)printf("Turning fan off.\r\n");
Azure.IoT Build 2:eef448cf9eb7 50 return EXECUTE_COMMAND_SUCCESS;
Azure.IoT Build 2:eef448cf9eb7 51 }
Azure.IoT Build 2:eef448cf9eb7 52
Azure.IoT Build 2:eef448cf9eb7 53 EXECUTE_COMMAND_RESULT SetAirResistance(ContosoAnemometer* device, int Position)
Azure.IoT Build 2:eef448cf9eb7 54 {
Azure.IoT Build 2:eef448cf9eb7 55 (void)device;
Azure.IoT Build 2:eef448cf9eb7 56 (void)printf("Setting Air Resistance Position to %d.\r\n", Position);
Azure.IoT Build 2:eef448cf9eb7 57 return EXECUTE_COMMAND_SUCCESS;
Azure.IoT Build 2:eef448cf9eb7 58 }
Azure.IoT Build 2:eef448cf9eb7 59
Azure.IoT Build 2:eef448cf9eb7 60 void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
Azure.IoT Build 2:eef448cf9eb7 61 {
Azure.IoT Build 2:eef448cf9eb7 62 int messageTrackingId = (intptr_t)userContextCallback;
Azure.IoT Build 2:eef448cf9eb7 63
Azure.IoT Build 2:eef448cf9eb7 64 (void)printf("Message Id: %d Received.\r\n", messageTrackingId);
Azure.IoT Build 2:eef448cf9eb7 65
Azure.IoT Build 2:eef448cf9eb7 66 (void)printf("Result Call Back Called! Result is: %s \r\n", ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
Azure.IoT Build 2:eef448cf9eb7 67 }
Azure.IoT Build 2:eef448cf9eb7 68
Azure.IoT Build 2:eef448cf9eb7 69 static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size)
Azure.IoT Build 2:eef448cf9eb7 70 {
Azure.IoT Build 2:eef448cf9eb7 71 static unsigned int messageTrackingId;
Azure.IoT Build 2:eef448cf9eb7 72 IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
Azure.IoT Build 2:eef448cf9eb7 73 if (messageHandle == NULL)
Azure.IoT Build 2:eef448cf9eb7 74 {
Azure.IoT Build 2:eef448cf9eb7 75 printf("unable to create a new IoTHubMessage\r\n");
Azure.IoT Build 2:eef448cf9eb7 76 }
Azure.IoT Build 2:eef448cf9eb7 77 else
Azure.IoT Build 2:eef448cf9eb7 78 {
Azure.IoT Build 2:eef448cf9eb7 79 if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)(uintptr_t)messageTrackingId) != IOTHUB_CLIENT_OK)
Azure.IoT Build 2:eef448cf9eb7 80 {
Azure.IoT Build 2:eef448cf9eb7 81 printf("failed to hand over the message to IoTHubClient");
Azure.IoT Build 2:eef448cf9eb7 82 }
Azure.IoT Build 2:eef448cf9eb7 83 else
Azure.IoT Build 2:eef448cf9eb7 84 {
Azure.IoT Build 2:eef448cf9eb7 85 printf("IoTHubClient accepted the message for delivery\r\n");
Azure.IoT Build 2:eef448cf9eb7 86 }
Azure.IoT Build 2:eef448cf9eb7 87 IoTHubMessage_Destroy(messageHandle);
Azure.IoT Build 2:eef448cf9eb7 88 }
Azure.IoT Build 2:eef448cf9eb7 89 free((void*)buffer);
Azure.IoT Build 2:eef448cf9eb7 90 messageTrackingId++;
Azure.IoT Build 2:eef448cf9eb7 91 }
Azure.IoT Build 2:eef448cf9eb7 92
Azure.IoT Build 2:eef448cf9eb7 93 /*this function "links" IoTHub to the serialization library*/
Azure.IoT Build 2:eef448cf9eb7 94 static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
Azure.IoT Build 2:eef448cf9eb7 95 {
Azure.IoT Build 2:eef448cf9eb7 96 IOTHUBMESSAGE_DISPOSITION_RESULT result;
Azure.IoT Build 2:eef448cf9eb7 97 const unsigned char* buffer;
Azure.IoT Build 2:eef448cf9eb7 98 size_t size;
Azure.IoT Build 2:eef448cf9eb7 99 if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK)
Azure.IoT Build 2:eef448cf9eb7 100 {
Azure.IoT Build 2:eef448cf9eb7 101 printf("unable to IoTHubMessage_GetByteArray\r\n");
Azure.IoT Build 2:eef448cf9eb7 102 result = EXECUTE_COMMAND_ERROR;
Azure.IoT Build 2:eef448cf9eb7 103 }
Azure.IoT Build 2:eef448cf9eb7 104 else
Azure.IoT Build 2:eef448cf9eb7 105 {
Azure.IoT Build 2:eef448cf9eb7 106 /*buffer is not zero terminated*/
Azure.IoT Build 2:eef448cf9eb7 107 char* temp = malloc(size + 1);
Azure.IoT Build 2:eef448cf9eb7 108 if (temp == NULL)
Azure.IoT Build 2:eef448cf9eb7 109 {
Azure.IoT Build 2:eef448cf9eb7 110 printf("failed to malloc\r\n");
Azure.IoT Build 2:eef448cf9eb7 111 result = EXECUTE_COMMAND_ERROR;
Azure.IoT Build 2:eef448cf9eb7 112 }
Azure.IoT Build 2:eef448cf9eb7 113 else
Azure.IoT Build 2:eef448cf9eb7 114 {
Azure.IoT Build 2:eef448cf9eb7 115 memcpy(temp, buffer, size);
Azure.IoT Build 2:eef448cf9eb7 116 temp[size] = '\0';
Azure.IoT Build 2:eef448cf9eb7 117 EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp);
Azure.IoT Build 2:eef448cf9eb7 118 result =
Azure.IoT Build 2:eef448cf9eb7 119 (executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED :
Azure.IoT Build 2:eef448cf9eb7 120 (executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED :
Azure.IoT Build 2:eef448cf9eb7 121 IOTHUBMESSAGE_REJECTED;
Azure.IoT Build 2:eef448cf9eb7 122 free(temp);
Azure.IoT Build 2:eef448cf9eb7 123 }
Azure.IoT Build 2:eef448cf9eb7 124 }
Azure.IoT Build 2:eef448cf9eb7 125 return result;
Azure.IoT Build 2:eef448cf9eb7 126 }
Azure.IoT Build 2:eef448cf9eb7 127
Azure.IoT Build 2:eef448cf9eb7 128 void simplesample_mqtt_run(void)
Azure.IoT Build 2:eef448cf9eb7 129 {
Azure.IoT Build 2:eef448cf9eb7 130 if (platform_init() != 0)
Azure.IoT Build 2:eef448cf9eb7 131 {
Azure.IoT Build 2:eef448cf9eb7 132 (void)printf("Failed to initialize platform.\r\n");
Azure.IoT Build 2:eef448cf9eb7 133 }
Azure.IoT Build 2:eef448cf9eb7 134 else
Azure.IoT Build 2:eef448cf9eb7 135 {
Azure.IoT Build 2:eef448cf9eb7 136 if (serializer_init(NULL) != SERIALIZER_OK)
Azure.IoT Build 2:eef448cf9eb7 137 {
Azure.IoT Build 2:eef448cf9eb7 138 (void)printf("Failed on serializer_init\r\n");
Azure.IoT Build 2:eef448cf9eb7 139 }
Azure.IoT Build 2:eef448cf9eb7 140 else
Azure.IoT Build 2:eef448cf9eb7 141 {
Azure.IoT Build 2:eef448cf9eb7 142 IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol);
Azure.IoT Build 2:eef448cf9eb7 143 srand((unsigned int)time(NULL));
Azure.IoT Build 2:eef448cf9eb7 144 int avgWindSpeed = 10;
Azure.IoT Build 2:eef448cf9eb7 145
Azure.IoT Build 2:eef448cf9eb7 146 if (iotHubClientHandle == NULL)
Azure.IoT Build 2:eef448cf9eb7 147 {
Azure.IoT Build 2:eef448cf9eb7 148 (void)printf("Failed on IoTHubClient_LL_Create\r\n");
Azure.IoT Build 2:eef448cf9eb7 149 }
Azure.IoT Build 2:eef448cf9eb7 150 else
Azure.IoT Build 2:eef448cf9eb7 151 {
Azure.IoT Build 2:eef448cf9eb7 152
Azure.IoT Build 2:eef448cf9eb7 153 ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);
Azure.IoT Build 2:eef448cf9eb7 154 if (myWeather == NULL)
Azure.IoT Build 2:eef448cf9eb7 155 {
Azure.IoT Build 2:eef448cf9eb7 156 (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
Azure.IoT Build 2:eef448cf9eb7 157 }
Azure.IoT Build 2:eef448cf9eb7 158 else
Azure.IoT Build 2:eef448cf9eb7 159 {
Azure.IoT Build 2:eef448cf9eb7 160 if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK)
Azure.IoT Build 2:eef448cf9eb7 161 {
Azure.IoT Build 2:eef448cf9eb7 162 printf("unable to IoTHubClient_SetMessageCallback\r\n");
Azure.IoT Build 2:eef448cf9eb7 163 }
Azure.IoT Build 2:eef448cf9eb7 164 else
Azure.IoT Build 2:eef448cf9eb7 165 {
Azure.IoT Build 2:eef448cf9eb7 166 myWeather->DeviceId = "myFirstDevice";
Azure.IoT Build 2:eef448cf9eb7 167 myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2);
Azure.IoT Build 2:eef448cf9eb7 168 {
Azure.IoT Build 2:eef448cf9eb7 169 unsigned char* destination;
Azure.IoT Build 2:eef448cf9eb7 170 size_t destinationSize;
Azure.IoT Build 2:eef448cf9eb7 171 if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed) != IOT_AGENT_OK)
Azure.IoT Build 2:eef448cf9eb7 172 {
Azure.IoT Build 2:eef448cf9eb7 173 (void)printf("Failed to serialize\r\n");
Azure.IoT Build 2:eef448cf9eb7 174 }
Azure.IoT Build 2:eef448cf9eb7 175 else
Azure.IoT Build 2:eef448cf9eb7 176 {
Azure.IoT Build 2:eef448cf9eb7 177 IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(destination, destinationSize);
Azure.IoT Build 2:eef448cf9eb7 178 if (messageHandle == NULL)
Azure.IoT Build 2:eef448cf9eb7 179 {
Azure.IoT Build 2:eef448cf9eb7 180 printf("unable to create a new IoTHubMessage\r\n");
Azure.IoT Build 2:eef448cf9eb7 181 }
Azure.IoT Build 2:eef448cf9eb7 182 else
Azure.IoT Build 2:eef448cf9eb7 183 {
Azure.IoT Build 2:eef448cf9eb7 184 if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)1) != IOTHUB_CLIENT_OK)
Azure.IoT Build 2:eef448cf9eb7 185 {
Azure.IoT Build 2:eef448cf9eb7 186 printf("failed to hand over the message to IoTHubClient");
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 printf("IoTHubClient accepted the message for delivery\r\n");
Azure.IoT Build 2:eef448cf9eb7 191 }
Azure.IoT Build 2:eef448cf9eb7 192
Azure.IoT Build 2:eef448cf9eb7 193 IoTHubMessage_Destroy(messageHandle);
Azure.IoT Build 2:eef448cf9eb7 194 }
Azure.IoT Build 2:eef448cf9eb7 195 free(destination);
Azure.IoT Build 2:eef448cf9eb7 196 }
Azure.IoT Build 2:eef448cf9eb7 197 }
Azure.IoT Build 2:eef448cf9eb7 198
Azure.IoT Build 2:eef448cf9eb7 199 /* wait for commands */
Azure.IoT Build 2:eef448cf9eb7 200 while (1)
Azure.IoT Build 2:eef448cf9eb7 201 {
Azure.IoT Build 2:eef448cf9eb7 202 IoTHubClient_LL_DoWork(iotHubClientHandle);
Azure.IoT Build 2:eef448cf9eb7 203 ThreadAPI_Sleep(100);
Azure.IoT Build 2:eef448cf9eb7 204 }
Azure.IoT Build 2:eef448cf9eb7 205 }
Azure.IoT Build 2:eef448cf9eb7 206
Azure.IoT Build 2:eef448cf9eb7 207 DESTROY_MODEL_INSTANCE(myWeather);
Azure.IoT Build 2:eef448cf9eb7 208 }
Azure.IoT Build 2:eef448cf9eb7 209 IoTHubClient_LL_Destroy(iotHubClientHandle);
Azure.IoT Build 2:eef448cf9eb7 210 }
Azure.IoT Build 2:eef448cf9eb7 211 serializer_deinit();
Azure.IoT Build 2:eef448cf9eb7 212 }
Azure.IoT Build 2:eef448cf9eb7 213 platform_deinit();
Azure.IoT Build 2:eef448cf9eb7 214 }
Azure.IoT Build 2:eef448cf9eb7 215 }