Example program demonstrating sending and reading SMS messages from the cellular modem using the MTSAS library.
main.cpp@1:1f5c9497a125, 2014-07-25 (annotated)
- Committer:
- Vanger
- Date:
- Fri Jul 25 16:47:08 2014 +0000
- Revision:
- 1:1f5c9497a125
- Parent:
- 0:d9fd19c8ca39
- Child:
- 2:d0d6e939ba70
Added for loops to ensure success for each step. Also removed connect step as it interferes and is uneccessary.
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:1f5c9497a125 | 49 | //Creates an instance of the radio interface called "radio" |
Vanger | 0:d9fd19c8ca39 | 50 | Cellular* radio = CellularFactory::create(io); |
Vanger | 1:1f5c9497a125 | 51 | for (int i = 0; i < 10; i++) { |
Vanger | 1:1f5c9497a125 | 52 | if (i >= 10) { |
Vanger | 1:1f5c9497a125 | 53 | logError("Failed to set APN\n"); |
Vanger | 1:1f5c9497a125 | 54 | } |
Vanger | 1:1f5c9497a125 | 55 | if (radio->setApn(APN) == MTS_SUCCESS) { |
Vanger | 1:1f5c9497a125 | 56 | logInfo("Successfully set APN\n"); |
Vanger | 1:1f5c9497a125 | 57 | break; |
Vanger | 1:1f5c9497a125 | 58 | } else { |
Vanger | 1:1f5c9497a125 | 59 | wait(1); |
Vanger | 1:1f5c9497a125 | 60 | } |
Vanger | 1:1f5c9497a125 | 61 | } |
Vanger | 0:d9fd19c8ca39 | 62 | |
Vanger | 1:1f5c9497a125 | 63 | //Delete any previously received SMS messages |
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 delete SMS messages\n"); |
Vanger | 1:1f5c9497a125 | 67 | } |
Vanger | 1:1f5c9497a125 | 68 | if (radio->deleteAllReceivedSms() == MTS_SUCCESS) { |
Vanger | 1:1f5c9497a125 | 69 | logInfo("Deleted all SMS messages\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 | // Send SMS message to phone |
Vanger | 1:1f5c9497a125 | 77 | for (int i = 1; i < 10; i++) { |
Vanger | 1:1f5c9497a125 | 78 | if(radio->sendSMS(txtmsg) == MTS_SUCCESS) { |
Vanger | 1:1f5c9497a125 | 79 | logInfo("Sent SMS successfully:\n%s\n", txtmsg.message.c_str()); |
Vanger | 1:1f5c9497a125 | 80 | break; |
Vanger | 1:1f5c9497a125 | 81 | } else { |
Vanger | 1:1f5c9497a125 | 82 | logError("Failed to send SMS\n%s\n", txtmsg.message.c_str()); |
Vanger | 1:1f5c9497a125 | 83 | } |
Vanger | 1:1f5c9497a125 | 84 | } |
Vanger | 0:d9fd19c8ca39 | 85 | |
Vanger | 1:1f5c9497a125 | 86 | for (int i = 0; i < 10; i++) { |
Vanger | 1:1f5c9497a125 | 87 | logInfo("Checking for received messages"); |
Vanger | 1:1f5c9497a125 | 88 | vector<Cellular::Sms> recv = radio->getReceivedSms(); |
Vanger | 1:1f5c9497a125 | 89 | if(recv.size() > 0) { |
Vanger | 1:1f5c9497a125 | 90 | int size = recv.size(); |
Vanger | 1:1f5c9497a125 | 91 | for (int i = 0; i < size; i++) { |
Vanger | 1:1f5c9497a125 | 92 | 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 | 93 | } |
Vanger | 1:1f5c9497a125 | 94 | } |
Vanger | 1:1f5c9497a125 | 95 | radio->deleteOnlyReceivedReadSms(); |
Vanger | 1:1f5c9497a125 | 96 | wait(10); |
Vanger | 1:1f5c9497a125 | 97 | } |
Vanger | 0:d9fd19c8ca39 | 98 | |
Vanger | 1:1f5c9497a125 | 99 | logDebug("End of example code\n"); |
Vanger | 0:d9fd19c8ca39 | 100 | return 0; |
Vanger | 0:d9fd19c8ca39 | 101 | } |