A library for talking to Multi-Tech's Cellular SocketModem Devices.
Dependents: M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more
tests/blinky_ping_test.h
- Committer:
- mfiore
- Date:
- 2013-12-31
- Revision:
- 115:b26176f23e89
- Parent:
- 105:1977b7154940
File content as of revision 115:b26176f23e89:
#include "mbed.h" #include "include_me.h" #define MAX_TRIES 5 #define MAX_REGISTRATION_TRIES 10 #define CELL_SHIELD 0 // if using a cell shield board, change to 1 using namespace mts; /* APN is ignored if using wifi */ bool cellPingTest(const std::string& apn, const std::string& server); bool wifiPingTest(const std::string& server, const std::string& ssid, Wifi::SecurityType type, const std::string& key); void blinkLed(DigitalOut led); void pingTest() { DigitalOut ledG(LED1); DigitalOut ledR(LED2); ledG = 1; ledR = 1; std::string server = "8.8.8.8"; // Google's DNS server #if CELL_SHIELD std::string apn = "wap.cingular"; // APN of sim card if (cellPingTest(apn, server)) { #else std::string ssid = "Mtech_guest"; // ssid of wireless network Wifi::SecurityType type = Wifi::WPA2; // NONE, WEP64, WEP128, WPA, WPA2 std::string key = "MultiModem2!"; // password for network (if type is not "NONE") if (wifiPingTest(server, ssid, type, key)) { #endif printf("success!\n\r"); blinkLed(ledG); } else { printf("failure!\n\r"); blinkLed(ledR); } } bool wifiPingTest(const std::string& server, const std::string& ssid, Wifi::SecurityType type, const std::string& key) { int i; Transport::setTransport(Transport::WIFI); MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256); serial->baud(9600); Wifi* wifi = Wifi::getInstance(); wifi->init(serial); printf("waiting for radio to come up\r\n"); wait(5); i = 0; while (i++ < MAX_TRIES) { if (wifi->setNetwork(ssid, type, key) == SUCCESS) { printf("set network\r\n"); break; } else { printf("failed to set network\r\n"); } wait(1); } i = 0; while (i++ < MAX_TRIES) { if (wifi->setDeviceIP() == SUCCESS) { printf("set IP\r\n"); break; } else { printf("failed to set IP\r\n"); } wait(1); } i = 0; while (i++ < MAX_TRIES) { if (wifi->connect()) { printf("connected\r\n"); break; } else { printf("failed to connect\r\n"); } wait(1); } printf("pinging %s\n\r", server.c_str()); return wifi->ping(server); } bool cellPingTest(const std::string& apn, const std::string& server) { int i; Transport::setTransport(Transport::CELLULAR); MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); serial->baud(115200); Cellular* cell = Cellular::getInstance(); cell->init(serial); printf("waiting for radio to come up\n\r"); wait(30); i = 0; while (i++ < MAX_REGISTRATION_TRIES) { if (cell->getRegistration() == Cellular::REGISTERED) { printf("registered with tower\n\r"); break; } else if (i >= MAX_REGISTRATION_TRIES) { printf("failed to register with tower\n\r"); return false; } wait(3); } i = 0; printf("setting APN to %s\n\r", apn.c_str()); while (i++ < MAX_TRIES) { if (cell->setApn(apn) == SUCCESS) { printf("successfully set APN\n\r"); break; } else if (i >= MAX_TRIES) { printf("failed to set APN\n\r"); return false; } wait(1); } i = 0; printf("bringing up PPP link\n\r"); while (i++ < MAX_TRIES) { if (cell->connect()) { printf("PPP link is up\n\r"); break; } else if (i >= MAX_TRIES) { printf("failed to bring PPP link up\n\r"); return false; } wait(1); } printf("pinging %s\n\r", server.c_str()); return cell->ping(server); } void blinkLed(DigitalOut led) { led = 0; while (true) { wait(0.25); led = !led; } }