1
votes

I have to read the incoming SMS on my gsm module SIM900 (which is connected to Arduino), and I want to print the sender number and message on to serial monitor.

I first configure 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 atlast 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 sender number printed on to my serial monitor but not the message body.

Where It has gone wrong. I could not understood.

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

2
Your code is very difficult to read without proper indentation.Mike G

2 Answers

0
votes

If it does work the first time, but not subsequent times it probably has to do with some variables not being reset. You declare all of your variables at the top of the file even though they are only needed in the RecSMS() function. Try moving the declarations to the top of RecSMS().

void RecSMS() {
  char RcvdMsg[200] = "";
  int RcvdCheck = 0;
  int RcvdConf = 0;
  int index = 0;
  int RcvdEnd = 0;
  char MsgMob[15];
  char MsgTxt[50];
  int MsgLength = 0;

  if(Serial.available()) {

  // Rest of the code goes here
0
votes

Thankyou @Michael. I think this also solves the issue.

The problem I found in the code is, we are not resetting all the variables in RecSMS function. So to solve this keep this below code before the Serial.flush() statement.

RcvdCheck = 0;
RcvdConf = 0;
index = 0;
RcvdEnd = 0;
MsgMob[15];
MsgTxt[50];
MsgLength = 0;

This will solve the problem