Zach Shelby / Mbed 2 deprecated NanoService_MTS_Cellular_Simple

Dependencies:   mbed SocketModem nanoservice_client_1_12

Committer:
zdshelby
Date:
Mon Feb 17 22:50:23 2014 +0000
Revision:
3:1e981a0aebfb
Parent:
2:1592223f12e1
Child:
4:5bfd59673a99
- Added TCP socket read loop

Who changed what in which revision?

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