M2X sample code using SocketModem for Cellular or Wifi access
Dependencies: SocketModem jsonlite mbed
main.cpp
00001 #include "mbed.h" 00002 #include "M2XStreamClient.h" 00003 #include "include_me.h" 00004 00005 using namespace mts; 00006 00007 const char key[] = ""; 00008 const char feed[] = ""; 00009 const char stream[] = ""; 00010 00011 // set to 1 for cellular shield board 00012 // set to 0 for wifi shield board 00013 #define CELL_SHIELD 1 00014 00015 int main() { 00016 #if CELL_SHIELD 00017 for (int i = 30; i >= 0; i = i - 2) { 00018 wait(2); 00019 printf("Waiting %d seconds...\n\r", i); 00020 } 00021 00022 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); 00023 serial->baud(115200); 00024 Transport::setTransport(Transport::CELLULAR); 00025 Cellular* cell = Cellular::getInstance(); 00026 cell->init(serial, PTA4, PTC9); 00027 00028 int max_tries = 5; 00029 int i; 00030 std::string apn = "wap.cingular"; 00031 00032 i = 0; 00033 while (i++ < max_tries) { 00034 if (cell->getRegistration() == Cellular::REGISTERED) { 00035 printf("registered with tower\n\r"); 00036 break; 00037 } else if (i >= max_tries) { 00038 printf("failed to register with tower\n\r"); 00039 } else { 00040 wait(3); 00041 } 00042 } 00043 00044 printf("signal strength: %d\n\r", cell->getSignalStrength()); 00045 00046 i = 0; 00047 printf("setting APN to %s\n\r", apn.c_str()); 00048 while (i++ < max_tries) { 00049 if (cell->setApn(apn) == SUCCESS) { 00050 printf("successfully set APN\n\r"); 00051 break; 00052 } else if (i >= max_tries) { 00053 printf("failed to set APN\n\r"); 00054 } else { 00055 wait(1); 00056 } 00057 } 00058 00059 i = 0; 00060 printf("bringing up PPP link\n\r"); 00061 while (i++ < max_tries) { 00062 if (cell->connect()) { 00063 printf("PPP link is up\n\r"); 00064 break; 00065 } else if (i >= max_tries) { 00066 printf("failed to bring PPP link up\n\r"); 00067 } else { 00068 wait(1); 00069 } 00070 } 00071 #else 00072 for (int i = 6; i >= 0; i = i - 2) { 00073 wait(2); 00074 printf("Waiting %d seconds...\n\r", i); 00075 } 00076 MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256); 00077 serial->baud(9600); 00078 Transport::setTransport(Transport::WIFI); 00079 Wifi* wifi = Wifi::getInstance(); 00080 printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE"); 00081 printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork("", Wifi::WPA2, "")).c_str()); 00082 printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str()); 00083 printf("Signal Strnegth (dBm): %d\n\r", wifi->getSignalStrength()); 00084 printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); 00085 printf("Connect: %s\n\r", wifi->connect() ? "Success" : "Failure"); 00086 printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); 00087 #endif 00088 00089 /* send some data */ 00090 Client client; 00091 M2XStreamClient m2xClient(&client, key); 00092 int ret; 00093 int num; 00094 while (true) { 00095 num = rand(); 00096 printf("sending %d\r\n", num); 00097 ret = m2xClient.send(feed, stream, num); 00098 printf("send() returned %d\r\n", ret); 00099 wait(5); 00100 } 00101 }
Generated on Mon Jul 18 2022 18:40:39 by 1.7.2