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.

Revision:
25:0e41808cdb35
Parent:
22:7a1416b3c440
diff -r bd37152f7783 -r 0e41808cdb35 main.cpp
--- a/main.cpp	Tue Feb 16 22:38:46 2016 +0000
+++ b/main.cpp	Fri Mar 11 17:04:44 2016 -0800
@@ -2,15 +2,13 @@
 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
 
 #include <stdlib.h>
-#include "EthernetInterface.h"
 #include "iothubtransporthttp.h"
 #include "serializer.h"
 #include "schemaserializer.h"
 #include "threadapi.h"
 #include "C12832.h"
 #include "LM75B.h"
-#include "NTPClient.h"
-#include "azureiot_common/platform.h"
+#include "platform.h"
 
 #ifdef MBED_BUILD_TIMESTAMP
 #include "certs.h"
@@ -247,119 +245,156 @@
 
 int main(void)
 {
-    pc.baud(115200);
 	int result;
-    (void)printf("Initializing mbed specific things...\r\n");
 
 	if ((result = platform_init()) != 0)
 	{
 		(void)printf("Error initializing the platform: %d\r\n",result);
-		return -1;
+		result = -1;
 	}
-
-    THREAD_HANDLE ThreadHandle;
-
-    /* clear the LED light upon startup */
-    red_led = 1;
-    blue_led = 1;
-    green_led = 1;
-
-    alarm_type = ALARM_NONE;
-    led_on = 0;
-
-    /* clear the screen */
-    lcd.cls();
-
-    if (ThreadAPI_Create(&ThreadHandle, LED_Update_Thread, NULL) != THREADAPI_OK)
-    {
-        (void)printf("Error spinning LED update thread.\r\n");
-        return -1;
-    }
-
-    /* initialize the IoTHubClient */
-    if (serializer_init(NULL) != SERIALIZER_OK)
-    {
-        (void)printf("Failed on serializer_init\r\n");
-    }
     else
     {
-        /* Setup IoTHub client configuration */
+
+        THREAD_HANDLE ThreadHandle;
 
-        IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol);
+        /* clear the LED light upon startup */
+        red_led = 1;
+        blue_led = 1;
+        green_led = 1;
 
-        if (iotHubClientHandle == NULL)
+        alarm_type = ALARM_NONE;
+        led_on = 0;
+
+        /* clear the screen */
+        lcd.cls();
+
+        if (ThreadAPI_Create(&ThreadHandle, LED_Update_Thread, NULL) != THREADAPI_OK)
         {
-            (void)printf("Failed on IoTHubClient_Create\r\n");
+            (void)printf("Error spinning LED update thread.\r\n");
+            result = -1;
         }
         else
         {
-#ifdef MBED_BUILD_TIMESTAMP
-        	// For mbed add the certificate information
-        	if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
-        	{
-            	printf("failure to set option \"TrustedCerts\"\r\n");
-        	}
-#endif // MBED_BUILD_TIMESTAMP
-
-            // Because it can poll "after 9 seconds" polls will happen 
-            // effectively at ~10 seconds.
-            // Note that for scalabilty, the default value of minimumPollingTime
-            // is 25 minutes. For more information, see:
-            // https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging
-            unsigned int minimumPollingTime = 9;
-            if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK)
+            /* initialize the IoTHubClient */
+            if (serializer_init(NULL) != SERIALIZER_OK)
             {
-                printf("failure to set option \"MinimumPollingTime\"\r\n");
-            }
-
-            FrdmDevice* frdmDevice = CREATE_MODEL_INSTANCE(Contoso, FrdmDevice, true);
-            if (frdmDevice == NULL)
-            {
-                (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
+                (void)printf("Failed on serializer_init\r\n");
+                result = -1;
             }
             else
             {
-                IOTHUB_CLIENT_RESULT setMessageResult = IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, IoTHubMessage, frdmDevice);
-                if (setMessageResult != IOTHUB_CLIENT_OK)
+                /* Setup IoTHub client configuration */
+
+                IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol);
+
+                if (iotHubClientHandle == NULL)
                 {
-                    (void)printf("unable to IoTHubClient_SetMessageCallback\r\n");
+                    (void)printf("Failed on IoTHubClient_Create\r\n");
+                    result = -1;
                 }
                 else
                 {
-                    STRING_HANDLE commandsMetadata;
-
-                    temp = (sensor.temp() * 9 / 5) + 32;
+#ifdef MBED_BUILD_TIMESTAMP
+                    // For mbed add the certificate information
+                    if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
+                    {
+                        printf("failure to set option \"TrustedCerts\"\r\n");
+                    }
+#endif // MBED_BUILD_TIMESTAMP
 
-                    /* send the device info upon startup so that the cloud app knows
-                    what commands are available and the fact that the device is up */
-                    frdmDevice->ObjectType = "DeviceInfo-HW";
-                    frdmDevice->ObjectName = "An ALARM device";
-                    frdmDevice->Version = "1.0";
-                    frdmDevice->SystemProperties.DeviceID = (char*)deviceId;
-                    frdmDevice->SystemProperties.Enabled = true;
+                    // Because it can poll "after 9 seconds" polls will happen 
+                    // effectively at ~10 seconds.
+                    // Note that for scalabilty, the default value of minimumPollingTime
+                    // is 25 minutes. For more information, see:
+                    // https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging
+                    unsigned int minimumPollingTime = 9;
+                    if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK)
+                    {
+                        printf("failure to set option \"MinimumPollingTime\"\r\n");
+                    }
 
-                    /* build the description of the commands on the device */
-                    commandsMetadata = STRING_new();
-                    if (commandsMetadata == NULL)
+                    FrdmDevice* frdmDevice = CREATE_MODEL_INSTANCE(Contoso, FrdmDevice, true);
+                    if (frdmDevice == NULL)
                     {
-                        (void)printf("Failed on creating string for commands metadata\r\n");
+                        (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
+                        result = -1;
                     }
                     else
                     {
-                        /* Serialize the commands metadata as a JSON string before sending */
-                        if (SchemaSerializer_SerializeCommandMetadata(GET_MODEL_HANDLE(Contoso, FrdmDevice), commandsMetadata) != SCHEMA_SERIALIZER_OK)
+                        IOTHUB_CLIENT_RESULT setMessageResult = IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, IoTHubMessage, frdmDevice);
+                        if (setMessageResult != IOTHUB_CLIENT_OK)
                         {
-                            (void)printf("Failed serializing commands metadata\r\n");
+                            (void)printf("unable to IoTHubClient_SetMessageCallback\r\n");
+                            result = -1;
                         }
                         else
                         {
-                            frdmDevice->Commands = (char*)STRING_c_str(commandsMetadata);
+                            STRING_HANDLE commandsMetadata;
+
+                            temp = (sensor.temp() * 9 / 5) + 32;
+
+                            /* send the device info upon startup so that the cloud app knows
+                            what commands are available and the fact that the device is up */
+                            frdmDevice->ObjectType = "DeviceInfo-HW";
+                            frdmDevice->ObjectName = "An ALARM device";
+                            frdmDevice->Version = "1.0";
+                            frdmDevice->SystemProperties.DeviceID = (char*)deviceId;
+                            frdmDevice->SystemProperties.Enabled = true;
 
-                            /* Send the device information and commands metadata to the cloud */
+                            /* build the description of the commands on the device */
+                            commandsMetadata = STRING_new();
+                            if (commandsMetadata == NULL)
+                            {
+                                (void)printf("Failed on creating string for commands metadata\r\n");
+                                result = -1;
+                            }
+                            else
+                            {
+                                /* Serialize the commands metadata as a JSON string before sending */
+                                if (SchemaSerializer_SerializeCommandMetadata(GET_MODEL_HANDLE(Contoso, FrdmDevice), commandsMetadata) != SCHEMA_SERIALIZER_OK)
+                                {
+                                    (void)printf("Failed serializing commands metadata\r\n");
+                                    result = -1;
+                                }
+                                else
+                                {
+                                    frdmDevice->Commands = (char*)STRING_c_str(commandsMetadata);
+
+                                    /* Send the device information and commands metadata to the cloud */
+                                    {
+                                        unsigned char* destination;
+                                        size_t destinationSize;
+                                        if (SERIALIZE(&destination, &destinationSize, frdmDevice->ObjectName, frdmDevice->ObjectType, frdmDevice->SystemProperties, frdmDevice->Version, frdmDevice->Commands) != IOT_AGENT_OK)
+                                        {
+                                            (void)printf("Failed to serialize\r\n");
+                                            result = -1;
+                                        }
+                                        else
+                                        {
+                                            sendMessage(iotHubClientHandle, destination, destinationSize);
+                                            free(destination);
+                                        }
+                                    }
+                                }
+
+                                STRING_delete(commandsMetadata);
+                            }
+
+                            frdmDevice->ObjectName = (ascii_char_ptr)deviceId;
+                            frdmDevice->ObjectType = "SensorTagEvent";
+                            frdmDevice->Version = "1.0";
+                            frdmDevice->TargetAlarmDevice = (ascii_char_ptr)deviceId;
+                            result = 0;
+
+                            while (1)
                             {
                                 unsigned char* destination;
                                 size_t destinationSize;
-                                if (SERIALIZE(&destination, &destinationSize, frdmDevice->ObjectName, frdmDevice->ObjectType, frdmDevice->SystemProperties, frdmDevice->Version, frdmDevice->Commands) != IOT_AGENT_OK)
+
+                                (void)printf("Sending %.02f\r\n", temp);
+                                frdmDevice->temp = temp;
+
+                                if (SERIALIZE(&destination, &destinationSize, frdmDevice->ObjectName, frdmDevice->ObjectType, frdmDevice->Version, frdmDevice->TargetAlarmDevice, frdmDevice->temp) != IOT_AGENT_OK)
                                 {
                                     (void)printf("Failed to serialize\r\n");
                                 }
@@ -368,44 +403,19 @@
                                     sendMessage(iotHubClientHandle, destination, destinationSize);
                                     free(destination);
                                 }
+
+                                /* schedule IoTHubClient to send events/receive commands */
+                                IoTHubClient_LL_DoWork(iotHubClientHandle);
                             }
                         }
-
-                        STRING_delete(commandsMetadata);
+                        DESTROY_MODEL_INSTANCE(frdmDevice);
                     }
-
-                    frdmDevice->ObjectName = (ascii_char_ptr)deviceId;
-                    frdmDevice->ObjectType = "SensorTagEvent";
-                    frdmDevice->Version = "1.0";
-                    frdmDevice->TargetAlarmDevice = (ascii_char_ptr)deviceId;
-
-                    while (1)
-                    {
-                        unsigned char* destination;
-                        size_t destinationSize;
-
-                        (void)printf("Sending %.02f\r\n", temp);
-                        frdmDevice->temp = temp;
-
-                        if (SERIALIZE(&destination, &destinationSize, frdmDevice->ObjectName, frdmDevice->ObjectType, frdmDevice->Version, frdmDevice->TargetAlarmDevice, frdmDevice->temp) != IOT_AGENT_OK)
-                        {
-                            (void)printf("Failed to serialize\r\n");
-                        }
-                        else
-                        {
-                            sendMessage(iotHubClientHandle, destination, destinationSize);
-                            free(destination);
-                        }
-
-                        /* schedule IoTHubClient to send events/receive commands */
-                        IoTHubClient_LL_DoWork(iotHubClientHandle);
-                    }
+                    IoTHubClient_LL_Destroy(iotHubClientHandle);
                 }
-                DESTROY_MODEL_INSTANCE(frdmDevice);
+                serializer_deinit();
             }
-            IoTHubClient_LL_Destroy(iotHubClientHandle);
         }
-        serializer_deinit();
+        platform_deinit();
     }
-	platform_deinit();
+    return result;
 }