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:
- 5:3d814faa2b2e
- Parent:
- 4:9b3da9969b1b
- Child:
- 6:0bffe8529f60
--- a/main.cpp Wed Mar 22 20:10:27 2017 +0000 +++ b/main.cpp Fri Mar 24 23:05:08 2017 +0000 @@ -26,6 +26,9 @@ #include "lock.h" #include "certs.h" +#include "NTPClient.h" + +NTPClient ntp; /* int readingToJSON(char *buffer, int bufferlen, READING &reading) @@ -109,12 +112,26 @@ static int JSONifyData(char *buffer, int bufferlen, int reading) { - static const char *format = "{ \"device\": %s, \"reading\": %f }"; + static const char *format = "{ \"device\": \"%s\", \"timestamp\": \"%s\", \"reading\": %f }"; + static const char *timeFormat = "%FT%X"; + char timeOut[80]; double work; int rc; + time_t rawtime = 0; + struct tm *ptm; + // gmtime() does not work on the FRDM K64F - set RTC to UTC + time(&rawtime); + printf("%x\r\n", ptm); + 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); work = sqrt((double)reading); - rc = snprintf(buffer, bufferlen, format, "mydevice", work); + rc = snprintf(buffer, bufferlen, format, "mydevice", timeOut, work); if (rc < 0) printf("*** ERROR *** out of buffer space\r\n"); @@ -230,6 +247,54 @@ printf("Connection terminated\r\n"); } +void calibrate(double *pmean, double *pdeviation) +{ + READING reading; + const int calibrationPeriod = 10; // in seconds + SingletonFXOS8700CQ &sfxos = SingletonFXOS8700CQ::getInstance(); + + int *data = new int[calibrationPeriod * 50]; + int i; + long sum = 0; + double mean = 0; + double temp; + + printf("Calibrating...\r\n"); + + for (i = 0; i < calibrationPeriod * 50; i++) + { + if (sfxos.getInt2Triggered()) + { + sfxos.setInt2Triggered(false); + sfxos.getData(reading); + data[i] = (reading.accelerometer.x * reading.accelerometer.x) + (reading.accelerometer.y * reading.accelerometer.y); + printf("x=%d\t\ty=%d\r\n", reading.accelerometer.x, reading.accelerometer.y); + sum += data[i]; + wait_ms(20); + } + else + { + printf("no data\r\n"); + i--; // Try that reading again + } + } + + mean = (double)sum / (double)(calibrationPeriod * 50); + + for (i = 0; i < calibrationPeriod * 50; i++) + { + temp += ((float)data[i] - mean) * ((float)data[i] - mean); + } + + temp /= (double)(calibrationPeriod * 50); + + delete [] data; + + *pmean = mean; + *pdeviation = sqrt(temp); + + printf("Calibration complete - mean=%f; devation=%f\r\n", *pmean, *pdeviation); +} int main() { @@ -241,10 +306,12 @@ IOTHUB_CLIENT_HANDLE iotHubClientHandle; int receiveContext = 0; int transmitCounter = 0; + double mean; + double deviation; pc.baud(115200); // Print quickly! 200Hz x line of output data! - printf("\n\nFXOS8700CQ identity = %X\n", sfxos.getWhoAmI()); + printf("\n\nFXOS8700CQ identity = %X\r\n", sfxos.getWhoAmI()); msgLock = Lock_Init(); // TODO: Check error code @@ -264,9 +331,12 @@ 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; while (LOOPCOUNT) { @@ -276,10 +346,11 @@ sfxos.getData(reading); curVal = (reading.accelerometer.x * reading.accelerometer.x) + (reading.accelerometer.y * reading.accelerometer.y); - printf("curVal=%d;maxVal=%d\r\n", curVal, maxVal); - if (curVal > maxVal) + { + printf("new maxVal=%d\r\n", maxVal); maxVal = curVal; + } //rc = readingToJSON(buffer, sizeof(buffer), reading); @@ -352,7 +423,6 @@ if (LOOPCOUNT > 0) LOOPCOUNT--; - readCount = 0; maxVal = LONG_MIN; } @@ -363,6 +433,19 @@ } // Read at 50 hz + + if (--oneHour == 0) + { + printf("INFO: Updating RTC from NTP server\r\n"); + + oneHour = 60 * 60 * 50; + + if (ntp.setTime("0.pool.ntp.org") != 0) + { + printf("ERROR: Failed to set current time from NTP server\r\n"); + } + } + wait_ms(20); }