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:
7:2564d95cbf81
Parent:
6:0bffe8529f60
--- a/main.cpp	Tue Apr 18 22:15:09 2017 +0000
+++ b/main.cpp	Tue Apr 25 01:33:13 2017 +0000
@@ -112,7 +112,7 @@
 
 static int JSONifyData(char *buffer, int bufferlen, int reading)
 {
-    static const char *format = "{ \"device\": \"%s\", \"timestamp\": \"%s\", \"reading\": %f }";
+    static const char *format = "{ \"device\": \"%s\", \"timestamp\": \"%s\", \"epochoffset\": %d, \"reading\": %f }";
     static const char *timeFormat = "%FT%X";
     char timeOut[80];
     double work;
@@ -121,17 +121,13 @@
     struct tm *ptm;
     
     // gmtime() does not work on the FRDM K64F - set RTC to UTC
-    time(&rawtime);
-    printf("%x\r\n", ptm);
+    rawtime = time(NULL);
     ptm = localtime(&rawtime);
-    printf("%x\r\n", ptm);
     
     strftime(timeOut, sizeof(timeOut), timeFormat, ptm);
-    printf("rawtime >>%d<<\r\n", rawtime);
-    printf("timeOut >>%s<<\r\n", timeOut);
-    printf("month=%d\r\n", ptm->tm_mon);
+    printf("rawtime=%d;time=%s\r\n", rawtime, timeOut);
     work = sqrt((double)reading);
-    rc = snprintf(buffer, bufferlen, format, "mydevice", timeOut, work);   
+    rc = snprintf(buffer, bufferlen, format, "mydevice", timeOut, rawtime, work);   
     
     if (rc < 0)
         printf("*** ERROR *** out of buffer space\r\n");
@@ -261,7 +257,9 @@
     
     printf("Calibrating...\r\n");
     
-    for (i = 0; i < calibrationPeriod * 50; i++)
+    i = calibrationPeriod * 50;
+    
+    while (i > 0)
     {
         if (sfxos.getInt2Triggered())
         {
@@ -271,14 +269,13 @@
 //            data[i] = reading.accelerometer.x + reading.accelerometer.y;
             printf("x=%d\t\ty=%d\t\tsum=%f\r\n", reading.accelerometer.x, reading.accelerometer.y, data[i]);
             sum += data[i];
-            wait_ms(20);
+            --i;
         }
         else
         {
-            printf("no data\r\n");
-            wait_ms(20);
-            i--;        // Try that reading again
+            printf("WARNING: Sensor was not ready in time during calibration\r\n");
         }
+        wait_ms(20);
     }
     
     mean = (double)sum / (double)(calibrationPeriod * 50);
@@ -301,6 +298,9 @@
 int main()
 {
     const char *connectionString = "HostName=MarkRadHub1.azure-devices.net;DeviceId=mrcc3200;SharedAccessKey=8pGKChTBsz0VGw234iLX7XDDKwcyWRC7hsrVZEHfZHs=";
+    const char *ntpServer = "0.pool.ntp.org";
+    const int ntpRefreshInterval = 60 * 60;
+    const int ledInterval = 300 / 20;
 
     READING reading;
     Serial pc(USBTX, USBRX); // Primary output to demonstrate library
@@ -310,8 +310,10 @@
     int transmitCounter = 0;
     double mean;
     double deviation;
+    DigitalOut ledBlue(LED_BLUE);    
     
     pc.baud(115200); // Print quickly! 200Hz x line of output data!
+    ledBlue = 1;
     
     printf("\n\n\rFXOS8700CQ identity = %X\r\n", sfxos.getWhoAmI());
     
@@ -332,13 +334,22 @@
     char buffer[200];
     
     iotHubClientHandle = setupConnection(pc, connectionString, MQTT_Protocol, &receiveContext);
-    
     calibrate(&mean, &deviation);
     
     int readCount = 0;
     int32_t maxVal = LONG_MIN;
     int32_t curVal;
-    int32_t oneHour = 60 * 60 * 50;
+    time_t lastUpdate = 0;
+    int ntpRc;
+    int ledOffAt = 0;
+    
+    while (NTP_OK != (ntpRc = ntp.setTime("0.pool.ntp.org")))
+    {
+        printf("ERROR: Failed to set current time from NTP server - rc = %d\r\n", ntpRc);
+        wait_ms(100);
+    }
+    
+    lastUpdate = time(NULL);
     
     while (LOOPCOUNT)
     {
@@ -389,6 +400,8 @@
                             else
                             {
                                 (void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT Hub.\r\n", (int)transmitCounter);
+                                ledBlue = 0;
+                                ledOffAt = ledInterval;
                             }
                             
                             IoTHubMessage_Destroy(msgHandle);
@@ -425,8 +438,20 @@
                             
                 if (LOOPCOUNT > 0)
                     LOOPCOUNT--;
+                    
                 readCount = 0;
                 maxVal = LONG_MIN;
+                
+                if (time(NULL) - lastUpdate > ntpRefreshInterval)
+                {
+                    while (NTP_OK != (ntpRc = ntp.setTime("0.pool.ntp.org")))
+                    {
+                        printf("ERROR: Failed to set current time from NTP server - rc = %d\r\n", ntpRc);
+                        wait_ms(100);
+                    }
+                    
+                    lastUpdate = time(NULL);
+                }
             }
         }
         else
@@ -434,22 +459,12 @@
             printf("WARNING: Sensor was not ready in time\r\n");
         }
         
-        // Read at 50 hz
-        
-        if (--oneHour <= 0)
+        if (!((int)ledBlue) && --ledOffAt <= 0)
         {
-            printf("INFO: Updating RTC from NTP server\r\n");
-            
-            if (ntp.setTime("0.pool.ntp.org") != 0)
-            {
-                printf("ERROR: Failed to set current time from NTP server\r\n");
-            }         
-            else
-            {
-                oneHour = 60 * 60 * 50;
-            }
+            ledBlue = 1;
         }
-            
+        
+        // Read at 50 hz
         wait_ms(20);
     }