A simple IoTHub sample using HTTP as transport

Dependencies:   EthernetInterface NTPClient iothub_client iothub_http_transport mbed-rtos mbed wolfSSL serializer azure_c_shared_utility

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

Revision:
8:a9db8d5c22ee
Parent:
7:b2c9d461ef42
Child:
10:c884d9d28696
--- a/simplesample_http.c	Thu Sep 17 00:26:54 2015 -0700
+++ b/simplesample_http.c	Tue Sep 22 20:40:26 2015 -0700
@@ -23,39 +23,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;
 }
 
@@ -93,12 +92,12 @@
 }
 
 /*this functiuon "links" IoTHub to the serialization library*/
-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;
@@ -135,7 +134,9 @@
     }
     else
     {
-        IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, IoTHubTransportHttp_ProvideTransportInterface);
+        IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol);
+        srand((unsigned int)time(NULL));
+        double avgWindSpeed = 10.0;
 
         if (iotHubClientHandle == NULL)
         {
@@ -153,29 +154,29 @@
             // For mbed add the certificate information
             if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
             {
-                printf("failure to set option \"TrustedCerts\"\r\n");
+                (void)printf("failure to set option \"TrustedCerts\"\r\n");
             }
 #endif // MBED_BUILD_TIMESTAMP
 
-            ContosoThermostat505* Thermostat = CREATE_MODEL_INSTANCE(MyThermostat, ContosoThermostat505);
-            if (Thermostat == NULL)
+            ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);
+            if (myWeather == NULL)
             {
                 (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
             }
             else
             {
-                if (IoTHubClient_LL_SetNotificationCallback(iotHubClientHandle, IoTHubNotification, Thermostat) != IOTHUB_CLIENT_OK)
+                if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK)
                 {
-                    printf("unable to IoTHubClient_SetNotificationCallback\r\n");
+                    printf("unable to IoTHubClient_SetMessageCallback\r\n");
                 }
                 else
                 {
-                    Thermostat->Temperature = 67;
-                    Thermostat->Humidity = 42;
+                    myWeather->DeviceId = "myFirstDevice";
+                    myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2);
                     {
                         unsigned char* destination;
                         size_t destinationSize;
-                        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");
                         }
@@ -207,11 +208,11 @@
                     while (1)
                     {
                         IoTHubClient_LL_DoWork(iotHubClientHandle);
-                        ThreadAPI_Sleep(1000);
+                        ThreadAPI_Sleep(100);
                     }
                 }
 
-                DESTROY_MODEL_INSTANCE(Thermostat);
+                DESTROY_MODEL_INSTANCE(myWeather);
             }
             IoTHubClient_LL_Destroy(iotHubClientHandle);
         }