A simple IoTHub sample using MQTT as transport

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed wolfSSL azure_c_shared_utility iothub_client azure_umqtt_c iothub_mqtt_transport serializer

Revision:
38:6661a95af557
Parent:
34:4eea18741566
Child:
47:5a11f794a6ae
diff -r a1f30b352847 -r 6661a95af557 simplesample_mqtt.c
--- a/simplesample_mqtt.c	Fri Apr 21 14:53:32 2017 -0700
+++ b/simplesample_mqtt.c	Mon May 08 10:53:41 2017 -0700
@@ -36,6 +36,8 @@
 DECLARE_MODEL(ContosoAnemometer,
 WITH_DATA(ascii_char_ptr, DeviceId),
 WITH_DATA(int, WindSpeed),
+WITH_DATA(float, Temperature),
+WITH_DATA(float, Humidity),
 WITH_ACTION(TurnFanOn),
 WITH_ACTION(TurnFanOff),
 WITH_ACTION(SetAirResistance, int, Position)
@@ -43,6 +45,7 @@
 
 END_NAMESPACE(WeatherStation);
 
+static char propText[1024];
 
 EXECUTE_COMMAND_RESULT TurnFanOn(ContosoAnemometer* device)
 {
@@ -74,7 +77,7 @@
     (void)printf("Result Call Back Called! Result is: %s \r\n", ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
 }
 
-static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size)
+static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size, ContosoAnemometer *myWeather)
 {
     static unsigned int messageTrackingId;
     IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
@@ -84,6 +87,13 @@
     }
     else
     {
+        MAP_HANDLE propMap = IoTHubMessage_Properties(messageHandle);
+        (void)sprintf_s(propText, sizeof(propText), myWeather->Temperature > 28 ? "true" : "false");
+        if (Map_AddOrUpdate(propMap, "temperatureAlert", propText) != MAP_OK)
+        {
+            (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
+        }
+
         if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)(uintptr_t)messageTrackingId) != IOTHUB_CLIENT_OK)
         {
             printf("failed to hand over the message to IoTHubClient");
@@ -149,6 +159,8 @@
             IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol);
             srand((unsigned int)time(NULL));
             int avgWindSpeed = 10;
+            float minTemperature = 20.0;
+            float minHumidity = 60.0;
 
             if (iotHubClientHandle == NULL)
             {
@@ -180,16 +192,18 @@
                     {
                         myWeather->DeviceId = "myFirstDevice";
                         myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2);
+                        myWeather->Temperature = minTemperature + (rand() % 10);
+                        myWeather->Humidity = minHumidity + (rand() % 20);
                         {
                             unsigned char* destination;
                             size_t destinationSize;
-                            if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed) != CODEFIRST_OK)
+                            if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed, myWeather->Temperature, myWeather->Humidity) != CODEFIRST_OK)
                             {
                                 (void)printf("Failed to serialize\r\n");
                             }
                             else
                             {
-                                sendMessage(iotHubClientHandle, destination, destinationSize);
+                                sendMessage(iotHubClientHandle, destination, destinationSize, myWeather);
                                 free(destination);
                             }
                         }