receive sms sim900 and pic, pic microcontroller sms receiving,receive sms with pic and sim,Receive SMS with AT Commands using Sim900 & PIC Microcontroller
Hello friends, I hope you all are fine and having fun with your lives. Today, I am going to share a new project which is Receive SMS with Sim900 & PIC Microcontroller using AT Commands. I have already posted this same project on Arduino named as Receive SMS with AT Commands using Sim900 & Arduino. In that project, I have interfaced SIM900 module with Arduino but today I am gonna interface it with PIC Microcontroller.

I have also designed a GSM Library for Proteus using which you can easily now simulate your SIM900D module in Proteus. Sim900 Module is used to send and receive SMS and it is widely used in automation and security projects. I have also posted another Project in which I have designed the Proteus simulation of GSM based Home Security System. I have already posted the Send SMS with PIC Microcontroller & SIM900 project in which we have seen How to send SMS from your GSM module to your mobile phone but today we will send SMS from our mobile phone and then will receive this SMS on the SIM900 module and will perform certain action. Forexample you want to turn your Light ON using GSM then what you need to do is to send SMS from your mobile to SIM900 module and when it receives the message, it will do the required task. Moreover if you are working on Sending SMS instead of receiving then you should have a look at How to Send SMS with PIC Microcontroller & SIM900d and if this sending is via Arduino board then there's How to Send SMS with Arduino and Sim900.

I have used PIC18F452 for this project but you can use this code for any PIC Microcontroller but it has to support the serial port. Moreover, I have used MikroC Pro for PIC Compiler to design code for receive SMS with AT commands using SIM900 & PIC Microcontroller. So, let's have a look at How to Receive SMS with Sim900 & PIC Microcontroller using AT command.

Receive SMS with Sim900 & PIC Microcontroller

  • SIM900 module works on Serial Protocol. If you don't know much about Serial Port then you should read What is Serial Port?
  • In serial Port we have two pins for communication, one is named as TX (transmitting) and other as RX (receiving).
  • In order to Receive SMS with Sim900 & PIC Microcontroller using AT command, connections are exactly the same as we did in Send SMS with PIC Microcontroller & SIM900.
  • The SIM900 module I have used for receiving SMS is shown in the below figure:
receive sms sim900 and pic, pic microcontroller sms receiving,receive sms with pic and sim,Receive SMS with AT Commands using Sim900 & PIC Microcontroller
  • If you have a different GSM module then there's no need to get panicked because most of the GSM modules work on Serial protocol so what you need to do is to simply find the TX and RX pins of your SIM900 module.
  • As you can see inthe above SIM900 image that it has TX and RX Pins so you all need to find these pins on your SIM900 module and then connect them with RX and TX pin of your PIC Microcontroller.
    • RX pin of Sim900 into Pin # 25 (TX) of PIC Microcontroller.
    • TX pin of Sim900 into Pin # 26 (RX) of PIC Microcontroller.
  • Moreover, you will also need to common the GND of SIM900 and PIC Microcontroller, if you are using separate power supplies for PIC Microcontroller and SIM900.
  • One more thing you will also need the basic circuit of PIC Microcontroller, which is as follows:
receive sms sim900 and pic, pic microcontroller sms receiving,receive sms with pic and sim,Receive SMS with AT Commands using Sim900 & PIC Microcontroller
  • Here's my design for How to Receive SMS with Sim900 & PIC Microcontroller using AT command on Vero Board:
receive sms sim900 and pic, pic microcontroller sms receiving,receive sms with pic and sim,Receive SMS with AT Commands using Sim900 & PIC Microcontroller
  • The above hardware design on Vero Board is exactly the same and I have done the connections of my SIM900 module with PIC Microcontrolelr on the back side.
Note:
  • The TX Pin of PIC Microcontroller will be connected with RX Pin of GSM Module.
  • The RX Pin of PIC Microcontroller will be connected with TX Pin of GSM Module.
  • GND of both GSM and PIC Microcontroller must be common.
  • Now upload the hex file of the below code in your PIC Microcontroller so that we can Receive SMS with Sim900 & PIC Microcontroller using AT command.
char IncData;
int x =  0;

char RcvdMsg[60] = "";
int RcvdCheck = 0;
int RcvdConf = 0;
int index = 0;
int RcvdEnd = 0;
char MsgMob[15];
char MsgTxt[10];
int MsgLength = 0;

void RecSMS();
void ClearBuffers();
void Config();

void main() {
  TRISC.F7 = 1;
  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize
  Config();

  while(1)
  {
          RecSMS();
  }

}
void RecSMS() // Receiving the SMS and extracting the Sender Mobile number & Message Text
{

        if (UART1_Data_Ready())
        {
          IncData = UART1_Read();
          //UART1_Write_Text(IncData);
          if(IncData == '+'){RcvdCheck = 1;}
          if((IncData == 'C') && (RcvdCheck == 1)){RcvdCheck = 2;}
          if((IncData == 'M') && (RcvdCheck == 2)){RcvdCheck = 3;}
          if((IncData == 'T') && (RcvdCheck == 3)){RcvdCheck = 4;}
          if(RcvdCheck == 4){index = 0;RcvdConf = 1; RcvdCheck = 0;}

          if(RcvdConf == 1)
          {
              if(IncData == 'n'){RcvdEnd++;}
              if(RcvdEnd == 3){RcvdEnd = 0;}
              RcvdMsg[index] = IncData;

              index++;
              if(RcvdEnd == 2){RcvdConf = 0;MsgLength = index-2;index = 0;}
              if(RcvdConf == 0)
              {
                  //PortD.F3 = 1;
                  //UART1_Write_Text("Mobile Number is: ");
                  for(x = 4;x < 17;x++)
                  {
                      MsgMob[x-4] = RcvdMsg[x];
                      //UART1_Write(MsgMob[x-4]);
                  }

                 // UART1_Write_Text("Message Text: ");
                  for(x = 46;x < MsgLength;x++)
                  {
                      MsgTxt[x-46] = RcvdMsg[x];
                      //UART1_Write(MsgTxt[x-46]);
                  }
                  if(MsgTxt[0] == 'A'){ PortD.F0 = 1;}//L1ON();}
                  if(MsgTxt[0] == 'B'){ PortD.F0 = 0;}//L1OF();}
                  if(MsgTxt[0] == 'C'){ PortD.F1 = 1;}//L2ON();}
                  if(MsgTxt[0] == 'D'){ PortD.F1 = 0;}//L2OF();}
                  if(MsgTxt[0] == 'E'){ PortD.F2 = 1;}//L3ON();}
                  if(MsgTxt[0] == 'F'){ PortD.F2 = 0;}//L3OF();}
                  if(MsgTxt[0] == 'G'){ PortD.F3 = 1;}//L4ON();}
                  if(MsgTxt[0] == 'H'){ PortD.F3 = 0;}//L4OF();}
                  ClearBuffers();
              }
          }
     }
}


void ClearBuffers()
{
    strcpy(RcvdMsg,"");
    RcvdCheck = 0;
    RcvdConf = 0;
    index = 0;
    RcvdEnd = 0;
    strcpy(MsgMob,"");
    strcpy(MsgTxt,"");
    MsgLength = 0;
}

void Config()
{
    Delay_ms(2000);
    UART1_Write_Text("ATE0rn");
    Delay_ms(1000);
    UART1_Write_Text("ATrn");
    Delay_ms(1000);
    UART1_Write_Text("AT+CMGF=1rn");
    Delay_ms(1000);
    UART1_Write_Text("AT+CNMI=1,2,0,0,0rn");
    Delay_ms(1000);
}
  • The above code is quite self explanatory but let's have a quick explanation of this code for How to Receive SMS with Sim900 & PIC Microcontroller using AT command.
Code Explanation
  • This code is designed in MikroC Pro For PIC Compiler and its written for PIC18F4520 but you can use it with any PIC Microcontroller which supports Serial Port.
  • I have shown the working of the code in a block diagram in below figure:
receive sms sim900 and pic, pic microcontroller sms receiving,receive sms with pic and sim,Receive SMS with AT Commands using Sim900 & PIC Microcontroller
  • In this code I am using three functions other than the main function, named as:
    • Config();
    • RecSMS();
    • ClearBuffers();
  • First of all, I have used the Config() function, which is doing the Configuration settings of our GSM Module.
  • In these configurations, I am making the Echo OFF and also converting my GSM modem to text messages.
  • In the RecSMS() function, I am checking for +CMT, once its received then I have extracted the SMS body and the mobile number from it.
  • In the ClearBuffers() function, I am clearing all the variables so that we can receive the next SMS.
So, that's all for today. I hope you guys are gonna enjoy this project How to Receive SMS with Sim900 & PIC Microcontroller using AT command. So, will meet you guys in the next tutorial. Till then take care and have fun !!! :)