Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FXOS8700Q M2XStreamClient SocketModem jsonlite mbed
Fork of M2X_MTS_Accel by
main.cpp
00001 #include "mbed.h" 00002 #include "FXOS8700Q.h" 00003 #include "M2XStreamClient.h" 00004 #include "include_me.h" 00005 #include "math.h" 00006 00007 using namespace mts; 00008 00009 char deviceId[] = "<device id>"; // Device you want to push to 00010 char streamName[] = "<stream name>"; // Stream you want to push to 00011 char m2xKey[] = "<m2x api key>"; // Your M2X API Key or Master API Key 00012 00013 // set to 1 for cellular shield board 00014 // set to 0 for wifi shield board 00015 #define CELL_SHIELD 0 00016 #define SUCCESS 1 00017 00018 // ssid and phrase for wifi 00019 std::string ssid = "<ssid>"; // Replace with your Wifi ID (SSID) 00020 std::string phrase = "<password>"; // Replace with your Wifi phrase (password) 00021 Wifi::SecurityType security_type = Wifi::WPA; // Replace with your Wifi security type 00022 00023 int main() 00024 { 00025 printf("starting\n\r"); 00026 #if CELL_SHIELD 00027 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTC17, PTC16, PTA1, PTC2); 00028 serial->baud(115200); 00029 Transport::setTransport(Transport::CELLULAR); 00030 Cellular* cell = Cellular::getInstance(); 00031 cell->init(serial, PTA2, PTE25); //DCD and DTR pins for K64F 00032 00033 int max_tries = 5; 00034 int i; 00035 std::string apn = "wap.cingular"; // set to the appropriate APN (i.e. "m2m.com.attz" for M2X SIMs, "wap.cingular" for others) 00036 00037 i = 0; 00038 while (i++ < max_tries) { 00039 if (cell->getRegistration() == Cellular::REGISTERED) { 00040 printf("registered with tower\n\r"); 00041 break; 00042 } else if (i >= max_tries) { 00043 printf("failed to register with tower\n\r"); 00044 } else { 00045 wait(3); 00046 } 00047 } 00048 00049 printf("signal strength: %d\n\r", cell->getSignalStrength()); 00050 00051 i = 0; 00052 printf("setting APN to %s\n\r", apn.c_str()); 00053 while (i++ < max_tries) { 00054 if (cell->setApn(apn) == SUCCESS) { 00055 printf("successfully set APN\n\r"); 00056 break; 00057 } else if (i >= max_tries) { 00058 printf("failed to set APN\n\r"); 00059 } else { 00060 wait(1); 00061 } 00062 } 00063 00064 i = 0; 00065 printf("bringing up PPP link\n\r"); 00066 while (i++ < max_tries) { 00067 if (cell->connect()) { 00068 printf("PPP link is up\n\r"); 00069 break; 00070 } else if (i >= max_tries) { 00071 printf("failed to bring PPP link up\n\r"); 00072 } else { 00073 wait(1); 00074 } 00075 } 00076 #else 00077 // WiFi shield 00078 for (int i = 6; i >= 0; i = i - 2) { 00079 wait(2); 00080 printf("Waiting %d seconds...\n\r", i); 00081 } 00082 MTSSerial* serial = new MTSSerial(PTC17, PTC16, 256, 256); 00083 serial->baud(9600); 00084 Transport::setTransport(Transport::WIFI); 00085 Wifi* wifi = Wifi::getInstance(); 00086 printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE"); 00087 printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, security_type, phrase)).c_str()); 00088 printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str()); 00089 printf("Signal Strnegth (dBm): %d\n\r", wifi->getSignalStrength()); 00090 printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); 00091 printf("Connect: %s\n\r", wifi->connect() ? "Success" : "Failure"); 00092 printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); 00093 #endif 00094 00095 // Initialize the M2X client 00096 Client client; 00097 M2XStreamClient m2xClient(&client, m2xKey); 00098 int ret; 00099 00100 // Add code below to read accelerometer data and post to M2X stream 00101 // ... 00102 00103 // Create an accelerometer instance 00104 FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board 00105 acc.enable(); 00106 printf("\r\n\nFXOS8700Q Who Am I= %X\r\n", acc.whoAmI()); 00107 00108 while (true) { 00109 00110 float x, y, z; 00111 acc.getX(&x); 00112 acc.getY(&y); 00113 acc.getZ(&z); 00114 00115 printf("Accel X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); 00116 00117 // Calculate pitch and roll. Find the maximum tilt angle. 00118 float pitch = atan(x / sqrt(y * y + z * z)); 00119 float roll = atan(y / sqrt(x * x + z * z)); 00120 float maxTilt = 00121 max(abs(roll), abs(pitch)) * 180.0 / 3.14159; 00122 printf("pitch: %5.1f roll: %5.1f maxTilt: %5.1f\n\r", 00123 pitch, roll, maxTilt); 00124 00125 // If the maximum title is over 20 degrees, then send 00126 // data to stream 00127 if (maxTilt > 20) { 00128 ret = m2xClient.updateStreamValue(deviceId, streamName, maxTilt); 00129 printf("send() returned %d\r\n", ret); 00130 wait(1.0); 00131 } 00132 } 00133 }
Generated on Fri Jul 15 2022 10:28:54 by
