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

Committer:
mfiore
Date:
Tue Sep 02 18:38:55 2014 +0000
Revision:
152:9a2c7ed27744
Parent:
143:c7d8fe37981b
Wifi: fix compatibility break with old shields by checking for both old and new style responses to "show connection" command

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kranjan 141:571e0ef6c8dc 1 /* Universal Socket Modem Interface Library
kranjan 141:571e0ef6c8dc 2 * Copyright (c) 2013 Multi-Tech Systems
kranjan 141:571e0ef6c8dc 3 *
kranjan 141:571e0ef6c8dc 4 * Licensed under the Apache License, Version 2.0 (the "License");
kranjan 141:571e0ef6c8dc 5 * you may not use this file except in compliance with the License.
kranjan 141:571e0ef6c8dc 6 * You may obtain a copy of the License at
kranjan 141:571e0ef6c8dc 7 *
kranjan 141:571e0ef6c8dc 8 * http://www.apache.org/licenses/LICENSE-2.0
kranjan 141:571e0ef6c8dc 9 *
kranjan 141:571e0ef6c8dc 10 * Unless required by applicable law or agreed to in writing, software
kranjan 141:571e0ef6c8dc 11 * distributed under the License is distributed on an "AS IS" BASIS,
kranjan 141:571e0ef6c8dc 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kranjan 141:571e0ef6c8dc 13 * See the License for the specific language governing permissions and
kranjan 141:571e0ef6c8dc 14 * limitations under the License.
kranjan 141:571e0ef6c8dc 15 */
kranjan 141:571e0ef6c8dc 16
mfiore 124:6d964b4343c8 17 #ifndef TESTPING_H
mfiore 124:6d964b4343c8 18 #define TESTPING_H
mfiore 124:6d964b4343c8 19
mfiore 26:f3e06d63967f 20 #include "mbed.h"
mfiore 26:f3e06d63967f 21 #include "include_me.h"
mfiore 26:f3e06d63967f 22
mfiore 26:f3e06d63967f 23 #define MAX_TRIES 5
mfiore 31:31b95ea90f80 24 #define MAX_REGISTRATION_TRIES 10
mfiore 26:f3e06d63967f 25
mfiore 124:6d964b4343c8 26 // 0 for shield board with wifi
mfiore 124:6d964b4343c8 27 // 1 for shield board with cellular
mfiore 124:6d964b4343c8 28 #define CELL_SHIELD 0
mfiore 124:6d964b4343c8 29
mfiore 124:6d964b4343c8 30 /* tries to ping 8.8.8.8 (Google's DNS server)
mfiore 124:6d964b4343c8 31 * blinks green LED if successful, red LED if failure */
mfiore 115:b26176f23e89 32
mfiore 115:b26176f23e89 33 using namespace mts;
mfiore 115:b26176f23e89 34
mfiore 115:b26176f23e89 35 bool cellPingTest(const std::string& apn, const std::string& server);
mfiore 115:b26176f23e89 36 bool wifiPingTest(const std::string& server, const std::string& ssid, Wifi::SecurityType type, const std::string& key);
mfiore 26:f3e06d63967f 37 void blinkLed(DigitalOut led);
mfiore 26:f3e06d63967f 38
mfiore 124:6d964b4343c8 39 void testPing() {
mfiore 26:f3e06d63967f 40 DigitalOut ledG(LED1);
mfiore 26:f3e06d63967f 41 DigitalOut ledR(LED2);
mfiore 26:f3e06d63967f 42
mfiore 26:f3e06d63967f 43 ledG = 1;
mfiore 26:f3e06d63967f 44 ledR = 1;
mfiore 115:b26176f23e89 45
mfiore 115:b26176f23e89 46 std::string server = "8.8.8.8"; // Google's DNS server
mfiore 115:b26176f23e89 47 #if CELL_SHIELD
mfiore 115:b26176f23e89 48 std::string apn = "wap.cingular"; // APN of sim card
mfiore 115:b26176f23e89 49 if (cellPingTest(apn, server)) {
mfiore 115:b26176f23e89 50 #else
mfiore 124:6d964b4343c8 51 std::string ssid = ""; // ssid of wireless network
mfiore 115:b26176f23e89 52 Wifi::SecurityType type = Wifi::WPA2; // NONE, WEP64, WEP128, WPA, WPA2
mfiore 124:6d964b4343c8 53 std::string key = ""; // password for network (if type is not "NONE")
mfiore 115:b26176f23e89 54 if (wifiPingTest(server, ssid, type, key)) {
mfiore 115:b26176f23e89 55 #endif
mfiore 26:f3e06d63967f 56 printf("success!\n\r");
mfiore 26:f3e06d63967f 57 blinkLed(ledG);
mfiore 26:f3e06d63967f 58 } else {
mfiore 26:f3e06d63967f 59 printf("failure!\n\r");
mfiore 26:f3e06d63967f 60 blinkLed(ledR);
mfiore 26:f3e06d63967f 61 }
mfiore 26:f3e06d63967f 62 }
mfiore 26:f3e06d63967f 63
mfiore 115:b26176f23e89 64 bool wifiPingTest(const std::string& server, const std::string& ssid, Wifi::SecurityType type, const std::string& key) {
mfiore 26:f3e06d63967f 65 int i;
mfiore 26:f3e06d63967f 66
mfiore 124:6d964b4343c8 67 for (int i = 6; i >= 0; i = i - 2) {
mfiore 124:6d964b4343c8 68 wait(2);
mfiore 124:6d964b4343c8 69 printf("Waiting %d seconds...\n\r", i);
mfiore 124:6d964b4343c8 70 }
mfiore 124:6d964b4343c8 71
mfiore 115:b26176f23e89 72 Transport::setTransport(Transport::WIFI);
mfiore 115:b26176f23e89 73 MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256);
mfiore 115:b26176f23e89 74 serial->baud(9600);
mfiore 115:b26176f23e89 75 Wifi* wifi = Wifi::getInstance();
mfiore 115:b26176f23e89 76 wifi->init(serial);
mfiore 115:b26176f23e89 77
mfiore 115:b26176f23e89 78 i = 0;
mfiore 115:b26176f23e89 79 while (i++ < MAX_TRIES) {
mfiore 115:b26176f23e89 80 if (wifi->setNetwork(ssid, type, key) == SUCCESS) {
mfiore 115:b26176f23e89 81 printf("set network\r\n");
mfiore 115:b26176f23e89 82 break;
mfiore 115:b26176f23e89 83 } else {
mfiore 115:b26176f23e89 84 printf("failed to set network\r\n");
mfiore 115:b26176f23e89 85 }
mfiore 115:b26176f23e89 86 wait(1);
mfiore 115:b26176f23e89 87 }
mfiore 115:b26176f23e89 88
mfiore 115:b26176f23e89 89 i = 0;
mfiore 115:b26176f23e89 90 while (i++ < MAX_TRIES) {
mfiore 115:b26176f23e89 91 if (wifi->setDeviceIP() == SUCCESS) {
mfiore 115:b26176f23e89 92 printf("set IP\r\n");
mfiore 115:b26176f23e89 93 break;
mfiore 115:b26176f23e89 94 } else {
mfiore 115:b26176f23e89 95 printf("failed to set IP\r\n");
mfiore 115:b26176f23e89 96 }
mfiore 115:b26176f23e89 97 wait(1);
mfiore 115:b26176f23e89 98 }
mfiore 115:b26176f23e89 99
mfiore 115:b26176f23e89 100 i = 0;
mfiore 115:b26176f23e89 101 while (i++ < MAX_TRIES) {
mfiore 115:b26176f23e89 102 if (wifi->connect()) {
mfiore 115:b26176f23e89 103 printf("connected\r\n");
mfiore 115:b26176f23e89 104 break;
mfiore 115:b26176f23e89 105 } else {
mfiore 115:b26176f23e89 106 printf("failed to connect\r\n");
mfiore 115:b26176f23e89 107 }
mfiore 115:b26176f23e89 108 wait(1);
mfiore 115:b26176f23e89 109 }
mfiore 115:b26176f23e89 110
mfiore 115:b26176f23e89 111 printf("pinging %s\n\r", server.c_str());
mfiore 115:b26176f23e89 112 return wifi->ping(server);
mfiore 115:b26176f23e89 113 }
mfiore 115:b26176f23e89 114
mfiore 115:b26176f23e89 115 bool cellPingTest(const std::string& apn, const std::string& server) {
mfiore 143:c7d8fe37981b 116 int i;
mfiore 124:6d964b4343c8 117
mfiore 115:b26176f23e89 118 Transport::setTransport(Transport::CELLULAR);
mfiore 26:f3e06d63967f 119 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8);
mfiore 26:f3e06d63967f 120 serial->baud(115200);
mfiore 105:1977b7154940 121 Cellular* cell = Cellular::getInstance();
mfiore 105:1977b7154940 122 cell->init(serial);
mfiore 26:f3e06d63967f 123
mfiore 31:31b95ea90f80 124 i = 0;
mfiore 31:31b95ea90f80 125 while (i++ < MAX_REGISTRATION_TRIES) {
mfiore 31:31b95ea90f80 126 if (cell->getRegistration() == Cellular::REGISTERED) {
mfiore 31:31b95ea90f80 127 printf("registered with tower\n\r");
mfiore 31:31b95ea90f80 128 break;
mfiore 31:31b95ea90f80 129 } else if (i >= MAX_REGISTRATION_TRIES) {
mfiore 31:31b95ea90f80 130 printf("failed to register with tower\n\r");
mfiore 31:31b95ea90f80 131 return false;
mfiore 31:31b95ea90f80 132 }
mfiore 31:31b95ea90f80 133 wait(3);
mfiore 31:31b95ea90f80 134 }
mfiore 31:31b95ea90f80 135
mfiore 26:f3e06d63967f 136 i = 0;
mfiore 26:f3e06d63967f 137 printf("setting APN to %s\n\r", apn.c_str());
mfiore 26:f3e06d63967f 138 while (i++ < MAX_TRIES) {
mfiore 105:1977b7154940 139 if (cell->setApn(apn) == SUCCESS) {
mfiore 26:f3e06d63967f 140 printf("successfully set APN\n\r");
mfiore 26:f3e06d63967f 141 break;
mfiore 31:31b95ea90f80 142 } else if (i >= MAX_TRIES) {
mfiore 26:f3e06d63967f 143 printf("failed to set APN\n\r");
mfiore 26:f3e06d63967f 144 return false;
mfiore 26:f3e06d63967f 145 }
mfiore 26:f3e06d63967f 146 wait(1);
mfiore 26:f3e06d63967f 147 }
mfiore 26:f3e06d63967f 148
mfiore 26:f3e06d63967f 149 i = 0;
mfiore 26:f3e06d63967f 150 printf("bringing up PPP link\n\r");
mfiore 26:f3e06d63967f 151 while (i++ < MAX_TRIES) {
mfiore 26:f3e06d63967f 152 if (cell->connect()) {
mfiore 26:f3e06d63967f 153 printf("PPP link is up\n\r");
mfiore 26:f3e06d63967f 154 break;
mfiore 31:31b95ea90f80 155 } else if (i >= MAX_TRIES) {
mfiore 26:f3e06d63967f 156 printf("failed to bring PPP link up\n\r");
mfiore 26:f3e06d63967f 157 return false;
mfiore 26:f3e06d63967f 158 }
mfiore 26:f3e06d63967f 159 wait(1);
mfiore 26:f3e06d63967f 160 }
mfiore 26:f3e06d63967f 161
mfiore 26:f3e06d63967f 162 printf("pinging %s\n\r", server.c_str());
mfiore 26:f3e06d63967f 163 return cell->ping(server);
mfiore 26:f3e06d63967f 164 }
mfiore 26:f3e06d63967f 165
mfiore 26:f3e06d63967f 166 void blinkLed(DigitalOut led) {
mfiore 26:f3e06d63967f 167 led = 0;
mfiore 26:f3e06d63967f 168
mfiore 26:f3e06d63967f 169 while (true) {
mfiore 26:f3e06d63967f 170 wait(0.25);
mfiore 26:f3e06d63967f 171 led = !led;
mfiore 26:f3e06d63967f 172 }
mfiore 124:6d964b4343c8 173 }
mfiore 124:6d964b4343c8 174
kranjan 141:571e0ef6c8dc 175 #endif