Ping a server using the onboard cellular radio.

Dependencies:   mbed mtsas

Committer:
mfiore
Date:
Fri Feb 26 16:47:54 2016 +0000
Revision:
2:0288328818cb
Parent:
1:1411b4274907
Updated mbed library to revision 112, disable regulator's battery charger

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:da114ae7c596 1 /** Dragonfly Cellular Ping Example
mfiore 0:da114ae7c596 2 * Configures the cellular radio, brings up the cellular link, and pings Google.
mfiore 0:da114ae7c596 3 *
mfiore 0:da114ae7c596 4 * NOTE: This example changes the baud rate of the debug port to 115200 baud!
mfiore 0:da114ae7c596 5 */
mfiore 0:da114ae7c596 6
mfiore 0:da114ae7c596 7 #include "mbed.h"
mfiore 0:da114ae7c596 8 #include "mtsas.h"
mfiore 0:da114ae7c596 9
mfiore 2:0288328818cb 10 // This line controls the regulator's battery charger.
mfiore 2:0288328818cb 11 // BC_NCE = 0 enables the battery charger
mfiore 2:0288328818cb 12 // BC_NCE = 1 disables the battery charger
mfiore 2:0288328818cb 13 DigitalOut bc_nce(PB_2);
mfiore 2:0288328818cb 14
mfiore 0:da114ae7c596 15 bool init_mtsas();
mfiore 0:da114ae7c596 16
mfiore 1:1411b4274907 17 // The MTSSerialFlowControl object represents the physical serial link between the processor and the cellular radio.
mfiore 0:da114ae7c596 18 mts::MTSSerialFlowControl* io;
mfiore 1:1411b4274907 19 // The Cellular object represents the cellular radio.
mfiore 0:da114ae7c596 20 mts::Cellular* radio;
mfiore 0:da114ae7c596 21
mfiore 1:1411b4274907 22 // An APN is required for GSM radios.
mfiore 0:da114ae7c596 23 static const char apn[] = "";
mfiore 0:da114ae7c596 24
mfiore 0:da114ae7c596 25 bool radio_ok = false;
mfiore 0:da114ae7c596 26
mfiore 0:da114ae7c596 27 int main() {
mfiore 2:0288328818cb 28 // Disable the battery charger unless a battery is attached.
mfiore 2:0288328818cb 29 bc_nce = 1;
mfiore 2:0288328818cb 30
mfiore 1:1411b4274907 31 // Change the baud rate of the debug port from the default 9600 to 115200.
mfiore 0:da114ae7c596 32 Serial debug(USBTX, USBRX);
mfiore 0:da114ae7c596 33 debug.baud(115200);
mfiore 0:da114ae7c596 34
mfiore 0:da114ae7c596 35 //Sets the log level to INFO, higher log levels produce more log output.
mfiore 0:da114ae7c596 36 //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
mfiore 0:da114ae7c596 37 mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL);
mfiore 0:da114ae7c596 38
mfiore 0:da114ae7c596 39 logInfo("initializing cellular radio");
mfiore 0:da114ae7c596 40 radio_ok = init_mtsas();
mfiore 0:da114ae7c596 41 if (! radio_ok) {
mfiore 0:da114ae7c596 42 while (true) {
mfiore 0:da114ae7c596 43 logError("failed to initialize cellular radio");
mfiore 0:da114ae7c596 44 wait(1);
mfiore 0:da114ae7c596 45 }
mfiore 0:da114ae7c596 46 }
mfiore 0:da114ae7c596 47
mfiore 0:da114ae7c596 48 logInfo("setting APN");
mfiore 0:da114ae7c596 49 if (radio->setApn(apn) != MTS_SUCCESS)
mfiore 0:da114ae7c596 50 logError("failed to set APN to \"%s\"", apn);
mfiore 0:da114ae7c596 51
mfiore 0:da114ae7c596 52 logInfo("bringing up the link");
mfiore 0:da114ae7c596 53 if (! radio->connect()) {
mfiore 0:da114ae7c596 54 logError("failed to bring up the link");
mfiore 0:da114ae7c596 55 } else {
mfiore 0:da114ae7c596 56 for (int i = 0; i < 10; i++) {
mfiore 0:da114ae7c596 57 logInfo("pinging");
mfiore 0:da114ae7c596 58 if (! radio->ping("www.google.com"))
mfiore 0:da114ae7c596 59 logError("failed to ping");
mfiore 0:da114ae7c596 60 else
mfiore 0:da114ae7c596 61 logInfo("ping succeeded");
mfiore 0:da114ae7c596 62 }
mfiore 0:da114ae7c596 63 }
mfiore 0:da114ae7c596 64
mfiore 0:da114ae7c596 65 logInfo("finished - bringing down link");
mfiore 0:da114ae7c596 66 radio->disconnect();
mfiore 0:da114ae7c596 67
mfiore 0:da114ae7c596 68 return 0;
mfiore 0:da114ae7c596 69 }
mfiore 0:da114ae7c596 70
mfiore 0:da114ae7c596 71 bool init_mtsas() {
mfiore 0:da114ae7c596 72 io = new mts::MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
mfiore 0:da114ae7c596 73 if (! io)
mfiore 0:da114ae7c596 74 return false;
mfiore 0:da114ae7c596 75
mfiore 0:da114ae7c596 76 // radio default baud rate is 115200
mfiore 0:da114ae7c596 77 io->baud(115200);
mfiore 0:da114ae7c596 78 radio = mts::CellularFactory::create(io);
mfiore 0:da114ae7c596 79 if (! radio)
mfiore 0:da114ae7c596 80 return false;
mfiore 0:da114ae7c596 81
mfiore 0:da114ae7c596 82 // Transport must be set properly before any TCPSocketConnection or UDPSocket objects are created
mfiore 0:da114ae7c596 83 Transport::setTransport(radio);
mfiore 0:da114ae7c596 84
mfiore 0:da114ae7c596 85 return true;
mfiore 0:da114ae7c596 86 }