ArduinoGeneral

Read SMS from serail to variable GPS/GPRS/GSM Module V3.0

userHead nDIRANGU 2013-08-28 05:42:11 7324 Views4 Replies
I have been trying to read the received sms from GPS/GPRS/GSM Module V3.0 via the serial port to a variable for manipulation purposes and all I receive is as below

AT
AT+CMGF=1
AT+CPMS="SM"
AT+CMGL = "ALL"
63
AT

OK
AT+CMGF=1

OK
AT+CPMS="SM"

+CPMS: 2,25,2,25,2,2



The sim has two SMSes that can be viewed when you run the commands via USD

The code is

byte gsmDriverPin[3] ={3,4,5};
int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];
String inData;

void setup() {


//pinMode(13, OUTPUT);
digitalWrite(5,HIGH);//Output GSM Timing
delay(1500);
digitalWrite(5,LOW);
digitalWrite(3,LOW);//Enable the GSM mode
digitalWrite(4,HIGH);//Disable the GPS mode
delay(2000);
mySerial.begin(9600); // the GPRS baud rate
mySerial.print("\r");
delay(1000);
Serial.begin(9600); //set the baud rate
delay(5000);//call ready
delay(5000);
delay(5000);

//Init the driver pins for GSM function
for(int i = 0 ; i < 3; i++)
{
pinMode(gsmDriverPin[i],OUTPUT);
}


}

void loop()
{
Serial.println("AT"); //AT is sent to the GSM / GPRS modem to test the connection
delay(2000);
//Serial.println("AT");
// p_char=Serial.read();
//Serial.write(p_char);
// delay(2000);


Serial.println("AT+CMGF=1"); //instruct the GSM/GPRS modem or mobile phone to operate in SMS text mode.
delay(2000);
// p_char=Serial.read();
//Serial.write(p_char);
delay(2000);

Serial.println("AT+CPMS=\"SM\""); // instruct the GSM/GPRS modem or mobile phone to use the message storage area in the SIM card
delay(2000);
//Serial.write(Serial.read());
//delay(2000);
Serial.println("AT+CMGL = \"ALL\""); //retrieve all SMS messages received
delay(2000);


if (Serial.available() )
{

int h=Serial.available();
Serial.println(h);

for (int i=0;i<h;i++)
{
inData += (char)Serial.read();
}
// if you are getting escape -characters try Serial.read(); here

//print it out
Serial.println(inData);
}

else
{
Serial.println("Not Available");
}

while (1);
}


any ideas where I am going wrong
2013-09-14 02:14:31 nDIRANGU,

Thats nice fix! thanks for sharing.

I wonder if reading the buffer faster would clear it so it could read the whole message, or if the buffer increase is the only option...

Cheers^^
userHeadPic Jose
2013-09-13 22:54:27 Thank you guys for your replies. Finally I got the solution. The size of the Serial Port Buffer is not big enough to store the output from the serial thus the reason it truncates so data. I increase the size from 64 bytes to 256 bytes and it worked.
[url=http://www.hobbytronics.dfrobot.uk/arduino-serial-buffer-size]http://www.hobbytronics.dfrobot.uk/arduino-serial-buffer-size[/url]
userHeadPic nDIRANGU
2013-08-30 19:10:02 Hello nDIRANGU,

Is just as N4rf said, since the module is using the serial you can't see half of it. With some wires, you can connect the serial port pins Rx/TX to other pints and use the software serial.

Or you can follow the wiki and do it two steps. First manually write the AT commands directly with the switch on USB, so you communicate with your GSM/GPS module directly, check if the commands are right, and move them to your code later on.

Another way to debug is using the integrated LED instead of the serial. Send your command, and if return is OK then light on the LED on pin 13.

Cheers
userHeadPic Jose
2013-08-30 08:17:00 mhm.. as far i concern, i dont know why you are sneding back the char you just received

[quote]if  (Serial.available() )
{
 
int h=Serial.available(); 
  Serial.println(h);

[/quote]

The serial is beeing used by the module, and anything you send will eb received by it, and might retrieve some error.

I mean, if you want to "debug" it, you need to use diferent serial to comunicate Arduino-Module and Arduino-USB(PC), lets say, use SoftwareSerial (pins 6,7 since 0,1 is serial, 3,4,5 is already in use) for the module, and Serial Rx/Tx for the USB(PC)

Also, I think, that here:

[code]

  Serial.println("AT+CPMS=\"SM\""); // instruct the GSM/GPRS modem or mobile phone to use the message storage area in the SIM card
  delay(2000); 
  //Serial.write(Serial.read());
  //delay(2000);
  Serial.println("AT+CMGL = \"ALL\""); //retrieve all SMS messages received
  delay(2000);
 

//right here you have a delay, by the time your delay is over, your serial info will be gone, thus, you will have no serial data, I think you have to call the serial.read right after you ask for an answer.

also, you are using if(serial.available), im not sure, but i think youshould use while(serial.available() > 0)


 
if  (Serial.available() ) 
{
 
int h=Serial.available(); 
  Serial.println(h);
 
  for (int i=0;i<h;i++)
{
            inData += (char)Serial.read();
}
    // if you are getting escape -characters try Serial.read(); here
 
    //print it out
    Serial.println(inData);
}

[/code]




And the code I use to store the answers form a module is this one:

[code]




char inChar;
char index[200];
int index;

void read_String() {
      index=0;
     
      while(Serial.available() > 0) // Don't read unless
                                                  // there you know there is data
  {
      if(index < 199) // One less than the size of the array
      {
          inChar = Serial.read(); // Read a character
          inData[index] = inChar; // Store it
          index++; // Increment where to write next
          inData[index] = '\0'; // Null terminate the string if there is nothing to write over it.
      }
  }
}


[/code]


you call it right after you ask for an answer...

[code]
Serial.println("AT+CMGL = \"ALL\"");
read_String();

[/code]


and once you have the char vector,  you can break it into comma separated values using

    strtok(inData, ","); //decode string into comma separated values
    strcpy(char_string1,strtok(NULL, ",")); // stores into a char string 1 what ever is from 1st comma until next one
    strcpy(char_string2,strtok(NULL, ",")); // stores into char string 2 what ever is from 2nd comma until next one

and so on

then you can analyze each string and respond according to message content.

also, remeber you can use:

AT+CMGR=i  //being i the message to read,

what I do, is everytime I receive a message, I respond and delete it... so I dont have to count how many messages I have, and read the very last one.

I receive, read, respond, delete. And so on...
userHeadPic N4rf