# reading sms

**URL:** <https://forum.arduino.cc/t/reading-sms/138363>\
**Category:** Programming\
**Created:** [January 9, 2013, 2:55pm UTC](https://forum.arduino.cc/t/reading-sms/138363 "2013-01-09T14:55:37Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![zoomkat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/zoomkat/32/1190177_2.png) [@zoomkat](https://forum.arduino.cc/u/zoomkat)\
**Post date:** [January 12, 2013, 12:18am UTC](https://forum.arduino.cc/t/reading-sms/138363/21 "2013-01-12T00:18:36Z")

</div>

> hi sir, not sure if its done correctly but there's my attempt,

The below web code shows how to capture an incomming string and see if it contains an "on' or "off", then act appropriately. Might have something useful for you.

```arduino
//zoomkat 12-08-12
//get submit box code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html or use a '
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84);; //server port

String readString; 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server text box test1"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //see what was captured

          //now output HTML data header

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>HTML form GET example</H1>");

          client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

          client.println("Pin 5 'on' or 'off': <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
");

          client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Change Pin 5!'>");

          client.println("</FORM>");

          client.println("
");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          /////////////////////
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 12, 2013, 3:55pm UTC](https://forum.arduino.cc/t/reading-sms/138363/22 "2013-01-12T15:55:31Z")

</div>

```cpp
this sketch reads incoming sms's and stores it perfectly,the stored sms  doesnt show the contents of it,the word 'on' doesnt appear anywhere on the serial monitor only the cellphone number and some other details,according to the sketch it should activate the led when an sms 'on' is sent,i checked all AT commands they seem to be right,not sure how to modify the part that is responsible for reading the sms,

```

The is NOT code. Learn how to post comments OUTSIDE of the code tags.

```cpp
    if(Serial.available())
    else  if(mySerial.available())

```

Why are these exclusive?

```cpp
        char SerialInByte;
        SerialInByte = (unsigned char)mySerial.read();

```

Why are you separating the declaration and the initialization?

```cpp
        char SerialInByte = (unsigned char)mySerial.read();

```

Casting to an unsigned char to store in a signed char is a bad idea, by the way. The cast is not needed at all.

```cpp
        // FR: Si le message se termine par un <CR> alors traiter le message 
        if( SerialInByte == 13 ){

```

Why not make it clear what the code is doing?

```cpp
        if( SerialInByte == '\n' ){

```

If you can add a space after the open parenthesis, why can't you add one after the close parenthesis? Studying the code every time is a waste of time. Organize the code so it is readable.

```cpp
        if( SerialInByte == 10 ){
            // EN: Skip Line feed

```

Again, why not make the code clearer?

```cpp
        if( SerialInByte == '\r' ) {

```

```cpp
          msg += String(SerialInByte);

```

Invoking the constructor, the copy constructor, the assignment operator, and the destructor for the String class to add one character is a waste of resources. The += operator is overloaded to add a character. Use that overload instead of converting the one character to a new String.

In fact, you should get rid of the String class, altogether.

Anyway, none of this has anything to do with why the AT commands are not returning entirely what you expect. That doesn't mean that they should not be fixed. What it tells me is that you are issuing the wrong AT command to read the SMS.

---

<div class="post-metadata">

**Author:** ![MikkoR](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@MikkoR](https://forum.arduino.cc/u/MikkoR)\
**Post date:** [January 21, 2013, 10:18pm UTC](https://forum.arduino.cc/t/reading-sms/138363/23 "2013-01-21T22:18:42Z")

</div>

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 🙂

```cpp
#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:

```cpp
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

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 21, 2013, 11:01pm UTC](https://forum.arduino.cc/t/reading-sms/138363/24 "2013-01-21T23:01:14Z")

</div>

> 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?

```arduino
		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.

---

<div class="post-metadata">

**Author:** ![MikkoR](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@MikkoR](https://forum.arduino.cc/u/MikkoR)\
**Post date:** [January 22, 2013, 7:04am UTC](https://forum.arduino.cc/t/reading-sms/138363/25 "2013-01-22T07:04:59Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 22, 2013, 10:55am UTC](https://forum.arduino.cc/t/reading-sms/138363/26 "2013-01-22T10:55:47Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 26, 2013, 11:09pm UTC](https://forum.arduino.cc/t/reading-sms/138363/27 "2013-03-26T23:09:42Z")

</div>

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](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:

```arduino
#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 [SoftwareSerial Buffer problem when not power of 2 - #4 by nickgammon - Programming Questions - Arduino Forum](http://arduino.cc/forum/index.php/topic,128544.msg967171.html)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 17, 2013, 5:33am UTC](https://forum.arduino.cc/t/reading-sms/138363/28 "2013-04-17T05:33:24Z")

</div>

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](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.

```arduino
 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
 }
 }
 }
 }
 }

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 17, 2013, 10:45am UTC](https://forum.arduino.cc/t/reading-sms/138363/29 "2013-04-17T10:45:22Z")

</div>

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.

```cpp
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

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 22, 2013, 6:36am UTC](https://forum.arduino.cc/t/reading-sms/138363/30 "2013-04-22T06:36:32Z")

</div>

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??

```cpp
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

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 22, 2013, 7:04am UTC](https://forum.arduino.cc/t/reading-sms/138363/31 "2013-04-22T07:04:52Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 22, 2013, 9:05am UTC](https://forum.arduino.cc/t/reading-sms/138363/32 "2013-04-22T09:05:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 22, 2013, 10:01am UTC](https://forum.arduino.cc/t/reading-sms/138363/33 "2013-04-22T10:01:31Z")

</div>

Ok,

According to this image of freeduino: [http://www.freeduino.org/images/freeduino\_serial2.jpg](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](http://www.rhydolabz.com/index.php?main_page=product_info&cPath=122&products_id=1088)  
tells that it would require input voltage of 12V. Confirm yours.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 22, 2013, 2:16pm UTC](https://forum.arduino.cc/t/reading-sms/138363/34 "2013-04-22T14:16:17Z")

</div>

ok  
thanks but my GSM module is this

> **[GSM/GPRS TTL UART MODEM - SIM900A (rhydoLABZ) \[GSM-1965\] : rhydoLABZ INDIA](https://www.rhydolabz.com/wireless-4g-lte3ggsm-gprs-c-130_185/gsmgprs-ttl-uart-modem-sim900a-rhydolabz-p-1080.html)**
>
> rhydoLABZ India : GSM / GPRS TTL UART MODEM - SIM900A, from rhydoLABZ, is built with Dual Band GSM/GPRS engine- SIM900A, works on frequencies 900/ 1800 MHz.

and my freeduino is this  
[http://www.embeddedmarket.com/products/Arduino-Compatible-Freeduino-DU/](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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 22, 2013, 6:08pm UTC](https://forum.arduino.cc/t/reading-sms/138363/35 "2013-04-22T18:08:28Z")

</div>

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: [EmbeddedMarket.com](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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 23, 2013, 4:47am UTC](https://forum.arduino.cc/t/reading-sms/138363/36 "2013-04-23T04:47:10Z")

</div>

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](mailto:shahdhruvit111@yahoo.com)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 23, 2013, 4:54am UTC](https://forum.arduino.cc/t/reading-sms/138363/37 "2013-04-23T04:54:11Z")

</div>

No probs,

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

The code:

```cpp
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

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 23, 2013, 1:05pm UTC](https://forum.arduino.cc/t/reading-sms/138363/38 "2013-04-23T13:05:02Z")

</div>

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

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

any suggestions?????

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 25, 2013, 4:31am UTC](https://forum.arduino.cc/t/reading-sms/138363/39 "2013-04-25T04:31:44Z")

</div>

hey man thanks

i just tried with sim300 module and it worked!!!!!!!!!!!!!! 🙂

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

still confused??

---

<div class="post-metadata">

**Author:** ![albert\_ep](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@albert\_ep](https://forum.arduino.cc/u/albert_ep)\
**Post date:** [March 21, 2016, 4:15pm UTC](https://forum.arduino.cc/t/reading-sms/138363/40 "2016-03-21T16:15:54Z")

</div>

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:
> 
> ```arduino
> 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](https://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](https://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')&nbsp; // to catch the 1st 0 in our string 0#101#101#101  
{&nbsp; &nbsp;  
&nbsp; &nbsp; &nbsp; // digitalWrite(led1, HIGH);  
&nbsp; &nbsp; &nbsp; // for #101\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
&nbsp; &nbsp; &nbsp; inchar=Serial.read(); // read next char i.e '#'  
&nbsp; &nbsp; &nbsp; if (inchar=='#')  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inchar=Serial.read(); //read next char i.e 1  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (inchar=='0')  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(led1, LOW);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (inchar=='1')  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(led1, HIGH);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } //end of 3rd 'if char==#' loop  
&nbsp; &nbsp; &nbsp;  
&nbsp; &nbsp; &nbsp; } // "ckeck 0 in front of string" wala loop close

[//Serial.println](https://Serial.println)("AT+CMGD=1,4"); // delete all SMS  
&nbsp; } // serial available condition close

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

> ```arduino
> 
> ```

[Previous page](https://forum.arduino.cc/t/reading-sms/138363.md?page=1)

[Next page](https://forum.arduino.cc/t/reading-sms/138363.md?page=3)
