CONTROLLING HOME APPLIANCES USING SMS

on Saturday, 14 December 2013

Again i'm back with a new project, this is the best project to learn reading data response of gsm module.I'm not gonna deal with unwanted concepts in this project i will make this post straight forward as far as possible..
AIM: The Aim of this project is to switch ON or OFF any home device corresponding to the Short Messaging Service i.e SMS.
 
Basic understanding:
I'm using Pic18f452 micro-controller and MikroC compiler for this project,but basic concept for all other micro-controllers and compilers will be the same. Just understand the concept, there by you can write your own code which suites your compiler.
It is necessary to know  AT commands, if you dint red my previous post then i strictly recommend you to read it.. Clickhere

It is foolish to explain again, so better go to the above link.

Algorithm:
STEP1: Making Echo off and using Text mode

STEP2: Wait for the SMS, if we get the SMS then check for +CMTI:  and save the slot number by which we can know in  which location sms have been saved.(as we have seen in our previous tutorial that next character after comma (,)  we need to save its the slot number).

STEP3: After we received the sms, we need to send this command at+cmgr=<n> in order to read the received message. Here <n> is the memory slot where our sms has been saved.

STEP4: We will get a response as soon as we send the command, so response will be a long string, its look like this

+CMGR: “STATUS”,”OA”,,”SCTS”<CR><LF>Message Body<CR><LF><CR><LF>OK<CR><LF>

We should check for +CMGR: and if it satisfies then we save Message Body in an array, which is after <LF> and before <CR> i.e after 0xa and before 0xd(ascii values of Lf and CR).

>> Now check the received sms with our predefined string and make high or low the corresponding output.

I mean , if the sms is ON 1then microcontroller portd.0 pin to high
ON 2 = pin portd.1 to high
ON 3=pin portd.2 to high
ON 4=pin portd.3 to high and so on...

If  sms received is OFF 1 then make  portd.0 to low
OFF 2=pin pord.1 to low
OFF 3= pin portd.2 to low
OFF 4= pin portd.3 to low and so on..

NOTE: we connect each pin to relay circuit and to home device, whenever micro controller pin is high then device will be switched ON , and if its low then switched OFF.

STEP5:After this, we delete the previous received message and looks again for a new message i.e again to step1.
SOFTWARE CODE EXPLANATION CODE:
I'm explaining only few important modules , other modules of software are self explanatory.

ECHO off and Text mode(algorithm 1st point):
uart1_write_text("ate0");// echo off command
uart1_write(0xd);//enter
delay_ms(40);//wait for some time
uart1_write_text("at+cmgf=1");//command to make text mode
uart1_write(0xd);//enter

Function to receive incoming data:
unsigned char rx_incoming()
{
   while(PIR1.RCIF==0);//if we dint receive data then stay here
   return RCREG;//if we received then take data from RCREG register
}

Checking +CMTI: (Algorithm step 2):
while(storedloc==0)//check until we received the message
{
   gotit= rx_incoming();//what ever we receive taking in 'gotit' variable
   if(gotit=='+')//if we get + then proceed
   {
      gotit= rx_incoming();//again take input
      if(gotit=='C')//if we receive C after + then proceed
      {
         gotit=rx_incoming();//again take input
         if(gotit=='M')//if we received M after +CM then proceed
        {
           gotit=rx_incoming();
           if(gotit=='T')
          {
            gotit=rx_incoming();
            if(gotit=='I')
            {
              gotit=rx_incoming();
              if(gotit==':')
              {
                  gotit=rx_incoming();
                  while(gotit!=',')gotit=rx_incoming();// after (,) we get the memory slot of SMS
                  storedloc=rx_incoming();//save memory slot in vairable 'storedloc'
              }
           }
        }
      }
    }
  }
}

SENDING COMMAND TO READ SMS(Algorithm step 3):
uart1_write_text("at+cmgr=");// its the command to read sms
uart1_write(storedloc);//read from this memory slot
uart1_write(0xd);//enter

EXTRACTING ACTUAL MESSAGE FROM THE RESPONSE(Algorithm step4):
while(yesgot==0)//flag to know whether we stored our message in an array or not
{
  gotit=rx_incoming();//take input
  if(gotit=='+')//check +
  {
    gotit=rx_incoming();
    if(gotit=='C')//check C after +
   {
     gotit=rx_incoming();
     if(gotit=='M')//check M after +C
     {
       gotit=rx_incoming();
       if(gotit=='G')//check G after +CM
       {
         gotit=rx_incoming();
         if(gotit=='R')//check R after +CMG
        {
          gotit=rx_incoming();
          if(gotit==':')//check : after +CMGR
          {
              gotit=rx_incoming();//if all satisfies then store data after 0xa and before 0xd in an
                                                            //   array  message[]
              while(gotit!=0xa){gotit=rx_incoming();}
              for(i=0;gotit!=0xd;i++){gotit=message[i]=rx_incoming();}
              uart1_write_text("at+cmgd=");//delete the message
              uart1_write(storedloc);uart1_write(0xd);
              lcd_out(1,1,message);
              yesgot=1;//set the flag to indicate that we received message
           }
         }
        }
       }
     }
   }
}

Making output corresponding to received message(only ON is shown):
if(message[0]=='O')
{
   if(message[1]=='N')
   {
     if(message[2]==32)//ascii for space
     {
       switch(message[3])
       {
           case '1' : portd.f0=1;break;
           case '2' : portd.f1=1;break;
           case '3' : portd.f2=1;break;
           case '4' : portd.f3=1;break;
        }
    }
  }
}

To download the full code go to the end of the page.
I strongly recommend you to read this post if you want to know how to connect relay to the micro-controller. clickhere

COMPONENTS REQUIRED:

Micro controller -pic18f452
Crystal oscillator - 10Mhz
LCD 16x2
GSM module sim300
RS232 IC
LM7805 voltage regulator
Capacitors (refer ckt diagram)

CIRCUIT DIAGRAMS:

Power Supply:



RELAY CIRCUIT:



GSM CONNECTIONS WITH RS232:


OVERALL CIRCUIT:  


CIRCUIT DIAGRAM FOR CONNECTING GSM WITH PC USING PROTEUS:


You can download the full code here, click on 'sms code' below
smscode

AUTHOR: YOUSTRON SIC
You can be in touch with me through facebook
https://www.facebook.com/youstron.sic

3 comments:

Richard C. Lambert said...

Rinnai Malaysia Do you have a delivery team who is experienced making HOME DELIVERIES of FURNITURE and APPLIANCES?

Anonymous said...

This is very much satisfied by the different info in this article. All the contents too good, can be very helpful for us. I love reading new post and I am always searching for helpful information like this. Thanks for sharing the information. Link

Nancy Garero said...

The blog has offered the great knowledge I really enjoyed a lot by reading thisgermguardian ac4825.

Post a Comment