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@4:5bfd59673a99, 2014-02-18 (annotated)
- Committer:
- zdshelby
- Date:
- Tue Feb 18 01:02:16 2014 +0000
- Revision:
- 4:5bfd59673a99
- Parent:
- 3:1e981a0aebfb
- Child:
- 5:6adec9967f93
- Added manual nanoservice library import
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 | 3:1e981a0aebfb | 11 | // NanoService includes |
| zdshelby | 4:5bfd59673a99 | 12 | #include "sn_nsdl.h" |
| zdshelby | 4:5bfd59673a99 | 13 | #include "sn_coap_header.h" |
| zdshelby | 3:1e981a0aebfb | 14 | |
| zdshelby | 1:5147d3fa7816 | 15 | using namespace mts; |
| zdshelby | 1:5147d3fa7816 | 16 | |
| zdshelby | 1:5147d3fa7816 | 17 | Cellular* cellular; |
| zdshelby | 2:1592223f12e1 | 18 | Endpoint nsp; |
| zdshelby | 1:5147d3fa7816 | 19 | |
| zdshelby | 1:5147d3fa7816 | 20 | // **************************************************************************** |
| zdshelby | 1:5147d3fa7816 | 21 | // Cellular initialization |
| zdshelby | 1:5147d3fa7816 | 22 | |
| zdshelby | 1:5147d3fa7816 | 23 | static void cellular_init() |
| zdshelby | 1:5147d3fa7816 | 24 | { |
| zdshelby | 1:5147d3fa7816 | 25 | //Setup serial interface to radio |
| zdshelby | 1:5147d3fa7816 | 26 | MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); |
| zdshelby | 1:5147d3fa7816 | 27 | serial->baud(115200); |
| zdshelby | 1:5147d3fa7816 | 28 | |
| zdshelby | 1:5147d3fa7816 | 29 | //Setup Cellular class |
| zdshelby | 1:5147d3fa7816 | 30 | cellular = Cellular::getInstance(); |
| zdshelby | 1:5147d3fa7816 | 31 | cellular->init(serial, PTA4, PTC9); //DCD and DTR pins for KL46Z |
| zdshelby | 1:5147d3fa7816 | 32 | |
| zdshelby | 1:5147d3fa7816 | 33 | //Run status and configuration commands |
| zdshelby | 1:5147d3fa7816 | 34 | DEBUG("\n\r////Start Status and Configuration Commands////"); |
| zdshelby | 1:5147d3fa7816 | 35 | DEBUG("Command Test: %s", getCodeNames(cellular->test()).c_str()); //Make sure you can talk to the radio |
| zdshelby | 1:5147d3fa7816 | 36 | DEBUG("Signal Strength: %d", cellular->getSignalStrength()); //Check the signal strength should be above 8 |
| zdshelby | 1:5147d3fa7816 | 37 | |
| zdshelby | 1:5147d3fa7816 | 38 | //Makes sure you are reistered with cell |
| zdshelby | 1:5147d3fa7816 | 39 | DEBUG("Registration State: %s", Cellular::getRegistrationNames(cellular->getRegistration()).c_str()); |
| zdshelby | 1:5147d3fa7816 | 40 | |
| zdshelby | 1:5147d3fa7816 | 41 | //Shows example of how to send other commands, look at AT command guide for more info |
| zdshelby | 1:5147d3fa7816 | 42 | DEBUG("Send Basic Command (AT): %s", getCodeNames(cellular->sendBasicCommand("AT", 1000)).c_str()); |
| zdshelby | 1:5147d3fa7816 | 43 | DEBUG("Send Command (AT+CSQ): %s", cellular->sendCommand("AT+CSQ", 1000).c_str()); |
| zdshelby | 1:5147d3fa7816 | 44 | |
| zdshelby | 1:5147d3fa7816 | 45 | //Start Test |
| zdshelby | 1:5147d3fa7816 | 46 | DEBUG("\n\r////Start Network Connectivity Test////"); |
| zdshelby | 1:5147d3fa7816 | 47 | DEBUG("Set APN: %s", getCodeNames(cellular->setApn(CELLULAR_APN)).c_str()); //Use APN from service provider!!! |
| zdshelby | 1:5147d3fa7816 | 48 | |
| zdshelby | 1:5147d3fa7816 | 49 | //Setup a data connection |
| zdshelby | 1:5147d3fa7816 | 50 | DEBUG("Attempting to Connect, this may take some time..."); |
| zdshelby | 1:5147d3fa7816 | 51 | while (!cellular->connect()) { |
| zdshelby | 1:5147d3fa7816 | 52 | DEBUG("Failed to connect... Trying again."); |
| zdshelby | 1:5147d3fa7816 | 53 | wait(1); |
| zdshelby | 1:5147d3fa7816 | 54 | } |
| zdshelby | 1:5147d3fa7816 | 55 | DEBUG("Connected to the Network!"); |
| zdshelby | 1:5147d3fa7816 | 56 | |
| zdshelby | 1:5147d3fa7816 | 57 | //Try pinging default server "8.8.8.8" (Google's DNS) |
| zdshelby | 1:5147d3fa7816 | 58 | DEBUG("Ping Valid: %s", cellular->ping() ? "true" : "false"); |
| zdshelby | 1:5147d3fa7816 | 59 | wait(3); |
| zdshelby | 1:5147d3fa7816 | 60 | |
| zdshelby | 1:5147d3fa7816 | 61 | } |
| zdshelby | 1:5147d3fa7816 | 62 | |
| zdshelby | 1:5147d3fa7816 | 63 | // **************************************************************************** |
| zdshelby | 1:5147d3fa7816 | 64 | // NSP initialization |
| zdshelby | 1:5147d3fa7816 | 65 | |
| zdshelby | 1:5147d3fa7816 | 66 | static void nsp_init() |
| zdshelby | 1:5147d3fa7816 | 67 | { |
| zdshelby | 2:1592223f12e1 | 68 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
| zdshelby | 2:1592223f12e1 | 69 | |
| zdshelby | 2:1592223f12e1 | 70 | // DEBUG("name: %s", endpoint_name); |
| zdshelby | 2:1592223f12e1 | 71 | DEBUG("NSP Location: coap://%s:%d\n", NSP_ADDRESS, NSP_PORT); |
| zdshelby | 2:1592223f12e1 | 72 | |
| zdshelby | 1:5147d3fa7816 | 73 | // Bind the port |
| zdshelby | 1:5147d3fa7816 | 74 | cellular->bind(EP_PORT); |
| zdshelby | 1:5147d3fa7816 | 75 | |
| zdshelby | 1:5147d3fa7816 | 76 | // Open a TCP connection |
| zdshelby | 1:5147d3fa7816 | 77 | while (!cellular->open(NSP_ADDRESS, NSP_PORT, mts::IPStack::TCP)) |
| zdshelby | 1:5147d3fa7816 | 78 | { |
| zdshelby | 1:5147d3fa7816 | 79 | DEBUG("TCP connection failed."); |
| zdshelby | 1:5147d3fa7816 | 80 | wait(3); |
| zdshelby | 1:5147d3fa7816 | 81 | } |
| zdshelby | 1:5147d3fa7816 | 82 | DEBUG("TCP connection to NSP successful."); |
| zdshelby | 1:5147d3fa7816 | 83 | } |
| zdshelby | 1:5147d3fa7816 | 84 | |
| zdshelby | 4:5bfd59673a99 | 85 | extern "C" void *nsdl_alloc(uint16_t size) |
| zdshelby | 4:5bfd59673a99 | 86 | { |
| zdshelby | 4:5bfd59673a99 | 87 | return malloc(size); |
| zdshelby | 4:5bfd59673a99 | 88 | } |
| zdshelby | 4:5bfd59673a99 | 89 | |
| zdshelby | 4:5bfd59673a99 | 90 | extern "C" void nsdl_free(void* ptr_to_free) |
| zdshelby | 4:5bfd59673a99 | 91 | { |
| zdshelby | 4:5bfd59673a99 | 92 | free(ptr_to_free); |
| zdshelby | 4:5bfd59673a99 | 93 | } |
| zdshelby | 4:5bfd59673a99 | 94 | |
| zdshelby | 3:1e981a0aebfb | 95 | void socket_event_loop() |
| zdshelby | 3:1e981a0aebfb | 96 | { |
| zdshelby | 3:1e981a0aebfb | 97 | //sn_nsdl_addr_s received_packet_address; |
| zdshelby | 3:1e981a0aebfb | 98 | //uint8_t received_address[4]; |
| zdshelby | 3:1e981a0aebfb | 99 | char buffer[2048]; |
| zdshelby | 3:1e981a0aebfb | 100 | |
| zdshelby | 3:1e981a0aebfb | 101 | //memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); |
| zdshelby | 3:1e981a0aebfb | 102 | //received_packet_address.addr_ptr = received_address; |
| zdshelby | 3:1e981a0aebfb | 103 | |
| zdshelby | 4:5bfd59673a99 | 104 | DEBUG("Starting socket read loop..."); |
| zdshelby | 3:1e981a0aebfb | 105 | while(1) |
| zdshelby | 3:1e981a0aebfb | 106 | { |
| zdshelby | 3:1e981a0aebfb | 107 | int n = cellular->read(buffer, sizeof(buffer), -1); |
| zdshelby | 3:1e981a0aebfb | 108 | if (n < 0) |
| zdshelby | 3:1e981a0aebfb | 109 | { |
| zdshelby | 3:1e981a0aebfb | 110 | DEBUG("Socket error\n\r"); |
| zdshelby | 3:1e981a0aebfb | 111 | } |
| zdshelby | 3:1e981a0aebfb | 112 | else |
| zdshelby | 3:1e981a0aebfb | 113 | { |
| zdshelby | 3:1e981a0aebfb | 114 | DEBUG("Received %d bytes\r\n", n); |
| zdshelby | 3:1e981a0aebfb | 115 | //sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address); |
| zdshelby | 3:1e981a0aebfb | 116 | } |
| zdshelby | 3:1e981a0aebfb | 117 | } |
| zdshelby | 3:1e981a0aebfb | 118 | } |
| zdshelby | 0:f739ace74102 | 119 | |
| zdshelby | 0:f739ace74102 | 120 | int main() |
| zdshelby | 0:f739ace74102 | 121 | { |
| zdshelby | 1:5147d3fa7816 | 122 | printf("\r\n*****************************************************************************\r\n"); |
| zdshelby | 0:f739ace74102 | 123 | DEBUG("NanoService Example for KL46Z + Multitech Cellular"); |
| zdshelby | 0:f739ace74102 | 124 | |
| zdshelby | 1:5147d3fa7816 | 125 | // Inititalize the Cellular modem |
| zdshelby | 1:5147d3fa7816 | 126 | cellular_init(); |
| zdshelby | 1:5147d3fa7816 | 127 | |
| zdshelby | 1:5147d3fa7816 | 128 | // Bind the socket and configure NSP settings |
| zdshelby | 1:5147d3fa7816 | 129 | nsp_init(); |
| zdshelby | 0:f739ace74102 | 130 | |
| zdshelby | 4:5bfd59673a99 | 131 | // Initalize NanoService library |
| zdshelby | 4:5bfd59673a99 | 132 | sn_coap_builder_and_parser_init(&nsdl_alloc, &nsdl_free); |
| zdshelby | 4:5bfd59673a99 | 133 | |
| zdshelby | 3:1e981a0aebfb | 134 | // Start socket listening loop |
| zdshelby | 3:1e981a0aebfb | 135 | socket_event_loop(); |
| zdshelby | 3:1e981a0aebfb | 136 | |
| zdshelby | 3:1e981a0aebfb | 137 | |
| zdshelby | 0:f739ace74102 | 138 | } |
