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:
- 10:9b21566a5ddb
- Parent:
- 8:d98e2dec0f40
- Child:
- 11:3a2e6eb9fbb8
--- a/IothubRobotArm.cpp Thu Dec 31 20:02:58 2015 +0000 +++ b/IothubRobotArm.cpp Wed Jan 06 00:58:41 2016 +0000 @@ -4,6 +4,11 @@ #include "mbed.h" #include "rtos.h" +#include "EthernetInterface.h" +#include "mbed/logging.h" +#include "mbed/mbedtime.h" +#include <NTPClient.h> + #include "iothub_client.h" #include "iothub_message.h" #include "crt_abstractions.h" @@ -19,6 +24,69 @@ // TODO: move to config file static const char* connectionString = "HostName=HenryrIot.azure-devices.net;DeviceId=RobotArm;SharedAccessKey=FUTqsobGrV0ldHbnmOKluN6W90FG1G5z/jnhz+Gr53k="; +extern void ShowLedColor(int col); + +int setupRealTime(void) +{ + int result; + + (void)printf("setupRealTime begin\r\n"); + if (EthernetInterface::connect()) + { + (void)printf("Error initializing EthernetInterface.\r\n"); + result = __LINE__; + } + else + { + (void)printf("setupRealTime NTP begin\r\n"); + NTPClient ntp; + if (ntp.setTime("0.pool.ntp.org") != 0) + { + (void)printf("Failed setting time.\r\n"); + result = __LINE__; + } + else + { + (void)printf("set time correctly!\r\n"); + result = 0; + } + (void)printf("setupRealTime NTP end\r\n"); + EthernetInterface::disconnect(); + } + (void)printf("setupRealTime end\r\n"); + + return result; +} + +int InitEthernet() +{ + (void)printf("Initializing ethernet..\r\n"); + + /* These are needed in order to initialize the time provider for Proton-C */ + mbed_log_init(); + mbedtime_init(); + + if (EthernetInterface::init()) + { + (void)printf("Error initializing EthernetInterface.\r\n"); + return -1; + } + + if (setupRealTime() != 0) + { + (void)printf("Failed setting up real time clock\r\n"); + return -1; + } + + if (EthernetInterface::connect()) + { + (void)printf("Error connecting EthernetInterface.\r\n"); + return -1; + } + + return 0; +} + DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES); @@ -33,7 +101,7 @@ static char msgText[MESSAGE_LEN]; //static char propText[MESSAGE_LEN]; -#define MESSAGE_COUNT 10 +#define MESSAGE_COUNT 1 EVENT_INSTANCE messages[MESSAGE_COUNT]; // context for send & receive @@ -44,6 +112,11 @@ static IOTHUB_CLIENT_HANDLE iotHubClientHandle; +// used to detect send confirmation timeout +#define SEND_CONFIRM_TO 30000 +RtosTimer* confirmTimer; + + extern void ControlArm(const char* cmd); @@ -56,7 +129,7 @@ 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 handle %x with Data: <%.*s> & Size=%d\r\n", message, (int)size, buffer, (int)size); int slen = size; if (size >= 20) slen = 19; @@ -65,28 +138,6 @@ ControlArm((const char*)cmdbuf); } - // Retrieve properties from the message - MAP_HANDLE mapProperties = IoTHubMessage_Properties(message); - if (mapProperties != NULL) - { - const char*const* keys; - const char*const* values; - size_t propertyCount = 0; - if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK) - { - if (propertyCount > 0) - { - printf("Message Properties:\r\n"); - for (size_t index = 0; index < propertyCount; index++) - { - printf("\tKey: %s Value: %s\r\n", keys[index], values[index]); - } - printf("\r\n"); - } - } - } - - /* Some device specific action code goes here... */ (*counter)++; return IOTHUBMESSAGE_ACCEPTED; } @@ -94,8 +145,10 @@ static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) { 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)); + (void)printf("Confirmation[%d] received for message handle %x tracking id = %d with result = %d\r\n", + eventInstance->messageHandle, callbackCounter, eventInstance->messageTrackingId, result); + confirmTimer->stop(); callbackCounter++; IoTHubMessage_Destroy(eventInstance->messageHandle); @@ -106,6 +159,13 @@ } } +// communication timeout +void CommunicationTO(void const * tid) +{ + (void)printf("*** Iothub thread communication Timeout\r\n"); + ShowLedColor(2); +} + // IoT Hub thread static Thread* IotThread = NULL; @@ -117,13 +177,18 @@ (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(); while (1) { - (void)printf("Iothub thread wait signal\r\n"); osEvent ev = Thread::signal_wait(IS_SendStatus); - //if ((ev.value.signals & IS_Close) == IS_Close) if (IotThreadClose) { (void)printf("Iothub thread close signal\r\n"); @@ -163,6 +228,7 @@ IotThread->signal_set(IS_SendStatus); } + IothubRobotArm::IothubRobotArm() { iotHubClientHandle = NULL; @@ -191,7 +257,7 @@ // For mbed add the certificate information if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) { - printf("failure to set option \"TrustedCerts\"\r\n"); + //printf("failure to set option \"TrustedCerts\"\r\n"); return false; } #endif // MBED_BUILD_TIMESTAMP @@ -256,13 +322,15 @@ //{ // (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n"); //} - + confirmTimer->stop(); + if (IoTHubClient_SendEventAsync(iotHubClientHandle, messages[i].messageHandle, SendConfirmationCallback, &messages[i]) != IOTHUB_CLIENT_OK) { (void)printf("ERROR: IoTHubClient_SendEventAsync..........FAILED!\r\n"); } 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