Temperature sensor anomaly IoTHub sample

Dependencies:   C12832 EthernetInterface LM75B NTPClient iothub_client iothub_http_transport mbed-rtos mbed serializer wolfSSL azure_c_shared_utility

This sample showcases the usage of Azure IoT client libraries to build an application sample that uploads temperature data and reacts to an alert for a temperature anomaly sent by a cloud service.

Committer:
AzureIoTClient
Date:
Thu Sep 17 00:03:37 2015 -0700
Revision:
4:b0b512a80572
Parent:
2:e6752ef906fc
Child:
5:4afe2073d473
New release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 0:d6584131df08 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 0:d6584131df08 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 0:d6584131df08 3
AzureIoTClient 0:d6584131df08 4 #include <stdlib.h>
AzureIoTClient 0:d6584131df08 5 #include "EthernetInterface.h"
AzureIoTClient 0:d6584131df08 6 #include "iothubtransporthttp.h"
AzureIoTClient 0:d6584131df08 7 #include "serializer.h"
AzureIoTClient 0:d6584131df08 8 #include "schemaserializer.h"
AzureIoTClient 0:d6584131df08 9 #include "threadapi.h"
AzureIoTClient 0:d6584131df08 10 #include "C12832.h"
AzureIoTClient 0:d6584131df08 11 #include "LM75B.h"
AzureIoTClient 0:d6584131df08 12 #include "NTPClient.h"
AzureIoTClient 0:d6584131df08 13
AzureIoTClient 0:d6584131df08 14 C12832 lcd(D11, D13, D12, D7, D10);
AzureIoTClient 0:d6584131df08 15 LM75B sensor(D14, D15);
AzureIoTClient 0:d6584131df08 16 DigitalIn Fire(D4);
AzureIoTClient 0:d6584131df08 17 DigitalOut red_led(D5);
AzureIoTClient 0:d6584131df08 18 DigitalOut blue_led(D8);
AzureIoTClient 0:d6584131df08 19 DigitalOut green_led(D9);
AzureIoTClient 0:d6584131df08 20 PwmOut spkr(D6);
AzureIoTClient 0:d6584131df08 21 Serial pc(USBTX, USBRX);
AzureIoTClient 0:d6584131df08 22
AzureIoTClient 4:b0b512a80572 23 static const char* connectionString = "[device connection string]";
AzureIoTClient 4:b0b512a80572 24 static const char* deviceId = "[deviceName]"; /*must match the one on connectionString*/
AzureIoTClient 0:d6584131df08 25
AzureIoTClient 0:d6584131df08 26 static Timer led_timer;
AzureIoTClient 0:d6584131df08 27 static unsigned char alarm_type;
AzureIoTClient 0:d6584131df08 28 static unsigned char led_on;
AzureIoTClient 0:d6584131df08 29 static unsigned int last_alarm_time;
AzureIoTClient 0:d6584131df08 30 static unsigned int last_edge_time;
AzureIoTClient 0:d6584131df08 31 static unsigned int blink_interval;
AzureIoTClient 0:d6584131df08 32 static float temp;
AzureIoTClient 0:d6584131df08 33 static char* sensorId = NULL;
AzureIoTClient 0:d6584131df08 34
AzureIoTClient 0:d6584131df08 35 #define ALARM_NONE 0
AzureIoTClient 0:d6584131df08 36 #define ALARM_ANOMALY 1
AzureIoTClient 0:d6584131df08 37 #define ALARM_THRESHOLD 2
AzureIoTClient 0:d6584131df08 38
AzureIoTClient 0:d6584131df08 39 #define BLINK_TIME 5000 /* ms */
AzureIoTClient 0:d6584131df08 40 #define BLINK_INTERVAL_ANOMALY 250 /* ms */
AzureIoTClient 0:d6584131df08 41 #define BLINK_INTERVAL_THRESHOLD 100 /* ms */
AzureIoTClient 0:d6584131df08 42
AzureIoTClient 0:d6584131df08 43 // Define the Model
AzureIoTClient 0:d6584131df08 44 BEGIN_NAMESPACE(Contoso);
AzureIoTClient 0:d6584131df08 45
AzureIoTClient 0:d6584131df08 46 DECLARE_STRUCT(SystemProperties,
AzureIoTClient 0:d6584131df08 47 ascii_char_ptr, DeviceID,
AzureIoTClient 0:d6584131df08 48 _Bool, Enabled
AzureIoTClient 0:d6584131df08 49 );
AzureIoTClient 0:d6584131df08 50
AzureIoTClient 0:d6584131df08 51 DECLARE_MODEL(FrdmDevice,
AzureIoTClient 0:d6584131df08 52
AzureIoTClient 0:d6584131df08 53 /* Device Info - This is command metadata + some extra fields */
AzureIoTClient 0:d6584131df08 54 WITH_DATA(ascii_char_ptr, ObjectName),
AzureIoTClient 0:d6584131df08 55 WITH_DATA(ascii_char_ptr, ObjectType),
AzureIoTClient 0:d6584131df08 56 WITH_DATA(ascii_char_ptr, Version),
AzureIoTClient 0:d6584131df08 57 WITH_DATA(ascii_char_ptr, TargetAlarmDevice),
AzureIoTClient 0:d6584131df08 58 WITH_DATA(EDM_DATE_TIME_OFFSET, Time),
AzureIoTClient 0:d6584131df08 59 WITH_DATA(float, temp),
AzureIoTClient 0:d6584131df08 60 WITH_DATA(SystemProperties, SystemProperties),
AzureIoTClient 0:d6584131df08 61 WITH_DATA(ascii_char_ptr_no_quotes, Commands),
AzureIoTClient 0:d6584131df08 62
AzureIoTClient 0:d6584131df08 63 /* Commands implemented by the device */
AzureIoTClient 0:d6584131df08 64 WITH_ACTION(AlarmAnomaly, ascii_char_ptr, SensorId),
AzureIoTClient 0:d6584131df08 65 WITH_ACTION(AlarmThreshold, ascii_char_ptr, SensorId)
AzureIoTClient 0:d6584131df08 66 );
AzureIoTClient 0:d6584131df08 67
AzureIoTClient 0:d6584131df08 68 END_NAMESPACE(Contoso);
AzureIoTClient 0:d6584131df08 69
AzureIoTClient 0:d6584131df08 70 static int LED_Update_Thread(void* threadArgument)
AzureIoTClient 0:d6584131df08 71 {
AzureIoTClient 0:d6584131df08 72 unsigned char display_counter = 0;
AzureIoTClient 0:d6584131df08 73 led_timer.start();
AzureIoTClient 0:d6584131df08 74
AzureIoTClient 0:d6584131df08 75 last_alarm_time = led_timer.read_ms() - BLINK_TIME;
AzureIoTClient 0:d6584131df08 76 while (1)
AzureIoTClient 0:d6584131df08 77 {
AzureIoTClient 0:d6584131df08 78 unsigned int current_ms = led_timer.read_ms();
AzureIoTClient 0:d6584131df08 79 float new_temp_value;
AzureIoTClient 0:d6584131df08 80
AzureIoTClient 0:d6584131df08 81 if (alarm_type != ALARM_NONE)
AzureIoTClient 0:d6584131df08 82 {
AzureIoTClient 0:d6584131df08 83 if (current_ms - last_alarm_time > BLINK_TIME)
AzureIoTClient 0:d6584131df08 84 {
AzureIoTClient 0:d6584131df08 85 /* no more alarm */
AzureIoTClient 0:d6584131df08 86 alarm_type = ALARM_NONE;
AzureIoTClient 0:d6584131df08 87 free(sensorId);
AzureIoTClient 0:d6584131df08 88 sensorId = NULL;
AzureIoTClient 0:d6584131df08 89 led_on = 0;
AzureIoTClient 0:d6584131df08 90
AzureIoTClient 0:d6584131df08 91 /* reset LED and clear display and speaker */
AzureIoTClient 0:d6584131df08 92 red_led = 1;
AzureIoTClient 0:d6584131df08 93
AzureIoTClient 0:d6584131df08 94 lcd.cls();
AzureIoTClient 0:d6584131df08 95 spkr = 0.0;
AzureIoTClient 0:d6584131df08 96 }
AzureIoTClient 0:d6584131df08 97 else
AzureIoTClient 0:d6584131df08 98 {
AzureIoTClient 0:d6584131df08 99 if (current_ms - last_edge_time > blink_interval)
AzureIoTClient 0:d6584131df08 100 {
AzureIoTClient 0:d6584131df08 101 led_on = 1 - led_on;
AzureIoTClient 0:d6584131df08 102 last_edge_time = current_ms;
AzureIoTClient 0:d6584131df08 103 }
AzureIoTClient 0:d6584131df08 104
AzureIoTClient 0:d6584131df08 105 if (led_on)
AzureIoTClient 0:d6584131df08 106 {
AzureIoTClient 0:d6584131df08 107 red_led = 0;
AzureIoTClient 0:d6584131df08 108 spkr.period(1.0 / 2000.0);
AzureIoTClient 0:d6584131df08 109 spkr = 0.5;
AzureIoTClient 0:d6584131df08 110 }
AzureIoTClient 0:d6584131df08 111 else
AzureIoTClient 0:d6584131df08 112 {
AzureIoTClient 0:d6584131df08 113 red_led = 1;
AzureIoTClient 0:d6584131df08 114 spkr = 0.0;
AzureIoTClient 0:d6584131df08 115 }
AzureIoTClient 0:d6584131df08 116 }
AzureIoTClient 0:d6584131df08 117 }
AzureIoTClient 0:d6584131df08 118 else
AzureIoTClient 0:d6584131df08 119 {
AzureIoTClient 0:d6584131df08 120 /* alarm off, do nothing */
AzureIoTClient 0:d6584131df08 121 }
AzureIoTClient 0:d6584131df08 122
AzureIoTClient 0:d6584131df08 123 new_temp_value = (sensor.temp() * 9 / 5) + 32;
AzureIoTClient 0:d6584131df08 124 temp = temp + (new_temp_value - temp) / 2;
AzureIoTClient 0:d6584131df08 125
AzureIoTClient 0:d6584131df08 126 display_counter++;
AzureIoTClient 0:d6584131df08 127 if (display_counter == 80)
AzureIoTClient 0:d6584131df08 128 {
AzureIoTClient 0:d6584131df08 129 display_counter = 0;
AzureIoTClient 0:d6584131df08 130 lcd.locate(0, 3);
AzureIoTClient 0:d6584131df08 131 lcd.printf("Temp = %.1f\n", temp);
AzureIoTClient 0:d6584131df08 132 if (sensorId != NULL)
AzureIoTClient 0:d6584131df08 133 {
AzureIoTClient 0:d6584131df08 134 lcd.locate(0, 15);
AzureIoTClient 0:d6584131df08 135 lcd.printf("%s : %s", (alarm_type == ALARM_ANOMALY) ? "Anomaly" : "Thrshld", sensorId);
AzureIoTClient 0:d6584131df08 136 }
AzureIoTClient 0:d6584131df08 137 }
AzureIoTClient 0:d6584131df08 138
AzureIoTClient 0:d6584131df08 139 ThreadAPI_Sleep(10);
AzureIoTClient 0:d6584131df08 140 }
AzureIoTClient 0:d6584131df08 141
AzureIoTClient 0:d6584131df08 142 led_timer.stop();
AzureIoTClient 0:d6584131df08 143
AzureIoTClient 0:d6584131df08 144 return 0;
AzureIoTClient 0:d6584131df08 145 }
AzureIoTClient 0:d6584131df08 146
AzureIoTClient 0:d6584131df08 147 EXECUTE_COMMAND_RESULT AlarmAnomaly(FrdmDevice* frdmDevice, ascii_char_ptr SensorId)
AzureIoTClient 0:d6584131df08 148 {
AzureIoTClient 0:d6584131df08 149 size_t length;
AzureIoTClient 0:d6584131df08 150
AzureIoTClient 0:d6584131df08 151 last_alarm_time = led_timer.read_ms();
AzureIoTClient 0:d6584131df08 152 if (alarm_type != ALARM_THRESHOLD)
AzureIoTClient 0:d6584131df08 153 {
AzureIoTClient 0:d6584131df08 154 if (alarm_type == ALARM_NONE)
AzureIoTClient 0:d6584131df08 155 {
AzureIoTClient 0:d6584131df08 156 last_edge_time = last_alarm_time;
AzureIoTClient 0:d6584131df08 157 }
AzureIoTClient 0:d6584131df08 158 blink_interval = BLINK_INTERVAL_ANOMALY;
AzureIoTClient 0:d6584131df08 159 }
AzureIoTClient 0:d6584131df08 160
AzureIoTClient 0:d6584131df08 161 alarm_type = ALARM_ANOMALY;
AzureIoTClient 0:d6584131df08 162
AzureIoTClient 0:d6584131df08 163 /* clear screen */
AzureIoTClient 0:d6584131df08 164 lcd.cls();
AzureIoTClient 0:d6584131df08 165
AzureIoTClient 0:d6584131df08 166 length = strlen(SensorId);
AzureIoTClient 0:d6584131df08 167 free(sensorId);
AzureIoTClient 0:d6584131df08 168 sensorId = (char*)malloc(length + 1);
AzureIoTClient 0:d6584131df08 169 strcpy(sensorId, SensorId);
AzureIoTClient 0:d6584131df08 170 return EXECUTE_COMMAND_SUCCESS;
AzureIoTClient 0:d6584131df08 171 }
AzureIoTClient 0:d6584131df08 172
AzureIoTClient 0:d6584131df08 173 EXECUTE_COMMAND_RESULT AlarmThreshold(FrdmDevice* frdmDevice, ascii_char_ptr SensorId)
AzureIoTClient 0:d6584131df08 174 {
AzureIoTClient 0:d6584131df08 175 size_t length;
AzureIoTClient 0:d6584131df08 176
AzureIoTClient 0:d6584131df08 177 last_alarm_time = led_timer.read_ms();
AzureIoTClient 0:d6584131df08 178 if (alarm_type != ALARM_THRESHOLD)
AzureIoTClient 0:d6584131df08 179 {
AzureIoTClient 0:d6584131df08 180 last_edge_time = last_alarm_time;
AzureIoTClient 0:d6584131df08 181 }
AzureIoTClient 0:d6584131df08 182 blink_interval = BLINK_INTERVAL_THRESHOLD;
AzureIoTClient 0:d6584131df08 183 alarm_type = ALARM_THRESHOLD;
AzureIoTClient 0:d6584131df08 184
AzureIoTClient 0:d6584131df08 185 /* clear screen */
AzureIoTClient 0:d6584131df08 186 lcd.cls();
AzureIoTClient 0:d6584131df08 187
AzureIoTClient 0:d6584131df08 188 /* print ALARM */
AzureIoTClient 0:d6584131df08 189 length = strlen(SensorId);
AzureIoTClient 0:d6584131df08 190 free(sensorId);
AzureIoTClient 0:d6584131df08 191 sensorId = (char*)malloc(length + 1);
AzureIoTClient 0:d6584131df08 192 strcpy(sensorId, SensorId);
AzureIoTClient 0:d6584131df08 193 return EXECUTE_COMMAND_SUCCESS;
AzureIoTClient 0:d6584131df08 194 }
AzureIoTClient 0:d6584131df08 195
AzureIoTClient 0:d6584131df08 196 /*this functiuon "links" IoTHub to the serialization library*/
AzureIoTClient 0:d6584131df08 197 static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubNotification(IOTHUB_MESSAGE_HANDLE notificationMessage, void* userContextCallback)
AzureIoTClient 0:d6584131df08 198 {
AzureIoTClient 0:d6584131df08 199 const unsigned char* buffer;
AzureIoTClient 0:d6584131df08 200 size_t size;
AzureIoTClient 0:d6584131df08 201 if (IoTHubMessage_GetByteArray(notificationMessage, &buffer, &size) != IOTHUB_MESSAGE_OK)
AzureIoTClient 0:d6584131df08 202 {
AzureIoTClient 0:d6584131df08 203 (void)printf("unable to IoTHubMessage_GetByteArray\r\n");
AzureIoTClient 0:d6584131df08 204 }
AzureIoTClient 0:d6584131df08 205 else
AzureIoTClient 0:d6584131df08 206 {
AzureIoTClient 0:d6584131df08 207 /*buffer is not zero terminated*/
AzureIoTClient 0:d6584131df08 208 STRING_HANDLE temp = STRING_construct_n((char*)buffer, size);
AzureIoTClient 0:d6584131df08 209 if (temp == NULL)
AzureIoTClient 0:d6584131df08 210 {
AzureIoTClient 0:d6584131df08 211 (void)printf("unable to STRING_construct_n\r\n");
AzureIoTClient 0:d6584131df08 212 }
AzureIoTClient 0:d6584131df08 213 else
AzureIoTClient 0:d6584131df08 214 {
AzureIoTClient 0:d6584131df08 215 EXECUTE_COMMAND(userContextCallback, STRING_c_str(temp));
AzureIoTClient 0:d6584131df08 216 STRING_delete(temp);
AzureIoTClient 0:d6584131df08 217 }
AzureIoTClient 0:d6584131df08 218 }
AzureIoTClient 0:d6584131df08 219 return IOTHUBMESSAGE_ACCEPTED;
AzureIoTClient 0:d6584131df08 220 }
AzureIoTClient 0:d6584131df08 221
AzureIoTClient 0:d6584131df08 222 static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size)
AzureIoTClient 0:d6584131df08 223 {
AzureIoTClient 0:d6584131df08 224 IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
AzureIoTClient 0:d6584131df08 225 if (messageHandle == NULL)
AzureIoTClient 0:d6584131df08 226 {
AzureIoTClient 0:d6584131df08 227 (void)printf("unable to create a new IoTHubMessage\r\n");
AzureIoTClient 0:d6584131df08 228 }
AzureIoTClient 0:d6584131df08 229 else
AzureIoTClient 0:d6584131df08 230 {
AzureIoTClient 0:d6584131df08 231 if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, NULL, NULL) != IOTHUB_CLIENT_OK)
AzureIoTClient 0:d6584131df08 232 {
AzureIoTClient 0:d6584131df08 233 (void)printf("failed to hand over the message to IoTHubClient");
AzureIoTClient 0:d6584131df08 234 }
AzureIoTClient 0:d6584131df08 235 else
AzureIoTClient 0:d6584131df08 236 {
AzureIoTClient 0:d6584131df08 237 (void)printf("IoTHubClient accepted the message for delivery\r\n");
AzureIoTClient 0:d6584131df08 238 }
AzureIoTClient 0:d6584131df08 239 IoTHubMessage_Destroy(messageHandle);
AzureIoTClient 0:d6584131df08 240 }
AzureIoTClient 0:d6584131df08 241 }
AzureIoTClient 0:d6584131df08 242
AzureIoTClient 0:d6584131df08 243
AzureIoTClient 0:d6584131df08 244 void setupRealTime(void)
AzureIoTClient 0:d6584131df08 245 {
AzureIoTClient 0:d6584131df08 246 (void)printf("setupRealTime begin\r\n");
AzureIoTClient 0:d6584131df08 247 if (EthernetInterface::connect())
AzureIoTClient 0:d6584131df08 248 {
AzureIoTClient 0:d6584131df08 249 (void)printf("Error initializing EthernetInterface.\r\n");
AzureIoTClient 0:d6584131df08 250 }
AzureIoTClient 0:d6584131df08 251 else
AzureIoTClient 0:d6584131df08 252 {
AzureIoTClient 0:d6584131df08 253 (void)printf("setupRealTime NTP begin\r\n");
AzureIoTClient 0:d6584131df08 254 NTPClient ntp;
AzureIoTClient 0:d6584131df08 255 if (ntp.setTime("0.pool.ntp.org") != 0)
AzureIoTClient 0:d6584131df08 256 {
AzureIoTClient 0:d6584131df08 257 (void)printf("Failed setting time.\r\n");
AzureIoTClient 0:d6584131df08 258 }
AzureIoTClient 0:d6584131df08 259 else
AzureIoTClient 0:d6584131df08 260 {
AzureIoTClient 0:d6584131df08 261 (void)printf("set time correctly!\r\n");
AzureIoTClient 0:d6584131df08 262 }
AzureIoTClient 0:d6584131df08 263 (void)printf("setupRealTime NTP end\r\n");
AzureIoTClient 0:d6584131df08 264 EthernetInterface::disconnect();
AzureIoTClient 0:d6584131df08 265 }
AzureIoTClient 0:d6584131df08 266 (void)printf("setupRealTime end\r\n");
AzureIoTClient 0:d6584131df08 267 }
AzureIoTClient 0:d6584131df08 268
AzureIoTClient 0:d6584131df08 269 int main(void)
AzureIoTClient 0:d6584131df08 270 {
AzureIoTClient 0:d6584131df08 271 pc.baud(115200);
AzureIoTClient 0:d6584131df08 272
AzureIoTClient 0:d6584131df08 273 THREAD_HANDLE ThreadHandle;
AzureIoTClient 0:d6584131df08 274
AzureIoTClient 0:d6584131df08 275 (void)printf("Initializing mbed specific things...\r\n");
AzureIoTClient 2:e6752ef906fc 276
AzureIoTClient 0:d6584131df08 277 (void)printf("doing a one time EthernetInterface::init();\r\n");
AzureIoTClient 2:e6752ef906fc 278 if (EthernetInterface::init() != 0)
AzureIoTClient 2:e6752ef906fc 279 {
AzureIoTClient 2:e6752ef906fc 280 (void)printf("Failed EthernetInterface::init();\r\n");
AzureIoTClient 2:e6752ef906fc 281 return -1;
AzureIoTClient 2:e6752ef906fc 282 }
AzureIoTClient 0:d6584131df08 283 (void)printf("done doing a one time EthernetInterface::init();\r\n");
AzureIoTClient 0:d6584131df08 284
AzureIoTClient 2:e6752ef906fc 285 if (setupRealTime() != 0)
AzureIoTClient 2:e6752ef906fc 286 {
AzureIoTClient 2:e6752ef906fc 287 (void)printf("Failed setting up real time clock\r\n");
AzureIoTClient 2:e6752ef906fc 288 return -1;
AzureIoTClient 2:e6752ef906fc 289 }
AzureIoTClient 0:d6584131df08 290
AzureIoTClient 0:d6584131df08 291 /* clear the LED light upon startup */
AzureIoTClient 0:d6584131df08 292 red_led = 1;
AzureIoTClient 0:d6584131df08 293 blue_led = 1;
AzureIoTClient 0:d6584131df08 294 green_led = 1;
AzureIoTClient 0:d6584131df08 295
AzureIoTClient 0:d6584131df08 296 alarm_type = ALARM_NONE;
AzureIoTClient 0:d6584131df08 297 led_on = 0;
AzureIoTClient 0:d6584131df08 298
AzureIoTClient 0:d6584131df08 299 /* clear the screen */
AzureIoTClient 0:d6584131df08 300 lcd.cls();
AzureIoTClient 0:d6584131df08 301
AzureIoTClient 0:d6584131df08 302 if (ThreadAPI_Create(&ThreadHandle, LED_Update_Thread, NULL) != THREADAPI_OK)
AzureIoTClient 0:d6584131df08 303 {
AzureIoTClient 0:d6584131df08 304 (void)printf("Error spinning LED update thread.\r\n");
AzureIoTClient 0:d6584131df08 305 return -1;
AzureIoTClient 0:d6584131df08 306 }
AzureIoTClient 0:d6584131df08 307
AzureIoTClient 0:d6584131df08 308 /* initialize the IoTHubClient */
AzureIoTClient 0:d6584131df08 309 if (serializer_init(NULL) != SERIALIZER_OK)
AzureIoTClient 0:d6584131df08 310 {
AzureIoTClient 0:d6584131df08 311 (void)printf("Failed on serializer_init\r\n");
AzureIoTClient 0:d6584131df08 312 }
AzureIoTClient 0:d6584131df08 313 else
AzureIoTClient 0:d6584131df08 314 {
AzureIoTClient 0:d6584131df08 315 /* Setup IoTHub client configuration */
AzureIoTClient 0:d6584131df08 316
AzureIoTClient 0:d6584131df08 317 IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, IoTHubTransportHttp_ProvideTransportInterface);
AzureIoTClient 0:d6584131df08 318
AzureIoTClient 0:d6584131df08 319 if (iotHubClientHandle == NULL)
AzureIoTClient 0:d6584131df08 320 {
AzureIoTClient 0:d6584131df08 321 (void)printf("Failed on IoTHubClient_Create\r\n");
AzureIoTClient 0:d6584131df08 322 }
AzureIoTClient 0:d6584131df08 323 else
AzureIoTClient 0:d6584131df08 324 {
AzureIoTClient 0:d6584131df08 325 unsigned int minimumPollingTime = 9; /*because it can poll "after 9 seconds" polls will happen effectively at ~10 seconds*/
AzureIoTClient 0:d6584131df08 326 if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK)
AzureIoTClient 0:d6584131df08 327 {
AzureIoTClient 0:d6584131df08 328 printf("failure to set option \"MinimumPollingTime\"\r\n");
AzureIoTClient 0:d6584131df08 329 }
AzureIoTClient 0:d6584131df08 330
AzureIoTClient 0:d6584131df08 331 FrdmDevice* frdmDevice = CREATE_MODEL_INSTANCE(Contoso, FrdmDevice, true);
AzureIoTClient 0:d6584131df08 332 if (frdmDevice == NULL)
AzureIoTClient 0:d6584131df08 333 {
AzureIoTClient 0:d6584131df08 334 (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
AzureIoTClient 0:d6584131df08 335 }
AzureIoTClient 0:d6584131df08 336 else
AzureIoTClient 0:d6584131df08 337 {
AzureIoTClient 0:d6584131df08 338 IOTHUB_CLIENT_RESULT setNotificationResult = IoTHubClient_LL_SetNotificationCallback(iotHubClientHandle, IoTHubNotification, frdmDevice);
AzureIoTClient 0:d6584131df08 339 if (setNotificationResult != IOTHUB_CLIENT_OK)
AzureIoTClient 0:d6584131df08 340 {
AzureIoTClient 0:d6584131df08 341 (void)printf("unable to IoTHubClient_SetNotificationCallback\r\n");
AzureIoTClient 0:d6584131df08 342 }
AzureIoTClient 0:d6584131df08 343 else
AzureIoTClient 0:d6584131df08 344 {
AzureIoTClient 0:d6584131df08 345 STRING_HANDLE commandsMetadata;
AzureIoTClient 0:d6584131df08 346
AzureIoTClient 0:d6584131df08 347 temp = (sensor.temp() * 9 / 5) + 32;
AzureIoTClient 0:d6584131df08 348
AzureIoTClient 0:d6584131df08 349 /* send the device info upon startup so that the cloud app knows
AzureIoTClient 0:d6584131df08 350 what commands are available and the fact that the device is up */
AzureIoTClient 0:d6584131df08 351 frdmDevice->ObjectType = "DeviceInfo-HW";
AzureIoTClient 0:d6584131df08 352 frdmDevice->ObjectName = "An ALARM device";
AzureIoTClient 0:d6584131df08 353 frdmDevice->Version = "1.0";
AzureIoTClient 0:d6584131df08 354 frdmDevice->SystemProperties.DeviceID = (char*)deviceId;
AzureIoTClient 0:d6584131df08 355 frdmDevice->SystemProperties.Enabled = true;
AzureIoTClient 0:d6584131df08 356
AzureIoTClient 0:d6584131df08 357 /* build the description of the commands on the device */
AzureIoTClient 0:d6584131df08 358 commandsMetadata = STRING_new();
AzureIoTClient 0:d6584131df08 359 if (commandsMetadata == NULL)
AzureIoTClient 0:d6584131df08 360 {
AzureIoTClient 0:d6584131df08 361 (void)printf("Failed on creating string for commands metadata\r\n");
AzureIoTClient 0:d6584131df08 362 }
AzureIoTClient 0:d6584131df08 363 else
AzureIoTClient 0:d6584131df08 364 {
AzureIoTClient 0:d6584131df08 365 /* Serialize the commands metadata as a JSON string before sending */
AzureIoTClient 0:d6584131df08 366 if (SchemaSerializer_SerializeCommandMetadata(GET_MODEL_HANDLE(Contoso, FrdmDevice), commandsMetadata) != SCHEMA_SERIALIZER_OK)
AzureIoTClient 0:d6584131df08 367 {
AzureIoTClient 0:d6584131df08 368 (void)printf("Failed serializing commands metadata\r\n");
AzureIoTClient 0:d6584131df08 369 }
AzureIoTClient 0:d6584131df08 370 else
AzureIoTClient 0:d6584131df08 371 {
AzureIoTClient 0:d6584131df08 372 frdmDevice->Commands = (char*)STRING_c_str(commandsMetadata);
AzureIoTClient 0:d6584131df08 373
AzureIoTClient 0:d6584131df08 374 /* Send the device information and commands metadata to the cloud */
AzureIoTClient 0:d6584131df08 375 {
AzureIoTClient 0:d6584131df08 376 unsigned char* destination;
AzureIoTClient 0:d6584131df08 377 size_t destinationSize;
AzureIoTClient 0:d6584131df08 378 if (SERIALIZE(&destination, &destinationSize, frdmDevice->ObjectName, frdmDevice->ObjectType, frdmDevice->SystemProperties, frdmDevice->Version, frdmDevice->Commands) != IOT_AGENT_OK)
AzureIoTClient 0:d6584131df08 379 {
AzureIoTClient 0:d6584131df08 380 (void)printf("Failed to serialize\r\n");
AzureIoTClient 0:d6584131df08 381 }
AzureIoTClient 0:d6584131df08 382 else
AzureIoTClient 0:d6584131df08 383 {
AzureIoTClient 0:d6584131df08 384 sendMessage(iotHubClientHandle, destination, destinationSize);
AzureIoTClient 0:d6584131df08 385 free(destination);
AzureIoTClient 0:d6584131df08 386 }
AzureIoTClient 0:d6584131df08 387 }
AzureIoTClient 0:d6584131df08 388 }
AzureIoTClient 0:d6584131df08 389
AzureIoTClient 0:d6584131df08 390 STRING_delete(commandsMetadata);
AzureIoTClient 0:d6584131df08 391 }
AzureIoTClient 0:d6584131df08 392
AzureIoTClient 0:d6584131df08 393 frdmDevice->ObjectName = (ascii_char_ptr)deviceId;
AzureIoTClient 0:d6584131df08 394 frdmDevice->ObjectType = "SensorTagEvent";
AzureIoTClient 0:d6584131df08 395 frdmDevice->Version = "1.0";
AzureIoTClient 0:d6584131df08 396 frdmDevice->TargetAlarmDevice = (ascii_char_ptr)deviceId;
AzureIoTClient 0:d6584131df08 397
AzureIoTClient 0:d6584131df08 398 while (1)
AzureIoTClient 0:d6584131df08 399 {
AzureIoTClient 0:d6584131df08 400 unsigned char* destination;
AzureIoTClient 0:d6584131df08 401 size_t destinationSize;
AzureIoTClient 0:d6584131df08 402
AzureIoTClient 0:d6584131df08 403 (void)printf("Sending %.02f\r\n", temp);
AzureIoTClient 0:d6584131df08 404 frdmDevice->temp = temp;
AzureIoTClient 0:d6584131df08 405
AzureIoTClient 0:d6584131df08 406 if (SERIALIZE(&destination, &destinationSize, frdmDevice->ObjectName, frdmDevice->ObjectType, frdmDevice->Version, frdmDevice->TargetAlarmDevice, frdmDevice->temp) != IOT_AGENT_OK)
AzureIoTClient 0:d6584131df08 407 {
AzureIoTClient 0:d6584131df08 408 (void)printf("Failed to serialize\r\n");
AzureIoTClient 0:d6584131df08 409 }
AzureIoTClient 0:d6584131df08 410 else
AzureIoTClient 0:d6584131df08 411 {
AzureIoTClient 0:d6584131df08 412 sendMessage(iotHubClientHandle, destination, destinationSize);
AzureIoTClient 0:d6584131df08 413 free(destination);
AzureIoTClient 0:d6584131df08 414 }
AzureIoTClient 0:d6584131df08 415
AzureIoTClient 0:d6584131df08 416 /* schedule IoTHubClient to send events/receive commands */
AzureIoTClient 0:d6584131df08 417 IoTHubClient_LL_DoWork(iotHubClientHandle);
AzureIoTClient 0:d6584131df08 418 }
AzureIoTClient 0:d6584131df08 419 }
AzureIoTClient 0:d6584131df08 420 DESTROY_MODEL_INSTANCE(frdmDevice);
AzureIoTClient 0:d6584131df08 421 }
AzureIoTClient 0:d6584131df08 422 IoTHubClient_LL_Destroy(iotHubClientHandle);
AzureIoTClient 0:d6584131df08 423 }
AzureIoTClient 0:d6584131df08 424 serializer_deinit();
AzureIoTClient 0:d6584131df08 425 }
AzureIoTClient 0:d6584131df08 426 }