Receive Message with Arduino with ordinary GSM module (no GSM shield) with AT commands

Recently I found many people asking queries about using an ordinary GSM module with Arduino. Like, is it possible to use a GSM module locally available instead of Arduino GSM Shield.. Answer is .Yes ! it is possible..


Steps to that ..

1.  Find your GSM modules Baud rate
2. Connecting to Arduino .. You need to connect only 3 pins from GSM to Arduino
        *  RX from Arduino to TX of GSM
        *  TX from Arduino to RX of GSM module
        *  GND pin from GSM to GND of Arduino..

MAKE SURE THAT U HAVE CONNECTED ALL PINS CORRECTLY !!

If yes ! .. your hardware interfacing is Over .. !

Now , about the Arduino program. We have a library already in Arduino called GSM.h .
Here , we are not using that because we are not using GSM shield so there is a chance of error..
This method is simple though . we use simple "Serial" function and "String"  function.

Program for receiving a message ..follows ..
////////////////////////////////////////////////////////////////////////////////////////////
String inchar;
void setup()
{
Serial.begin(9600);
Serial.println("Message receiver Testing");
Serial.println("AT\r");
delay(1000);                 
Serial.println("AT+CMGF=1\r");   
delay(1000);
Serial.println("AT+CNMI=1,2,0,0\r");
}
void loop()
{
while (Serial.available())
{
 inchar = Serial.readString();
Serial.println(inchar);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////

LOGIC OF program 
* Declare "inchar" as string for receving the message 
* In setup() set your GSM for incoming message mode
* AT commands are used for this . (not going detail in to AT commands )
* In loop() , if a data is available from serial port, it will be saved to "inchar" 
* "inchar" is displayed using Serial.println command

/////////////////////////////////////////////////////////////////////////////////////////////////////
another code for the same ..


void SMS_Read();
String inchar;
void setup()
{
Serial.begin(9600);
Serial.println("Message receiver Testing");
Serial.println("AT\r");
delay(1000);                 
Serial.println("AT+CMGF=1\r");   
delay(1000);
Serial.println("AT+CNMI=1,2,0,0\r");
}
void loop()
{
while (Serial.available())
{

 inchar = Serial.readString();
if(inhar.indexOf("+CMTI:") >=0)
{
SMS_Read();

}
}
}
void SMS_Read()
{
delay(500);
Serial.println("AT+CMGR=1\r");
delay(5000);
return;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
and if you want to turn on an LED by sending and sms "ledON" ...try this..

While receiving an sms you will get details like date, time, sms, sms sender number etc...you can check this in the serial terminal .. so what we need to do is to check whether the incoming data has got the required data.. 
for example if you want to turn on an led by sendin sms "ledON" ... try this program code


void SMS_Read();
String inchar;
void setup()
{
pinMode(ledPin1,OUTPUT);
Serial.begin(9600);
delay(10)
;Serial.println("AT\r");   
delay(1000);                  
Serial.println("AT+CMGF=1\r");    
delay(1000);

Serial.println("AT+CNMI=1,2,0,0\r");
}
void loop()
{
while (Serial.available())
{
 delay(3); 
 d = Serial.readString();
 if(d.indexOf("ledON") >=0)
   {
  digitalWrite(ledPin1,HIGH); 
  Serial.println("light one is ON");
    }
if(d.indexOf("+CMTI:") >=0)
{
SMS_Read();

}
}
void SMS_Read()
{
delay(500);
Serial.println("AT+CMGR=1\r");
delay(5000);
return;

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


These two programs worked for me in Arduino UNO .. Hope you will find this helpful :) 
Queries accepted ..willing to help if your problem is inside my range of knowledge :)  







Comments

  1. Sir, I want to print only the message content to the lcd 16*2 , I tried a lots of but my every attempt was failed . I m using gsm sim900 module not a shield, are u having any code for my problem? thank u in advance..

    ReplyDelete
    Replies
    1. Hi siddharaj.. Sorry for the late reply .. was busy with some work related schedules.. You got it working ??? Or you still stuck with that?? Let me know..glad to help.. mail me for any help.. nikhiljoji@gmail.com.. Sorry again for late response..
      Regards ,
      nikhil

      Delete
  2. thx for the code...... but is it posible to get all of the code please
    i cant make it work.. i use a uno

    i changed some of it to work with softwareserial but there is a lot of var that is not defined / declared.

    ReplyDelete
  3. well i got it to work....

    #include SoftwareSerial.h
    #include LiquidCrystal.h

    int unoRxPin = 2; // connected to Tx pin of the GPS
    int unoTxPin = 3; // connected to Rx pin of the GPS
    SoftwareSerial ss(unoRxPin, unoTxPin);

    LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
    int backLight = 13; // pin 13 will control the backlight
    int ledPin1 = 11;
    String d;

    void SMS_Read();
    String inchar;

    void setup()
    {
    //DisplaySetup
    pinMode(backLight, OUTPUT);
    digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
    lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
    lcd.print("Starting up"); // start with a "blank screen"
    //

    pinMode(ledPin1,OUTPUT);
    Serial.begin(9600);
    ss.begin(9600);
    delay(10);
    Serial.println("AT\r");
    ss.println("AT\r");

    delay(1000);
    Serial.println("AT+CMGF=1\r");
    ss.println("AT+CMGF=1\r");

    delay(1000);
    Serial.println("AT+CNMI=1,2,0,0\r");
    ss.println("AT+CNMI=1,2,0,0\r");

    Serial.println("Ready To Send");
    lcd.setCursor(0,0); // set cursor to column 0, row 0 (the first row)
    lcd.print("Ready to send");

    }
    void loop()
    {
    while (ss.available())
    {
    delay(3);
    d = ss.readString();
    if(d.indexOf("LedON") >=0)
    {
    digitalWrite(ledPin1,HIGH);
    Serial.println("light one is ON ");
    lcd.setCursor(0,0); // set cursor to column 0, row 1 (the first row)
    lcd.print("Pin 11 is on");
    lcd.setCursor(0,1); // set cursor to column 0, row 1 (the first row)
    lcd.print("SMS OK");
    }
    else if(d.indexOf("LedOFF") >=0)
    {
    digitalWrite(ledPin1,LOW);
    Serial.println("light one is OFF ");
    lcd.setCursor(0,0); // set cursor to column 0, row 1 (the first row)
    lcd.print("Pin 11 is off");
    lcd.setCursor(0,1); // set cursor to column 0, row 1 (the first row)
    lcd.print("SMS OK");
    }
    if(d.indexOf("+CMTI:") >=0)
    {
    SMS_Read();

    }
    }
    }

    void SMS_Read()
    {
    delay(500);

    Serial.println("AT+CMGR=1\r");
    ss.println("AT+CMGR=1\r");

    delay(5000);
    return;

    }

    ReplyDelete
    Replies
    1. Hi Thomas.. Sorry for the late reply .. was busy with some work related schedules..sorry again and glad to here that you get it worked.. if you need help with anything in future .. feel free to contact ..here s my mail id. nikhiljoji@gmail.com ..
      Thanks

      Delete
  4. can i get code for call rejection using gsm module and when call rejects sms directly send to the called party

    ReplyDelete
    Replies
    1. Sorry that I am not a regular visitor. Hope you got your code running fine. nikhiljoji@gmail.com - mail me if you need any help :)

      Delete
  5. I dont see any AT Commands for sending sms above? Isnt it supposted to be AT+CMGS?

    ReplyDelete
  6. Replies
    1. Sorry that I am not a regular visitor. Hope you got your code running fine. nikhiljoji@gmail.com - mail me if you need any help :)

      Delete
  7. Sir please help me I'm trying to modify this program if the message received is LEDON then led will turn on if the message received is LEDOFF then led will turn off and print the status of LED in serial monitor

    ReplyDelete
    Replies
    1. Sorry that I am not a regular visitor. Hope you got your code running fine. nikhiljoji@gmail.com - mail me if you need any help :)

      Delete

Post a Comment

Popular Posts