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
- Committer:
- zdshelby
- Date:
- 2014-02-18
- Revision:
- 4:5bfd59673a99
- Parent:
- 3:1e981a0aebfb
- Child:
- 5:6adec9967f93
File content as of revision 4:5bfd59673a99:
#include "mbed.h"
#include "config.h"
#include "debug.h"
// Multitech Cellular includes
#include "Cellular.h"
#include "Endpoint.h"
#include "IPStack.h"
#include "MTSSerialFlowControl.h"
// NanoService includes
#include "sn_nsdl.h"
#include "sn_coap_header.h"
using namespace mts;
Cellular* cellular;
Endpoint nsp;
// ****************************************************************************
// Cellular initialization
static void cellular_init()
{
//Setup serial interface to radio
MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8);
serial->baud(115200);
//Setup Cellular class
cellular = Cellular::getInstance();
cellular->init(serial, PTA4, PTC9); //DCD and DTR pins for KL46Z
//Run status and configuration commands
DEBUG("\n\r////Start Status and Configuration Commands////");
DEBUG("Command Test: %s", getCodeNames(cellular->test()).c_str()); //Make sure you can talk to the radio
DEBUG("Signal Strength: %d", cellular->getSignalStrength()); //Check the signal strength should be above 8
//Makes sure you are reistered with cell
DEBUG("Registration State: %s", Cellular::getRegistrationNames(cellular->getRegistration()).c_str());
//Shows example of how to send other commands, look at AT command guide for more info
DEBUG("Send Basic Command (AT): %s", getCodeNames(cellular->sendBasicCommand("AT", 1000)).c_str());
DEBUG("Send Command (AT+CSQ): %s", cellular->sendCommand("AT+CSQ", 1000).c_str());
//Start Test
DEBUG("\n\r////Start Network Connectivity Test////");
DEBUG("Set APN: %s", getCodeNames(cellular->setApn(CELLULAR_APN)).c_str()); //Use APN from service provider!!!
//Setup a data connection
DEBUG("Attempting to Connect, this may take some time...");
while (!cellular->connect()) {
DEBUG("Failed to connect... Trying again.");
wait(1);
}
DEBUG("Connected to the Network!");
//Try pinging default server "8.8.8.8" (Google's DNS)
DEBUG("Ping Valid: %s", cellular->ping() ? "true" : "false");
wait(3);
}
// ****************************************************************************
// NSP initialization
static void nsp_init()
{
nsp.set_address(NSP_ADDRESS, NSP_PORT);
// DEBUG("name: %s", endpoint_name);
DEBUG("NSP Location: coap://%s:%d\n", NSP_ADDRESS, NSP_PORT);
// Bind the port
cellular->bind(EP_PORT);
// Open a TCP connection
while (!cellular->open(NSP_ADDRESS, NSP_PORT, mts::IPStack::TCP))
{
DEBUG("TCP connection failed.");
wait(3);
}
DEBUG("TCP connection to NSP successful.");
}
extern "C" void *nsdl_alloc(uint16_t size)
{
return malloc(size);
}
extern "C" void nsdl_free(void* ptr_to_free)
{
free(ptr_to_free);
}
void socket_event_loop()
{
//sn_nsdl_addr_s received_packet_address;
//uint8_t received_address[4];
char buffer[2048];
//memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
//received_packet_address.addr_ptr = received_address;
DEBUG("Starting socket read loop...");
while(1)
{
int n = cellular->read(buffer, sizeof(buffer), -1);
if (n < 0)
{
DEBUG("Socket error\n\r");
}
else
{
DEBUG("Received %d bytes\r\n", n);
//sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address);
}
}
}
int main()
{
printf("\r\n*****************************************************************************\r\n");
DEBUG("NanoService Example for KL46Z + Multitech Cellular");
// Inititalize the Cellular modem
cellular_init();
// Bind the socket and configure NSP settings
nsp_init();
// Initalize NanoService library
sn_coap_builder_and_parser_init(&nsdl_alloc, &nsdl_free);
// Start socket listening loop
socket_event_loop();
}
