Demo app to read data from FXOS8700Q accelerometer on FRDM-K64F and send values to AT&T M2X Data Service via Ethernet. For use with DevLab

Dependencies:   EthernetInterface FXOS8700Q M2XStreamClient jsonlite mbed-rtos mbed

Fork of M2X_K64F_ACCEL by Joe Bossalini

Committer:
jb8414
Date:
Sat Aug 29 17:11:28 2015 +0000
Revision:
1:c7b9e522cc8e
Parent:
0:ece599ab76bb
Child:
2:68b759c89fd9
Update with new M2XStreamClient

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jb8414 0:ece599ab76bb 1 #include "mbed.h"
jb8414 0:ece599ab76bb 2 #include "FXOS8700Q.h"
jb8414 0:ece599ab76bb 3 #include "M2XStreamClient.h"
jb8414 0:ece599ab76bb 4 #include "EthernetInterface.h"
jb8414 0:ece599ab76bb 5
jb8414 0:ece599ab76bb 6
jb8414 0:ece599ab76bb 7 #ifndef MAX
jb8414 0:ece599ab76bb 8 #define max(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a > _b ? _a : _b; })
jb8414 0:ece599ab76bb 9 #endif
jb8414 0:ece599ab76bb 10
jb8414 0:ece599ab76bb 11 #ifndef MIN
jb8414 0:ece599ab76bb 12 #define min(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a < _b ? _a : _b; })
jb8414 0:ece599ab76bb 13 #endif
jb8414 0:ece599ab76bb 14
jb8414 1:c7b9e522cc8e 15 char deviceId[] = "<device id>"; // Device you want to push to
jb8414 1:c7b9e522cc8e 16 char streamName[] = "<stream name>"; // Stream you want to push to
jb8414 1:c7b9e522cc8e 17 char m2xKey[] = "<m2x api key>"; // Your M2X API Key or Master API Key
jb8414 0:ece599ab76bb 18
jb8414 0:ece599ab76bb 19 int main()
jb8414 0:ece599ab76bb 20 {
jb8414 0:ece599ab76bb 21 // Setup Ethernet
jb8414 0:ece599ab76bb 22 printf("Start\r\n");
jb8414 0:ece599ab76bb 23 EthernetInterface eth;
jb8414 0:ece599ab76bb 24 printf("Init...\r\n");
jb8414 0:ece599ab76bb 25 eth.init(); //Use DHCP
jb8414 0:ece599ab76bb 26 //eth.init(ip,mask,gateway); //Use Static IP Configuration
jb8414 0:ece599ab76bb 27 printf("Connect\r\n");
jb8414 0:ece599ab76bb 28 eth.connect();
jb8414 0:ece599ab76bb 29 printf("Device IP Address is %s\r\n", eth.getIPAddress());
jb8414 0:ece599ab76bb 30
jb8414 0:ece599ab76bb 31 // Initialize the M2X client
jb8414 0:ece599ab76bb 32 Client client;
jb8414 1:c7b9e522cc8e 33 M2XStreamClient m2xClient(&client, m2xKey);
jb8414 0:ece599ab76bb 34 int ret;
jb8414 0:ece599ab76bb 35
jb8414 0:ece599ab76bb 36 // Create an accelerometer instance
jb8414 0:ece599ab76bb 37 FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board
jb8414 0:ece599ab76bb 38 acc.enable();
jb8414 0:ece599ab76bb 39 printf("\r\n\nFXOS8700Q Who Am I= %X\r\n", acc.whoAmI());
jb8414 0:ece599ab76bb 40
jb8414 0:ece599ab76bb 41 while (true) {
jb8414 0:ece599ab76bb 42
jb8414 0:ece599ab76bb 43 float x, y, z;
jb8414 0:ece599ab76bb 44 acc.getX(&x);
jb8414 0:ece599ab76bb 45 acc.getY(&y);
jb8414 0:ece599ab76bb 46 acc.getZ(&z);
jb8414 0:ece599ab76bb 47
jb8414 0:ece599ab76bb 48 printf("Accel X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
jb8414 0:ece599ab76bb 49
jb8414 0:ece599ab76bb 50 // Calculate pitch and roll. Find the maximum tilt angle.
jb8414 0:ece599ab76bb 51 float pitch = atan(x / sqrt(y * y + z * z));
jb8414 0:ece599ab76bb 52 float roll = atan(y / sqrt(x * x + z * z));
jb8414 0:ece599ab76bb 53 float maxTilt =
jb8414 0:ece599ab76bb 54 max(abs(roll), abs(pitch)) * 180.0 / 3.14159;
jb8414 0:ece599ab76bb 55 printf("pitch: %5.1f roll: %5.1f maxTilt: %5.1f\n\r",
jb8414 0:ece599ab76bb 56 pitch, roll, maxTilt);
jb8414 0:ece599ab76bb 57
jb8414 0:ece599ab76bb 58 // If the maximum title is over 20 degrees, then send
jb8414 0:ece599ab76bb 59 // data to stream
jb8414 0:ece599ab76bb 60 if (maxTilt > 20) {
jb8414 1:c7b9e522cc8e 61 ret = m2xClient.updateStreamValue(deviceId, streamName, maxTilt);
jb8414 0:ece599ab76bb 62 printf("send() returned %d\r\n", ret);
jb8414 1:c7b9e522cc8e 63 wait(0.5);
jb8414 0:ece599ab76bb 64 }
jb8414 0:ece599ab76bb 65 }
jb8414 0:ece599ab76bb 66 }