M2X demo using the Freescale FRDM-KL46Z and Multitech Socketmodem MTSMS-H5
Dependencies: M2XStreamClient MMA8451Q SocketModem jsonlite mbed
Fork of MTS_M2x_Example by
main.cpp
00001 #include "mbed.h" 00002 #include "MMA8451Q.h" 00003 #include "M2XStreamClient.h" 00004 #include "include_me.h" 00005 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[] = ""; // enter your m2x user account master key 00022 const char feed[] = ""; // enter your blueprint feed id 00023 const char* names[] = { "x", "y", "z" }; // user will need to create these Streams manually in the Dev Portal 00024 int counts[] = { 1, 1, 1 }; 00025 00026 00027 00028 // set to 1 for cellular shield board 00029 // set to 0 for wifi shield board 00030 #define CELL_SHIELD 1 00031 00032 // ssid and phrase for wifi 00033 std::string ssid = "belkin54g"; 00034 std::string phrase = "hackathon"; 00035 Wifi::SecurityType security_type = Wifi::WPA; 00036 00037 int main() 00038 { 00039 #if CELL_SHIELD 00040 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); 00041 serial->baud(115200); 00042 Transport::setTransport(Transport::CELLULAR); 00043 Cellular* cell = Cellular::getInstance(); 00044 cell->init(serial, PTA4, PTC9); //DCD and DTR pins for KL46Z 00045 00046 int max_tries = 5; 00047 int i; 00048 std::string apn = "m2m.com.attz"; 00049 00050 i = 0; 00051 while (i++ < max_tries) { 00052 if (cell->getRegistration() == Cellular::REGISTERED) { 00053 printf("registered with tower\n\r"); 00054 break; 00055 } else if (i >= max_tries) { 00056 printf("failed to register with tower\n\r"); 00057 } else { 00058 wait(3); 00059 } 00060 } 00061 00062 printf("signal strength: %d\n\r", cell->getSignalStrength()); 00063 00064 i = 0; 00065 printf("setting APN to %s\n\r", apn.c_str()); 00066 while (i++ < max_tries) { 00067 if (cell->setApn(apn) == SUCCESS) { 00068 printf("successfully set APN\n\r"); 00069 break; 00070 } else if (i >= max_tries) { 00071 printf("failed to set APN\n\r"); 00072 } else { 00073 wait(1); 00074 } 00075 } 00076 00077 i = 0; 00078 printf("bringing up PPP link\n\r"); 00079 while (i++ < max_tries) { 00080 if (cell->connect()) { 00081 printf("PPP link is up\n\r"); 00082 break; 00083 } else if (i >= max_tries) { 00084 printf("failed to bring PPP link up\n\r"); 00085 } else { 00086 wait(1); 00087 } 00088 } 00089 #else 00090 for (int i = 6; i >= 0; i = i - 2) { 00091 wait(2); 00092 printf("Waiting %d seconds...\n\r", i); 00093 } 00094 MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256); 00095 serial->baud(9600); 00096 Transport::setTransport(Transport::WIFI); 00097 Wifi* wifi = Wifi::getInstance(); 00098 printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE"); 00099 printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, security_type, phrase)).c_str()); 00100 printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str()); 00101 printf("Signal Strnegth (dBm): %d\n\r", wifi->getSignalStrength()); 00102 printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); 00103 printf("Connect: %s\n\r", wifi->connect() ? "Success" : "Failure"); 00104 printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); 00105 #endif 00106 00107 /* send some data */ 00108 Client client; 00109 M2XStreamClient m2xClient(&client, key); 00110 int ret; 00111 int streamNum = 3; 00112 00113 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); 00114 PwmOut rled(LED1); 00115 PwmOut gled(LED2); 00116 PwmOut bled(LED3); 00117 00118 printf("MMA8451 ID: %d\n", acc.getWhoAmI()); 00119 00120 while (true) { 00121 float x, y, z; 00122 x = rled = 1.0 - abs(acc.getAccX()); 00123 y = gled = 1.0 - abs(acc.getAccY()); 00124 z = bled = 1.0 - abs(acc.getAccZ()); 00125 printf("Sending X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); 00126 double values[] = { x, y, z }; 00127 ret = m2xClient.postMultiple(feed, streamNum, names, counts, NULL, values); 00128 printf("send() returned %d\r\n", ret); 00129 wait(5); 00130 } 00131 00132 00133 }
Generated on Thu Jul 21 2022 13:55:24 by 1.7.2