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