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