A simple IoTHub sample using AMQP as transport

Dependencies:   EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed azure_c_shared_utility serializer wolfSSL azure_uamqp_c

This sample showcases the usage of Azure IoT client libraries with the AMQP transport for sending/receiving raw messages from an IoT Hub.

Revision:
7:678d6405a200
Parent:
4:02142ed0a6af
--- a/simplesample_amqp.c	Thu Sep 17 00:24:46 2015 -0700
+++ b/simplesample_amqp.c	Tue Sep 22 20:39:57 2015 -0700
@@ -1,6 +1,8 @@
 // Copyright (c) Microsoft. All rights reserved.
 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
 
+#include <stdlib.h>
+
 #include <stdio.h>
 #include <stdint.h>
 
@@ -18,39 +20,38 @@
 static const char* connectionString = "[device connection string]";
 
 // Define the Model
-BEGIN_NAMESPACE(MyThermostat);
+BEGIN_NAMESPACE(WeatherStation);
 
-DECLARE_MODEL(ContosoThermostat505,
-WITH_DATA(int, Temperature),
-WITH_DATA(int, Humidity),
-WITH_DATA(bool, LowTemperatureAlarm),
+DECLARE_MODEL(ContosoAnemometer,
+WITH_DATA(ascii_char_ptr, DeviceId),
+WITH_DATA(double, WindSpeed),
 WITH_ACTION(TurnFanOn),
 WITH_ACTION(TurnFanOff),
-WITH_ACTION(SetTemperature, int, DesiredTemperature)
+WITH_ACTION(SetAirResistance, int, Position)
 );
 
-END_NAMESPACE(MyThermostat); 
+END_NAMESPACE(WeatherStation);
 
 DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES)
 
-EXECUTE_COMMAND_RESULT TurnFanOn(ContosoThermostat505* device)
+EXECUTE_COMMAND_RESULT TurnFanOn(ContosoAnemometer* device)
 {
     (void)device;
     (void)printf("Turning fan on.\r\n");
     return EXECUTE_COMMAND_SUCCESS;
 }
 
-EXECUTE_COMMAND_RESULT TurnFanOff(ContosoThermostat505* device)
+EXECUTE_COMMAND_RESULT TurnFanOff(ContosoAnemometer* device)
 {
     (void)device;
     (void)printf("Turning fan off.\r\n");
     return EXECUTE_COMMAND_SUCCESS;
 }
 
-EXECUTE_COMMAND_RESULT SetTemperature(ContosoThermostat505* device, int DesiredTemperature)
+EXECUTE_COMMAND_RESULT SetAirResistance(ContosoAnemometer* device, int Position)
 {
     (void)device;
-    (void)printf("Setting home temperature to %d degrees.\r\n", DesiredTemperature);
+    (void)printf("Setting Air Resistance Position to %d.\r\n", Position);
     return EXECUTE_COMMAND_SUCCESS;
 }
 
@@ -89,12 +90,12 @@
     messageTrackingId++;
 }
 
-static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubNotification(IOTHUB_MESSAGE_HANDLE notificationMessage, void* userContextCallback)
+static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
 {
     IOTHUBMESSAGE_DISPOSITION_RESULT result;
     const unsigned char* buffer;
     size_t size;
-    if (IoTHubMessage_GetByteArray(notificationMessage, &buffer, &size) != IOTHUB_MESSAGE_OK)
+    if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK)
     {
         printf("unable to IoTHubMessage_GetByteArray\r\n");
         result = EXECUTE_COMMAND_ERROR;
@@ -131,18 +132,10 @@
     }
     else
     {
-
         /* Setup IoTHub client configuration */
-
-        IOTHUB_CLIENT_HANDLE iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, IoTHubTransportAmqp_ProvideTransportInterface);
-
-#ifdef MBED_BUILD_TIMESTAMP
-		// For mbed add the certificate information
-		if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
-		{
-			printf("failure to set option \"TrustedCerts\"\r\n");
-		}
-#endif // MBED_BUILD_TIMESTAMP
+        IOTHUB_CLIENT_HANDLE iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol);
+        srand((unsigned int)time(NULL));
+        double avgWindSpeed = 10.0;
 
         if (iotHubClientHandle == NULL)
         {
@@ -150,8 +143,16 @@
         }
         else
         {
-            ContosoThermostat505* Thermostat = CREATE_MODEL_INSTANCE(MyThermostat, ContosoThermostat505);
-            if (Thermostat == NULL)
+#ifdef MBED_BUILD_TIMESTAMP
+            // For mbed add the certificate information
+            if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
+            {
+                (void)printf("failure to set option \"TrustedCerts\"\r\n");
+            }
+#endif // MBED_BUILD_TIMESTAMP
+
+            ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);
+            if (myWeather == NULL)
             {
                 (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
             }
@@ -160,17 +161,16 @@
                 unsigned char* destination;
                 size_t destinationSize;
 
-                if (IoTHubClient_SetNotificationCallback(iotHubClientHandle, IoTHubNotification, Thermostat) != IOTHUB_CLIENT_OK)
+                if (IoTHubClient_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK)
                 {
-                    printf("unable to IoTHubClient_SetNotificationCallback\r\n");
+                    printf("unable to IoTHubClient_SetMessageCallback\r\n");
                 }
                 else
                 {
+                    myWeather->DeviceId = "myFirstDevice";
+                    myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2);
 
-                    Thermostat->Temperature = 67;
-                    Thermostat->Humidity = 42;
-
-                    if (SERIALIZE(&destination, &destinationSize, Thermostat->Temperature, Thermostat->Humidity) != IOT_AGENT_OK)
+                    if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed) != IOT_AGENT_OK)
                     {
                         (void)printf("Failed to serialize\r\n");
                     }
@@ -182,7 +182,7 @@
                     /* wait for commands */
                     (void)getchar();
                 }
-                DESTROY_MODEL_INSTANCE(Thermostat);
+                DESTROY_MODEL_INSTANCE(myWeather);
             }
             IoTHubClient_Destroy(iotHubClientHandle);
         }