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:
4:9b3da9969b1b
Parent:
3:c0556ff7b8e3
Child:
5:3d814faa2b2e
--- a/main.cpp	Thu Dec 08 00:11:40 2016 +0000
+++ b/main.cpp	Wed Mar 22 20:10:27 2017 +0000
@@ -13,6 +13,7 @@
 \* -------------------------------------------------------------------------- */
 
 #include <string.h>
+#include <limits.h>
 
 #include "SingletonFXOS8700CQ.h"
 
@@ -26,6 +27,7 @@
 
 #include "certs.h"
 
+/*
 int readingToJSON(char *buffer, int bufferlen, READING &reading)
 {
     static const char READING[] = "\"reading\"";
@@ -103,6 +105,22 @@
     
     return strlen(work);
 }
+*/
+
+static int JSONifyData(char *buffer, int bufferlen, int reading)
+{
+    static const char *format = "{ \"device\": %s, \"reading\": %f }";
+    double work;
+    int rc;
+    
+    work = sqrt((double)reading);
+    rc = snprintf(buffer, bufferlen, format, "mydevice", work);   
+    
+    if (rc < 0)
+        printf("*** ERROR *** out of buffer space\r\n");
+        
+    return rc;
+}    
 
 static LOCK_HANDLE msgLock;
 static int msgCount = 0;
@@ -156,7 +174,8 @@
     printf(message);
     printf("stalled ");
     
-    while(true) {
+    while(true) 
+    {
         pc.putc('.'); // idle dots
         wait(1.0);
     }
@@ -214,7 +233,7 @@
 
 int main()
 {
-    const char *connectionString = "HostName=MarkRadML.azure-devices.net;DeviceId=MBEDTest;SharedAccessKey=rgxlnR0rIBW4vnnnDkrbAv+mSOc/Mt60mg1CEjLx7pY=";
+    const char *connectionString = "HostName=MarkRadHub1.azure-devices.net;DeviceId=mrcc3200;SharedAccessKey=8pGKChTBsz0VGw234iLX7XDDKwcyWRC7hsrVZEHfZHs=";
 
     READING reading;
     Serial pc(USBTX, USBRX); // Primary output to demonstrate library
@@ -245,81 +264,106 @@
     
     iotHubClientHandle = setupConnection(pc, connectionString, MQTT_Protocol, &receiveContext);
     
+    int readCount = 0;
+    int32_t maxVal = LONG_MIN;
+    int32_t curVal;
+    
     while (LOOPCOUNT)
     {
         if (sfxos.getInt2Triggered())
         {
             sfxos.setInt2Triggered(false);
             sfxos.getData(reading);
-            rc = readingToJSON(buffer, sizeof(buffer), reading);
+            curVal = (reading.accelerometer.x * reading.accelerometer.x) + (reading.accelerometer.y * reading.accelerometer.y);
+            
+            printf("curVal=%d;maxVal=%d\r\n", curVal, maxVal);
             
-            if (rc > sizeof(buffer))
-                printf("ERROR: JSON buffer too small - require %d characters\n", rc);
+            if (curVal > maxVal)
+                maxVal = curVal;
+            
+            //rc = readingToJSON(buffer, sizeof(buffer), reading);
+            
+            //if (rc > sizeof(buffer))
+                //printf("ERROR: JSON buffer too small - require %d characters\n", rc);
 
-            Lock(msgLock);
-            localMsgCount = msgCount;
-            Unlock(msgLock);
-            
-            if (localMsgCount < 2)
+            if (++readCount >= 50)
             {
-                if ((msgHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)buffer, rc)) == NULL)
+                Lock(msgLock);
+                localMsgCount = msgCount;
+                Unlock(msgLock);
+                
+                if (localMsgCount < 2)
                 {
-                    (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
-                }
-                else
-                {
-                    userContext = (int *) malloc(sizeof(userContext));
+                    rc = JSONifyData(buffer, sizeof(buffer), maxVal);
+                    printf("DATA >>>%s<<<\r\n", buffer);
                     
-                    if (userContext != NULL)
+                    if ((msgHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)buffer, rc)) == NULL)
+                    {
+                        (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
+                    }
+                    else
                     {
-                        *userContext = transmitCounter;
-    
-                        if (IoTHubClient_SendEventAsync(iotHubClientHandle, msgHandle, SendConfirmationCallback, userContext) != 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++;
+                            t.start();
+                            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");
                         }
-                        
-                        IoTHubMessage_Destroy(msgHandle);
-                        Lock(msgLock);
-                        msgCount++;
-                        t.start();
-                        Unlock(msgLock);
-                        
-                        transmitCounter++;
-                    }
-                    else
-                    {
-                        (void)printf("ERROR: malloc - unable to allocate user context\r\n");
                     }
                 }
-            }
-            else
-            {
-                (void)printf("Message dropped queue length %d\r\n", localMsgCount);
+                else
+                {
+                    (void)printf("Message dropped queue length %d\r\n", localMsgCount);
+                }
+                
+                Lock(msgLock);
+                elapsedTime = t.read_ms();
+                Unlock(msgLock);
+                
+                if (elapsedTime > CONNECTIONTIMEOUT)
+                {
+                    printf("No response for %d milliseconds - attempt reconnection\r\n", elapsedTime);
+                    NVIC_SystemReset(); // Just blow it all away
+                    terminateConnection(pc, iotHubClientHandle);
+                    iotHubClientHandle = setupConnection(pc, connectionString, MQTT_Protocol, &receiveContext);
+                    printf("Reconnection complete\r\n");
+                }
+                            
+                if (LOOPCOUNT > 0)
+                    LOOPCOUNT--;
+                    
+                readCount = 0;
+                maxVal = LONG_MIN;
             }
-            
-            Lock(msgLock);
-            elapsedTime = t.read_ms();
-            Unlock(msgLock);
-            
-            if (elapsedTime > CONNECTIONTIMEOUT)
-            {
-                printf("No response for %d milliseconds - attempt reconnection\r\n", elapsedTime);
-                NVIC_SystemReset(); // Just blow it all away
-                terminateConnection(pc, iotHubClientHandle);
-                iotHubClientHandle = setupConnection(pc, connectionString, MQTT_Protocol, &receiveContext);
-                printf("Reconnection complete\r\n");
-            }
-                        
-            if (LOOPCOUNT > 0)
-                LOOPCOUNT--;
+        }
+        else
+        {
+            printf("*** WARNING*** Sensor was not ready in time\r\n");
         }
         
-        wait_ms(500);
+        // Read at 50 hz
+        wait_ms(20);
     }
     
     printf("Loop complete - clean up\n");
@@ -330,7 +374,8 @@
     printf("Test complete\n");
     
 
-    while(true) {
+    while(true) 
+    {
         pc.putc('.'); // idle dots
         wait(1.0);
     }