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

Revision:
0:6c46c366f500
Child:
1:0366fad6e60c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Oct 14 21:16:25 2016 +0000
@@ -0,0 +1,261 @@
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file at https://github.com/Azure/azure-iot-sdks/blob/master/LICENSE for full license information.
+
+/* -------------------------------------------------------------------------- *\
+
+   Simple progam to demonstrate reading the FRDM-K64F FXOS8700CQ 
+   accelerometer, convert the data to JSON and send to an Azure IoT Hub. You 
+   must provide your hub's connection string in the variable 
+   'connectionString'.
+
+   markrad
+
+\* -------------------------------------------------------------------------- */
+
+#include <string.h>
+
+#include "SingletonFXOS8700CQ.h"
+
+#include "iothub_client.h"
+#include "iothub_message.h"
+#include "azure_c_shared_utility/threadapi.h"
+#include "azure_c_shared_utility/crt_abstractions.h"
+#include "azure_c_shared_utility/platform.h"
+#include "iothubtransportmqtt.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\"";
+    static const char ACCELEROMETER[] = "\"accelerometer\"";
+    static const char MAGNOMETER[] = "\"magnometer\"";
+    static const char X[] = "\"X\"";
+    static const char Y[] = "\"Y\"";
+    static const char Z[] = "\"Z\"";
+    static const char STARTOBJ[] = " : {\n";
+    static const char ENDOBJ[] = "}\n";
+    static const char PREPEND[] = "{\n";
+    static const int MINBUFFERLEN = 
+        sizeof(READING) + 
+        sizeof(ACCELEROMETER) +
+        sizeof(MAGNOMETER) +
+        2 * (sizeof(X) + sizeof(Y) + sizeof(Z)) +
+        3 * sizeof(STARTOBJ) +
+        4 * sizeof(ENDOBJ) +
+        sizeof(PREPEND) +
+        6 * 9;
+    static const char numConvert[] = "%d";
+    
+    char toNum[10];
+    char work[MINBUFFERLEN + 1];
+    
+    if (buffer == NULL)
+        return 0;
+    
+    buffer[0] = '\0';
+        
+    strcpy(work, PREPEND);
+    strcat(work, READING);
+    strcat(work, STARTOBJ);
+    strcat(work, ACCELEROMETER);
+    strcat(work, STARTOBJ);
+    strcat(work, X);
+    strcat(work, " : ");
+    sprintf(toNum, numConvert, reading.accelerometer.x);
+    strcat(work, toNum);
+    strcat(work, ",\n");
+    strcat(work, Y);
+    strcat(work, " : ");
+    sprintf(toNum, numConvert, reading.accelerometer.y);
+    strcat(work, toNum);
+    strcat(work, ",\n");
+    strcat(work, Z);
+    strcat(work, " : ");
+    sprintf(toNum, numConvert, reading.accelerometer.z);
+    strcat(work, toNum);
+    strcat(work, "\n");
+    strcat(work, ENDOBJ);
+    strcat(work, MAGNOMETER);
+    strcat(work, STARTOBJ);
+    strcat(work, X);
+    strcat(work, " : ");
+    sprintf(toNum, numConvert, reading.magnometer.x);
+    strcat(work, toNum);
+    strcat(work, ",\n");
+    strcat(work, Y);
+    strcat(work, " : ");
+    sprintf(toNum, numConvert, reading.magnometer.y);
+    strcat(work, toNum);
+    strcat(work, ",\n");
+    strcat(work, Z);
+    strcat(work, " : ");
+    sprintf(toNum, numConvert, reading.magnometer.z);
+    strcat(work, toNum);
+    strcat(work, "\n");
+    strcat(work, ENDOBJ);
+    strcat(work, ENDOBJ);
+    strcat(work, ENDOBJ);
+    
+    if (strlen(work) + 1 < bufferlen)
+        strcpy(buffer, work); 
+    
+    return strlen(work);
+}
+
+static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
+{
+    int* counter = (int*)userContextCallback;
+    const char* buffer;
+    size_t size;
+
+    if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK)
+    {
+        (void)printf("unable to retrieve the message data\r\n");
+    }
+    else
+    {
+        (void)printf("Received Message [%d] with Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size);
+    }
+
+    // Some device specific action code goes here...
+    (*counter)++;
+
+    return IOTHUBMESSAGE_ACCEPTED;
+}
+
+static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
+{
+    EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)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));
+        
+    /* Some device specific action code goes here... */
+    //callbackCounter++;
+    
+    IoTHubMessage_Destroy(eventInstance->messageHandle);
+    free(eventInstance);
+}
+
+void stall(Serial &pc, char *message)
+{
+    printf(message);
+    printf("stalled ");
+    
+    while(true) {
+        pc.putc('.'); // idle dots
+        wait(1.0);
+    }
+}
+
+int main()
+{
+    const char *connectionString = "Your Azure IoT Hub connection string here";
+
+    READING reading;
+    Serial pc(USBTX, USBRX); // Primary output to demonstrate library
+    SingletonFXOS8700CQ &sfxos = SingletonFXOS8700CQ::getInstance();
+    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());
+    
+    sfxos.enable();
+    sfxos.getData(reading);
+    
+    int rc;
+    
+    if (platform_init() != 0)
+        stall(pc, "Failed to initialize platform\n");
+        
+    if ((iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, MQTT_Protocol)) == NULL)
+        stall(pc, "ERROR: Could not create iotHubClientHandle\n");
+        
+    //bool traceOn = false;
+    bool traceOn = true;
+    
+    IoTHubClient_SetOption(iotHubClientHandle, "logtrace", &traceOn);
+
+    // For mbed add the certificate information
+    if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
+        stall(pc, "ERROR: failure to set option \"TrustedCerts\"\n");
+
+    if (IoTHubClient_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK)
+        stall(pc, "ERROR: IoTHubClient_SetMessageCallback failed\r\n");
+
+    int LOOPCOUNT = 100;     // Set to -1 to run forever
+    
+    char buffer[200];
+    
+    while (LOOPCOUNT)
+    {
+        if (sfxos.getInt2Triggered())
+        {
+            sfxos.setInt2Triggered(false);
+            sfxos.getData(reading);
+            rc = readingToJSON(buffer, sizeof(buffer), reading);
+            
+            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));
+            
+            if (message == NULL)
+            {
+                (void)printf("ERROR: Unable to allocate EVENT_INSTANCE!\r\n");
+            }
+            else
+            {
+                if ((message->messageHandle = 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)
+                    {
+                        (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);
+                    }
+
+                    transmitCounter++;
+                }
+            }
+            
+            LOOPCOUNT--;
+        }
+        
+        wait_ms(500);
+    }
+    
+    printf("Loop complete - clean up\n");
+    
+    IoTHubClient_Destroy(iotHubClientHandle);
+    
+    platform_deinit();
+    
+    printf("Test complete\n");
+    
+
+    while(true) {
+        pc.putc('.'); // idle dots
+        wait(1.0);
+    }
+}
\ No newline at end of file