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.

examples/src/tcp_example.cpp

Committer:
Leon Lindenfelser
Date:
2018-05-21
Revision:
3:c458e80d3705
Parent:
2:31cc1a3ebcf3

File content as of revision 3:c458e80d3705:

// This example program will make a cellular connection, open a TCP socket then
// send and receive data on the socket. After transmission, it will close
// the socket and disconnect the cellular connection.
//
// ** Configure the apn[] value below for MTQ-H5 or MTQ-LAT3 radios.
// ** INTERVAL sets the number of seconds between repeated cycles of this example.

#include "mbed.h"
#include "MTSCellularInterface.h"
#include "MTSLog.h"
#include "example_config.h"

#if ACTIVE_EXAMPLE == TCP_EXAMPLE

// Dragonfly debug port. 
Serial debug_port(USBTX, USBRX);

#define INTERVAL 30

int main(){
    //Sets the log level to INFO, higher log levels produce more log output.
    //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG and TRACE.
    //For the Dragonfly, installed on the UDK 2.0 board, messages are output on the
    // UDK 2.0 USB port “COMxx: STMicroelectronics STLink Virtual COM port (xx)”
    // at 9600bps.    
    mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL);
    //Sets the debug port to 115200bps.
    debug_port.baud(115200);

    // Create an MTSCellularInterface object. Serial pins for the Dragonfly board that connect
    // to the on board cellular radio:
    // RADIO_TX = pin PC_7, RADIO_RX = pin PC_6
    MTSCellularInterface *radio = new MTSCellularInterface(RADIO_TX, RADIO_RX);

    // Print the MTSCellularInterface version
    logInfo("MTSCellularInterface Version %s", radio->get_library_version().c_str());

    //Modify to match your apn if you are using the MTQ-H5 or MTQ-LAT3.
    const char apn[] = "";

    // Basic HTTP request.
    std::string request = "GET / HTTP/1.1\r\nHost: developer.mbed.org\r\n\r\n";

    int cycle_count = 1;
    
    while (true) {       
        logInfo("-------- CYCLE #%d --------\r\n", cycle_count++);
        
        while(!radio->is_registered()){
            logWarning("radio not registered, try again in 2s");
            wait(2);
        }

        Timer tmr;  //mbed Timer has a 30 minute maximum timeout.
        tmr.start();
        while (radio->connect(apn) != NSAPI_ERROR_OK) {
            logWarning("Radio did not connect");
            while (tmr.read() < 30);    //make sure we wait at least 30s.
            tmr.reset();
        }
        tmr.stop();

        // Show the network address
        const char *ip = radio->get_ip_address();
        logInfo("IP address is: %s\n", ip ? ip : "No IP");

        // Open a socket on the network interface, and create a TCP connection to mbed.org
        TCPSocket socket;

        // Open a socket on the network interface.
        if (socket.open(radio) != NSAPI_ERROR_OK) {
            logWarning("socket did not open");
            socket.close();
            continue;
        }

        // Make a socket connection.
        if (socket.connect("developer.mbed.org", 80) != NSAPI_ERROR_OK) {
            logWarning("socket did not connect");
            socket.close();
            continue;
        }

        // Send tcp data
        int scount = socket.send(request.c_str(), request.size());
        logInfo("sent %d bytes: %s", scount, request.c_str());

        // Recieve and print. Give a couple seonds to receive.
        int size = 512;
        char rbuffer[size];
        memset(rbuffer, 0, size);
        bool got_data = false;
        Timer rcv_timer;
        rcv_timer.start();
        do {
            int rcount = socket.recv(rbuffer, size-1);  //leave room for a null character
            if (rcount > 0) {
                got_data = true;
                while (rcount > 0) {
                    logInfo("recv %d bytes: %s", rcount, rbuffer);
                    memset(rbuffer, 0, size);
                    rcount = socket.recv(rbuffer, size-1);
                }
            }
        } while (rcv_timer < 2 && !got_data);
        
        // Close the socket to return its memory and bring down the network interface
        socket.close();

        radio->disconnect();

        logInfo("waiting %d seconds\r\n", INTERVAL);
        wait(INTERVAL);
    }
        
}

#endif