reading sms

I’m fighting with a similar issue. I have a bit different code, and yes, it is far away from optimized or good-looking or professional etc. code. But, never mind, it works almost. I took it from the GeeTech GPRS module wiki page and made some modifications.

This code waits and reacts when a new SMS has received. It reads it (or try to read) and in the future it should do something based on the message content and then delete it. Currently it only deletes it. This part works, it notices a new SMS, reads it and deletes it. Next SMS works similar way.

The problem is in the message itself. If I send a SMS “Testing”, only part of it will be displayed, like in the example output below, there is only “Test”. If I wrote a longer SMS, the result is not truncated but contains random letters from the SMS sent. Any ideas why?

All I want is to get it working, may the code be brilliant or not :slight_smile:

#include <SoftwareSerial.h>
#include <string.h>
SoftwareSerial GSM(7, 8);

char buffer[300];

boolean clear = true;

int count=0;

void setup ()
{
  GSM.begin (19200); 
  Serial.begin (19200); 
  delay (4000); 
}

void ClearBufferArray ()
{
  for (int i=0; i<count; i++)
      buffer[i] = NULL;
}

void loop ()
{
  if (clear) {

// First delete possibly existing messages and put into text mode
// Somehow these does not work in Setup (), maybe...
    
    GSM.println ("AT+CMGD=1,4");
    clear = false;  
    Serial.println ("Existing SMSs deleted");
    delay (1000);
    GSM.println ("AT+CMGF=1");
    Serial.println ("Text mode");
  }

  if (GSM.available()) {
    
//Read data from GSM into buffer

    while (GSM.available ()) {
      
      buffer[count++] = GSM.read (); 
      buffer[count] = '\0';
      
// Avoid overflow      
      if(count == 300)
        break;
    }

//New message arrives, print the message announcement
    
    if (strstr (buffer,"+CMTI:")) {
  
      Serial.println ("SMS Received");

// Send reading command to GSM      
      
      GSM.println ("AT+CMGR=1");    
    }

// Handle the message
    
    if (strstr (buffer,"+CMGR:")) {

// So far only print the new one and delete it (all)
      
      Serial.println ("Reading SMS");
      GSM.println ("AT+CMGD=1,4");
    }  
    
// If no more data, transmission ends, print the buffer

    Serial.println (buffer);            

    ClearBufferArray ();
    count = 0;
  }

  if (Serial.available ())
    GSM.write (Serial.read ());
}

And output:

Existing SMSs deleted
Text mode
AT+CMGD=1,4

OK

AT+CMGF=1

OK

SMS Received

+CMTI: "SM",1

AT+CMGR=1

Reading SMS

+CMGR: "REC UNREAD","+358405833XXX","","13/01/21,23:56:46+08"
Test

OK

AT+CMGD=1,4


OK

MikkoR:
If I send a SMS "Testing", only part of it will be displayed, like in the example output below, there is only "Test". If I wrote a longer SMS, the result is not truncated but contains random letters from the SMS sent. Any ideas why?

		while (GSM.available ()) {
			
			buffer[count++] = GSM.read (); 
			buffer[count] = '\0';
			
			// Avoid overflow      
			if(count == 300)
			break;
		}

I doubt that it is causing you any problems, but for robustness you should do the overflow check before appending the received character to the buffer rather than after.

In your message handling code you assume that a complete message has been received as soon as GSM.available() returns zero, but all that really means is that the Arduino is reading characters from the serial connection faster than the GSM module is writing them. You should keep reading until you know you have received the complete response to your command. I don't know why you're seeing garbled messages but the fact you may only be reading the first few characters of each response from the GSM module could mean that the serial connection is overflowing at the sending side so perhaps that is the problem. In any case you need to sort out the serial port handling here before you look for any further problems.

PeterH:
In your message handling code you assume that a complete message has been received as soon as GSM.available() returns zero, but all that really means is that the Arduino is reading characters from the serial connection faster than the GSM module is writing them. You should keep reading until you know you have received the complete response to your command. I don't know why you're seeing garbled messages but the fact you may only be reading the first few characters of each response from the GSM module could mean that the serial connection is overflowing at the sending side so perhaps that is the problem. In any case you need to sort out the serial port handling here before you look for any further problems.

Sounds very reasonable, but how can I be sure that the serial input is actually finished? I'm not so familiar with these. Or should I read until "\nOK" is received? I think it is in the end of the message given by AT command (if everything goes right). Adding some delay did not work and is not a good idea anyway.

MikkoR:
Sounds very reasonable, but how can I be sure that the serial input is actually finished? I'm not so familiar with these. Or should I read until "\nOK" is received? I think it is in the end of the message given by AT command (if everything goes right). Adding some delay did not work and is not a good idea anyway.

A delay is certainly not the way to do it, agreed.

You need to work out how to detect the end of the response. If you know that each command response is followed by "\nOK\n" then you could wait until you receive that.

Sorry for bumping an old thread but Google led me here so this might help others.

You are probably overflowing the SoftwareSerial buffer which is set to 64 bytes.

You can confirm this by checking GSM.overflow() returns true

(reference http://arduino.cc/en/Reference/SoftwareSerialOverflow)

A dirty hack is to increase the SoftwareSerial buffer. Under libraries/SoftwareSerial/SoftwareSerial.h you'll find this line:

#define _SS_MAX_RX_BUFF 64 // RX buffer size

Increase it, keeping in mind the SRAM limitations on your Arduino device.

Edit:

After playing around a bit more I found the following in addition:

  • I had to change _SS_MAX_RX_BUFF directly in the SoftwareSerial.h class. Trying to #define it in my own project (either before or after the #include) resulted in failures still. I'm not sure why...printing out _SS_MAX_RX_BUFF and doing a sizeof(_receive_buffer) matched my values but it was still overflowing at 64.
  • I get corruption if I use any buffer size that isn't a power of 2. I don't know why.
  • To use a buffer size over 255 (well, 128) the variables _receive_buffer_tail and _receive_buffer_head need to be changed from uint8_t to uint16_t. I couldn't go above 128 without making that change.

Some reference info related to the two above things I found at Arduino Forum

Hello,

Has someone come up with a solution with the above stated problem?

We are using Arduino Duemilanove and SIM 900 GSM module (http://robokits.co.in/shop/index.php?main_page=product_info&products_id=303)

We've tried to work on the similar problem of lightning Leds from port 9-12 when we send an sms #aibicidi, where i = 0 or 1, 0 =off, 1=on.
Eg. #a1b1c1d1 will switch on all the Led's.

When we upload the code and run it through serial monitor and enter the #a1b1c1d1 in the serial monitor, we can see all the led's lighten up. But if we send the sms with having content "#a1b1c1d1", we dont see any function of leds.

It would be great if anyone can give some guidance about the same.

 char inchar; //Will hold the incoming character from the Serial Port.


 int led1 = 9;
 int led2 = 10;
 int led3 = 11;
 int led4 = 12;

 void setup()
 {
 // prepare the digital output pins
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(led4, OUTPUT);
 digitalWrite(led1, LOW);
 digitalWrite(led2, LOW);
 digitalWrite(led3, LOW);
 digitalWrite(led4, LOW);
 //Initialize GSM module serial port for communication.
 
 
 Serial.begin(9600);
 delay(3000); // give time for GSM module to register on network etc.
 Serial.println("AT+CMGF=1"); // set SMS mode to text
 delay(200);
 Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt 
 delay(200);
 }

 void loop() 
 {
 //If #a1b1c1d1 comes as sms, all led's should light up.
 if(Serial.available() >0)
 {
 inchar=Serial.read(); 
 if (inchar=='#')
   {
   delay(10);
   inchar=Serial.read(); 
   
 //first led
   if (inchar=='a')
     {
   delay(10);
   inchar=Serial.read();
   
 if (inchar=='0')
   {
   digitalWrite(led1, LOW);
   } 
 else if (inchar=='1')
   {
   digitalWrite(led1, HIGH);
   }
 delay(10);

 
 //Second led
 inchar=Serial.read(); 
 
 if (inchar=='b')
   {
   inchar=Serial.read();
 if (inchar=='0')
 {
 digitalWrite(led2, LOW);
 } 
 
 else if (inchar=='1')
 {
 digitalWrite(led2, HIGH);
 }
 delay(10);
 
 // Third led
 inchar=Serial.read(); 
 if (inchar=='c')
 {
 inchar=Serial.read();
 if (inchar=='0')
 {
 digitalWrite(led3, LOW);
 } 
 else if (inchar=='1')
 {
 digitalWrite(led3, HIGH);
 }
 delay(10);
 
 //Fourth led
 
 inchar=Serial.read(); 
 if (inchar=='d')
 {
 delay(10);
 inchar=Serial.read();
 if (inchar=='0')
 {
 digitalWrite(led4, LOW);
 } 
 else if (inchar=='1')
 {
 digitalWrite(led4, HIGH);
 }
 delay(10);
 }
 }
 Serial.println("AT+CMGD=1,4"); // delete all SMS
 }
 }
 }
 }
 }

We were able to lighten up the LED with an sms.

Keeping the code below. This code will lighten 9 leds which are kept from port 4 to 12. The sms code is "0#iii#iii#iii, where i =0 or 1 meaning off or on respectively. Ex. 0#111#111#111 will switch on all the leds.

The modification was AT+CNMI=2,2,0,0,0 instead of AT+CNMI=3,3,0,0 from our previous code.

char inchar; //Will hold the incoming character from the Serial Port.


 int led1 = 4;
 int led2 = 5;
 int led3 = 6;
 int led4 = 7; 
 int led5 = 8; 
 int led6 = 9; 
 int led7 = 10; 
 int led8 = 11; 
 int led9 = 12; 

 void setup()
 {
 // prepare the digital output pins
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(led4, OUTPUT);
 pinMode(led5, OUTPUT);
 pinMode(led6, OUTPUT);
 pinMode(led7, OUTPUT);
 pinMode(led8, OUTPUT);
 pinMode(led9, OUTPUT);
 
 
 // initially all are off
 digitalWrite(led1, LOW);
 digitalWrite(led2, LOW);
 digitalWrite(led3, LOW);
 digitalWrite(led4, LOW);
 digitalWrite(led5, LOW);
 digitalWrite(led6, LOW);
 digitalWrite(led7, LOW);
 digitalWrite(led8, LOW);
 digitalWrite(led9, LOW);
 
 
 
 
 //Initialize GSM module serial port for communication.
 Serial.begin(9600);
 delay(3000); // give time for GSM module to register on network etc.
 Serial.println("AT+CMGF=1\r"); // set SMS mode to text
 delay(200);
 Serial.println("AT+CSMS=1\r"); 
 delay(200);
 
// Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt 
 Serial.println("AT+CNMI=2,2,0,0,0"); // set module to send SMS data to serial out upon receipt 

 delay(200);
 }

 void loop() 
 {
 //If a character comes in from the Serialular module...

 if(Serial.available() >0)
 {  

 delay(10);
 inchar=Serial.read(); 
 if (inchar=='0')   // to catch the 1st 0 in our string 0#101#101#101
 {     
       // digitalWrite(13, HIGH);
       // for #101________________
       inchar=Serial.read(); // read next char i.e '#'
       if (inchar=='#')
       {
             inchar=Serial.read(); //read next char i.e 1
             if (inchar=='0')
             {
                 digitalWrite(led1, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led1, HIGH);
             }
             
             
       delay(10);
             inchar=Serial.read(); //read next char i.e 0
             if (inchar=='0')
             {
                 digitalWrite(led2, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led2, HIGH);
             }
             
       delay(10);
             inchar=Serial.read(); //read next char i.e 1
             if (inchar=='0')
             {
                 digitalWrite(led3, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led3, HIGH);
             }      
       } //end of  1st 'if char==#' loop
       
       
       
       
       //for _____  #101  _______
       inchar=Serial.read(); // read next char i.e '#'
       if (inchar=='#')
       {
             inchar=Serial.read(); //read next char i.e 1
             if (inchar=='0')
             {
                 digitalWrite(led4, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led4, HIGH);
             }
             
             
       delay(10);
             inchar=Serial.read(); //read next char i.e 0
             if (inchar=='0')
             {
                 digitalWrite(led5, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led5, HIGH);
             }
             
       delay(10);
             inchar=Serial.read(); //read next char i.e 1
             if (inchar=='0')
             {
                 digitalWrite(led6, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led6, HIGH);
             }      
       } //end of 2nd 'if char==#' loop
       
       
       
       
       //for _____ _____ #101
       inchar=Serial.read(); // read next char i.e '#'
       if (inchar=='#')
       {
             inchar=Serial.read(); //read next char i.e 1
             if (inchar=='0')
             {
                 digitalWrite(led7, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led7, HIGH);
             }
             
             
       delay(10);
             inchar=Serial.read(); //read next char i.e 0
             if (inchar=='0')
             {
                 digitalWrite(led8, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led8, HIGH);
             }
             
       delay(10);
             inchar=Serial.read(); //read next char i.e 1
             if (inchar=='0')
             {
                 digitalWrite(led9, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led9, HIGH);
             }      
       } //end of 3rd 'if char==#' loop
       
       
       
 } // "ckeck 0 in front of string" wala loop close

 //Serial.println("AT+CMGD=1,4"); // delete all SMS
 
 } // serial available condition close
 
 
//   Serial.println("AT+CMGR=1\r");
//   delay(4000);
 } // LOOP function close

hello i am doing the same thing tried to use the above code but it is not working

i can control the led pin 13 using serial consol by sending 0#1 and can make off by 0#0 but when i am sending the message 0#1 the led is not glowing here is my code
can anybody tell me what is the problem??

char inchar; //Will hold the incoming character from the Serial Port.


 int led1 = 13;
void setup()
 {
 // prepare the digital output pins
 pinMode(led1, OUTPUT);

 // initially all are off
 digitalWrite(led1, LOW);
//Initialize GSM module serial port for communication.
 Serial.begin(9600);
 delay(3000); // give time for GSM module to register on network etc.
 Serial.println("AT+CMGF=1\r"); // set SMS mode to text
 delay(200);
 Serial.println("AT+CSMS=1\r"); 
 delay(200);
 
//Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt 
 Serial.println("AT+CNMI=2,2,0,0,0"); // set module to send SMS data to serial out upon receipt 
//Serial.println("AT+CSMP=17,167,0,0");
 delay(200);
 }
void loop() 
 {
 //If a character comes in from the Serialular module...
if(Serial.available() >0)
 {  

 delay(10);
 inchar=Serial.read(); 
 if (inchar=='0')   // to catch the 1st 0 in our string 0#101#101#101
 {     
      // digitalWrite(led1, HIGH);
       // for #101________________
       inchar=Serial.read(); // read next char i.e '#'
       if (inchar=='#')
       {
             inchar=Serial.read(); //read next char i.e 1
             if (inchar=='0')
             {
                 digitalWrite(led1, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led1, HIGH);
             }
                  } //end of 3rd 'if char==#' loop
       
       } // "ckeck 0 in front of string" wala loop close

 //Serial.println("AT+CMGD=1,4"); // delete all SMS
  } // serial available condition close
 
 //   Serial.println("AT+CMGR=1\r");
//   delay(4000);
 } // LOOP function close

Hi,

The code was working properly in my case of Arduino Duemilanove and SIM 900 gsm module. Which one are you using?

Your can verify the below points.

  1. See that you close the serial console, when you are messaging to the gsm module. The arduino should be connected to the laptop only for power supply.

  2. See that you connection properly Rx of arduino to Tx of gsm, Tx of arduino to Rx of gsm. Connect both the ground together and DONT connect the Vin of arduino and gsm module together, otherwise there are high chances of damaging the arduino as gsm module is using lot of power to send/receive messages.

I will look try to run your code on my arduino and let you know its outcome. But keep on trying...

hi

thanks for the instant reply

i am using arduino freeduino(ATMEGA 328) and sim 900(which i bought from rhydolabs)

i am using pin2 as rx and pin3 as tx is it correct????

here are my connections

Rx of arduino(pin no-2)to Tx of gsm sim900
Tx of arduino(pin no-3)to Rx of gsm sim900
Vinterface of sim900 to 3.3v of arduino
i am using USB cable for arduino supply so i don't require any external power supply
i am using arduino 5v supply for powering up the sim900
i have also connected ground together

but unfortunately stilll didnt work????????? :~

have you tried this code in your laptop??

thanks again for your intrest in my post

Ok,

According to this image of freeduino: http://www.freeduino.org/images/freeduino_serial2.jpg
The Rx is pin 0(the first pin from right on digital side) and Tx is pin 1(second pin from right) on the digital side.

The GSM module will require another power supply from an adapter of 12Vdc , 2amps (you need to buy on). The power supply from USB is not enough to power the GSM module. Once you give the power supply to GSM module, DONT connect the Vinterface of sim900 to 3.3v of arduino (as the arduino may get damaged). The power supply from USB is enough for arduino.

So now you have only three connection, Rx, Tx and ground. I will try your code in the coming days. It should work.

P.S.:
The gsm module from rhydolabs
eg. GSM/GPRS MODEM- SIM900 -RS232
tells that it would require input voltage of 12V. Confirm yours.

ok
thanks but my GSM module is this

and my freeduino is this
http://www.embeddedmarket.com/products/Arduino-Compatible-Freeduino-DU/

it can work on 5v and it is working well for me as i can send ths sms and also can recive them by above connections
which i have mentioned in my earlier post

i am keep trying......... and also waiting for your reply

Hey I tried your code.
Its working fine in Duemilanove and SIM900 gsm module. Led 13 is switching on.

Possible changes to your system would be.

  1. Trying out with 12Vdc, 2amp power supply for GSM module.
  2. According to this figure: http://www.embeddedmarket.com/products/Arduino-Compatible-Freeduino-DU/Datasheet.asp
    Pin 0 is Tx and Pin 1 is Rx. Normally Rx and Tx are written on the pin numbers. So try changing pin if it works.
  3. Switch on/off and delete all the messages in GSM module and try again.

Let me know if it still doesn't work.

hey man thanks for your keen interest in my posts

i tried all the things
but its still not working :~

its really frustrating

ok i know this is stupid question but still i want to ask you what message
you are sending when you are running my code
its looking like i am doing something wrong in sending message i belive
this could be the one reason
and will you please mail me the code which you have run
i belive i am becoming superstitious now!!!
sorry for troubling you

once again i am trying..... and waiting for your reply

my email address is shahdhruvit111@yahoo.com

No probs,

  1. The message I send is : 0#1 to switch on the led and 0#0 to switch off.

The code:

char inchar; //Will hold the incoming character from the Serial Port.


 int led1 = 13;
void setup()
 {
 // prepare the digital output pins
 pinMode(led1, OUTPUT);

 // initially all are off
 digitalWrite(led1, LOW);
//Initialize GSM module serial port for communication.
 Serial.begin(9600);
 delay(3000); // give time for GSM module to register on network etc.
 Serial.println("AT+CMGF=1\r"); // set SMS mode to text
 delay(200);
 Serial.println("AT+CSMS=1\r"); 
 delay(200);
 
//Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt 
 Serial.println("AT+CNMI=2,2,0,0,0"); // set module to send SMS data to serial out upon receipt 
//Serial.println("AT+CSMP=17,167,0,0");
 delay(200);
 }
void loop() 
 {
 //If a character comes in from the Serialular module...
if(Serial.available() >0)
 {  

 delay(10);
 inchar=Serial.read(); 
 if (inchar=='0')   // to catch the 1st 0 in our string 0#101#101#101
 {     
      // digitalWrite(led1, HIGH);
       // for #101________________
       inchar=Serial.read(); // read next char i.e '#'
       if (inchar=='#')
       {
             inchar=Serial.read(); //read next char i.e 1
             if (inchar=='0')
             {
                 digitalWrite(led1, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led1, HIGH);
             }
                  } //end of 3rd 'if char==#' loop
       
       } // "ckeck 0 in front of string" wala loop close

 //Serial.println("AT+CMGD=1,4"); // delete all SMS
  } // serial available condition close
 
 //   Serial.println("AT+CMGR=1\r");
//   delay(4000);
 } // LOOP function close

still not working man!!!!!!!!!!!!

but still trying...........

any suggestions?????

hey man thanks

i just tried with sim300 module and it worked!!!!!!!!!!!!!! :slight_smile:

thanks again
but why it is not working with sim900???

still confused??

Sir,

i tried your code, and the result is my arduino cant turn off the LED after i send 0#0.
Whats the problem?

uttampal19:
No probs,

  1. The message I send is : 0#1 to switch on the led and 0#0 to switch off.

The code:

char inchar; //Will hold the incoming character from the Serial Port.

int led1 = 13;
void setup()
{
// prepare the digital output pins
pinMode(led1, OUTPUT);

// initially all are off
digitalWrite(led1, LOW);
//Initialize GSM module serial port for communication.
Serial.begin(9600);
delay(3000); // give time for GSM module to register on network etc.
Serial.println("AT+CMGF=1\r"); // set SMS mode to text
delay(200);
Serial.println("AT+CSMS=1\r");
delay(200);

//Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt
Serial.println("AT+CNMI=2,2,0,0,0"); // set module to send SMS data to serial out upon receipt
//Serial.println("AT+CSMP=17,167,0,0");
delay(200);
}
void loop()
{
//If a character comes in from the Serialular module...
if(Serial.available() >0)
{

delay(10);
inchar=Serial.read();
if (inchar=='0')  // to catch the 1st 0 in our string 0#101#101#101
{   
      // digitalWrite(led1, HIGH);
      // for #101________________
      inchar=Serial.read(); // read next char i.e '#'
      if (inchar=='#')
      {
            inchar=Serial.read(); //read next char i.e 1
            if (inchar=='0')
            {
                digitalWrite(led1, LOW);
            }
            else if (inchar=='1')
            {
                digitalWrite(led1, HIGH);
            }
                  } //end of 3rd 'if char==#' loop
     
      } // "ckeck 0 in front of string" wala loop close

//Serial.println("AT+CMGD=1,4"); // delete all SMS
  } // serial available condition close

//  Serial.println("AT+CMGR=1\r");
//  delay(4000);
} // LOOP function close

if(Serial.available() >0)
 { 

 delay(10);
 inchar=Serial.read();
 if (inchar=='0')   // to catch the 1st 0 in our string 0#101#101#101
 {     
      // digitalWrite(led1, HIGH);
       // for #101________________
       inchar=Serial.read(); // read next char i.e '#'

If there is one byte of serial data to be read, it is NOT okay to read two of them.