0

I have to read the incoming SMS on my GSM module SIM900, and I want to print the sender number and message to the serial monitor.

I first configure the GSM module with AT commands and Response() function will give me the response to AT commands.

as any SMS will be in the following pattern

+CMT: "[Mobile number]", "[Date and Time]>" [message body]

So, I first extract +CMT and after that I will take mobile number and at last we have message body. The code I have used is

char RcvdMsg[200] = "";
int RcvdCheck = 0;
int RcvdConf = 0;
int index = 0;
int RcvdEnd = 0;
char MsgMob[15];
char MsgTxt[50];
int MsgLength = 0;

void Config() // This function is configuring our SIM900 module i.e. sending the initial AT commands
{
    delay(1000);
    Serial.print("ATE0\r");
    Response();
    Serial.print("AT\r");
    Response();
    Serial.print("AT+CMGF=1\r");
    Response();
    Serial.print("AT+CNMI=1,2,0,0,0\r");
    Response();
}

void setup()
{
    Serial.begin(9600);
    Config();
}

void loop()
{
  RecSMS();
}

void Response() // Get the Response of each AT Command
{
    int count = 0;
    Serial.println();
    while(1)
    {
        if(Serial.available())
        {
            char data =Serial.read();
            if(data == 'K')
            {
                Serial.println("OK");
                break;
            }
            if(data == 'R')
            {
                Serial.println("GSM Not Working");
                break;
            }
        }
        count++;
        delay(10);
        if(count == 1000)
        {
            Serial.println("GSM not Found");
            break;
        }
    }
}

void RecSMS() // Receiving the SMS and extracting the Sender Mobile number & Message Text
{
    if(Serial.available())
    {
        char data = Serial.read();
        if(data == '+'){RcvdCheck = 1;}
        if((data == 'C') && (RcvdCheck == 1)) {RcvdCheck = 2;}
        if((data == 'M') && (RcvdCheck == 2)) {RcvdCheck = 3;}
        if((data == 'T') && (RcvdCheck == 3)) {RcvdCheck = 4;}
        if(RcvdCheck == 4)
        {
            RcvdConf = 1;
            RcvdCheck = 0;
        }

        if(RcvdConf == 1)
        {
            if(data == '\n'){RcvdEnd++;}
            if(RcvdEnd == 3){RcvdEnd = 0;}
            RcvdMsg[index] = data;

            index++;
            if(RcvdEnd == 2){RcvdConf = 0;MsgLength = index-2;index = 0;}
            if(RcvdConf == 0)
            {
                Serial.print("Mobile Number is: ");
                for(int x = 4;x < 17;x++)
                {
                    MsgMob[x-4] = RcvdMsg[x];
                    Serial.print(MsgMob[x-4]);
                }
                Serial.println();
                Serial.print("Message Text: ");
                for(int x = 46; x < MsgLength; x++)
                {
                    MsgTxt[x-46] = RcvdMsg[x];
                    Serial.print(MsgTxt[x-46]);
                }

                Serial.println();
                Serial.flush();
            }
        }
    }
}

The problem of the code is

After receiving first SMS I am getting my mobile number and message body. After that I am only getting the sender number printed to my serial monitor but not the message body.

Where has it gone wrong. I could not understood.

Please help me.......Thanks in advance.

  • Can you provide me this code if you found solution for your bug. It will be very important for my college project. Thank you for your support! – Prerak Shah Mar 19 '17 at 7:55
0

It looks like RcvdEnd never gets reset back to zero because RcvdConf is set to zero when RcvdEnd is 2, which means it will never get to 3 and reset it (at least not when you are expecting it to). I'm not sure how many new lines you are supposed to be checking for but the way it is set up now or is looking for two on the first message, and three for every message thereafter. You might want to rework the logic and maybe move where that value gets reset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.