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

Dependencies:   mbed-src mtsas

main.cpp

Committer:
Vanger
Date:
2014-07-25
Revision:
1:1f5c9497a125
Parent:
0:d9fd19c8ca39
Child:
2:d0d6e939ba70

File content as of revision 1:1f5c9497a125:

#include "mbed.h"
#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[] = "";
    
    //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
    * the pin mapping of the Nucleo F401RE. Therefore, the MTSAS serial TX
    * pin (JP8 Pin 2) must be manually jumped to Serial1 RX (Shield pin D2)
    * and the MTSAS serial RX pin (JP9 Pin 2) pin must be manually jumped to
    * Serial1 TX (Shield pin D8).
    * Uncomment the following line to use the STMicro Nuceleo F401RE
    */
    MTSSerialFlowControl* io = new MTSSerialFlowControl(D8, D2, D3, D6);
    
    /** Freescale KL46Z
    * To configure the pins for the Freescale KL46Z board, use configuration B
    * for the SocketModem. The TX pin should be jumped to pin D2 (JP8), and the
    * RX pin should be jumped to pin D9 (JP9). 
    * Uncomment te following line to use the Freescale KL46Z board
    */
    //MTSSerialFlowControl* io = new MTSSerialFlowControl(D2, D9, D3, D6);
    
    /** Freescale KL64F
    * To configure the pins for the Freescale KL46Z board, use configuration A
    * for the SocketModem. The TX pin should be jumped to pin D1 (JP8), and the
    * RX pin should be jumped to pin D0 (JP9). 
    * Uncomment te following line to use the Freescale KL46F board
    */
    //MTSSerialFlowControl* io = new MTSSerialFlowControl(D1, D0, D3, D6);
    
    //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);
    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);
        }
    }
    
    //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);
        }
    }
    
    // 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());
        }
    }
    
    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);
    }
    
    logDebug("End of example code\n");
    return 0;
}