Example program demonstrating sending and reading SMS messages from the cellular modem using the MTSAS library.

Dependencies:   mbed-src mtsas

Committer:
Vanger
Date:
Mon Mar 23 18:28:05 2015 +0000
Revision:
7:1ac5be54eb3b
Parent:
5:597d5a5e9d24
Child:
9:58dba06c3042
removed configureSignals() call, as it is unused at this time.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vanger 0:d9fd19c8ca39 1 #include "mbed.h"
Vanger 0:d9fd19c8ca39 2 #include "mtsas.h"
Vanger 0:d9fd19c8ca39 3
Vanger 0:d9fd19c8ca39 4 int main(){
Vanger 3:7fac2f012338 5
Vanger 3:7fac2f012338 6 //Sets the log level to INFO, higher log levels produce more log output.
Vanger 3:7fac2f012338 7 //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
Vanger 1:1f5c9497a125 8 MTSLog::setLogLevel(MTSLog::INFO_LEVEL);
Vanger 1:1f5c9497a125 9
Vanger 0:d9fd19c8ca39 10 //Modify to match your apn if you are using an HSPA radio with a SIM card
Vanger 0:d9fd19c8ca39 11 const char APN[] = "";
Vanger 0:d9fd19c8ca39 12
Vanger 1:1f5c9497a125 13 //Phone number to send to and receive from. Must be in the form "1xxxxxxxxxx"
Vanger 1:1f5c9497a125 14 string PHONE_NUMBER = "";
Vanger 1:1f5c9497a125 15
Vanger 1:1f5c9497a125 16 Cellular::Sms txtmsg;
Vanger 1:1f5c9497a125 17 txtmsg.phoneNumber = PHONE_NUMBER;
Vanger 1:1f5c9497a125 18 txtmsg.message = "Hello World! MTSAS is up and running!";
Vanger 0:d9fd19c8ca39 19
Vanger 0:d9fd19c8ca39 20 /** STMicro Nucelo F401RE
Vanger 0:d9fd19c8ca39 21 * The supported jumper configurations of the MTSAS do not line up with
Vanger 0:d9fd19c8ca39 22 * the pin mapping of the Nucleo F401RE. Therefore, the MTSAS serial TX
Vanger 0:d9fd19c8ca39 23 * pin (JP8 Pin 2) must be manually jumped to Serial1 RX (Shield pin D2)
Vanger 0:d9fd19c8ca39 24 * and the MTSAS serial RX pin (JP9 Pin 2) pin must be manually jumped to
Vanger 0:d9fd19c8ca39 25 * Serial1 TX (Shield pin D8).
Vanger 0:d9fd19c8ca39 26 * Uncomment the following line to use the STMicro Nuceleo F401RE
Vanger 0:d9fd19c8ca39 27 */
Vanger 0:d9fd19c8ca39 28 MTSSerialFlowControl* io = new MTSSerialFlowControl(D8, D2, D3, D6);
Vanger 0:d9fd19c8ca39 29
Vanger 5:597d5a5e9d24 30 /** Dragonfly
Vanger 5:597d5a5e9d24 31 * To configure the serial pins for the Dragonfly board, use:
Vanger 5:597d5a5e9d24 32 * RADIO_TX = pin PC_7, RADIO_RX = pin PC_6
Vanger 5:597d5a5e9d24 33 * RADIO_RTS = pin PB_10,RADIO_CTS = pin PB_12
Vanger 7:1ac5be54eb3b 34 * Uncomment the following line to use the Dragonfly board
Vanger 5:597d5a5e9d24 35 */
Vanger 5:597d5a5e9d24 36 //MTSSerialFlowControl* io = new MTSSerialFlowControl(PC_7, PC_6, PB_10, PB_12);
Vanger 5:597d5a5e9d24 37
Vanger 0:d9fd19c8ca39 38 /** Freescale KL46Z
Vanger 4:ad889da9578c 39 * To configure the serial pins for the Freescale KL46Z board, use MTSAS jumper
Vanger 4:ad889da9578c 40 * configuration B. Uncomment the following line to use the Freescale KL46Z board
Vanger 0:d9fd19c8ca39 41 */
Vanger 0:d9fd19c8ca39 42 //MTSSerialFlowControl* io = new MTSSerialFlowControl(D2, D9, D3, D6);
Vanger 0:d9fd19c8ca39 43
Vanger 3:7fac2f012338 44 /** Freescale K64F
Vanger 4:ad889da9578c 45 * To configure the serial pins for the Freescale K64F board, use MTSAS jumper
Vanger 4:ad889da9578c 46 * configuration A. Uncomment the following line to use the Freescale K64F board
Vanger 0:d9fd19c8ca39 47 */
Vanger 0:d9fd19c8ca39 48 //MTSSerialFlowControl* io = new MTSSerialFlowControl(D1, D0, D3, D6);
Vanger 0:d9fd19c8ca39 49
Vanger 4:ad889da9578c 50 //Sets the baud rate for communicating with the radio
Vanger 0:d9fd19c8ca39 51 io->baud(115200);
Vanger 0:d9fd19c8ca39 52
Vanger 2:d0d6e939ba70 53 //Creates a radio object
Vanger 0:d9fd19c8ca39 54 Cellular* radio = CellularFactory::create(io);
Vanger 2:d0d6e939ba70 55
Vanger 3:7fac2f012338 56 if (! radio) {
Vanger 3:7fac2f012338 57 logFatal("Failed to initialize radio");
Vanger 3:7fac2f012338 58 return 1;
Vanger 3:7fac2f012338 59 }
Vanger 3:7fac2f012338 60
Vanger 4:ad889da9578c 61 Transport::setTransport(radio);
Vanger 4:ad889da9578c 62
Vanger 2:d0d6e939ba70 63 //Set radio APN
Vanger 1:1f5c9497a125 64 for (int i = 0; i < 10; i++) {
Vanger 1:1f5c9497a125 65 if (i >= 10) {
Vanger 1:1f5c9497a125 66 logError("Failed to set APN\n");
Vanger 1:1f5c9497a125 67 }
Vanger 1:1f5c9497a125 68 if (radio->setApn(APN) == MTS_SUCCESS) {
Vanger 1:1f5c9497a125 69 logInfo("Successfully set APN\n");
Vanger 1:1f5c9497a125 70 break;
Vanger 1:1f5c9497a125 71 } else {
Vanger 1:1f5c9497a125 72 wait(1);
Vanger 1:1f5c9497a125 73 }
Vanger 1:1f5c9497a125 74 }
Vanger 0:d9fd19c8ca39 75
Vanger 1:1f5c9497a125 76 //Delete any previously received SMS messages
Vanger 1:1f5c9497a125 77 for (int i = 0; i < 10; i++) {
Vanger 1:1f5c9497a125 78 if (i >= 10) {
Vanger 1:1f5c9497a125 79 logError("Failed to delete SMS messages\n");
Vanger 1:1f5c9497a125 80 }
Vanger 1:1f5c9497a125 81 if (radio->deleteAllReceivedSms() == MTS_SUCCESS) {
Vanger 1:1f5c9497a125 82 logInfo("Deleted all SMS messages\n");
Vanger 1:1f5c9497a125 83 break;
Vanger 1:1f5c9497a125 84 } else {
Vanger 1:1f5c9497a125 85 wait(1);
Vanger 1:1f5c9497a125 86 }
Vanger 1:1f5c9497a125 87 }
Vanger 0:d9fd19c8ca39 88
Vanger 1:1f5c9497a125 89 // Send SMS message to phone
Vanger 1:1f5c9497a125 90 for (int i = 1; i < 10; i++) {
Vanger 1:1f5c9497a125 91 if(radio->sendSMS(txtmsg) == MTS_SUCCESS) {
Vanger 2:d0d6e939ba70 92 logInfo("Sent SMS successfully:<%s>\n", txtmsg.message.c_str());
Vanger 1:1f5c9497a125 93 break;
Vanger 1:1f5c9497a125 94 } else {
Vanger 2:d0d6e939ba70 95 logError("Failed to send SMS<%s>\n", txtmsg.message.c_str());
Vanger 1:1f5c9497a125 96 }
Vanger 1:1f5c9497a125 97 }
Vanger 0:d9fd19c8ca39 98
Vanger 3:7fac2f012338 99 //Checking for received SMS messages
Vanger 3:7fac2f012338 100 while (true) {
Vanger 1:1f5c9497a125 101 logInfo("Checking for received messages");
Vanger 1:1f5c9497a125 102 vector<Cellular::Sms> recv = radio->getReceivedSms();
Vanger 1:1f5c9497a125 103 if(recv.size() > 0) {
Vanger 1:1f5c9497a125 104 int size = recv.size();
Vanger 1:1f5c9497a125 105 for (int i = 0; i < size; i++) {
Vanger 1:1f5c9497a125 106 logInfo("Message %d: [%s] [%s] [%s]", i, recv[i].phoneNumber.c_str(), recv[i].timestamp.c_str(), recv[i].message.c_str());
Vanger 1:1f5c9497a125 107 }
Vanger 1:1f5c9497a125 108 }
Vanger 3:7fac2f012338 109
Vanger 3:7fac2f012338 110 if(radio->deleteOnlyReceivedReadSms() != MTS_SUCCESS) {
Vanger 3:7fac2f012338 111 logError("Failed to delete received and read SMS messages");
Vanger 3:7fac2f012338 112 }
Vanger 1:1f5c9497a125 113 wait(10);
Vanger 1:1f5c9497a125 114 }
Vanger 0:d9fd19c8ca39 115
Vanger 0:d9fd19c8ca39 116 return 0;
Vanger 0:d9fd19c8ca39 117 }