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/sms_example.cpp@3:c458e80d3705, 2018-05-21 (annotated)
- Committer:
- Leon Lindenfelser
- Date:
- Mon May 21 15:59:45 2018 -0500
- Revision:
- 3:c458e80d3705
- Parent:
- 2:31cc1a3ebcf3
add MTSCellularInterface.lib
Who changed what in which revision?
User | Revision | Line number | New 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" |
Mike Fiore |
1:be566f16398d | 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. |
Leon Lindenfelser |
2:31cc1a3ebcf3 | 16 | Serial debug_port(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. |
Leon Lindenfelser |
2:31cc1a3ebcf3 | 26 | debug_port.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 | |
Mike Fiore |
1:be566f16398d | 33 | // Print the MTSCellularInterface version |
Mike Fiore |
1:be566f16398d | 34 | logInfo("MTSCellularInterface Version %s", radio->get_library_version().c_str()); |
Mike Fiore |
1:be566f16398d | 35 | |
leon123 | 0:2c9b8730c55d | 36 | //Modify to match your apn if you are using an HSPA radio with a SIM card. |
leon123 | 0:2c9b8730c55d | 37 | const char apn[] = ""; |
leon123 | 0:2c9b8730c55d | 38 | |
leon123 | 0:2c9b8730c55d | 39 | //Phone number to send to and receive from. Must be in the form "1xxxxxxxxxx" |
leon123 | 0:2c9b8730c55d | 40 | MTSCellularRadio::Sms txtmsg; |
leon123 | 0:2c9b8730c55d | 41 | txtmsg.phone_number = ""; |
leon123 | 0:2c9b8730c55d | 42 | txtmsg.message = "Hello World! MTSCellularInterface is up and running!"; |
leon123 | 0:2c9b8730c55d | 43 | |
leon123 | 0:2c9b8730c55d | 44 | //Set radio apn |
leon123 | 0:2c9b8730c55d | 45 | for (int i = 0; i < 10; i++) { |
leon123 | 0:2c9b8730c55d | 46 | if (i >= 10) { |
leon123 | 0:2c9b8730c55d | 47 | logError("Failed to set apn\n"); |
leon123 | 0:2c9b8730c55d | 48 | } |
leon123 | 0:2c9b8730c55d | 49 | else if (radio->set_credentials(apn) == NSAPI_ERROR_OK) { |
leon123 | 0:2c9b8730c55d | 50 | logInfo("Successfully set apn\n"); |
leon123 | 0:2c9b8730c55d | 51 | break; |
leon123 | 0:2c9b8730c55d | 52 | } else { |
leon123 | 0:2c9b8730c55d | 53 | wait(1); |
leon123 | 0:2c9b8730c55d | 54 | } |
leon123 | 0:2c9b8730c55d | 55 | } |
leon123 | 0:2c9b8730c55d | 56 | |
leon123 | 0:2c9b8730c55d | 57 | //Delete any previously received SMS messages |
leon123 | 0:2c9b8730c55d | 58 | for (int i = 0; i < 10; i++) { |
leon123 | 0:2c9b8730c55d | 59 | if (i >= 10) { |
leon123 | 0:2c9b8730c55d | 60 | logError("Failed to delete SMS messages\n"); |
leon123 | 0:2c9b8730c55d | 61 | } |
leon123 | 0:2c9b8730c55d | 62 | else if (radio->delete_all_received_sms() == NSAPI_ERROR_OK) { |
leon123 | 0:2c9b8730c55d | 63 | logInfo("Deleted all SMS messages\n"); |
leon123 | 0:2c9b8730c55d | 64 | break; |
leon123 | 0:2c9b8730c55d | 65 | } else { |
leon123 | 0:2c9b8730c55d | 66 | wait(1); |
leon123 | 0:2c9b8730c55d | 67 | } |
leon123 | 0:2c9b8730c55d | 68 | } |
leon123 | 0:2c9b8730c55d | 69 | |
leon123 | 0:2c9b8730c55d | 70 | // Send SMS message to phone |
leon123 | 0:2c9b8730c55d | 71 | for (int i = 1; i < 10; i++) { |
leon123 | 0:2c9b8730c55d | 72 | if(radio->send_sms(txtmsg) == NSAPI_ERROR_OK) { |
leon123 | 0:2c9b8730c55d | 73 | logInfo("Sent SMS successfully:<%s>\n", txtmsg.message.c_str()); |
leon123 | 0:2c9b8730c55d | 74 | break; |
leon123 | 0:2c9b8730c55d | 75 | } else { |
leon123 | 0:2c9b8730c55d | 76 | logError("Failed to send SMS<%s>\n", txtmsg.message.c_str()); |
leon123 | 0:2c9b8730c55d | 77 | } |
leon123 | 0:2c9b8730c55d | 78 | } |
leon123 | 0:2c9b8730c55d | 79 | |
leon123 | 0:2c9b8730c55d | 80 | //Checking for received SMS messages |
leon123 | 0:2c9b8730c55d | 81 | while (true) { |
leon123 | 0:2c9b8730c55d | 82 | logInfo("Checking for received messages"); |
leon123 | 0:2c9b8730c55d | 83 | vector<MTSCellularRadio::Sms> recv = radio->get_received_sms(); |
leon123 | 0:2c9b8730c55d | 84 | if(recv.size() > 0) { |
leon123 | 0:2c9b8730c55d | 85 | int size = recv.size(); |
leon123 | 0:2c9b8730c55d | 86 | for (int i = 0; i < size; i++) { |
leon123 | 0:2c9b8730c55d | 87 | 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 | 88 | } |
leon123 | 0:2c9b8730c55d | 89 | } |
leon123 | 0:2c9b8730c55d | 90 | |
leon123 | 0:2c9b8730c55d | 91 | if(radio->delete_only_read_sms() != NSAPI_ERROR_OK) { |
leon123 | 0:2c9b8730c55d | 92 | logError("Failed to delete received and read SMS messages"); |
leon123 | 0:2c9b8730c55d | 93 | } |
leon123 | 0:2c9b8730c55d | 94 | wait(10); |
leon123 | 0:2c9b8730c55d | 95 | } |
leon123 | 0:2c9b8730c55d | 96 | |
leon123 | 0:2c9b8730c55d | 97 | return 0; |
leon123 | 0:2c9b8730c55d | 98 | } |
leon123 | 0:2c9b8730c55d | 99 | |
Mike Fiore |
1:be566f16398d | 100 | #endif |