demo project
Dependencies: AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL
Diff: IothubRobotArm.cpp
- Revision:
- 5:36916b1c5a06
- Parent:
- 4:36a4eceb1b7f
- Child:
- 7:6723f6887d00
--- a/IothubRobotArm.cpp Wed Dec 23 18:34:06 2015 +0000 +++ b/IothubRobotArm.cpp Mon Dec 28 17:19:37 2015 +0000 @@ -16,9 +16,9 @@ #include "certs.h" #endif // MBED_BUILD_TIMESTAMP +// TODO: move to config file static const char* connectionString = "HostName=HenryrIot.azure-devices.net;DeviceId=RobotArm;SharedAccessKey=FUTqsobGrV0ldHbnmOKluN6W90FG1G5z/jnhz+Gr53k="; -static int callbackCounter; -static int msgNumber; + DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES); @@ -28,15 +28,39 @@ int messageTrackingId; // For tracking the messages within the user callback. } EVENT_INSTANCE; +// message buffers to use +#define MESSAGE_LEN 1024 +static char msgText[MESSAGE_LEN]; +static char propText[MESSAGE_LEN]; +#define MESSAGE_COUNT 10 +EVENT_INSTANCE messages[MESSAGE_COUNT]; + +// context for send & receive +static int receiveContext; +static int callbackCounter; +static int msgNumber; + +static IOTHUB_CLIENT_HANDLE iotHubClientHandle; + + +extern void ControlArm(const char* cmd); + static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) { int* counter = (int*)userContextCallback; const char* buffer; size_t size; - + char cmdbuf[20]; + if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) == IOTHUB_MESSAGE_OK) { - (void)printf("Received Message [%d] with Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size); + (void)printf("Received Message [%d] with Data: <%.*s> & Size=%d\r\n", (int)size, *counter, buffer, (int)size); + int slen = size; + if (size >= 20) + slen = 19; + strncpy(cmdbuf, buffer, slen); + cmdbuf[slen] = 0; + ControlArm((const char*)cmdbuf); } // Retrieve properties from the message @@ -144,7 +168,7 @@ bool IothubRobotArm::Init() { - int receiveContext = 0; + receiveContext = 0; callbackCounter = 0; msgNumber = 0; @@ -194,25 +218,20 @@ } } -#define MESSAGE_LEN 1024 -static char msgText[MESSAGE_LEN]; -static char propText[MESSAGE_LEN]; -#define MESSAGE_COUNT 10 void IothubRobotArm::SendMeasurements(void) { - EVENT_INSTANCE messages[MESSAGE_COUNT]; - - int i = 0; + // send until circular buf empty or no sending buffers avail + // may drop message if confirmations are slow + while (msgNumber - callbackCounter < MESSAGE_COUNT) + { + // get buffer from array + int i = msgNumber % MESSAGE_COUNT; - // send until circular buf empty or send buffer max - while (i < MESSAGE_COUNT) - { int msglen = msgSerialize.MeasureBufToString(msgText, MESSAGE_LEN); if (msglen > 0) { - (void)printf("Sending data bytes %d\r\n", msglen); if ((messages[i].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, msglen)) == NULL) { (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n"); @@ -234,14 +253,13 @@ } else { - (void)printf("IoTHubClient_SendEventAsync accepted data for transmission to IoT Hub. bytes: %d\r\n", msglen); + (void)printf("IoTHubClient_SendEventAsync accepted transmission to IoT Hub. tracking id: %d, bytes: %d\r\n", msgNumber, msglen); } msgNumber++; } } else if (msglen == 0) { - (void)printf("No more data to send \r\n"); break; } else if (msglen < 0)