Example programs for MultiTech Dragonfly devices demonstrating how to use the MultiTech MTSCellularInterface library for cellular radio communication and control.

Library Not Included!

The MTSCellularInterface library is not included and must be cloned from GitHub.

mbed-os version

The MTSCellularInterface library does not currently support mbed-os-5.5.x releases. We recommend using mbed-os version 5.4.7.

Example Programs Description

This application contains multiple example programs. Each example demonstrates a different way to configure and use a Dragonfly. A short summary of each example is provided below.

All examples print logging on the USB debug port at 115200 baud.

Most cellular radios require an APN to be set before the radio can be used. Please set the APN in the example you're using before trying to run it. The SMS example also requires a phone number to be set. The number should be in 10 digit format (1xxxyyyxxxx).

TCP Example

This example demonstrates how to use MTSCellularInterface to make TCP socket connections. It brings up the cellular link, does a HTTP GET request on http://developer.mbed.org, and displays the response.

The APN must be set in the TCP example source code before this example is run.

SMS Example

This example demonstrates how to use MTSCellularInterface to send and receive SMS messages. It brings up the cellular link, sends a SMS message to the configured phone number, then loops forever displaying any received SMS messages.

The APN and phone number must be set in the SMS example source code before this example is run.


Choosing An Example Program

Only the active example is compiled. The active example can be updated by changing the ACTIVE_EXAMPLE definition in the examples/example_config.h file.

By default the TCP example will be compiled.

example_config.h

// This is where you choose which example program will be compiled
// to run on the target device.

#ifndef __EXAMPLE__CONFIG_H__
#define __EXAMPLE__CONFIG_H__

// List of available example programs.
#define SMS_EXAMPLE              1  // see sms_example.cpp
#define TCP_EXAMPLE              2  // see tcp_example.cpp

// Example program that will be compiled to run on target.
#if !defined(ACTIVE_EXAMPLE)
#define ACTIVE_EXAMPLE  TCP_EXAMPLE
#endif

#endif


Compile the SMS example instead.

example_config.h

// This is where you choose which example program will be compiled
// to run on the target device.

#ifndef __EXAMPLE__CONFIG_H__
#define __EXAMPLE__CONFIG_H__

// List of available example programs.
#define SMS_EXAMPLE              1  // see sms_example.cpp
#define TCP_EXAMPLE              2  // see tcp_example.cpp

// Example program that will be compiled to run on target.
#if !defined(ACTIVE_EXAMPLE)
#define ACTIVE_EXAMPLE  SMS_EXAMPLE
#endif

#endif



Library

The MTSCellularInterface library can be cloned from MultiTech's GitHub page.

Committer:
leon123
Date:
Tue Jun 13 22:24:22 2017 +0000
Revision:
0:2c9b8730c55d
Child:
1:be566f16398d
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leon123 0:2c9b8730c55d 1 // This example program will make a cellular connection, open a TCP socket then
leon123 0:2c9b8730c55d 2 // send and receive data on the socket. After transmission, it will close
leon123 0:2c9b8730c55d 3 // the socket and disconnect the cellular connection.
leon123 0:2c9b8730c55d 4 //
leon123 0:2c9b8730c55d 5 // ** Configure the apn[] value below for MTQ-H5 or MTQ-LAT3 radios.
leon123 0:2c9b8730c55d 6 // ** INTERVAL sets the number of seconds between repeated cycles of this example.
leon123 0:2c9b8730c55d 7
leon123 0:2c9b8730c55d 8 #include "mbed.h"
leon123 0:2c9b8730c55d 9 #include "MTSCellularInterface.h"
leon123 0:2c9b8730c55d 10 #include "MTSLog.h"
leon123 0:2c9b8730c55d 11 #include "Example_config.h"
leon123 0:2c9b8730c55d 12
leon123 0:2c9b8730c55d 13 #if ACTIVE_EXAMPLE == TCP_EXAMPLE
leon123 0:2c9b8730c55d 14
leon123 0:2c9b8730c55d 15 // Dragonfly debug port.
leon123 0:2c9b8730c55d 16 Serial debug(USBTX, USBRX);
leon123 0:2c9b8730c55d 17
leon123 0:2c9b8730c55d 18 #define INTERVAL 30
leon123 0:2c9b8730c55d 19
leon123 0:2c9b8730c55d 20 int main(){
leon123 0:2c9b8730c55d 21 //Sets the log level to INFO, higher log levels produce more log output.
leon123 0:2c9b8730c55d 22 //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG and TRACE.
leon123 0:2c9b8730c55d 23 //For the Dragonfly, installed on the UDK 2.0 board, messages are output on the
leon123 0:2c9b8730c55d 24 // UDK 2.0 USB port “COMxx: STMicroelectronics STLink Virtual COM port (xx)”
leon123 0:2c9b8730c55d 25 // at 9600bps.
leon123 0:2c9b8730c55d 26 mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL);
leon123 0:2c9b8730c55d 27 //Sets the debug port to 115200bps.
leon123 0:2c9b8730c55d 28 debug.baud(115200);
leon123 0:2c9b8730c55d 29
leon123 0:2c9b8730c55d 30 // Create an MTSCellularInterface object. Serial pins for the Dragonfly board that connect
leon123 0:2c9b8730c55d 31 // to the on board cellular radio:
leon123 0:2c9b8730c55d 32 // RADIO_TX = pin PC_7, RADIO_RX = pin PC_6
leon123 0:2c9b8730c55d 33 MTSCellularInterface *radio = new MTSCellularInterface(RADIO_TX, RADIO_RX);
leon123 0:2c9b8730c55d 34
leon123 0:2c9b8730c55d 35 //Modify to match your apn if you are using the MTQ-H5 or MTQ-LAT3.
leon123 0:2c9b8730c55d 36 const char apn[] = "";
leon123 0:2c9b8730c55d 37
leon123 0:2c9b8730c55d 38 // Basic HTTP request.
leon123 0:2c9b8730c55d 39 std::string request = "GET / HTTP/1.1\r\nHost: developer.mbed.org\r\n\r\n";
leon123 0:2c9b8730c55d 40
leon123 0:2c9b8730c55d 41 int cycle_count = 1;
leon123 0:2c9b8730c55d 42
leon123 0:2c9b8730c55d 43 while (true) {
leon123 0:2c9b8730c55d 44 logInfo("-------- CYCLE #%d --------\r\n", cycle_count++);
leon123 0:2c9b8730c55d 45
leon123 0:2c9b8730c55d 46 while(!radio->is_registered()){
leon123 0:2c9b8730c55d 47 logWarning("radio not registered, try again in 2s");
leon123 0:2c9b8730c55d 48 wait(2);
leon123 0:2c9b8730c55d 49 }
leon123 0:2c9b8730c55d 50
leon123 0:2c9b8730c55d 51 Timer tmr; //mbed Timer has a 30 minute maximum timeout.
leon123 0:2c9b8730c55d 52 tmr.start();
leon123 0:2c9b8730c55d 53 while (radio->connect(apn) != NSAPI_ERROR_OK) {
leon123 0:2c9b8730c55d 54 logWarning("Radio did not connect");
leon123 0:2c9b8730c55d 55 while (tmr.read() < 30); //make sure we wait at least 30s.
leon123 0:2c9b8730c55d 56 tmr.reset();
leon123 0:2c9b8730c55d 57 }
leon123 0:2c9b8730c55d 58 tmr.stop();
leon123 0:2c9b8730c55d 59
leon123 0:2c9b8730c55d 60 // Show the network address
leon123 0:2c9b8730c55d 61 const char *ip = radio->get_ip_address();
leon123 0:2c9b8730c55d 62 logInfo("IP address is: %s\n", ip ? ip : "No IP");
leon123 0:2c9b8730c55d 63
leon123 0:2c9b8730c55d 64 // Open a socket on the network interface, and create a TCP connection to mbed.org
leon123 0:2c9b8730c55d 65 TCPSocket socket;
leon123 0:2c9b8730c55d 66
leon123 0:2c9b8730c55d 67 // Open a socket on the network interface.
leon123 0:2c9b8730c55d 68 if (socket.open(radio) != NSAPI_ERROR_OK) {
leon123 0:2c9b8730c55d 69 logWarning("socket did not open");
leon123 0:2c9b8730c55d 70 socket.close();
leon123 0:2c9b8730c55d 71 continue;
leon123 0:2c9b8730c55d 72 }
leon123 0:2c9b8730c55d 73
leon123 0:2c9b8730c55d 74 // Make a socket connection.
leon123 0:2c9b8730c55d 75 if (socket.connect("developer.mbed.org", 80) != NSAPI_ERROR_OK) {
leon123 0:2c9b8730c55d 76 logWarning("socket did not connect");
leon123 0:2c9b8730c55d 77 socket.close();
leon123 0:2c9b8730c55d 78 continue;
leon123 0:2c9b8730c55d 79 }
leon123 0:2c9b8730c55d 80
leon123 0:2c9b8730c55d 81 // Send tcp data
leon123 0:2c9b8730c55d 82 int scount = socket.send(request.c_str(), request.size());
leon123 0:2c9b8730c55d 83 logInfo("sent %d bytes: %s", scount, request.c_str());
leon123 0:2c9b8730c55d 84
leon123 0:2c9b8730c55d 85 // Recieve and print. Give a couple seonds to receive.
leon123 0:2c9b8730c55d 86 int size = 512;
leon123 0:2c9b8730c55d 87 char rbuffer[size];
leon123 0:2c9b8730c55d 88 memset(rbuffer, 0, size);
leon123 0:2c9b8730c55d 89 bool got_data = false;
leon123 0:2c9b8730c55d 90 Timer rcv_timer;
leon123 0:2c9b8730c55d 91 rcv_timer.start();
leon123 0:2c9b8730c55d 92 do {
leon123 0:2c9b8730c55d 93 int rcount = socket.recv(rbuffer, size-1); //leave room for a null character
leon123 0:2c9b8730c55d 94 if (rcount > 0) {
leon123 0:2c9b8730c55d 95 got_data = true;
leon123 0:2c9b8730c55d 96 while (rcount > 0) {
leon123 0:2c9b8730c55d 97 logInfo("recv %d bytes: %s", rcount, rbuffer);
leon123 0:2c9b8730c55d 98 memset(rbuffer, 0, size);
leon123 0:2c9b8730c55d 99 rcount = socket.recv(rbuffer, size-1);
leon123 0:2c9b8730c55d 100 }
leon123 0:2c9b8730c55d 101 }
leon123 0:2c9b8730c55d 102 } while (rcv_timer < 2 && !got_data);
leon123 0:2c9b8730c55d 103
leon123 0:2c9b8730c55d 104 // Close the socket to return its memory and bring down the network interface
leon123 0:2c9b8730c55d 105 socket.close();
leon123 0:2c9b8730c55d 106
leon123 0:2c9b8730c55d 107 radio->disconnect();
leon123 0:2c9b8730c55d 108
leon123 0:2c9b8730c55d 109 logInfo("waiting %d seconds\r\n", INTERVAL);
leon123 0:2c9b8730c55d 110 wait(INTERVAL);
leon123 0:2c9b8730c55d 111 }
leon123 0:2c9b8730c55d 112
leon123 0:2c9b8730c55d 113 }
leon123 0:2c9b8730c55d 114
leon123 0:2c9b8730c55d 115 #endif