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

Dependencies:   mbed-src mtsas

Committer:
Vanger
Date:
Tue Aug 05 14:27:36 2014 +0000
Revision:
2:d0d6e939ba70
Parent:
1:1f5c9497a125
Child:
3:7fac2f012338
Tweaked length of for-loop checking for messages, and tweaked output message format.

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 1:1f5c9497a125 5 /* set logging for INFO, which is a good middle ground
Vanger 1:1f5c9497a125 6 * available levels are TRACE, DEBUG, INFO, WARNING, ERROR, and NONE
Vanger 1:1f5c9497a125 7 */
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 0:d9fd19c8ca39 30 /** Freescale KL46Z
Vanger 0:d9fd19c8ca39 31 * To configure the pins for the Freescale KL46Z board, use configuration B
Vanger 0:d9fd19c8ca39 32 * for the SocketModem. The TX pin should be jumped to pin D2 (JP8), and the
Vanger 0:d9fd19c8ca39 33 * RX pin should be jumped to pin D9 (JP9).
Vanger 0:d9fd19c8ca39 34 * Uncomment te following line to use the Freescale KL46Z board
Vanger 0:d9fd19c8ca39 35 */
Vanger 0:d9fd19c8ca39 36 //MTSSerialFlowControl* io = new MTSSerialFlowControl(D2, D9, D3, D6);
Vanger 0:d9fd19c8ca39 37
Vanger 0:d9fd19c8ca39 38 /** Freescale KL64F
Vanger 0:d9fd19c8ca39 39 * To configure the pins for the Freescale KL46Z board, use configuration A
Vanger 0:d9fd19c8ca39 40 * for the SocketModem. The TX pin should be jumped to pin D1 (JP8), and the
Vanger 0:d9fd19c8ca39 41 * RX pin should be jumped to pin D0 (JP9).
Vanger 0:d9fd19c8ca39 42 * Uncomment te following line to use the Freescale KL46F board
Vanger 0:d9fd19c8ca39 43 */
Vanger 0:d9fd19c8ca39 44 //MTSSerialFlowControl* io = new MTSSerialFlowControl(D1, D0, D3, D6);
Vanger 0:d9fd19c8ca39 45
Vanger 0:d9fd19c8ca39 46 //Sets the baudrate for communicating with the radio
Vanger 0:d9fd19c8ca39 47 io->baud(115200);
Vanger 0:d9fd19c8ca39 48
Vanger 2:d0d6e939ba70 49 //Creates a radio object
Vanger 0:d9fd19c8ca39 50 Cellular* radio = CellularFactory::create(io);
Vanger 2:d0d6e939ba70 51 radio->configureSignals(D4,D7,RESET);
Vanger 2:d0d6e939ba70 52 Transport::setTransport(radio);
Vanger 2:d0d6e939ba70 53
Vanger 2:d0d6e939ba70 54 //Set radio APN
Vanger 1:1f5c9497a125 55 for (int i = 0; i < 10; i++) {
Vanger 1:1f5c9497a125 56 if (i >= 10) {
Vanger 1:1f5c9497a125 57 logError("Failed to set APN\n");
Vanger 1:1f5c9497a125 58 }
Vanger 1:1f5c9497a125 59 if (radio->setApn(APN) == MTS_SUCCESS) {
Vanger 1:1f5c9497a125 60 logInfo("Successfully set APN\n");
Vanger 1:1f5c9497a125 61 break;
Vanger 1:1f5c9497a125 62 } else {
Vanger 1:1f5c9497a125 63 wait(1);
Vanger 1:1f5c9497a125 64 }
Vanger 1:1f5c9497a125 65 }
Vanger 0:d9fd19c8ca39 66
Vanger 1:1f5c9497a125 67 //Delete any previously received SMS messages
Vanger 1:1f5c9497a125 68 for (int i = 0; i < 10; i++) {
Vanger 1:1f5c9497a125 69 if (i >= 10) {
Vanger 1:1f5c9497a125 70 logError("Failed to delete SMS messages\n");
Vanger 1:1f5c9497a125 71 }
Vanger 1:1f5c9497a125 72 if (radio->deleteAllReceivedSms() == MTS_SUCCESS) {
Vanger 1:1f5c9497a125 73 logInfo("Deleted all SMS messages\n");
Vanger 1:1f5c9497a125 74 break;
Vanger 1:1f5c9497a125 75 } else {
Vanger 1:1f5c9497a125 76 wait(1);
Vanger 1:1f5c9497a125 77 }
Vanger 1:1f5c9497a125 78 }
Vanger 0:d9fd19c8ca39 79
Vanger 1:1f5c9497a125 80 // Send SMS message to phone
Vanger 1:1f5c9497a125 81 for (int i = 1; i < 10; i++) {
Vanger 1:1f5c9497a125 82 if(radio->sendSMS(txtmsg) == MTS_SUCCESS) {
Vanger 2:d0d6e939ba70 83 logInfo("Sent SMS successfully:<%s>\n", txtmsg.message.c_str());
Vanger 1:1f5c9497a125 84 break;
Vanger 1:1f5c9497a125 85 } else {
Vanger 2:d0d6e939ba70 86 logError("Failed to send SMS<%s>\n", txtmsg.message.c_str());
Vanger 1:1f5c9497a125 87 }
Vanger 1:1f5c9497a125 88 }
Vanger 0:d9fd19c8ca39 89
Vanger 2:d0d6e939ba70 90 //Checking for received SMS message
Vanger 2:d0d6e939ba70 91 for (int i = 0; i < 4; i++) {
Vanger 1:1f5c9497a125 92 logInfo("Checking for received messages");
Vanger 1:1f5c9497a125 93 vector<Cellular::Sms> recv = radio->getReceivedSms();
Vanger 1:1f5c9497a125 94 if(recv.size() > 0) {
Vanger 1:1f5c9497a125 95 int size = recv.size();
Vanger 1:1f5c9497a125 96 for (int i = 0; i < size; i++) {
Vanger 1:1f5c9497a125 97 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 98 }
Vanger 1:1f5c9497a125 99 }
Vanger 1:1f5c9497a125 100 radio->deleteOnlyReceivedReadSms();
Vanger 1:1f5c9497a125 101 wait(10);
Vanger 1:1f5c9497a125 102 }
Vanger 0:d9fd19c8ca39 103
Vanger 1:1f5c9497a125 104 logDebug("End of example code\n");
Vanger 0:d9fd19c8ca39 105 return 0;
Vanger 0:d9fd19c8ca39 106 }