Simple sample that demonstrates reading the FXOS8700CQ accelerometer, convert the data to JSON and send to an Azure IoT Hub.
Dependencies: azure_umqtt_c iothub_mqtt_transport mbed-rtos mbed wolfSSL Socket lwip-eth lwip-sys lwip
Diff: main.cpp
- Revision:
- 2:2b9acda15ef0
- Parent:
- 1:0366fad6e60c
- Child:
- 3:c0556ff7b8e3
--- a/main.cpp Mon Oct 24 21:56:19 2016 +0000 +++ b/main.cpp Thu Oct 27 22:44:50 2016 +0000 @@ -22,15 +22,10 @@ #include "azure_c_shared_utility/crt_abstractions.h" #include "azure_c_shared_utility/platform.h" #include "iothubtransportmqtt.h" +#include "lock.h" #include "certs.h" -typedef struct EVENT_INSTANCE_TAG -{ - IOTHUB_MESSAGE_HANDLE messageHandle; - int messageTrackingId; // For tracking the messages within the user callback. -} EVENT_INSTANCE; - int readingToJSON(char *buffer, int bufferlen, READING &reading) { static const char READING[] = "\"reading\""; @@ -109,6 +104,9 @@ return strlen(work); } +static LOCK_HANDLE msgLock; +static int msgCount = 0; + static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) { int* counter = (int*)userContextCallback; @@ -132,16 +130,16 @@ static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) { - EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback; + int* messageTrackingId = (int*)userContextCallback; (void)printf("Confirmation received for message tracking id = %d with result = %s\r\n", - eventInstance->messageTrackingId, ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result)); + *messageTrackingId, ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result)); - /* Some device specific action code goes here... */ - //callbackCounter++; - - IoTHubMessage_Destroy(eventInstance->messageHandle); - free(eventInstance); + free(userContextCallback); + Lock(msgLock); + msgCount--; + Unlock(msgLock); + } void stall(Serial &pc, char *message) @@ -157,7 +155,7 @@ int main() { - const char *connectionString = "HostName=MarkRadML.azure-devices.net;DeviceId=MBEDTest;SharedAccessKey=O9UeI84dX74oZKd1xX/Mlavw+2CT7BWuv7QoGfg8Crk="; + const char *connectionString = "HostName=MarkRadML.azure-devices.net;DeviceId=MBEDTest;SharedAccessKey=rgxlnR0rIBW4vnnnDkrbAv+mSOc/Mt60mg1CEjLx7pY="; READING reading; Serial pc(USBTX, USBRX); // Primary output to demonstrate library @@ -165,11 +163,13 @@ IOTHUB_CLIENT_HANDLE iotHubClientHandle; int receiveContext = 0; int transmitCounter = 0; - + pc.baud(115200); // Print quickly! 200Hz x line of output data! printf("\n\nFXOS8700CQ identity = %X\n", sfxos.getWhoAmI()); + msgLock = Lock_Init(); // TODO: Check error code + sfxos.enable(); sfxos.getData(reading); @@ -195,6 +195,10 @@ int LOOPCOUNT = -1; // Set to -1 to run forever + int localMsgCount; + int *userContext; + IOTHUB_MESSAGE_HANDLE msgHandle; + char buffer[200]; while (LOOPCOUNT) @@ -208,36 +212,50 @@ if (rc > sizeof(buffer)) printf("ERROR: JSON buffer too small - require %d characters\n", rc); - //printf(buffer); - - EVENT_INSTANCE* message = (EVENT_INSTANCE*)malloc(sizeof(EVENT_INSTANCE)); + Lock(msgLock); + localMsgCount = msgCount; + Unlock(msgLock); - if (message == NULL) + if (localMsgCount < 2) { - (void)printf("ERROR: Unable to allocate EVENT_INSTANCE!\r\n"); - } - else - { - if ((message->messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)buffer, rc)) == NULL) + if ((msgHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)buffer, rc)) == NULL) { (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n"); } else { - message->messageTrackingId = transmitCounter; - - if (IoTHubClient_SendEventAsync(iotHubClientHandle, message->messageHandle, SendConfirmationCallback, message) != IOTHUB_CLIENT_OK) + userContext = (int *) malloc(sizeof(userContext)); + + if (userContext != NULL) { - (void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n"); + *userContext = transmitCounter; + + if (IoTHubClient_SendEventAsync(iotHubClientHandle, msgHandle, SendConfirmationCallback, userContext) != IOTHUB_CLIENT_OK) + { + (void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n"); + } + else + { + (void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT Hub.\r\n", (int)transmitCounter); + } + + IoTHubMessage_Destroy(msgHandle); + Lock(msgLock); + msgCount++; + Unlock(msgLock); + + transmitCounter++; } else { - (void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT Hub.\r\n", (int)transmitCounter); + (void)printf("ERROR: malloc - unable to allocate user context\r\n"); } - - transmitCounter++; } } + else + { + (void)printf("Message dropped queue length %d\r\n", localMsgCount); + } LOOPCOUNT--; } @@ -249,6 +267,8 @@ IoTHubClient_Destroy(iotHubClientHandle); + Lock_Deinit(msgLock); + platform_deinit(); printf("Test complete\n");