demo project

Dependencies:   AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL

Revision:
7:6723f6887d00
Parent:
5:36916b1c5a06
Child:
8:d98e2dec0f40
--- a/IothubRobotArm.cpp	Mon Dec 28 17:29:12 2015 +0000
+++ b/IothubRobotArm.cpp	Tue Dec 29 23:31:28 2015 +0000
@@ -31,7 +31,8 @@
 // message buffers to use
 #define MESSAGE_LEN 1024
 static char msgText[MESSAGE_LEN];
-static char propText[MESSAGE_LEN];
+//static char propText[MESSAGE_LEN];
+
 #define MESSAGE_COUNT 10
 EVENT_INSTANCE messages[MESSAGE_COUNT];
 
@@ -40,6 +41,7 @@
 static int callbackCounter;
 static int msgNumber;
 
+
 static  IOTHUB_CLIENT_HANDLE iotHubClientHandle;
 
 
@@ -93,16 +95,18 @@
 {
     EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback;
     (void)printf("Confirmation[%d] received for message tracking id = %d with result = %s\r\n", callbackCounter, eventInstance->messageTrackingId, ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
-    /* Some device specific action code goes here... */
+
     callbackCounter++;
     IoTHubMessage_Destroy(eventInstance->messageHandle);
     
     if (callbackCounter == msgNumber)
     {
         // call SendMeasurements again in case more to send
+        SendIothubData();
     }
 }
 
+
 // IoT Hub thread
 static Thread* IotThread = NULL;
 static bool IotThreadClose;
@@ -130,7 +134,6 @@
         {
             if ((ev.value.signals & IS_SendStatus) == IS_SendStatus)
             {
-                (void)printf("Iothub thread send status signal\r\n");
                 iotRobot.SendMeasurements();
             }
             else
@@ -148,9 +151,8 @@
     return true;
 }
 
-bool SendIothubMeasurements()
+bool SendIothubData()
 {
-    (void)printf("Iothub thread signal to send data\r\n");
     IotThread->signal_set(IS_SendStatus);
     return true;
 }
@@ -228,8 +230,16 @@
         // get buffer from array
         int i = msgNumber % MESSAGE_COUNT;
     
-        int msglen = msgSerialize.MeasureBufToString(msgText, MESSAGE_LEN);
-
+        int msglen = 0;
+        bool ismeasure = false;
+        // get alert if any, otherwise get measure data
+        msglen = msgSerialize.AlertBufToString(msgText, MESSAGE_LEN);
+        if (msglen == 0)
+        {
+            ismeasure = true;
+            msglen = msgSerialize.MeasureBufToString(msgText, MESSAGE_LEN);
+        }
+        
         if (msglen > 0)
         {
             if ((messages[i].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, msglen)) == NULL)
@@ -240,12 +250,12 @@
             {
                 messages[i].messageTrackingId = msgNumber;
                 
-                MAP_HANDLE propMap = IoTHubMessage_Properties(messages[i].messageHandle);
-                sprintf_s(propText, sizeof(propText), "PropMsg_%d", msgNumber);
-                if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK)
-                {
-                    (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
-                }
+                //MAP_HANDLE propMap = IoTHubMessage_Properties(messages[i].messageHandle);
+                //sprintf_s(propText, sizeof(propText), "PropMsg_%d", msgNumber);
+                //if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK)
+                //{
+                //    (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
+                //}
 
                 if (IoTHubClient_SendEventAsync(iotHubClientHandle, messages[i].messageHandle, SendConfirmationCallback, &messages[i]) != IOTHUB_CLIENT_OK)
                 {
@@ -253,7 +263,10 @@
                 }
                 else
                 {
-                    (void)printf("IoTHubClient_SendEventAsync accepted transmission to IoT Hub. tracking id: %d,  bytes: %d\r\n", msgNumber, msglen);
+                    if (ismeasure)
+                        (void)printf("IoTHubClient_SendEventAsync sending data to IoT Hub. tracking id: %d,  bytes: %d\r\n", msgNumber, msglen);
+                    else
+                        (void)printf("IoTHubClient_SendEventAsync sending alert to IoT Hub. tracking id: %d,  bytes: %d\r\n", msgNumber, msglen);
                 }
                 msgNumber++;
             }
@@ -270,3 +283,4 @@
     } // while
 
 }
+