Send and receive SMS messages using the onboard cellular radio.

Dependencies:   mbed mtsas

Committer:
mfiore
Date:
Wed Sep 30 16:22:21 2015 +0000
Revision:
1:fb975e637cc1
Parent:
0:aa373fce4e20
Child:
2:6d830bd5d1c9
fix typo

Who changed what in which revision?

UserRevisionLine numberNew 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 0:aa373fce4e20 13 bool init_mtsas();
mfiore 0:aa373fce4e20 14
mfiore 0:aa373fce4e20 15 // The MTSSerialFlowControl object represents the physical serial link between the processor and the cellular radio.
mfiore 0:aa373fce4e20 16 mts::MTSSerialFlowControl* io;
mfiore 0:aa373fce4e20 17 // The Cellular object represents the cellular radio.
mfiore 0:aa373fce4e20 18 mts::Cellular* radio;
mfiore 0:aa373fce4e20 19
mfiore 0:aa373fce4e20 20 // An APN is required for GSM radios.
mfiore 0:aa373fce4e20 21 static const char apn[] = "";
mfiore 0:aa373fce4e20 22
mfiore 0:aa373fce4e20 23 // A valid phone number must be configured in order to successfully send SMS messages.
mfiore 0:aa373fce4e20 24 // The phone number must have the 1 in front of it (11 digits total).
mfiore 0:aa373fce4e20 25 static std::string phone_number = "1xxxxxxxxxx";
mfiore 0:aa373fce4e20 26
mfiore 0:aa373fce4e20 27 bool radio_ok = false;
mfiore 0:aa373fce4e20 28
mfiore 0:aa373fce4e20 29 int main() {
mfiore 0:aa373fce4e20 30 // Change the baud rate of the debug port from the default 9600 to 115200.
mfiore 0:aa373fce4e20 31 Serial debug(USBTX, USBRX);
mfiore 0:aa373fce4e20 32 debug.baud(115200);
mfiore 0:aa373fce4e20 33
mfiore 0:aa373fce4e20 34 //Sets the log level to INFO, higher log levels produce more log output.
mfiore 0:aa373fce4e20 35 //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
mfiore 0:aa373fce4e20 36 mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL);
mfiore 0:aa373fce4e20 37
mfiore 0:aa373fce4e20 38 logInfo("initializing cellular radio");
mfiore 0:aa373fce4e20 39 radio_ok = init_mtsas();
mfiore 0:aa373fce4e20 40 if (! radio_ok) {
mfiore 0:aa373fce4e20 41 while (true) {
mfiore 0:aa373fce4e20 42 logError("failed to initialize cellular radio");
mfiore 0:aa373fce4e20 43 wait(1);
mfiore 0:aa373fce4e20 44 }
mfiore 0:aa373fce4e20 45 }
mfiore 0:aa373fce4e20 46
mfiore 0:aa373fce4e20 47 logInfo("setting APN");
mfiore 0:aa373fce4e20 48 if (radio->setApn(apn) != MTS_SUCCESS)
mfiore 0:aa373fce4e20 49 logError("failed to set APN to \"%s\"", apn);
mfiore 0:aa373fce4e20 50
mfiore 0:aa373fce4e20 51 logInfo("sending SMS to %s", phone_number.c_str());
mfiore 0:aa373fce4e20 52 mts::Cellular::Sms msg;
mfiore 0:aa373fce4e20 53 msg.phoneNumber = phone_number;
mfiore 0:aa373fce4e20 54 msg.message = "Hello from MultiTech Dragonfly!";
mfiore 0:aa373fce4e20 55 if (radio->sendSMS(msg) != MTS_SUCCESS)
mfiore 0:aa373fce4e20 56 logError("sending SMS failed");
mfiore 0:aa373fce4e20 57
mfiore 0:aa373fce4e20 58 // Display any received SMS messages.
mfiore 0:aa373fce4e20 59 while (true) {
mfiore 0:aa373fce4e20 60 std::vector<mts::Cellular::Sms> msgs = radio->getReceivedSms();
mfiore 0:aa373fce4e20 61 for (std::vector<mts::Cellular::Sms>::iterator it = msgs.begin(); it != msgs.end(); it++) {
mfiore 0:aa373fce4e20 62 logInfo("[%s][%s]\r\n%s\r\n", it->phoneNumber.c_str(), it->timestamp.c_str(), it->message.c_str());
mfiore 0:aa373fce4e20 63 }
mfiore 0:aa373fce4e20 64
mfiore 0:aa373fce4e20 65 radio->deleteOnlyReceivedReadSms();
mfiore 0:aa373fce4e20 66
mfiore 0:aa373fce4e20 67 wait(5);
mfiore 0:aa373fce4e20 68 }
mfiore 0:aa373fce4e20 69
mfiore 0:aa373fce4e20 70 return 0;
mfiore 0:aa373fce4e20 71 }
mfiore 0:aa373fce4e20 72
mfiore 0:aa373fce4e20 73 bool init_mtsas() {
mfiore 0:aa373fce4e20 74 io = new mts::MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
mfiore 0:aa373fce4e20 75 if (! io)
mfiore 0:aa373fce4e20 76 return false;
mfiore 0:aa373fce4e20 77
mfiore 0:aa373fce4e20 78 // radio default baud rate is 115200
mfiore 0:aa373fce4e20 79 io->baud(115200);
mfiore 0:aa373fce4e20 80 radio = mts::CellularFactory::create(io);
mfiore 0:aa373fce4e20 81 if (! radio)
mfiore 0:aa373fce4e20 82 return false;
mfiore 0:aa373fce4e20 83
mfiore 0:aa373fce4e20 84 // Transport must be set properly before any TCPSocketConnection or UDPSocket objects are created
mfiore 0:aa373fce4e20 85 Transport::setTransport(radio);
mfiore 0:aa373fce4e20 86
mfiore 0:aa373fce4e20 87 return true;
mfiore 0:aa373fce4e20 88 }