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:
Tue Aug 05 14:46:55 2014 +0000
Revision:
0:ece599ab76bb
Child:
1:c7b9e522cc8e
initial commit

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 0:ece599ab76bb 15 const char key[] = "<key>"; // Replace with your M2X user account master key
jb8414 0:ece599ab76bb 16 const char feed[] = "<feed>"; // Replace with your blueprint feed ID
jb8414 0:ece599ab76bb 17 const char stream[] = "<stream>"; // Replace with your stream name
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 0:ece599ab76bb 33 M2XStreamClient m2xClient(&client, key);
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 0:ece599ab76bb 61 ret = m2xClient.post(feed, stream, maxTilt);
jb8414 0:ece599ab76bb 62 printf("send() returned %d\r\n", ret);
jb8414 0:ece599ab76bb 63 // wait(0.5);
jb8414 0:ece599ab76bb 64 }
jb8414 0:ece599ab76bb 65 }
jb8414 0:ece599ab76bb 66 }