MultiTech
/
Dragonfly_Cellular_SMS_Example
Send and receive SMS messages using the onboard cellular radio.
main.cpp@2:6d830bd5d1c9, 2016-02-26 (annotated)
- Committer:
- mfiore
- Date:
- Fri Feb 26 16:49:24 2016 +0000
- Revision:
- 2:6d830bd5d1c9
- Parent:
- 1:fb975e637cc1
Updated mbed library to revision 112, disable regulator's battery charger
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 1:fb975e637cc1 | 1 | /** Dragonfly Cellular SMS Example |
mfiore | 0:aa373fce4e20 | 2 | * Configures the cellular radio, sends a SMS message to the configured number, and displays any received messages. |
mfiore | 0:aa373fce4e20 | 3 | * |
mfiore | 0:aa373fce4e20 | 4 | * NOTE: This example changes the baud rate of the debug port to 115200 baud! |
mfiore | 0:aa373fce4e20 | 5 | */ |
mfiore | 0:aa373fce4e20 | 6 | |
mfiore | 0:aa373fce4e20 | 7 | #include "mbed.h" |
mfiore | 0:aa373fce4e20 | 8 | #include "mtsas.h" |
mfiore | 0:aa373fce4e20 | 9 | #include <string> |
mfiore | 0:aa373fce4e20 | 10 | #include <vector> |
mfiore | 0:aa373fce4e20 | 11 | #include <iterator> |
mfiore | 0:aa373fce4e20 | 12 | |
mfiore | 2:6d830bd5d1c9 | 13 | // This line controls the regulator's battery charger. |
mfiore | 2:6d830bd5d1c9 | 14 | // BC_NCE = 0 enables the battery charger |
mfiore | 2:6d830bd5d1c9 | 15 | // BC_NCE = 1 disables the battery charger |
mfiore | 2:6d830bd5d1c9 | 16 | DigitalOut bc_nce(PB_2); |
mfiore | 2:6d830bd5d1c9 | 17 | |
mfiore | 0:aa373fce4e20 | 18 | bool init_mtsas(); |
mfiore | 0:aa373fce4e20 | 19 | |
mfiore | 0:aa373fce4e20 | 20 | // The MTSSerialFlowControl object represents the physical serial link between the processor and the cellular radio. |
mfiore | 0:aa373fce4e20 | 21 | mts::MTSSerialFlowControl* io; |
mfiore | 0:aa373fce4e20 | 22 | // The Cellular object represents the cellular radio. |
mfiore | 0:aa373fce4e20 | 23 | mts::Cellular* radio; |
mfiore | 0:aa373fce4e20 | 24 | |
mfiore | 0:aa373fce4e20 | 25 | // An APN is required for GSM radios. |
mfiore | 0:aa373fce4e20 | 26 | static const char apn[] = ""; |
mfiore | 0:aa373fce4e20 | 27 | |
mfiore | 0:aa373fce4e20 | 28 | // A valid phone number must be configured in order to successfully send SMS messages. |
mfiore | 0:aa373fce4e20 | 29 | // The phone number must have the 1 in front of it (11 digits total). |
mfiore | 0:aa373fce4e20 | 30 | static std::string phone_number = "1xxxxxxxxxx"; |
mfiore | 0:aa373fce4e20 | 31 | |
mfiore | 0:aa373fce4e20 | 32 | bool radio_ok = false; |
mfiore | 0:aa373fce4e20 | 33 | |
mfiore | 0:aa373fce4e20 | 34 | int main() { |
mfiore | 2:6d830bd5d1c9 | 35 | // Disable the battery charger unless a battery is attached. |
mfiore | 2:6d830bd5d1c9 | 36 | bc_nce = 1; |
mfiore | 2:6d830bd5d1c9 | 37 | |
mfiore | 0:aa373fce4e20 | 38 | // Change the baud rate of the debug port from the default 9600 to 115200. |
mfiore | 0:aa373fce4e20 | 39 | Serial debug(USBTX, USBRX); |
mfiore | 0:aa373fce4e20 | 40 | debug.baud(115200); |
mfiore | 0:aa373fce4e20 | 41 | |
mfiore | 0:aa373fce4e20 | 42 | //Sets the log level to INFO, higher log levels produce more log output. |
mfiore | 0:aa373fce4e20 | 43 | //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE |
mfiore | 0:aa373fce4e20 | 44 | mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL); |
mfiore | 0:aa373fce4e20 | 45 | |
mfiore | 0:aa373fce4e20 | 46 | logInfo("initializing cellular radio"); |
mfiore | 0:aa373fce4e20 | 47 | radio_ok = init_mtsas(); |
mfiore | 0:aa373fce4e20 | 48 | if (! radio_ok) { |
mfiore | 0:aa373fce4e20 | 49 | while (true) { |
mfiore | 0:aa373fce4e20 | 50 | logError("failed to initialize cellular radio"); |
mfiore | 0:aa373fce4e20 | 51 | wait(1); |
mfiore | 0:aa373fce4e20 | 52 | } |
mfiore | 0:aa373fce4e20 | 53 | } |
mfiore | 0:aa373fce4e20 | 54 | |
mfiore | 0:aa373fce4e20 | 55 | logInfo("setting APN"); |
mfiore | 0:aa373fce4e20 | 56 | if (radio->setApn(apn) != MTS_SUCCESS) |
mfiore | 0:aa373fce4e20 | 57 | logError("failed to set APN to \"%s\"", apn); |
mfiore | 0:aa373fce4e20 | 58 | |
mfiore | 0:aa373fce4e20 | 59 | logInfo("sending SMS to %s", phone_number.c_str()); |
mfiore | 0:aa373fce4e20 | 60 | mts::Cellular::Sms msg; |
mfiore | 0:aa373fce4e20 | 61 | msg.phoneNumber = phone_number; |
mfiore | 0:aa373fce4e20 | 62 | msg.message = "Hello from MultiTech Dragonfly!"; |
mfiore | 0:aa373fce4e20 | 63 | if (radio->sendSMS(msg) != MTS_SUCCESS) |
mfiore | 0:aa373fce4e20 | 64 | logError("sending SMS failed"); |
mfiore | 0:aa373fce4e20 | 65 | |
mfiore | 0:aa373fce4e20 | 66 | // Display any received SMS messages. |
mfiore | 0:aa373fce4e20 | 67 | while (true) { |
mfiore | 0:aa373fce4e20 | 68 | std::vector<mts::Cellular::Sms> msgs = radio->getReceivedSms(); |
mfiore | 0:aa373fce4e20 | 69 | for (std::vector<mts::Cellular::Sms>::iterator it = msgs.begin(); it != msgs.end(); it++) { |
mfiore | 0:aa373fce4e20 | 70 | logInfo("[%s][%s]\r\n%s\r\n", it->phoneNumber.c_str(), it->timestamp.c_str(), it->message.c_str()); |
mfiore | 0:aa373fce4e20 | 71 | } |
mfiore | 0:aa373fce4e20 | 72 | |
mfiore | 0:aa373fce4e20 | 73 | radio->deleteOnlyReceivedReadSms(); |
mfiore | 0:aa373fce4e20 | 74 | |
mfiore | 0:aa373fce4e20 | 75 | wait(5); |
mfiore | 0:aa373fce4e20 | 76 | } |
mfiore | 0:aa373fce4e20 | 77 | |
mfiore | 0:aa373fce4e20 | 78 | return 0; |
mfiore | 0:aa373fce4e20 | 79 | } |
mfiore | 0:aa373fce4e20 | 80 | |
mfiore | 0:aa373fce4e20 | 81 | bool init_mtsas() { |
mfiore | 0:aa373fce4e20 | 82 | io = new mts::MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS); |
mfiore | 0:aa373fce4e20 | 83 | if (! io) |
mfiore | 0:aa373fce4e20 | 84 | return false; |
mfiore | 0:aa373fce4e20 | 85 | |
mfiore | 0:aa373fce4e20 | 86 | // radio default baud rate is 115200 |
mfiore | 0:aa373fce4e20 | 87 | io->baud(115200); |
mfiore | 0:aa373fce4e20 | 88 | radio = mts::CellularFactory::create(io); |
mfiore | 0:aa373fce4e20 | 89 | if (! radio) |
mfiore | 0:aa373fce4e20 | 90 | return false; |
mfiore | 0:aa373fce4e20 | 91 | |
mfiore | 0:aa373fce4e20 | 92 | // Transport must be set properly before any TCPSocketConnection or UDPSocket objects are created |
mfiore | 0:aa373fce4e20 | 93 | Transport::setTransport(radio); |
mfiore | 0:aa373fce4e20 | 94 | |
mfiore | 0:aa373fce4e20 | 95 | return true; |
mfiore | 0:aa373fce4e20 | 96 | } |