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: mbed SocketModem nanoservice_client_1_12
main.cpp@1:5147d3fa7816, 2014-02-17 (annotated)
- Committer:
- zdshelby
- Date:
- Mon Feb 17 22:26:04 2014 +0000
- Revision:
- 1:5147d3fa7816
- Parent:
- 0:f739ace74102
- Child:
- 2:1592223f12e1
- Added Cellular and TCP init calls
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| zdshelby | 0:f739ace74102 | 1 | #include "mbed.h" |
| zdshelby | 0:f739ace74102 | 2 | #include "config.h" |
| zdshelby | 0:f739ace74102 | 3 | #include "debug.h" |
| zdshelby | 0:f739ace74102 | 4 | |
| zdshelby | 1:5147d3fa7816 | 5 | // Multitech Cellular includes |
| zdshelby | 1:5147d3fa7816 | 6 | #include "Cellular.h" |
| zdshelby | 1:5147d3fa7816 | 7 | #include "Endpoint.h" |
| zdshelby | 1:5147d3fa7816 | 8 | #include "IPStack.h" |
| zdshelby | 1:5147d3fa7816 | 9 | #include "MTSSerialFlowControl.h" |
| zdshelby | 1:5147d3fa7816 | 10 | |
| zdshelby | 1:5147d3fa7816 | 11 | using namespace mts; |
| zdshelby | 1:5147d3fa7816 | 12 | |
| zdshelby | 1:5147d3fa7816 | 13 | Cellular* cellular; |
| zdshelby | 1:5147d3fa7816 | 14 | |
| zdshelby | 1:5147d3fa7816 | 15 | // **************************************************************************** |
| zdshelby | 1:5147d3fa7816 | 16 | // Cellular initialization |
| zdshelby | 1:5147d3fa7816 | 17 | |
| zdshelby | 1:5147d3fa7816 | 18 | static void cellular_init() |
| zdshelby | 1:5147d3fa7816 | 19 | { |
| zdshelby | 1:5147d3fa7816 | 20 | |
| zdshelby | 1:5147d3fa7816 | 21 | //Setup serial interface to radio |
| zdshelby | 1:5147d3fa7816 | 22 | MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); |
| zdshelby | 1:5147d3fa7816 | 23 | serial->baud(115200); |
| zdshelby | 1:5147d3fa7816 | 24 | |
| zdshelby | 1:5147d3fa7816 | 25 | //Setup Cellular class |
| zdshelby | 1:5147d3fa7816 | 26 | cellular = Cellular::getInstance(); |
| zdshelby | 1:5147d3fa7816 | 27 | cellular->init(serial, PTA4, PTC9); //DCD and DTR pins for KL46Z |
| zdshelby | 1:5147d3fa7816 | 28 | |
| zdshelby | 1:5147d3fa7816 | 29 | //Run status and configuration commands |
| zdshelby | 1:5147d3fa7816 | 30 | DEBUG("\n\r////Start Status and Configuration Commands////"); |
| zdshelby | 1:5147d3fa7816 | 31 | DEBUG("Command Test: %s", getCodeNames(cellular->test()).c_str()); //Make sure you can talk to the radio |
| zdshelby | 1:5147d3fa7816 | 32 | DEBUG("Signal Strength: %d", cellular->getSignalStrength()); //Check the signal strength should be above 8 |
| zdshelby | 1:5147d3fa7816 | 33 | |
| zdshelby | 1:5147d3fa7816 | 34 | //Makes sure you are reistered with cell |
| zdshelby | 1:5147d3fa7816 | 35 | DEBUG("Registration State: %s", Cellular::getRegistrationNames(cellular->getRegistration()).c_str()); |
| zdshelby | 1:5147d3fa7816 | 36 | |
| zdshelby | 1:5147d3fa7816 | 37 | //Shows example of how to send other commands, look at AT command guide for more info |
| zdshelby | 1:5147d3fa7816 | 38 | DEBUG("Send Basic Command (AT): %s", getCodeNames(cellular->sendBasicCommand("AT", 1000)).c_str()); |
| zdshelby | 1:5147d3fa7816 | 39 | DEBUG("Send Command (AT+CSQ): %s", cellular->sendCommand("AT+CSQ", 1000).c_str()); |
| zdshelby | 1:5147d3fa7816 | 40 | |
| zdshelby | 1:5147d3fa7816 | 41 | //Start Test |
| zdshelby | 1:5147d3fa7816 | 42 | DEBUG("\n\r////Start Network Connectivity Test////"); |
| zdshelby | 1:5147d3fa7816 | 43 | DEBUG("Set APN: %s", getCodeNames(cellular->setApn(CELLULAR_APN)).c_str()); //Use APN from service provider!!! |
| zdshelby | 1:5147d3fa7816 | 44 | |
| zdshelby | 1:5147d3fa7816 | 45 | //Setup a data connection |
| zdshelby | 1:5147d3fa7816 | 46 | DEBUG("Attempting to Connect, this may take some time..."); |
| zdshelby | 1:5147d3fa7816 | 47 | while (!cellular->connect()) { |
| zdshelby | 1:5147d3fa7816 | 48 | DEBUG("Failed to connect... Trying again."); |
| zdshelby | 1:5147d3fa7816 | 49 | wait(1); |
| zdshelby | 1:5147d3fa7816 | 50 | } |
| zdshelby | 1:5147d3fa7816 | 51 | DEBUG("Connected to the Network!"); |
| zdshelby | 1:5147d3fa7816 | 52 | |
| zdshelby | 1:5147d3fa7816 | 53 | //Try pinging default server "8.8.8.8" (Google's DNS) |
| zdshelby | 1:5147d3fa7816 | 54 | DEBUG("Ping Valid: %s", cellular->ping() ? "true" : "false"); |
| zdshelby | 1:5147d3fa7816 | 55 | wait(3); |
| zdshelby | 1:5147d3fa7816 | 56 | |
| zdshelby | 1:5147d3fa7816 | 57 | } |
| zdshelby | 1:5147d3fa7816 | 58 | |
| zdshelby | 1:5147d3fa7816 | 59 | // **************************************************************************** |
| zdshelby | 1:5147d3fa7816 | 60 | // NSP initialization |
| zdshelby | 1:5147d3fa7816 | 61 | |
| zdshelby | 1:5147d3fa7816 | 62 | static void nsp_init() |
| zdshelby | 1:5147d3fa7816 | 63 | { |
| zdshelby | 1:5147d3fa7816 | 64 | // Bind the port |
| zdshelby | 1:5147d3fa7816 | 65 | cellular->bind(EP_PORT); |
| zdshelby | 1:5147d3fa7816 | 66 | |
| zdshelby | 1:5147d3fa7816 | 67 | // Open a TCP connection |
| zdshelby | 1:5147d3fa7816 | 68 | while (!cellular->open(NSP_ADDRESS, NSP_PORT, mts::IPStack::TCP)) |
| zdshelby | 1:5147d3fa7816 | 69 | { |
| zdshelby | 1:5147d3fa7816 | 70 | DEBUG("TCP connection failed."); |
| zdshelby | 1:5147d3fa7816 | 71 | wait(3); |
| zdshelby | 1:5147d3fa7816 | 72 | } |
| zdshelby | 1:5147d3fa7816 | 73 | DEBUG("TCP connection to NSP successful."); |
| zdshelby | 1:5147d3fa7816 | 74 | cellular->write("test", 4, -1); |
| zdshelby | 1:5147d3fa7816 | 75 | |
| zdshelby | 1:5147d3fa7816 | 76 | // nsp.set_address(NSP_ADDRESS, NSP_PORT); |
| zdshelby | 1:5147d3fa7816 | 77 | |
| zdshelby | 1:5147d3fa7816 | 78 | // DEBUG("name: %s", endpoint_name); |
| zdshelby | 1:5147d3fa7816 | 79 | // DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
| zdshelby | 1:5147d3fa7816 | 80 | } |
| zdshelby | 1:5147d3fa7816 | 81 | |
| zdshelby | 0:f739ace74102 | 82 | |
| zdshelby | 0:f739ace74102 | 83 | int main() |
| zdshelby | 0:f739ace74102 | 84 | { |
| zdshelby | 1:5147d3fa7816 | 85 | printf("\r\n*****************************************************************************\r\n"); |
| zdshelby | 0:f739ace74102 | 86 | DEBUG("NanoService Example for KL46Z + Multitech Cellular"); |
| zdshelby | 0:f739ace74102 | 87 | |
| zdshelby | 1:5147d3fa7816 | 88 | // Inititalize the Cellular modem |
| zdshelby | 1:5147d3fa7816 | 89 | cellular_init(); |
| zdshelby | 1:5147d3fa7816 | 90 | |
| zdshelby | 1:5147d3fa7816 | 91 | // Bind the socket and configure NSP settings |
| zdshelby | 1:5147d3fa7816 | 92 | nsp_init(); |
| zdshelby | 0:f739ace74102 | 93 | |
| zdshelby | 0:f739ace74102 | 94 | } |
