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


In general the microcontroller gives output as 5v, but in order to switch ON any home device we require 230v AC , so to get this we use device 'Relay'. Relay acts as mechanical switch. 
Let’s have a look.. 

Relay: 

I don’t want to make you bore, i just touch few topics which are essential for us in our next project.
There are different types of relays have been in the market , just we only concern with the SPST type of relay in this section.
I said you before Relays are used as Switch right, now let us look at its pins, working and connections.
The basic symbol  look something like this


It is similar to normal switch symbol, but a coil on its left. When we excite coil (i.e when we give certain voltage to coil) then point 'A' will be connected to point 'B'.
Relay which we are using is ‘SPST 5v DC’ rated relay, then we need to give 5v Dc to make coil to excite.
To know how it actually  work as switch,lets  analyze the below image



>>In the above fig,  1,2,3,4,5  are pins of the relay.
1 and 2 = coil pins,
3= common pin
4= Normally open pin(No)
5=Normally closed pin(NC)
'(i.e NC pin 5 always connected to pin 3 inside the relay so we will leave that pin(5) unconnected outside)


After excitation of coil the pin 3 will be connected to pin 4 leaving pin5 alone.

>> To excite the coil we need to connect coil to microcontroller pin through diode and transistor as shown above. It means if mc pin is high(1) then coil will be excited and switch will be ON if not OFF.

>>Observe the pins 5 and 4 , 4 is connected to the +ve of main power supply and 3 is connected to the +ve of our device(i.e bulb)

>> Connect -ve of the main power supply directly to the -ve of our device(i.e bulb).

>> It means if the microcontroller pin is high(1) > coil will be excited>switch will be on(i.e 3 and 4 will be shorted)> +ve of main power is connected to +ve of device(i.e bulb) > current will be flow though the device(i.e bulb) and it makes ON if not then OFF.

In this way it acts as a switch. Now we got a bright picture in our mind how it works.
Below fig shows the bottom view of the relay(spst 5v)



Observe two pins on right hand side and 3 pins on left hand side from the above fig. (i.e one common pin, two coil pins, one NC, one NO).
The problem with it is, we should know which pins are Coil pins and which one are switch pins and which one is common pin.

Finding pins of the Relay(Read note below before reading these steps): 

We can find which pin is what by using Mulitmeter. Les see how to find them step by step.

>> switch your multimeter to the continuity mode

>> Touch one of the probe of multimeter(i.e red wire or black wire of multimeter)  to any one of the right side pins of relay.

>>Now one wire of multimeter left unconnected, so connect it  to left hand side pins one by one(i.e left side have 3 pins, touch multimeter wire to each pin one after the other) until we get beep sound.

>>If you dint get any beep sound then, multimeter probe which is connected to right hand side pin, remove it from that pin  and connected to other remaining pin of the right hand side. Again check other probe connecting to left side pins one by one until you get beep sound

>> Beep sound  at which pin(left side) we get, it is pin no. 3 and  pin which is connected to the multimeter on the right side is pin no. 5

>>We found one pin on the right hand side(i.e pin5) and remaining pin which is on the right side is pin 4

>>we found pin no. 3,4,5.. now remaining pins on left side are the pins 1 and 2 i.e coil pins, you can treat any pins as 1 and 2  or 2 and 1...

If you feel its difficult for you to follow above steps, just assume pin numbers as below..


[ Most important note: The section "Finding the pins of the relay" is strictly i have explained by keeping in mind  of 'spst 5v rated relay' , for other relays they may have different pins in right and left sides..  so to know the pins of them the basic concept is same but pins on left side and right side may differ. Better dont use these steps for knowing pins of other relays]


General Steps to find pins of any relay(you can skip these steps):

>> Set the multimeter in resistance mode
>>Only coil pins show non zero resistance value.
>>Remaining pins show either zero or infinite value.
>> Where we get beep sound they are common pin and Normally closed pin>> Check all pins using multimeter by trail and error method..
AUTHOR: YOUSTRON SIC





Hi guys , i'm back with the Gsm(SIM 300) tutorial. In this tutorial I will be explaining how to send and receive sms by interfacing gms module with microcontroller.
I have used PIC18f452 microcontroller and mikroC as a vehicle to explain these concepts but the concept will be the same for all compilers and microcontrollers.
Note, i'm just writing as easy as possible for you to understand better, so this tutorial is limited.
In this tutorial mainly i will deal with few AT commands for sending , receiving, extracting the actual message, deleting the message. To understand practically we will look at one of the projects of gsm in the next post. Project which we do is the "Controlling home appliances by simple SMS ".

Basic Understanding:
As we know GSM stands for Global System for Mobile communication. By using Gsm module we can handle all the basic operations of any mobile phone i.e making a call , receiving a call, sending sms , receiving sms and so on..

It supports AT commands to deal with all these operations. There are many AT commands which are supported by SIM 300 GSM module. SIM 300 is one of its type, which available easily in the market. SIM 900 is its advanced version. Most of the commands which supports in SIM 300 also support for SIM 900 but few of them varies. So to know all the AT commands which are supported by your device better refer its datasheet.This tutorial is based upon SIM 300 type. So let’s see one by one.

BASIC TEST:

Just to check whether our device working or not we send this AT command
AT()
If we get the response as below then it’s working all right
OK

ECHO:

>whenever we sends any character to the module it retransmits back, i.e if we connect gsm module to our computer or micro controller(or any other device) whenever we sends any data then it reflects back to computer or micro controller(or any other device).
If we dont want GSM module to retransmits back what we have sent , an AT command to do this.
ATE0 < enter>
If we want to enable the ECHO i.e we want gsm module to retransmit the data which we send then
ATE1< enter>
We should send the ascii value for ENTER i.e 0xd(in hex) , now to disable the ECHO "ATE0 0xd" and for enable "ATE1 0xd"
Note: While we are using computer, then we just press "Enter key"() for Enter, but while using microcontroller we send 0xd (i.e its ascii value).

TEXT MODE:

There are two modes to get the data when we read any message; they are in TEXT MODE and PSU MODE. In text mode data will be similar to that of our conventional readable type, but in PSU mode its in hex code (ex: 132543682A2D456..).

To set our module to TEXT MODE we use this command

AT+CMGF=<mode><enter or CR>
if mode =1 then TEXT mode
=0 then PSU mode
Enter or CR = 0xd
Example: To make the Text mode , AT=CMGF=1()

Passing Mobile Number To Send SMS:

while sending any message we send the mobile number to our gsm module. AT command to do so is
AT+CMGS="mobile number" ()
mobile number= number to which we want to send sms
semi colon         = 0x22 Ascii value
Enter ()             = 0xd

STEPS FOR SENDING SMS:

AT()
after some milli sec delay
ATE0()
after some milli sec delay
AT+CMGF=1()
after some milli sec delay
AT+CMGS="XXXXXXXX"()
after some milli sec delay
Enter message body which you want to send , press ctrl+z after complition. Ascii code for ctrl+z is 26(decimal)
Example: we want to send SMS to the number "1234567890" and message body to send is "how are you my friend".
AT()
after some milli sec delay
ATE0()
after some milli sec delay
AT+CMGF=1()
after some milli sec delay
AT+CMGS="1234567890"()
how are you my friend ctrl+z

Important Notes:
1.We need to note few important points here, in the above steps for sending sms, first three commands we use only for the first time while sending sms.
For example we want to send 10 messages then its not necessary to use first three commands(AT,ATE0,AT+CMGF) for all 10 messages, we only send them for the first message, from the 2nd message we only send from step 4(AT+CMGS="1234567890"() ).
2. When we send any command then gsm module will respond it back i.e if we send command 'AT' then response will be 'OK', above we neglected the response from the module that is the reason we use delay after every command.

RECEIVING SMS:


STEP1: When a text message (SMS) arrives on SIM300 GSM Module it sends a response as
 <CR><LF>+CMTI: <mem>,<n><CR><LF>
To confirm whether we received SMS or not we need to check +CMTI: continuously.
Here <n> is the memory slot at which SMS have been saved. So we need to save character which comes after comma (,)

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


STEP3: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). 



Based on this tutorial we will look at a project "Controlling home appliances by simple SMS " in our next post.
AUTHOR: YOUSTRON SIC