Example program demonstrating sending and reading SMS messages from the cellular modem using the MTSAS library.
Revision 1:1f5c9497a125, committed 2014-07-25
- Comitter:
- Vanger
- Date:
- Fri Jul 25 16:47:08 2014 +0000
- Parent:
- 0:d9fd19c8ca39
- Child:
- 2:d0d6e939ba70
- Commit message:
- Added for loops to ensure success for each step. Also removed connect step as it interferes and is uneccessary.
Changed in this revision
--- a/main.cpp Thu Jul 24 19:24:37 2014 +0000 +++ b/main.cpp Fri Jul 25 16:47:08 2014 +0000 @@ -2,12 +2,20 @@ #include "mtsas.h" int main(){ + /* set logging for INFO, which is a good middle ground + * available levels are TRACE, DEBUG, INFO, WARNING, ERROR, and NONE + */ + MTSLog::setLogLevel(MTSLog::INFO_LEVEL); + //Modify to match your apn if you are using an HSPA radio with a SIM card const char APN[] = ""; - //Input the phone number below that you want to send the SMS messages to. - //Send the AT command AT+CNUM to the radio to obtain the phone number of the radio. - const char PHONE_NUMBER[] = ""; + //Phone number to send to and receive from. Must be in the form "1xxxxxxxxxx" + string PHONE_NUMBER = ""; + + Cellular::Sms txtmsg; + txtmsg.phoneNumber = PHONE_NUMBER; + txtmsg.message = "Hello World! MTSAS is up and running!"; /** STMicro Nucelo F401RE * The supported jumper configurations of the MTSAS do not line up with @@ -38,36 +46,56 @@ //Sets the baudrate for communicating with the radio io->baud(115200); + //Creates an instance of the radio interface called "radio" Cellular* radio = CellularFactory::create(io); - radio->setApn(APN); - wait(15); - - //Delete all SMS messages - radio->sendBasicCommand("AT+CMGD=1,4", 1000); - - //Setup commands and settings - radio->sendBasicCommand("AT+CMGF=1", 1000); - - /** If the radio is and H5 type (MTSMC_H5, MTSMC_H5_IP) then - * uncomment the first line. Otherwise, uncomment the second line. - */ - radio->sendBasicCommand("AT+CSMP=17,167,0,0", 1000); - //radio->sendBasicCommand("AT+CSMP=,4098,0,2", 1000); + for (int i = 0; i < 10; i++) { + if (i >= 10) { + logError("Failed to set APN\n"); + } + if (radio->setApn(APN) == MTS_SUCCESS) { + logInfo("Successfully set APN\n"); + break; + } else { + wait(1); + } + } - //Format message and send to radio - char command[100] = {0}; - sprintf(command, "AT+CMGS=\"%s\"", PHONE_NUMBER); - radio->sendCommand(command, 1000); - radio->sendCommand("Hello from MultiTechSystems!", 5000, CTRL_Z); - wait(5); + //Delete any previously received SMS messages + for (int i = 0; i < 10; i++) { + if (i >= 10) { + logError("Failed to delete SMS messages\n"); + } + if (radio->deleteAllReceivedSms() == MTS_SUCCESS) { + logInfo("Deleted all SMS messages\n"); + break; + } else { + wait(1); + } + } - //Read all messages received - std::string received = radio->sendCommand("AT+CMGL=\"ALL\"", 2000); - printf("Messages received:\n%s", received.c_str()); + // Send SMS message to phone + for (int i = 1; i < 10; i++) { + if(radio->sendSMS(txtmsg) == MTS_SUCCESS) { + logInfo("Sent SMS successfully:\n%s\n", txtmsg.message.c_str()); + break; + } else { + logError("Failed to send SMS\n%s\n", txtmsg.message.c_str()); + } + } - //Delete all messages - radio->sendBasicCommand("AT+CMGD=1,4", 1000); + for (int i = 0; i < 10; i++) { + logInfo("Checking for received messages"); + vector<Cellular::Sms> recv = radio->getReceivedSms(); + if(recv.size() > 0) { + int size = recv.size(); + for (int i = 0; i < size; i++) { + logInfo("Message %d: [%s] [%s] [%s]", i, recv[i].phoneNumber.c_str(), recv[i].timestamp.c_str(), recv[i].message.c_str()); + } + } + radio->deleteOnlyReceivedReadSms(); + wait(10); + } - printf("End of example code\n"); + logDebug("End of example code\n"); return 0; } \ No newline at end of file
--- a/mtsas-dev.lib Thu Jul 24 19:24:37 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/teams/Multi-Hackers/code/mtsas-dev/#a01d59c20e38
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mtsas.lib Fri Jul 25 16:47:08 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/Multi-Hackers/code/mtsas/#b599bdb14c12