An m2x sample code that uses accelerometers. Not working yet.
Dependencies: MMA8451Q SocketModem jsonlite mbed
Fork of MTS_M2x_Example by
main.cpp@8:3b02a48c4990, 2014-04-22 (annotated)
- Committer:
- pgruenbaum
- Date:
- Tue Apr 22 20:08:43 2014 +0000
- Revision:
- 8:3b02a48c4990
- Parent:
- 6:0dbcf434679b
An M2X sample that uses the accelerometer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:032beb056844 | 1 | #include "mbed.h" |
mfiore | 0:032beb056844 | 2 | #include "M2XStreamClient.h" |
mfiore | 0:032beb056844 | 3 | #include "include_me.h" |
pgruenbaum | 8:3b02a48c4990 | 4 | #include "MMA8451Q.h" |
mfiore | 0:032beb056844 | 5 | |
mfiore | 0:032beb056844 | 6 | using namespace mts; |
mfiore | 0:032beb056844 | 7 | |
pgruenbaum | 8:3b02a48c4990 | 8 | const char key[] = "8531911e8b2c361ad535fcb2da6a7064"; // enter your m2x user account master key |
pgruenbaum | 8:3b02a48c4990 | 9 | const char feed[] = "84a7221ec4a9a3d9b19c30b0a95f2c52"; // enter your blueprint feed id |
pgruenbaum | 8:3b02a48c4990 | 10 | const char stream[] = "speed"; // Create a stream name |
mfiore | 0:032beb056844 | 11 | |
pgruenbaum | 8:3b02a48c4990 | 12 | PinName const SDA = PTB4; |
pgruenbaum | 8:3b02a48c4990 | 13 | PinName const SCL = PTB3; |
pgruenbaum | 8:3b02a48c4990 | 14 | |
pgruenbaum | 8:3b02a48c4990 | 15 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
pgruenbaum | 8:3b02a48c4990 | 16 | #define TARGET_KL05Z |
pgruenbaum | 8:3b02a48c4990 | 17 | |
mfiore | 1:b12a128aae0e | 18 | // set to 1 for cellular shield board |
mfiore | 1:b12a128aae0e | 19 | // set to 0 for wifi shield board |
pgruenbaum | 8:3b02a48c4990 | 20 | #define CELL_SHIELD 0 |
sam_grove | 2:76e0d5968991 | 21 | |
sam_grove | 3:d24e8bec5610 | 22 | // ssid and phrase for wifi |
pgruenbaum | 8:3b02a48c4990 | 23 | std::string ssid = "KittyHawk2"; |
pgruenbaum | 8:3b02a48c4990 | 24 | std::string phrase = "KateAndOrville"; |
pgruenbaum | 8:3b02a48c4990 | 25 | Wifi::SecurityType security_type = Wifi::WPA2; |
mfiore | 1:b12a128aae0e | 26 | |
sam_grove | 3:d24e8bec5610 | 27 | int main() |
sam_grove | 3:d24e8bec5610 | 28 | { |
pgruenbaum | 8:3b02a48c4990 | 29 | |
mfiore | 1:b12a128aae0e | 30 | #if CELL_SHIELD |
mfiore | 0:032beb056844 | 31 | MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); |
mfiore | 0:032beb056844 | 32 | serial->baud(115200); |
mfiore | 1:b12a128aae0e | 33 | Transport::setTransport(Transport::CELLULAR); |
mfiore | 0:032beb056844 | 34 | Cellular* cell = Cellular::getInstance(); |
sam_grove | 2:76e0d5968991 | 35 | cell->init(serial, PTA4, PTC9); //DCD and DTR pins for KL46Z |
mfiore | 0:032beb056844 | 36 | |
mfiore | 0:032beb056844 | 37 | int max_tries = 5; |
mfiore | 0:032beb056844 | 38 | int i; |
mfiore | 0:032beb056844 | 39 | std::string apn = "wap.cingular"; |
sam_grove | 3:d24e8bec5610 | 40 | |
mfiore | 0:032beb056844 | 41 | i = 0; |
mfiore | 0:032beb056844 | 42 | while (i++ < max_tries) { |
mfiore | 0:032beb056844 | 43 | if (cell->getRegistration() == Cellular::REGISTERED) { |
mfiore | 0:032beb056844 | 44 | printf("registered with tower\n\r"); |
mfiore | 0:032beb056844 | 45 | break; |
mfiore | 0:032beb056844 | 46 | } else if (i >= max_tries) { |
mfiore | 0:032beb056844 | 47 | printf("failed to register with tower\n\r"); |
mfiore | 0:032beb056844 | 48 | } else { |
mfiore | 0:032beb056844 | 49 | wait(3); |
mfiore | 0:032beb056844 | 50 | } |
mfiore | 0:032beb056844 | 51 | } |
sam_grove | 3:d24e8bec5610 | 52 | |
mfiore | 1:b12a128aae0e | 53 | printf("signal strength: %d\n\r", cell->getSignalStrength()); |
mfiore | 1:b12a128aae0e | 54 | |
mfiore | 0:032beb056844 | 55 | i = 0; |
mfiore | 1:b12a128aae0e | 56 | printf("setting APN to %s\n\r", apn.c_str()); |
mfiore | 0:032beb056844 | 57 | while (i++ < max_tries) { |
mfiore | 0:032beb056844 | 58 | if (cell->setApn(apn) == SUCCESS) { |
mfiore | 0:032beb056844 | 59 | printf("successfully set APN\n\r"); |
mfiore | 0:032beb056844 | 60 | break; |
mfiore | 0:032beb056844 | 61 | } else if (i >= max_tries) { |
mfiore | 0:032beb056844 | 62 | printf("failed to set APN\n\r"); |
mfiore | 0:032beb056844 | 63 | } else { |
mfiore | 0:032beb056844 | 64 | wait(1); |
mfiore | 0:032beb056844 | 65 | } |
mfiore | 0:032beb056844 | 66 | } |
sam_grove | 3:d24e8bec5610 | 67 | |
mfiore | 0:032beb056844 | 68 | i = 0; |
mfiore | 1:b12a128aae0e | 69 | printf("bringing up PPP link\n\r"); |
mfiore | 0:032beb056844 | 70 | while (i++ < max_tries) { |
mfiore | 0:032beb056844 | 71 | if (cell->connect()) { |
mfiore | 0:032beb056844 | 72 | printf("PPP link is up\n\r"); |
mfiore | 0:032beb056844 | 73 | break; |
mfiore | 0:032beb056844 | 74 | } else if (i >= max_tries) { |
mfiore | 0:032beb056844 | 75 | printf("failed to bring PPP link up\n\r"); |
mfiore | 0:032beb056844 | 76 | } else { |
mfiore | 0:032beb056844 | 77 | wait(1); |
mfiore | 0:032beb056844 | 78 | } |
mfiore | 0:032beb056844 | 79 | } |
mfiore | 1:b12a128aae0e | 80 | #else |
mfiore | 1:b12a128aae0e | 81 | for (int i = 6; i >= 0; i = i - 2) { |
mfiore | 1:b12a128aae0e | 82 | wait(2); |
mfiore | 1:b12a128aae0e | 83 | printf("Waiting %d seconds...\n\r", i); |
mfiore | 0:032beb056844 | 84 | } |
mfiore | 0:032beb056844 | 85 | MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256); |
mfiore | 0:032beb056844 | 86 | serial->baud(9600); |
mfiore | 0:032beb056844 | 87 | Transport::setTransport(Transport::WIFI); |
mfiore | 0:032beb056844 | 88 | Wifi* wifi = Wifi::getInstance(); |
mfiore | 0:032beb056844 | 89 | printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE"); |
mfiore | 5:d4a4e2577be3 | 90 | printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, security_type, phrase)).c_str()); |
mfiore | 0:032beb056844 | 91 | printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str()); |
mfiore | 0:032beb056844 | 92 | printf("Signal Strnegth (dBm): %d\n\r", wifi->getSignalStrength()); |
mfiore | 0:032beb056844 | 93 | printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); |
mfiore | 0:032beb056844 | 94 | printf("Connect: %s\n\r", wifi->connect() ? "Success" : "Failure"); |
mfiore | 0:032beb056844 | 95 | printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); |
mfiore | 1:b12a128aae0e | 96 | #endif |
sam_grove | 3:d24e8bec5610 | 97 | |
pgruenbaum | 8:3b02a48c4990 | 98 | |
pgruenbaum | 8:3b02a48c4990 | 99 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
pgruenbaum | 8:3b02a48c4990 | 100 | printf("MMA8451 ID: %d\n", acc.getWhoAmI()); |
pgruenbaum | 8:3b02a48c4990 | 101 | |
mfiore | 0:032beb056844 | 102 | /* send some data */ |
pgruenbaum | 8:3b02a48c4990 | 103 | /* |
mfiore | 1:b12a128aae0e | 104 | Client client; |
mfiore | 1:b12a128aae0e | 105 | M2XStreamClient m2xClient(&client, key); |
mfiore | 0:032beb056844 | 106 | int ret; |
pgruenbaum | 8:3b02a48c4990 | 107 | int speed = 0; |
pgruenbaum | 8:3b02a48c4990 | 108 | while (true) { |
pgruenbaum | 8:3b02a48c4990 | 109 | speed += 10; |
pgruenbaum | 8:3b02a48c4990 | 110 | printf("sending %d\r\n", speed); |
pgruenbaum | 8:3b02a48c4990 | 111 | ret = m2xClient.send(feed, stream, speed); |
pgruenbaum | 8:3b02a48c4990 | 112 | printf("send() returned %d\r\n", ret); |
pgruenbaum | 8:3b02a48c4990 | 113 | wait(3); |
pgruenbaum | 8:3b02a48c4990 | 114 | } |
pgruenbaum | 8:3b02a48c4990 | 115 | */ |
pgruenbaum | 8:3b02a48c4990 | 116 | Client client; |
pgruenbaum | 8:3b02a48c4990 | 117 | M2XStreamClient m2xClient(&client, key); |
pgruenbaum | 8:3b02a48c4990 | 118 | int ret; |
mfiore | 0:032beb056844 | 119 | while (true) { |
pgruenbaum | 8:3b02a48c4990 | 120 | float x, y, z; |
pgruenbaum | 8:3b02a48c4990 | 121 | x = abs(acc.getAccX()); |
pgruenbaum | 8:3b02a48c4990 | 122 | y = abs(acc.getAccY()); |
pgruenbaum | 8:3b02a48c4990 | 123 | z = abs(acc.getAccZ()); |
pgruenbaum | 8:3b02a48c4990 | 124 | int tiltAngleX = atan(x / sqrt(x * x + z * z)) * 180 / 3.14; |
pgruenbaum | 8:3b02a48c4990 | 125 | int tiltAngleY = atan(y / sqrt(y * y + z * z)) * 180 / 3.14; |
pgruenbaum | 8:3b02a48c4990 | 126 | printf("x: %d, y: %d", tiltAngleX, tiltAngleY); |
pgruenbaum | 8:3b02a48c4990 | 127 | int maxTilt = max(tiltAngleX, tiltAngleY); |
pgruenbaum | 8:3b02a48c4990 | 128 | printf("sending %d\r\n", maxTilt); |
pgruenbaum | 8:3b02a48c4990 | 129 | ret = m2xClient.send(feed, stream, maxTilt); |
mfiore | 0:032beb056844 | 130 | printf("send() returned %d\r\n", ret); |
pgruenbaum | 8:3b02a48c4990 | 131 | wait(3); |
mfiore | 0:032beb056844 | 132 | } |
mfiore | 0:032beb056844 | 133 | } |