mikeflores2000 AT&T MultiTech M2X Exercises book
Dependencies: M2XStreamClient MMA8451Q SocketModem jsonlite mbed
Fork of M2X_MTS_Accel by
main.cpp@1:f6254f28c89c, 2014-07-16 (annotated)
- Committer:
- jb8414
- Date:
- Wed Jul 16 16:17:03 2014 +0000
- Revision:
- 1:f6254f28c89c
- Parent:
- 0:b4f0b744fbc3
- Child:
- 2:93ca5135c262
changed APN endpoint
Who changed what in which revision?
User | Revision | Line number | New 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 | |
pgruenbaum | 0:b4f0b744fbc3 | 21 | const char key[] = "<key>"; // Replace with your M2X user account master key |
pgruenbaum | 0:b4f0b744fbc3 | 22 | const char feed[] = "<feed>"; // Replace with your blueprint feed ID |
pgruenbaum | 0:b4f0b744fbc3 | 23 | const char stream[] = "<stream>"; // 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 |
pgruenbaum | 0:b4f0b744fbc3 | 30 | std::string ssid = "<ssid>"; // Replace with your Wifi ID (SSID) |
pgruenbaum | 0:b4f0b744fbc3 | 31 | std::string phrase = "<password>"; // 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 |
pgruenbaum | 0:b4f0b744fbc3 | 116 | // ... |
pgruenbaum | 0:b4f0b744fbc3 | 117 | |
pgruenbaum | 0:b4f0b744fbc3 | 118 | } |