mikeflores2000 AT&T MultiTech M2X Exercises book

Dependencies:   M2XStreamClient MMA8451Q SocketModem jsonlite mbed

Fork of M2X_MTS_Accel by AT&T M2X Team

Committer:
mikeflores2000
Date:
Fri Aug 22 07:42:15 2014 +0000
Revision:
3:3aa188afd648
Parent:
2:93ca5135c262
Child:
4:8157d7b1cd01
version 1.01

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pgruenbaum 0:b4f0b744fbc3 1 #include "mbed.h"
pgruenbaum 0:b4f0b744fbc3 2 #include "MMA8451Q.h"
pgruenbaum 0:b4f0b744fbc3 3 #include "M2XStreamClient.h"
pgruenbaum 0:b4f0b744fbc3 4 #include "include_me.h"
pgruenbaum 0:b4f0b744fbc3 5 #include "math.h"
pgruenbaum 0:b4f0b744fbc3 6
pgruenbaum 0:b4f0b744fbc3 7 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
pgruenbaum 0:b4f0b744fbc3 8 PinName const SDA = PTE25;
pgruenbaum 0:b4f0b744fbc3 9 PinName const SCL = PTE24;
pgruenbaum 0:b4f0b744fbc3 10 #elif defined (TARGET_KL05Z)
pgruenbaum 0:b4f0b744fbc3 11 PinName const SDA = PTB4;
pgruenbaum 0:b4f0b744fbc3 12 PinName const SCL = PTB3;
pgruenbaum 0:b4f0b744fbc3 13 #else
pgruenbaum 0:b4f0b744fbc3 14 #error TARGET NOT DEFINED
pgruenbaum 0:b4f0b744fbc3 15 #endif
pgruenbaum 0:b4f0b744fbc3 16
pgruenbaum 0:b4f0b744fbc3 17 #define MMA8451_I2C_ADDRESS (0x1d<<1)
pgruenbaum 0:b4f0b744fbc3 18
pgruenbaum 0:b4f0b744fbc3 19 using namespace mts;
pgruenbaum 0:b4f0b744fbc3 20
mikeflores2000 2:93ca5135c262 21 const char key[] = "7a453ac1769a6f027e20fa4460f3934a"; // Replace with your M2X user account master key
mikeflores2000 2:93ca5135c262 22 const char feed[] = "c34e9db73edb0b171fa3e92bbad63d86"; // Replace with your blueprint feed ID
mikeflores2000 2:93ca5135c262 23 const char stream[] = "tilt"; // Replace with your stream name
pgruenbaum 0:b4f0b744fbc3 24
pgruenbaum 0:b4f0b744fbc3 25 // set to 1 for cellular shield board
pgruenbaum 0:b4f0b744fbc3 26 // set to 0 for wifi shield board
pgruenbaum 0:b4f0b744fbc3 27 #define CELL_SHIELD 0
pgruenbaum 0:b4f0b744fbc3 28
pgruenbaum 0:b4f0b744fbc3 29 // ssid and phrase for wifi
mikeflores2000 3:3aa188afd648 30 std::string ssid = "HD-Members-5Ghz"; // Replace with your Wifi ID (SSID)
mikeflores2000 3:3aa188afd648 31 std::string phrase = "hackerdojo"; // Replace with your Wifi phrase (password)
pgruenbaum 0:b4f0b744fbc3 32 Wifi::SecurityType security_type = Wifi::WPA; // Replace with your Wifi security type
pgruenbaum 0:b4f0b744fbc3 33
pgruenbaum 0:b4f0b744fbc3 34 int main()
pgruenbaum 0:b4f0b744fbc3 35 {
pgruenbaum 0:b4f0b744fbc3 36 #if CELL_SHIELD
pgruenbaum 0:b4f0b744fbc3 37 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8);
pgruenbaum 0:b4f0b744fbc3 38 serial->baud(115200);
pgruenbaum 0:b4f0b744fbc3 39 Transport::setTransport(Transport::CELLULAR);
pgruenbaum 0:b4f0b744fbc3 40 Cellular* cell = Cellular::getInstance();
pgruenbaum 0:b4f0b744fbc3 41 cell->init(serial, PTA4, PTC9); //DCD and DTR pins for KL46Z
pgruenbaum 0:b4f0b744fbc3 42
pgruenbaum 0:b4f0b744fbc3 43 int max_tries = 5;
pgruenbaum 0:b4f0b744fbc3 44 int i;
jb8414 1:f6254f28c89c 45 std::string apn = "wap.cingular"; // set to the appropriate APN (i.e. "m2m.com.attz" for M2X SIMs)
pgruenbaum 0:b4f0b744fbc3 46
pgruenbaum 0:b4f0b744fbc3 47 i = 0;
pgruenbaum 0:b4f0b744fbc3 48 while (i++ < max_tries) {
pgruenbaum 0:b4f0b744fbc3 49 if (cell->getRegistration() == Cellular::REGISTERED) {
pgruenbaum 0:b4f0b744fbc3 50 printf("registered with tower\n\r");
pgruenbaum 0:b4f0b744fbc3 51 break;
pgruenbaum 0:b4f0b744fbc3 52 } else if (i >= max_tries) {
pgruenbaum 0:b4f0b744fbc3 53 printf("failed to register with tower\n\r");
pgruenbaum 0:b4f0b744fbc3 54 } else {
pgruenbaum 0:b4f0b744fbc3 55 wait(3);
pgruenbaum 0:b4f0b744fbc3 56 }
pgruenbaum 0:b4f0b744fbc3 57 }
pgruenbaum 0:b4f0b744fbc3 58
pgruenbaum 0:b4f0b744fbc3 59 printf("signal strength: %d\n\r", cell->getSignalStrength());
pgruenbaum 0:b4f0b744fbc3 60
pgruenbaum 0:b4f0b744fbc3 61 i = 0;
pgruenbaum 0:b4f0b744fbc3 62 printf("setting APN to %s\n\r", apn.c_str());
pgruenbaum 0:b4f0b744fbc3 63 while (i++ < max_tries) {
pgruenbaum 0:b4f0b744fbc3 64 if (cell->setApn(apn) == SUCCESS) {
pgruenbaum 0:b4f0b744fbc3 65 printf("successfully set APN\n\r");
pgruenbaum 0:b4f0b744fbc3 66 break;
pgruenbaum 0:b4f0b744fbc3 67 } else if (i >= max_tries) {
pgruenbaum 0:b4f0b744fbc3 68 printf("failed to set APN\n\r");
pgruenbaum 0:b4f0b744fbc3 69 } else {
pgruenbaum 0:b4f0b744fbc3 70 wait(1);
pgruenbaum 0:b4f0b744fbc3 71 }
pgruenbaum 0:b4f0b744fbc3 72 }
pgruenbaum 0:b4f0b744fbc3 73
pgruenbaum 0:b4f0b744fbc3 74 i = 0;
pgruenbaum 0:b4f0b744fbc3 75 printf("bringing up PPP link\n\r");
pgruenbaum 0:b4f0b744fbc3 76 while (i++ < max_tries) {
pgruenbaum 0:b4f0b744fbc3 77 if (cell->connect()) {
pgruenbaum 0:b4f0b744fbc3 78 printf("PPP link is up\n\r");
pgruenbaum 0:b4f0b744fbc3 79 break;
pgruenbaum 0:b4f0b744fbc3 80 } else if (i >= max_tries) {
pgruenbaum 0:b4f0b744fbc3 81 printf("failed to bring PPP link up\n\r");
pgruenbaum 0:b4f0b744fbc3 82 } else {
pgruenbaum 0:b4f0b744fbc3 83 wait(1);
pgruenbaum 0:b4f0b744fbc3 84 }
pgruenbaum 0:b4f0b744fbc3 85 }
pgruenbaum 0:b4f0b744fbc3 86 #else
pgruenbaum 0:b4f0b744fbc3 87 // WiFi shield
pgruenbaum 0:b4f0b744fbc3 88 for (int i = 6; i >= 0; i = i - 2) {
pgruenbaum 0:b4f0b744fbc3 89 wait(2);
pgruenbaum 0:b4f0b744fbc3 90 printf("Waiting %d seconds...\n\r", i);
pgruenbaum 0:b4f0b744fbc3 91 }
pgruenbaum 0:b4f0b744fbc3 92 MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256);
pgruenbaum 0:b4f0b744fbc3 93 serial->baud(9600);
pgruenbaum 0:b4f0b744fbc3 94 Transport::setTransport(Transport::WIFI);
pgruenbaum 0:b4f0b744fbc3 95 Wifi* wifi = Wifi::getInstance();
pgruenbaum 0:b4f0b744fbc3 96 printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE");
pgruenbaum 0:b4f0b744fbc3 97 printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, security_type, phrase)).c_str());
pgruenbaum 0:b4f0b744fbc3 98 printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str());
pgruenbaum 0:b4f0b744fbc3 99 printf("Signal Strnegth (dBm): %d\n\r", wifi->getSignalStrength());
pgruenbaum 0:b4f0b744fbc3 100 printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False");
pgruenbaum 0:b4f0b744fbc3 101 printf("Connect: %s\n\r", wifi->connect() ? "Success" : "Failure");
pgruenbaum 0:b4f0b744fbc3 102 printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False");
pgruenbaum 0:b4f0b744fbc3 103 #endif
pgruenbaum 0:b4f0b744fbc3 104
pgruenbaum 0:b4f0b744fbc3 105 // Initialize the M2X client
pgruenbaum 0:b4f0b744fbc3 106 Client client;
pgruenbaum 0:b4f0b744fbc3 107 M2XStreamClient m2xClient(&client, key);
pgruenbaum 0:b4f0b744fbc3 108 int ret;
pgruenbaum 0:b4f0b744fbc3 109
pgruenbaum 0:b4f0b744fbc3 110 // Create an accelerometer instance
pgruenbaum 0:b4f0b744fbc3 111 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
pgruenbaum 0:b4f0b744fbc3 112
pgruenbaum 0:b4f0b744fbc3 113 printf("MMA8451 ID: %d\n", acc.getWhoAmI());
pgruenbaum 0:b4f0b744fbc3 114
pgruenbaum 0:b4f0b744fbc3 115 // Add code here to read accelerometer data and post to M2X stream
mikeflores2000 2:93ca5135c262 116 while (true) {
mikeflores2000 2:93ca5135c262 117 // Get accelerometer data
mikeflores2000 2:93ca5135c262 118 float x, y, z;
mikeflores2000 2:93ca5135c262 119 x = acc.getAccX();
mikeflores2000 2:93ca5135c262 120 y = acc.getAccY();
mikeflores2000 2:93ca5135c262 121 z = acc.getAccZ();
mikeflores2000 2:93ca5135c262 122 printf("Accel X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
mikeflores2000 3:3aa188afd648 123 // Calculate pitch and roll. Find the maximum tilt angle.
mikeflores2000 3:3aa188afd648 124 float pitch = atan(x / sqrt(y * y + z * z));
mikeflores2000 2:93ca5135c262 125 float roll = atan(y / sqrt(x * x + z * z));
mikeflores2000 2:93ca5135c262 126 float maxTilt =
mikeflores2000 3:3aa188afd648 127 max(abs(roll), abs(pitch)) * 180.0 / 3.14159;
mikeflores2000 3:3aa188afd648 128 printf("pitch: %5.1f roll: %5.1f maxTilt: %5.1f\n\r",
mikeflores2000 2:93ca5135c262 129 pitch, roll, maxTilt);
mikeflores2000 2:93ca5135c262 130 // If the maximum title is over 20 degrees, then send // data to stream
mikeflores2000 2:93ca5135c262 131 if (maxTilt > 20) {
mikeflores2000 2:93ca5135c262 132 ret = m2xClient.post(feed, stream, maxTilt);
mikeflores2000 2:93ca5135c262 133 printf("send() returned %d\r\n", ret);
mikeflores2000 2:93ca5135c262 134 wait(1);
mikeflores2000 2:93ca5135c262 135 } }
mikeflores2000 3:3aa188afd648 136
pgruenbaum 0:b4f0b744fbc3 137 }