Saturday, 7 February 2015

SMS BASED HOME AUTOMATION SYSTEM

SMS BASED HOME AUTOMATION SYSTEM

Hello readers, in this article I am discussing with you about the art of designing SMS based home automation system.  SMS based control of appliances is a very popular application and the one with very much utility for different domains. Sometimes it becomes difficult to imagine and believe that your mobile phone can act as a remote for the commonly used home appliances and you are always connected to your home through your mobile phone no matter in which part of the world you are. SMS service can be used to control different appliances of our home and for that it is quite obvious that there must be an intelligent system installed at home which will receive the sms, read its content and finally will be able to perform the desired action for switching on/off the appliances. This type of intelligent system can be easily designed using microcontroller, gsm modem and some electro-mechanical relays. Fig. below shows a simple block diagram of the home automation system design using arduino uno development platform.    

Block Diagram





























Working

GSM module (SIM900) is interfaced with arduino using serial pins (0 and 1).  AT commands are sent to gsm modem for configuring it in sms mode and also to route the new incoming message to the serial reception pin of the arduino. The incoming message is decoded by the arduino and is compared with the predefined codes. If the match is found then the corresponding action for that code will be performed by the arduino. The following codes are used-

     D1N----------device 1 on
     D1F----------device 1 off
     D2N----------device 2 on
     D2F----------device 2 off
     DAN----------all devices on
     DAF----------all devices off 

If the received message contents are D1N then relay 1 will be triggered through BC547 transistor and device 1 is switched on, here transistor BC547 acts as a relay driver and it drives the SPDT relay. Similarly for other codes the corresponding programmed functions are performed.

RELAY

A relay is an electrically controllable switch widely used in industrial controls, automobiles, and appliances.

Fig. below shows a SPDT relay-


As shown in the above figure a SPDT relay has 5 terminals out of which two are for the coil, one for common, one for normally open contact and one for normally closed contact. When the coil terminal is energized then due to the magnetic force the common terminal contact is pulled from normally closed position to normally open position. A simple npn transistor can be used to energize the coil terminals of the relay.

Fig. below shows the connection diagram of BC547 transistor with the SPDT relay for controlling a lamp. A button is connected at the base terminal with a resistor R2. When the button is pressed then the emitter base junction gets forward biased and transistor switches to “ON” state. In this state the relay coil gets energized and the common terminal of the relay gets shifted to the normally open terminal where a lamp is connected. The common terminal is connected to the voltage at which it is desired to operate the device which is connected on the normally open terminal. So the device will remain in the “ON” state till the button is in the pressed state. When the button is released then the emitter base junction will not get the enough forward bias voltage to turn the transistor to “ON” state and because of this the transistor switches to off state and the relay coil will get de- energized. The magnetic force collapses and the common terminal of the relay is pulled back to the normally closed position and the device switches to “OFF” state.     
































AT COMMANDS to initialize GSM modem

     AT+CNMI, New Message Indications to TE

This command selects the procedure, how receiving of new messages from the network is indicated to the DTE when DTE is active.

AT+CNMI=[<mode>[,<mt>[,<bm>[,<ds>[,<bfr>]]]]]

Here I have used the command-

AT+CNMI=2,2,0,0,0

<mode> value here is 2  which indicates buffer unsolicited result codes in the modem when modem-DTE link is reserved (e.g. in on-line data mode) and flush them to the DTE after reservation. Otherwise forward them directly to the DTE.

The rules for storing received sms depends on its coding scheme, preferred memory storage (using command at+cpms) settings and the value of <mt> field that is specified in the at+cnmi command.

Here I have used <mt> field value as 2 and this indicates that SMS-DELIVERs (except class 2 messages and messages in the message waiting indication group (store message)) are routed directly to the DTE
using unsolicited result code +CMT:

<bm> field is et to 0 and this indicates that no CBM (Cell Broadcast Message) indications are routed to the DTE.

<ds> field is also set to 0 and this indicates that no SMS-STATUS-REPORTs are routed to the DTE.

<bfr> field is set to 0 and this means modem buffer of unsolicited result codes defined within this command is
          flushed to the DTE when <mode> 1 - 3 is entered (OK response shall be given before flushing the codes)

      AT+CMGF, SMS Format

AT+CMGF=<mode>
mode = 0: PDU mode
mode = 1: Text mode
Here I have used the command AT+CMGF=1 and this configures the modem in the sms mode.

Circuit Diagram






























CODE

// code for the home automation using arduino
// gsm module sim900 is used with arduino uno atmega328
// written by  UMESH DUTTA

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(12, 11, 7, 6, 5, 4);

//temporary variable to store the contents of the received sms

char temp[50];
int m;
int inByte =0;        
void setup()
{
  // initialize serial communication with baud rate of 9600 bps
  Serial.begin(9600);
  // wait for a while till the serial port is ready
  delay(100);
// configure the gsm modemin sms mode   
  Serial.print("AT+CMGF=1\r");
  delay(500);
 // route the contents of the received sms on to serial port
  Serial.print("AT+CNMI=2,2,0,0,0\r");
 // initialize 20X4 the lcd
  lcd.begin(20, 4);
  //print inittial message on lcd
  lcd.print("**Device Control..**");
  //define pins 2 ad 3 as o/p pins
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  // make default status of pin 2 and 3 low
  digitalWrite(2,LOW);
  digitalWrite(3,LOW);
  delay(500);
}
//  loop function starts here
void loop()
{
  // clear the temporary variable temp
    m=0;
    for(m=0;m<45;m++)
      {
        temp[m]=0;
      }
   // wait for the reception of the message
   // " is a identifier for the start of the message
    do
    {
        while ( !Serial.available() );   
    } while ( '"' != Serial.read() );

   // read the sms which will have date, time, sms body and the senders number
      for(m=0;m<45;m++)
      {
        while ( !Serial.available() );
        inByte = Serial.read();
        temp[m]=inByte;
      }

      // set the cursor position of the lcd
      lcd.setCursor(0,1);

      // after testing it is found that the main body of the sms willl start from psition 42 ie. for m=42
      // I have used 3 digit code for the sms and so position 42, 43 and 44 will contain the main body of the sms
      // print the received sms main body on the lcd screen
    
      for(m=42;m<45;m++)
      {
        lcd.print(temp[m]);
      }

     //now write different conditions and required actions
    
    /*codes are
     D1N----------device 1 on
     D1F----------device 1 off
     D2N----------device 2 on
     D2F----------device 2 off
     DAN----------all devices on
     DAF----------all devices off 
     */    
     // device 1 is connected to pin 2 and device 2 is connected to pin 3 of the arduino
   
      if(temp[42]=='D' && temp[43]=='1' && temp[44]=='N')
      {
        digitalWrite(2,HIGH);
        lcd.setCursor(0,2);
        lcd.print("device 1 on");
      }
     
      if(temp[42]=='D' && temp[43]=='1' && temp[44]=='F')
      {
        digitalWrite(2,LOW);
        lcd.setCursor(0,2);
        lcd.print("device 1 off");
      }
     
      if(temp[42]=='D' && temp[43]=='2' && temp[44]=='N')
      {
        digitalWrite(3,HIGH);
        lcd.setCursor(0,2);
        lcd.print("device 2 on");
      }
     
      if(temp[42]=='D' && temp[43]=='2' && temp[44]=='F')
      {
        digitalWrite(3,LOW);
        lcd.setCursor(0,2);
        lcd.print("device 2 off");
      }
     
      if(temp[42]=='D' && temp[43]=='A' && temp[44]=='N')
      {
        digitalWrite(2,HIGH);
        digitalWrite(3,HIGH);
        lcd.setCursor(0,2);
        lcd.print("all devices on");
      }
     
      if(temp[42]=='D' && temp[43]=='A' && temp[44]=='F')
      {
        digitalWrite(2,LOW);
        digitalWrite(3,LOW);
        lcd.setCursor(0,2);
        lcd.print("all devices off");
      }
      delay(1500); // give some delay before the next cycle starts
      lcd.clear(); // clear the lcd screen
      lcd.print("**Device Control..**");// print this message on lcd  
}

Advancements

The project can be extended by including the number sensitive feature in which SMS trigger from a particular mobile phone number will be accepted. For sending the SMS using android mobile phone an application can be designed for easy user interface. The system can also be equipped to give feedback messages indicating the status of all the appliances on demand and also on the successful execution of the issued command. I will discuss the app designing fundamentals for sending the SMS in my upcoming articles.

THANKS FOR READING

UMESH DUTTA

6 comments:

  1. very simple and working code

    ReplyDelete
  2. Hi!
    please can you help me
    I tried to compile this code to my arduino uno but is not responding when i send switching on command to gsm module.
    can you tell me where there is mistake?
    send to me working codes please
    you can email me aqually83@gmail.com

    ReplyDelete
  3. the code is not working not responding. the gsm module not put the data on to arduino

    ReplyDelete
  4. which place mobile number add pls sent

    ReplyDelete
  5. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
    Home Automation in Chennai
    smart home in Chennai
    Home security in Chennai
    Burglar alarm in Chennai
    Door sensors Chennai

    ReplyDelete