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