An m2x sample code that uses accelerometers. Not working yet.

Dependencies:   MMA8451Q SocketModem jsonlite mbed

Fork of MTS_M2x_Example by Multi-Hackers

Files at this revision

API Documentation at this revision

Comitter:
pgruenbaum
Date:
Tue Apr 22 20:08:43 2014 +0000
Parent:
7:b90e8119f3cd
Commit message:
An M2X sample that uses the accelerometer

Changed in this revision

MMA8451Q.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r b90e8119f3cd -r 3b02a48c4990 MMA8451Q.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA8451Q.lib	Tue Apr 22 20:08:43 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/emilmont/code/MMA8451Q/#c4d879a39775
diff -r b90e8119f3cd -r 3b02a48c4990 main.cpp
--- a/main.cpp	Wed Jan 22 23:26:47 2014 +0000
+++ b/main.cpp	Tue Apr 22 20:08:43 2014 +0000
@@ -1,24 +1,32 @@
 #include "mbed.h"
 #include "M2XStreamClient.h"
 #include "include_me.h"
+#include "MMA8451Q.h"
 
 using namespace mts;
 
-const char key[] = "";  // enter your m2x user account master key
-const char feed[] = ""; // enter your blueprint feed id
-const char stream[] = "amb-temp";   // Create a stream name
+const char key[] = "8531911e8b2c361ad535fcb2da6a7064";  // enter your m2x user account master key
+const char feed[] = "84a7221ec4a9a3d9b19c30b0a95f2c52"; // enter your blueprint feed id
+const char stream[] = "speed";   // Create a stream name
 
+PinName const SDA = PTB4;
+PinName const SCL = PTB3;
+  
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+#define TARGET_KL05Z
+  
 // set to 1 for cellular shield board
 // set to 0 for wifi shield board
-#define CELL_SHIELD 1
+#define CELL_SHIELD 0
 
 // ssid and phrase for wifi
-std::string ssid = "belkin54g";
-std::string phrase = "hackathon";
-Wifi::SecurityType security_type = Wifi::WPA;
+std::string ssid = "KittyHawk2";
+std::string phrase = "KateAndOrville";
+Wifi::SecurityType security_type = Wifi::WPA2;
 
 int main()
 {
+    
 #if CELL_SHIELD
     MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8);
     serial->baud(115200);
@@ -87,16 +95,39 @@
     printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False");
 #endif
 
+
+    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
+    printf("MMA8451 ID: %d\n", acc.getWhoAmI());
+
     /* send some data */
+    /*
     Client client;
     M2XStreamClient m2xClient(&client, key);
     int ret;
-    int num;
+    int speed = 0;
+    while (true) {
+        speed += 10;
+        printf("sending %d\r\n", speed);
+        ret = m2xClient.send(feed, stream, speed);
+        printf("send() returned %d\r\n", ret);
+        wait(3);
+    }
+    */
+    Client client;
+    M2XStreamClient m2xClient(&client, key);
+    int ret;
     while (true) {
-        num = rand();
-        printf("sending %d\r\n", num);
-        ret = m2xClient.send(feed, stream, num);
+        float x, y, z;
+        x = abs(acc.getAccX());
+        y = abs(acc.getAccY());
+        z = abs(acc.getAccZ());
+        int tiltAngleX = atan(x / sqrt(x * x + z * z)) * 180 / 3.14;
+        int tiltAngleY = atan(y / sqrt(y * y + z * z)) * 180 / 3.14;
+        printf("x: %d, y: %d", tiltAngleX, tiltAngleY);
+        int maxTilt = max(tiltAngleX, tiltAngleY);
+        printf("sending %d\r\n", maxTilt);
+        ret = m2xClient.send(feed, stream, maxTilt);
         printf("send() returned %d\r\n", ret);
-        wait(5);
+        wait(3);
     }
 }
\ No newline at end of file
diff -r b90e8119f3cd -r 3b02a48c4990 mbed.bld
--- a/mbed.bld	Wed Jan 22 23:26:47 2014 +0000
+++ b/mbed.bld	Tue Apr 22 20:08:43 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/a842253909c9
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/8e73be2a2ac1
\ No newline at end of file