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 first delete any stored messages held in the radio.
leon123 0:2c9b8730c55d 2 // Then it will send an SMS message before looping while reading received SMS
leon123 0:2c9b8730c55d 3 // message.
leon123 0:2c9b8730c55d 4 //
leon123 0:2c9b8730c55d 5 // ** Configure the apn[] value below for MTQ-H5 or MTQ-LAT3 radios.
leon123 0:2c9b8730c55d 6 // ** Configure txtmsg.phone_number below for SMS destination.
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 == SMS_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 int main(){
leon123 0:2c9b8730c55d 19 //Sets the log level to INFO, higher log levels produce more log output.
leon123 0:2c9b8730c55d 20 //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG and TRACE.
leon123 0:2c9b8730c55d 21 //For the Dragonfly, installed on the UDK 2.0 board, messages are output on the
leon123 0:2c9b8730c55d 22 // UDK 2.0 USB port “COMxx: STMicroelectronics STLink Virtual COM port (xx)”
leon123 0:2c9b8730c55d 23 // at 9600bps.
leon123 0:2c9b8730c55d 24 mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL);
leon123 0:2c9b8730c55d 25 //Sets the debug port to 115200bps.
leon123 0:2c9b8730c55d 26 debug.baud(115200);
leon123 0:2c9b8730c55d 27
leon123 0:2c9b8730c55d 28 // Create an MTSCellularInterface object. Serial pins for the Dragonfly board that connect
leon123 0:2c9b8730c55d 29 // to the on board cellular radio:
leon123 0:2c9b8730c55d 30 // RADIO_TX = pin PC_7, RADIO_RX = pin PC_6
leon123 0:2c9b8730c55d 31 MTSCellularInterface *radio = new MTSCellularInterface(RADIO_TX, RADIO_RX);
leon123 0:2c9b8730c55d 32
leon123 0:2c9b8730c55d 33 //Modify to match your apn if you are using an HSPA radio with a SIM card.
leon123 0:2c9b8730c55d 34 const char apn[] = "";
leon123 0:2c9b8730c55d 35
leon123 0:2c9b8730c55d 36 //Phone number to send to and receive from. Must be in the form "1xxxxxxxxxx"
leon123 0:2c9b8730c55d 37 MTSCellularRadio::Sms txtmsg;
leon123 0:2c9b8730c55d 38 txtmsg.phone_number = "";
leon123 0:2c9b8730c55d 39 txtmsg.message = "Hello World! MTSCellularInterface is up and running!";
leon123 0:2c9b8730c55d 40
leon123 0:2c9b8730c55d 41 //Set radio apn
leon123 0:2c9b8730c55d 42 for (int i = 0; i < 10; i++) {
leon123 0:2c9b8730c55d 43 if (i >= 10) {
leon123 0:2c9b8730c55d 44 logError("Failed to set apn\n");
leon123 0:2c9b8730c55d 45 }
leon123 0:2c9b8730c55d 46 else if (radio->set_credentials(apn) == NSAPI_ERROR_OK) {
leon123 0:2c9b8730c55d 47 logInfo("Successfully set apn\n");
leon123 0:2c9b8730c55d 48 break;
leon123 0:2c9b8730c55d 49 } else {
leon123 0:2c9b8730c55d 50 wait(1);
leon123 0:2c9b8730c55d 51 }
leon123 0:2c9b8730c55d 52 }
leon123 0:2c9b8730c55d 53
leon123 0:2c9b8730c55d 54 //Delete any previously received SMS messages
leon123 0:2c9b8730c55d 55 for (int i = 0; i < 10; i++) {
leon123 0:2c9b8730c55d 56 if (i >= 10) {
leon123 0:2c9b8730c55d 57 logError("Failed to delete SMS messages\n");
leon123 0:2c9b8730c55d 58 }
leon123 0:2c9b8730c55d 59 else if (radio->delete_all_received_sms() == NSAPI_ERROR_OK) {
leon123 0:2c9b8730c55d 60 logInfo("Deleted all SMS messages\n");
leon123 0:2c9b8730c55d 61 break;
leon123 0:2c9b8730c55d 62 } else {
leon123 0:2c9b8730c55d 63 wait(1);
leon123 0:2c9b8730c55d 64 }
leon123 0:2c9b8730c55d 65 }
leon123 0:2c9b8730c55d 66
leon123 0:2c9b8730c55d 67 // Send SMS message to phone
leon123 0:2c9b8730c55d 68 for (int i = 1; i < 10; i++) {
leon123 0:2c9b8730c55d 69 if(radio->send_sms(txtmsg) == NSAPI_ERROR_OK) {
leon123 0:2c9b8730c55d 70 logInfo("Sent SMS successfully:<%s>\n", txtmsg.message.c_str());
leon123 0:2c9b8730c55d 71 break;
leon123 0:2c9b8730c55d 72 } else {
leon123 0:2c9b8730c55d 73 logError("Failed to send SMS<%s>\n", txtmsg.message.c_str());
leon123 0:2c9b8730c55d 74 }
leon123 0:2c9b8730c55d 75 }
leon123 0:2c9b8730c55d 76
leon123 0:2c9b8730c55d 77 //Checking for received SMS messages
leon123 0:2c9b8730c55d 78 while (true) {
leon123 0:2c9b8730c55d 79 logInfo("Checking for received messages");
leon123 0:2c9b8730c55d 80 vector<MTSCellularRadio::Sms> recv = radio->get_received_sms();
leon123 0:2c9b8730c55d 81 if(recv.size() > 0) {
leon123 0:2c9b8730c55d 82 int size = recv.size();
leon123 0:2c9b8730c55d 83 for (int i = 0; i < size; i++) {
leon123 0:2c9b8730c55d 84 logInfo("Message %d: [%s] [%s] [%s]", i, recv[i].phone_number.c_str(), recv[i].timestamp.c_str(), recv[i].message.c_str());
leon123 0:2c9b8730c55d 85 }
leon123 0:2c9b8730c55d 86 }
leon123 0:2c9b8730c55d 87
leon123 0:2c9b8730c55d 88 if(radio->delete_only_read_sms() != NSAPI_ERROR_OK) {
leon123 0:2c9b8730c55d 89 logError("Failed to delete received and read SMS messages");
leon123 0:2c9b8730c55d 90 }
leon123 0:2c9b8730c55d 91 wait(10);
leon123 0:2c9b8730c55d 92 }
leon123 0:2c9b8730c55d 93
leon123 0:2c9b8730c55d 94 return 0;
leon123 0:2c9b8730c55d 95 }
leon123 0:2c9b8730c55d 96
leon123 0:2c9b8730c55d 97 #endif