Send and receive SMS messages using the onboard cellular radio.

Dependencies:   mbed mtsas

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /** Dragonfly Cellular SMS Example
00002  * Configures the cellular radio, sends a SMS message to the configured number, and displays any received messages.
00003  *
00004  * NOTE: This example changes the baud rate of the debug port to 115200 baud!
00005  */
00006 
00007 #include "mbed.h"
00008 #include "mtsas.h"
00009 #include <string>
00010 #include <vector>
00011 #include <iterator>
00012 
00013 // This line controls the regulator's battery charger.
00014 // BC_NCE = 0 enables the battery charger
00015 // BC_NCE = 1 disables the battery charger
00016 DigitalOut bc_nce(PB_2);
00017 
00018 bool init_mtsas();
00019 
00020 // The MTSSerialFlowControl object represents the physical serial link between the processor and the cellular radio.
00021 mts::MTSSerialFlowControl* io;
00022 // The Cellular object represents the cellular radio.
00023 mts::Cellular* radio;
00024 
00025 // An APN is required for GSM radios.
00026 static const char apn[] = "";
00027 
00028 // A valid phone number must be configured in order to successfully send SMS messages.
00029 // The phone number must have the 1 in front of it (11 digits total).
00030 static std::string phone_number = "1xxxxxxxxxx";
00031 
00032 bool radio_ok = false;
00033 
00034 int main() {
00035     // Disable the battery charger unless a battery is attached.
00036     bc_nce = 1;
00037     
00038     // Change the baud rate of the debug port from the default 9600 to 115200.
00039     Serial debug(USBTX, USBRX);
00040     debug.baud(115200);
00041     
00042     //Sets the log level to INFO, higher log levels produce more log output.
00043     //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
00044     mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL);
00045     
00046     logInfo("initializing cellular radio");
00047     radio_ok = init_mtsas();
00048     if (! radio_ok) {
00049         while (true) {
00050             logError("failed to initialize cellular radio");
00051             wait(1);
00052         }
00053     }
00054     
00055     logInfo("setting APN");
00056     if (radio->setApn(apn) != MTS_SUCCESS)
00057         logError("failed to set APN to \"%s\"", apn);
00058         
00059     logInfo("sending SMS to %s", phone_number.c_str());
00060     mts::Cellular::Sms msg;
00061     msg.phoneNumber = phone_number;
00062     msg.message = "Hello from MultiTech Dragonfly!";
00063     if (radio->sendSMS(msg) != MTS_SUCCESS)
00064         logError("sending SMS failed");
00065         
00066     // Display any received SMS messages.
00067     while (true) {
00068         std::vector<mts::Cellular::Sms> msgs = radio->getReceivedSms();
00069         for (std::vector<mts::Cellular::Sms>::iterator it = msgs.begin(); it != msgs.end(); it++) {
00070             logInfo("[%s][%s]\r\n%s\r\n", it->phoneNumber.c_str(), it->timestamp.c_str(), it->message.c_str());
00071         }
00072         
00073         radio->deleteOnlyReceivedReadSms();
00074         
00075         wait(5);
00076     }
00077     
00078     return 0;
00079 }
00080 
00081 bool init_mtsas() {
00082     io = new mts::MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
00083     if (! io)
00084         return false;
00085         
00086     // radio default baud rate is 115200
00087     io->baud(115200);
00088     radio = mts::CellularFactory::create(io);
00089     if (! radio)
00090         return false;
00091         
00092     // Transport must be set properly before any TCPSocketConnection or UDPSocket objects are created
00093     Transport::setTransport(radio);
00094     
00095     return true;
00096 }