Sample code for a FRDM-KL46Z device that can read accelerometer data and then post it to the M2X platform. For use with AT&T DevLab.
Dependencies: M2XStreamClient MMA8451Q SocketModem jsonlite mbed
main.cpp
00001 #include "mbed.h" 00002 #include "MMA8451Q.h" 00003 #include "M2XStreamClient.h" 00004 #include "include_me.h" 00005 #include "math.h" 00006 00007 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) 00008 PinName const SDA = PTE25; 00009 PinName const SCL = PTE24; 00010 #elif defined (TARGET_KL05Z) 00011 PinName const SDA = PTB4; 00012 PinName const SCL = PTB3; 00013 #else 00014 #error TARGET NOT DEFINED 00015 #endif 00016 00017 #define MMA8451_I2C_ADDRESS (0x1d<<1) 00018 00019 using namespace mts; 00020 00021 const char key[] = "<key>"; // Replace with your M2X user account master key 00022 const char feed[] = "<feed>"; // Replace with your blueprint feed ID 00023 const char stream[] = "<stream>"; // Replace with your stream name 00024 00025 // set to 1 for cellular shield board 00026 // set to 0 for wifi shield board 00027 #define CELL_SHIELD 0 00028 00029 // ssid and phrase for wifi 00030 std::string ssid = "<ssid>"; // Replace with your Wifi ID (SSID) 00031 std::string phrase = "<password>"; // Replace with your Wifi phrase (password) 00032 Wifi::SecurityType security_type = Wifi::WPA; // Replace with your Wifi security type 00033 00034 int main() 00035 { 00036 #if CELL_SHIELD 00037 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); 00038 serial->baud(115200); 00039 Transport::setTransport(Transport::CELLULAR); 00040 Cellular* cell = Cellular::getInstance(); 00041 cell->init(serial, PTA4, PTC9); //DCD and DTR pins for KL46Z 00042 00043 int max_tries = 5; 00044 int i; 00045 std::string apn = "m2m.com.attz"; 00046 00047 i = 0; 00048 while (i++ < max_tries) { 00049 if (cell->getRegistration() == Cellular::REGISTERED) { 00050 printf("registered with tower\n\r"); 00051 break; 00052 } else if (i >= max_tries) { 00053 printf("failed to register with tower\n\r"); 00054 } else { 00055 wait(3); 00056 } 00057 } 00058 00059 printf("signal strength: %d\n\r", cell->getSignalStrength()); 00060 00061 i = 0; 00062 printf("setting APN to %s\n\r", apn.c_str()); 00063 while (i++ < max_tries) { 00064 if (cell->setApn(apn) == SUCCESS) { 00065 printf("successfully set APN\n\r"); 00066 break; 00067 } else if (i >= max_tries) { 00068 printf("failed to set APN\n\r"); 00069 } else { 00070 wait(1); 00071 } 00072 } 00073 00074 i = 0; 00075 printf("bringing up PPP link\n\r"); 00076 while (i++ < max_tries) { 00077 if (cell->connect()) { 00078 printf("PPP link is up\n\r"); 00079 break; 00080 } else if (i >= max_tries) { 00081 printf("failed to bring PPP link up\n\r"); 00082 } else { 00083 wait(1); 00084 } 00085 } 00086 #else 00087 // WiFi shield 00088 for (int i = 6; i >= 0; i = i - 2) { 00089 wait(2); 00090 printf("Waiting %d seconds...\n\r", i); 00091 } 00092 MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256); 00093 serial->baud(9600); 00094 Transport::setTransport(Transport::WIFI); 00095 Wifi* wifi = Wifi::getInstance(); 00096 printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE"); 00097 printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, security_type, phrase)).c_str()); 00098 printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str()); 00099 printf("Signal Strnegth (dBm): %d\n\r", wifi->getSignalStrength()); 00100 printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); 00101 printf("Connect: %s\n\r", wifi->connect() ? "Success" : "Failure"); 00102 printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); 00103 #endif 00104 00105 // Initialize the M2X client 00106 Client client; 00107 M2XStreamClient m2xClient(&client, key); 00108 int ret; 00109 00110 // Create an accelerometer instance 00111 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); 00112 00113 printf("MMA8451 ID: %d\n", acc.getWhoAmI()); 00114 00115 // Add code here to read accelerometer data and post to M2X stream 00116 // ... 00117 00118 }
Generated on Tue Jul 12 2022 21:16:05 by 1.7.2