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@2:31cc1a3ebcf3, 2017-07-06 (annotated)
- Committer:
- Leon Lindenfelser
- Date:
- Thu Jul 06 16:24:06 2017 -0500
- Revision:
- 2:31cc1a3ebcf3
- Parent:
- 1:be566f16398d
rename debug to debug_port to avoid conflict with mbed-os vs 5.5.x
Who changed what in which revision?
User | Revision | Line number | New 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" |
Mike Fiore |
1:be566f16398d | 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. |
Leon Lindenfelser |
2:31cc1a3ebcf3 | 16 | Serial debug_port(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. |
Leon Lindenfelser |
2:31cc1a3ebcf3 | 28 | debug_port.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 | |
Mike Fiore |
1:be566f16398d | 35 | // Print the MTSCellularInterface version |
Mike Fiore |
1:be566f16398d | 36 | logInfo("MTSCellularInterface Version %s", radio->get_library_version().c_str()); |
Mike Fiore |
1:be566f16398d | 37 | |
leon123 | 0:2c9b8730c55d | 38 | //Modify to match your apn if you are using the MTQ-H5 or MTQ-LAT3. |
leon123 | 0:2c9b8730c55d | 39 | const char apn[] = ""; |
leon123 | 0:2c9b8730c55d | 40 | |
leon123 | 0:2c9b8730c55d | 41 | // Basic HTTP request. |
leon123 | 0:2c9b8730c55d | 42 | std::string request = "GET / HTTP/1.1\r\nHost: developer.mbed.org\r\n\r\n"; |
leon123 | 0:2c9b8730c55d | 43 | |
leon123 | 0:2c9b8730c55d | 44 | int cycle_count = 1; |
leon123 | 0:2c9b8730c55d | 45 | |
leon123 | 0:2c9b8730c55d | 46 | while (true) { |
leon123 | 0:2c9b8730c55d | 47 | logInfo("-------- CYCLE #%d --------\r\n", cycle_count++); |
leon123 | 0:2c9b8730c55d | 48 | |
leon123 | 0:2c9b8730c55d | 49 | while(!radio->is_registered()){ |
leon123 | 0:2c9b8730c55d | 50 | logWarning("radio not registered, try again in 2s"); |
leon123 | 0:2c9b8730c55d | 51 | wait(2); |
leon123 | 0:2c9b8730c55d | 52 | } |
leon123 | 0:2c9b8730c55d | 53 | |
leon123 | 0:2c9b8730c55d | 54 | Timer tmr; //mbed Timer has a 30 minute maximum timeout. |
leon123 | 0:2c9b8730c55d | 55 | tmr.start(); |
leon123 | 0:2c9b8730c55d | 56 | while (radio->connect(apn) != NSAPI_ERROR_OK) { |
leon123 | 0:2c9b8730c55d | 57 | logWarning("Radio did not connect"); |
leon123 | 0:2c9b8730c55d | 58 | while (tmr.read() < 30); //make sure we wait at least 30s. |
leon123 | 0:2c9b8730c55d | 59 | tmr.reset(); |
leon123 | 0:2c9b8730c55d | 60 | } |
leon123 | 0:2c9b8730c55d | 61 | tmr.stop(); |
leon123 | 0:2c9b8730c55d | 62 | |
leon123 | 0:2c9b8730c55d | 63 | // Show the network address |
leon123 | 0:2c9b8730c55d | 64 | const char *ip = radio->get_ip_address(); |
leon123 | 0:2c9b8730c55d | 65 | logInfo("IP address is: %s\n", ip ? ip : "No IP"); |
leon123 | 0:2c9b8730c55d | 66 | |
leon123 | 0:2c9b8730c55d | 67 | // Open a socket on the network interface, and create a TCP connection to mbed.org |
leon123 | 0:2c9b8730c55d | 68 | TCPSocket socket; |
leon123 | 0:2c9b8730c55d | 69 | |
leon123 | 0:2c9b8730c55d | 70 | // Open a socket on the network interface. |
leon123 | 0:2c9b8730c55d | 71 | if (socket.open(radio) != NSAPI_ERROR_OK) { |
leon123 | 0:2c9b8730c55d | 72 | logWarning("socket did not open"); |
leon123 | 0:2c9b8730c55d | 73 | socket.close(); |
leon123 | 0:2c9b8730c55d | 74 | continue; |
leon123 | 0:2c9b8730c55d | 75 | } |
leon123 | 0:2c9b8730c55d | 76 | |
leon123 | 0:2c9b8730c55d | 77 | // Make a socket connection. |
leon123 | 0:2c9b8730c55d | 78 | if (socket.connect("developer.mbed.org", 80) != NSAPI_ERROR_OK) { |
leon123 | 0:2c9b8730c55d | 79 | logWarning("socket did not connect"); |
leon123 | 0:2c9b8730c55d | 80 | socket.close(); |
leon123 | 0:2c9b8730c55d | 81 | continue; |
leon123 | 0:2c9b8730c55d | 82 | } |
leon123 | 0:2c9b8730c55d | 83 | |
leon123 | 0:2c9b8730c55d | 84 | // Send tcp data |
leon123 | 0:2c9b8730c55d | 85 | int scount = socket.send(request.c_str(), request.size()); |
leon123 | 0:2c9b8730c55d | 86 | logInfo("sent %d bytes: %s", scount, request.c_str()); |
leon123 | 0:2c9b8730c55d | 87 | |
leon123 | 0:2c9b8730c55d | 88 | // Recieve and print. Give a couple seonds to receive. |
leon123 | 0:2c9b8730c55d | 89 | int size = 512; |
leon123 | 0:2c9b8730c55d | 90 | char rbuffer[size]; |
leon123 | 0:2c9b8730c55d | 91 | memset(rbuffer, 0, size); |
leon123 | 0:2c9b8730c55d | 92 | bool got_data = false; |
leon123 | 0:2c9b8730c55d | 93 | Timer rcv_timer; |
leon123 | 0:2c9b8730c55d | 94 | rcv_timer.start(); |
leon123 | 0:2c9b8730c55d | 95 | do { |
leon123 | 0:2c9b8730c55d | 96 | int rcount = socket.recv(rbuffer, size-1); //leave room for a null character |
leon123 | 0:2c9b8730c55d | 97 | if (rcount > 0) { |
leon123 | 0:2c9b8730c55d | 98 | got_data = true; |
leon123 | 0:2c9b8730c55d | 99 | while (rcount > 0) { |
leon123 | 0:2c9b8730c55d | 100 | logInfo("recv %d bytes: %s", rcount, rbuffer); |
leon123 | 0:2c9b8730c55d | 101 | memset(rbuffer, 0, size); |
leon123 | 0:2c9b8730c55d | 102 | rcount = socket.recv(rbuffer, size-1); |
leon123 | 0:2c9b8730c55d | 103 | } |
leon123 | 0:2c9b8730c55d | 104 | } |
leon123 | 0:2c9b8730c55d | 105 | } while (rcv_timer < 2 && !got_data); |
leon123 | 0:2c9b8730c55d | 106 | |
leon123 | 0:2c9b8730c55d | 107 | // Close the socket to return its memory and bring down the network interface |
leon123 | 0:2c9b8730c55d | 108 | socket.close(); |
leon123 | 0:2c9b8730c55d | 109 | |
leon123 | 0:2c9b8730c55d | 110 | radio->disconnect(); |
leon123 | 0:2c9b8730c55d | 111 | |
leon123 | 0:2c9b8730c55d | 112 | logInfo("waiting %d seconds\r\n", INTERVAL); |
leon123 | 0:2c9b8730c55d | 113 | wait(INTERVAL); |
leon123 | 0:2c9b8730c55d | 114 | } |
leon123 | 0:2c9b8730c55d | 115 | |
leon123 | 0:2c9b8730c55d | 116 | } |
leon123 | 0:2c9b8730c55d | 117 | |
leon123 | 0:2c9b8730c55d | 118 | #endif |