![](/media/cache/group/m2x.jpg.50x50_q85.jpg)
Demo app to read data from FXOS8700Q accelerometer on FRDM-K64F and send values to AT&T M2X Data Service via MTS shield. For use with DevLab
Dependencies: FXOS8700Q M2XStreamClient SocketModem jsonlite mbed
Fork of M2X_MTS_Accel by
main.cpp@3:e425cec88d5f, 2014-09-05 (annotated)
- Committer:
- jb8414
- Date:
- Fri Sep 05 02:05:55 2014 +0000
- Revision:
- 3:e425cec88d5f
- Parent:
- 1:f6254f28c89c
- Child:
- 5:0ef6b1335b27
Convert for K64F board
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pgruenbaum | 0:b4f0b744fbc3 | 1 | #include "mbed.h" |
jb8414 | 3:e425cec88d5f | 2 | #include "FXOS8700Q.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 | using namespace mts; |
pgruenbaum | 0:b4f0b744fbc3 | 8 | |
pgruenbaum | 0:b4f0b744fbc3 | 9 | const char key[] = "<key>"; // Replace with your M2X user account master key |
pgruenbaum | 0:b4f0b744fbc3 | 10 | const char feed[] = "<feed>"; // Replace with your blueprint feed ID |
pgruenbaum | 0:b4f0b744fbc3 | 11 | const char stream[] = "<stream>"; // Replace with your stream name |
pgruenbaum | 0:b4f0b744fbc3 | 12 | |
pgruenbaum | 0:b4f0b744fbc3 | 13 | // set to 1 for cellular shield board |
pgruenbaum | 0:b4f0b744fbc3 | 14 | // set to 0 for wifi shield board |
pgruenbaum | 0:b4f0b744fbc3 | 15 | #define CELL_SHIELD 0 |
jb8414 | 3:e425cec88d5f | 16 | #define SUCCESS 1 |
pgruenbaum | 0:b4f0b744fbc3 | 17 | |
pgruenbaum | 0:b4f0b744fbc3 | 18 | // ssid and phrase for wifi |
pgruenbaum | 0:b4f0b744fbc3 | 19 | std::string ssid = "<ssid>"; // Replace with your Wifi ID (SSID) |
pgruenbaum | 0:b4f0b744fbc3 | 20 | std::string phrase = "<password>"; // Replace with your Wifi phrase (password) |
pgruenbaum | 0:b4f0b744fbc3 | 21 | Wifi::SecurityType security_type = Wifi::WPA; // Replace with your Wifi security type |
pgruenbaum | 0:b4f0b744fbc3 | 22 | |
pgruenbaum | 0:b4f0b744fbc3 | 23 | int main() |
pgruenbaum | 0:b4f0b744fbc3 | 24 | { |
jb8414 | 3:e425cec88d5f | 25 | printf("starting\n\r"); |
pgruenbaum | 0:b4f0b744fbc3 | 26 | #if CELL_SHIELD |
jb8414 | 3:e425cec88d5f | 27 | MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTC17, PTC16, PTA1, PTC2); |
pgruenbaum | 0:b4f0b744fbc3 | 28 | serial->baud(115200); |
pgruenbaum | 0:b4f0b744fbc3 | 29 | Transport::setTransport(Transport::CELLULAR); |
pgruenbaum | 0:b4f0b744fbc3 | 30 | Cellular* cell = Cellular::getInstance(); |
jb8414 | 3:e425cec88d5f | 31 | cell->init(serial, PTA2, PTE25); //DCD and DTR pins for K64F |
pgruenbaum | 0:b4f0b744fbc3 | 32 | |
pgruenbaum | 0:b4f0b744fbc3 | 33 | int max_tries = 5; |
pgruenbaum | 0:b4f0b744fbc3 | 34 | int i; |
jb8414 | 3:e425cec88d5f | 35 | std::string apn = "wap.cingular"; // set to the appropriate APN (i.e. "m2m.com.attz" for M2X SIMs, "wap.cingular" for others) |
pgruenbaum | 0:b4f0b744fbc3 | 36 | |
pgruenbaum | 0:b4f0b744fbc3 | 37 | i = 0; |
pgruenbaum | 0:b4f0b744fbc3 | 38 | while (i++ < max_tries) { |
pgruenbaum | 0:b4f0b744fbc3 | 39 | if (cell->getRegistration() == Cellular::REGISTERED) { |
pgruenbaum | 0:b4f0b744fbc3 | 40 | printf("registered with tower\n\r"); |
pgruenbaum | 0:b4f0b744fbc3 | 41 | break; |
pgruenbaum | 0:b4f0b744fbc3 | 42 | } else if (i >= max_tries) { |
pgruenbaum | 0:b4f0b744fbc3 | 43 | printf("failed to register with tower\n\r"); |
pgruenbaum | 0:b4f0b744fbc3 | 44 | } else { |
pgruenbaum | 0:b4f0b744fbc3 | 45 | wait(3); |
pgruenbaum | 0:b4f0b744fbc3 | 46 | } |
pgruenbaum | 0:b4f0b744fbc3 | 47 | } |
pgruenbaum | 0:b4f0b744fbc3 | 48 | |
pgruenbaum | 0:b4f0b744fbc3 | 49 | printf("signal strength: %d\n\r", cell->getSignalStrength()); |
pgruenbaum | 0:b4f0b744fbc3 | 50 | |
pgruenbaum | 0:b4f0b744fbc3 | 51 | i = 0; |
pgruenbaum | 0:b4f0b744fbc3 | 52 | printf("setting APN to %s\n\r", apn.c_str()); |
pgruenbaum | 0:b4f0b744fbc3 | 53 | while (i++ < max_tries) { |
pgruenbaum | 0:b4f0b744fbc3 | 54 | if (cell->setApn(apn) == SUCCESS) { |
pgruenbaum | 0:b4f0b744fbc3 | 55 | printf("successfully set APN\n\r"); |
pgruenbaum | 0:b4f0b744fbc3 | 56 | break; |
pgruenbaum | 0:b4f0b744fbc3 | 57 | } else if (i >= max_tries) { |
pgruenbaum | 0:b4f0b744fbc3 | 58 | printf("failed to set APN\n\r"); |
pgruenbaum | 0:b4f0b744fbc3 | 59 | } else { |
pgruenbaum | 0:b4f0b744fbc3 | 60 | wait(1); |
pgruenbaum | 0:b4f0b744fbc3 | 61 | } |
pgruenbaum | 0:b4f0b744fbc3 | 62 | } |
pgruenbaum | 0:b4f0b744fbc3 | 63 | |
pgruenbaum | 0:b4f0b744fbc3 | 64 | i = 0; |
pgruenbaum | 0:b4f0b744fbc3 | 65 | printf("bringing up PPP link\n\r"); |
pgruenbaum | 0:b4f0b744fbc3 | 66 | while (i++ < max_tries) { |
pgruenbaum | 0:b4f0b744fbc3 | 67 | if (cell->connect()) { |
pgruenbaum | 0:b4f0b744fbc3 | 68 | printf("PPP link is up\n\r"); |
pgruenbaum | 0:b4f0b744fbc3 | 69 | break; |
pgruenbaum | 0:b4f0b744fbc3 | 70 | } else if (i >= max_tries) { |
pgruenbaum | 0:b4f0b744fbc3 | 71 | printf("failed to bring PPP link up\n\r"); |
pgruenbaum | 0:b4f0b744fbc3 | 72 | } else { |
pgruenbaum | 0:b4f0b744fbc3 | 73 | wait(1); |
pgruenbaum | 0:b4f0b744fbc3 | 74 | } |
pgruenbaum | 0:b4f0b744fbc3 | 75 | } |
pgruenbaum | 0:b4f0b744fbc3 | 76 | #else |
pgruenbaum | 0:b4f0b744fbc3 | 77 | // WiFi shield |
pgruenbaum | 0:b4f0b744fbc3 | 78 | for (int i = 6; i >= 0; i = i - 2) { |
pgruenbaum | 0:b4f0b744fbc3 | 79 | wait(2); |
pgruenbaum | 0:b4f0b744fbc3 | 80 | printf("Waiting %d seconds...\n\r", i); |
pgruenbaum | 0:b4f0b744fbc3 | 81 | } |
jb8414 | 3:e425cec88d5f | 82 | MTSSerial* serial = new MTSSerial(PTC17, PTC16, 256, 256); |
pgruenbaum | 0:b4f0b744fbc3 | 83 | serial->baud(9600); |
pgruenbaum | 0:b4f0b744fbc3 | 84 | Transport::setTransport(Transport::WIFI); |
pgruenbaum | 0:b4f0b744fbc3 | 85 | Wifi* wifi = Wifi::getInstance(); |
pgruenbaum | 0:b4f0b744fbc3 | 86 | printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE"); |
pgruenbaum | 0:b4f0b744fbc3 | 87 | printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, security_type, phrase)).c_str()); |
pgruenbaum | 0:b4f0b744fbc3 | 88 | printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str()); |
pgruenbaum | 0:b4f0b744fbc3 | 89 | printf("Signal Strnegth (dBm): %d\n\r", wifi->getSignalStrength()); |
pgruenbaum | 0:b4f0b744fbc3 | 90 | printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); |
pgruenbaum | 0:b4f0b744fbc3 | 91 | printf("Connect: %s\n\r", wifi->connect() ? "Success" : "Failure"); |
pgruenbaum | 0:b4f0b744fbc3 | 92 | printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); |
pgruenbaum | 0:b4f0b744fbc3 | 93 | #endif |
pgruenbaum | 0:b4f0b744fbc3 | 94 | |
pgruenbaum | 0:b4f0b744fbc3 | 95 | // Initialize the M2X client |
pgruenbaum | 0:b4f0b744fbc3 | 96 | Client client; |
pgruenbaum | 0:b4f0b744fbc3 | 97 | M2XStreamClient m2xClient(&client, key); |
pgruenbaum | 0:b4f0b744fbc3 | 98 | int ret; |
jb8414 | 3:e425cec88d5f | 99 | |
jb8414 | 3:e425cec88d5f | 100 | // Add code below to read accelerometer data and post to M2X stream |
jb8414 | 3:e425cec88d5f | 101 | // ... |
jb8414 | 3:e425cec88d5f | 102 | |
pgruenbaum | 0:b4f0b744fbc3 | 103 | // Create an accelerometer instance |
jb8414 | 3:e425cec88d5f | 104 | FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board |
jb8414 | 3:e425cec88d5f | 105 | acc.enable(); |
jb8414 | 3:e425cec88d5f | 106 | printf("\r\n\nFXOS8700Q Who Am I= %X\r\n", acc.whoAmI()); |
jb8414 | 3:e425cec88d5f | 107 | |
jb8414 | 3:e425cec88d5f | 108 | while (true) { |
jb8414 | 3:e425cec88d5f | 109 | |
jb8414 | 3:e425cec88d5f | 110 | float x, y, z; |
jb8414 | 3:e425cec88d5f | 111 | acc.getX(&x); |
jb8414 | 3:e425cec88d5f | 112 | acc.getY(&y); |
jb8414 | 3:e425cec88d5f | 113 | acc.getZ(&z); |
jb8414 | 3:e425cec88d5f | 114 | |
jb8414 | 3:e425cec88d5f | 115 | printf("Accel X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); |
pgruenbaum | 0:b4f0b744fbc3 | 116 | |
jb8414 | 3:e425cec88d5f | 117 | // Calculate pitch and roll. Find the maximum tilt angle. |
jb8414 | 3:e425cec88d5f | 118 | float pitch = atan(x / sqrt(y * y + z * z)); |
jb8414 | 3:e425cec88d5f | 119 | float roll = atan(y / sqrt(x * x + z * z)); |
jb8414 | 3:e425cec88d5f | 120 | float maxTilt = |
jb8414 | 3:e425cec88d5f | 121 | max(abs(roll), abs(pitch)) * 180.0 / 3.14159; |
jb8414 | 3:e425cec88d5f | 122 | printf("pitch: %5.1f roll: %5.1f maxTilt: %5.1f\n\r", |
jb8414 | 3:e425cec88d5f | 123 | pitch, roll, maxTilt); |
jb8414 | 3:e425cec88d5f | 124 | |
jb8414 | 3:e425cec88d5f | 125 | // If the maximum title is over 20 degrees, then send |
jb8414 | 3:e425cec88d5f | 126 | // data to stream |
jb8414 | 3:e425cec88d5f | 127 | if (maxTilt > 20) { |
jb8414 | 3:e425cec88d5f | 128 | ret = m2xClient.post(feed, stream, maxTilt); |
jb8414 | 3:e425cec88d5f | 129 | printf("send() returned %d\r\n", ret); |
jb8414 | 3:e425cec88d5f | 130 | wait(0.5); |
jb8414 | 3:e425cec88d5f | 131 | } |
jb8414 | 3:e425cec88d5f | 132 | } |
pgruenbaum | 0:b4f0b744fbc3 | 133 | } |