Test

main.cpp

Committer:
JohnnyK
Date:
2022-03-29
Revision:
6:b38819d5db73
Parent:
5:70b84276766a
Child:
7:90bb5b736fe4

File content as of revision 6:b38819d5db73:

#include "mbed.h"
#include <string>
#define DEBUG 1

/* !!!Change pins according to your connection!!! */
BufferedSerial uart(PD_5,PD_6, 115200); // UART TX, RX
ATCmdParser module(&uart,"\r\n");

bool modulInit(int simPin);
bool sendSMS(string phoneNum, string text);

// setup
int main() {
    printf("Mbed A9G test\n");
    module.debug_on(true);
    module.set_timeout(1000);
    
    if(modulInit(1234)){ // replace 1234 with your Pin code of your SIM card or replace it with 0, when your SIM have no Pin code.
        if(sendSMS("+123123456789", "Testerino")){ // replace +123123456789 with destination Phone number
            printf("SMS sended\n");
        }
    }

    // loop
    while(true)
    {
        //do something in loop
        thread_sleep_for(1000);
    }
}

bool modulInit(int simPin){
    bool result = true;
    // power on
    for(int i=0;i<5;i++){
        module.send("AT");
        thread_sleep_for(500);
        if(module.recv("OK")){
            debug_if(DEBUG, "Module is up!\n");  
            break;
        } else {
            debug_if(DEBUG, "No communication with module!\n");
            return false;  
        }
    }
    // SIM autentication
    if(simPin != 0){
        module.send("AT+CPIN=%d", simPin);
        if(module.recv("OK")){
            debug_if(DEBUG, "Sim unlocked\n");  
        } else {
            debug_if(DEBUG, "SIM locked!\n"); 
            return false;
        }
    }
    
    // Wait for network registration
    string network_status[] = { "not registered, not searching", 
                                "registered, home network", 
                                "not registered, but is searching", 
                                "registration denied", 
                                "unknown", 
                                "registered, roaming"};
    int state = 0;
    debug_if(DEBUG, "Connecting to network...\n");
    module.set_timeout(10000);
    for(int i=0;i<5;i++){
        if(module.recv("+CREG: %d",&state)){
            if(state == 1)break;
            debug_if(DEBUG, "State: %s...\n",network_status[state].c_str());
        } else {
            debug_if(DEBUG, "State: timeout\n");
        }
        thread_sleep_for(500);
    }  
    if(state == 1){
        debug_if(DEBUG, "Connected\n");
    }else{
        debug_if(DEBUG, "Connection failed!\n");
        return false;
    }
    thread_sleep_for(1000);
    module.set_timeout(1000);    
    return true;
}

bool sendSMS(string phoneNum, string text){
    bool result = true;
    // set mode to text mode
    int mode = 1;
    module.send("AT+CMGF=%d", mode);
    if(module.recv("OK")){
        debug_if(DEBUG, "Sms mode was set to %d\n", mode);  
    } else {
        debug_if(DEBUG, "Set mode failed!\n");  
    }
    
    module.set_timeout(10000);
    module.send("AT+CMGS=\"%s\"", phoneNum.c_str());
    if(module.recv(">")){
        debug_if(DEBUG, "Sms mode was set to %d\n", mode); 
        module.send("%s\n%c", text.c_str(), 0x1A); 
        int id = 0;
        if(module.recv("+CMGS: %d", &id)){
            debug_if(DEBUG, "Sms was send to %s under id: %d \n",phoneNum.c_str(), id); 
        } else {
            debug_if(DEBUG, "Sms send failed\n"); 
            result = false;
        }
    } else {
        debug_if(DEBUG, "Sms send failed\n");
        result = false;
    }
    module.set_timeout(1000);  
    return result;
}