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