demo project

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

Revision:
13:ffeff9b5e513
Parent:
11:3a2e6eb9fbb8
Child:
14:570c8071f577
--- a/IothubRobotArm.cpp	Thu Jan 07 17:31:23 2016 +0000
+++ b/IothubRobotArm.cpp	Fri Jan 15 22:02:46 2016 +0000
@@ -11,6 +11,7 @@
 
 #include "iothub_client.h"
 #include "iothub_message.h"
+#include "threadapi.h"
 #include "crt_abstractions.h"
 #include "iothubtransportamqp.h"
 #include "MeasureBuf.h"
@@ -96,13 +97,17 @@
     int messageTrackingId;  // For tracking the messages within the user callback.
 } EVENT_INSTANCE;
 
+
 // message buffers to use
-#define MESSAGE_LEN 1024
+#define MESSAGE_LEN         1024
 static char msgText[MESSAGE_LEN];
-//static char propText[MESSAGE_LEN];
+
+#define MESSAGE_COUNT       2
+EVENT_INSTANCE messages[MESSAGE_COUNT];
 
-#define MESSAGE_COUNT 1
-EVENT_INSTANCE messages[MESSAGE_COUNT];
+// send thread poll ms
+#define SEND_POLL_MS        500
+
 
 // context for send & receive
 static int receiveContext;
@@ -129,12 +134,14 @@
     
     if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) == IOTHUB_MESSAGE_OK)
     {
-        (void)printf("Received Message handle %x with Data: <%.*s> & Size=%d\r\n", message, (int)size, buffer, (int)size);
         int slen = size;
+
         if (size >= 20)
             slen = 19;
         strncpy(cmdbuf, buffer, slen);
         cmdbuf[slen] = 0;
+        (void)printf(cmdbuf);
+        (void)printf("\r\n");
         ControlArm((const char*)cmdbuf);
     }
 
@@ -150,18 +157,12 @@
         printf("SendConfirmation with NULL context\r\n");
         return;
     }
-    (void)printf("Confirmation[%d] received for tracking id = %d with result = %d\r\n", 
-                eventInstance->messageHandle, eventInstance->messageTrackingId, result);
+
+    (void)printf("Confirmation received\r\n");
 
     confirmTimer->stop();
     callbackCounter++;
     IoTHubMessage_Destroy(eventInstance->messageHandle);
-    
-    if (callbackCounter == msgNumber)
-    {
-        // call SendMeasurements again in case more to send
-        SendIothubData();
-    }
 }
 
 // communication timeout
@@ -173,57 +174,59 @@
 
 
 // IoT Hub thread
-static Thread* IotThread = NULL;
+static THREAD_HANDLE IotThread;
 static bool IotThreadClose;
 
 // entry point for ITHub sending thread
-void IothubThread(void const *args)
+int IothubThread(void *args)
 {
     (void)printf("Iothub thread start\r\n");
     IotThreadClose = false;
     IothubRobotArm iotRobot;
  
     confirmTimer = new RtosTimer(CommunicationTO, osTimerOnce, (void *)osThreadGetId());
-    
-    confirmTimer->start(SEND_CONFIRM_TO);
-    InitEthernet();
-    confirmTimer->stop();
-    
+  
     iotRobot.Init();
+    // wait for connection establishment for SSL
+    ThreadAPI_Sleep(15000);
     
     while (1)
     {
-        osEvent ev = Thread::signal_wait(IS_SendStatus);
         if (IotThreadClose)
         {
-            (void)printf("Iothub thread close signal\r\n");
+            (void)printf("Iothub thread close\r\n");
             iotRobot.Terminate();
             break;
         }
         else
         {
-            iotRobot.SendMeasurements();
+            iotRobot.SendMessage();
         }
+        ThreadAPI_Sleep(SEND_POLL_MS);        
     }
+    
+    return 0;
 }
 
 
 bool StartIothubThread()
 {
-    IotThread = new Thread(IothubThread, NULL, osPriorityLow); 
+    InitEthernet();
+    
+    ThreadAPI_Create(&IotThread, IothubThread, NULL);
+ 
+     //IotThread = new Thread(IothubThread, NULL, osPriorityLow); 
     return true;
 }
 
 bool SendIothubData()
 {
-    IotThread->signal_set(IS_SendStatus);
     return true;
 }
 
 void EndIothubThread()
 {
     IotThreadClose = true;
-    IotThread->signal_set(IS_SendStatus);
 }
 
 
@@ -238,9 +241,6 @@
     callbackCounter = 0;
     msgNumber = 0;
     
-    // in case calling init twice without terminate
-    Terminate();
-    
     (void)printf("Starting the IoTHub RobotArm sample AMQP...\r\n");
 
     if ((iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol)) == NULL)
@@ -285,7 +285,7 @@
 }
 
 
-void IothubRobotArm::SendMeasurements(void)
+void IothubRobotArm::SendMessage(void)
 {
     // send until circular buf empty or no sending buffers avail
     // may drop message if confirmations are slow
@@ -295,12 +295,11 @@
         int i = msgNumber % MESSAGE_COUNT;
     
         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);
         }
         
@@ -315,7 +314,6 @@
                 messages[i].messageTrackingId = msgNumber;
                 
                 confirmTimer->stop();
-                
                 if (IoTHubClient_SendEventAsync(iotHubClientHandle, messages[i].messageHandle, SendConfirmationCallback, &messages[i]) != IOTHUB_CLIENT_OK)
                 {
                     (void)printf("ERROR: IoTHubClient_SendEventAsync..........FAILED!\r\n");
@@ -323,17 +321,14 @@
                 else
                 {
                     confirmTimer->start(SEND_CONFIRM_TO);
-                    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);
+                   (void)printf("Send async completed\r\n");
                 }
                 msgNumber++;
             }
         }
         else if (msglen == 0)
         {
-            break;
+           break;
         }
         else if (msglen < 0)
         {