Home Automation using Arduino and GSM Module

Shone V.J April 7, 2017 5 Comments

In this  article, we are publishing a highly useful home application – GSM based home automation using Arduino. The project consists of a 16×2 LCD module for displaying the status of the home appliances. The status (turn ON or turn OFF) of the connected devices can be changed by sending an SMS from your mobile phone. Upon receiving SMS commands through GSM module, arduino will change the status (turn ON/OFF) of the device that is mentioned in the SMS.

Objectives of the Project

  • Send/Monitor the status of the connected devices to a specified mobile number using GSM.
  • Change the status (turn ON/OFF) of the device upon receiving commands through SMS.
  • Display status of the devices in an LCD using a 16×2 LCD module.

Note: – For demonstration purpose, we are using 3 LED’s to represent 3 different devices. We have also connected a 12Volt dc motor as a 4th device. You can replace these LED’s with actual electrical light bulbs, devices like motor, fan, TV, refrigerator etc. You should use an appropriate supporting circuit to interface these devices to the circuit diagram given below.

Let’s begin to build our project – GSM Based Home Automation Using  Arduino.

Circuit Diagram – Home Automation using Arduino

Circuit_diagram_arduino_home_automation

Assemble the circuit as shown in diagram! Important connections are explained below.

Here we are using SIM900 GSM module. The communication between GSM module and arduino is serial. The problem with this connection is that, while programming Arduino uses serial ports to load the program from the Arduino IDE. If these pins are used in wiring,  the program will not be loaded successfully to Arduino. So you have to disconnect wiring in Rx and Tx each time you burn the program to Arduino. Once the program is loaded successfully, you can reconnect these pins and have the system working.

To avoid this difficulty, I am using an alternate method in which two digital pins of Arduino are used for serial communication. We need to select two PWM enabled pins of Arduino for this method. So I choose pins 9 and 10 (which are PWM enabled pins). This method is made possible with the SoftwareSerial Library of Ardunio. SoftwareSerial is a library of Arduino which enables serial data communication through other digital pins of Arduino. The library replicates hardware functions and handles the task of serial communication. This will help the programmer to use hardware serial pins for debugging purpose.

Note: Read our complete tutorial on Interfacing GSM Module to Arduino.

Another section of the circuit is the interfacing of Arduino to 16×2 LCD. JHD162A is the LCD module used here. JHD162A is a 16×2 LCD module based on the HD44780 driver from Hitachi. The JHD162A has 16 pins and can be operated in 4-bit mode (using only 4 data lines) or 8-bit mode (using all 8 data lines). Here we are using the LCD module in 4-bit mode. To facilitate communication between Arduino and LCD module, we make use of a built-in library in Arduino <LiquidCrystal.h> – which is written for LCD modules making use of the Hitachi HD44780 chipset (or a compatible chipset).

In order to demonstrate the working of the home appliances, and  awe are using three LED bulbs and a DC motor. These are connected to A0- A3 pins of Arand amicro buzzer is connected to A4. The buzzer will produce a beep sound upon receiving a valid SMS. Here we are using a 12V DC motor, which can’t be driven directly from Arduino board (arduino has very limited current capabilities). So a BC547 transistor is used here to drive the DC motor.

Program/Code – Home Automation using Arduino

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>


#define bulb_1Pin A0
#define bulb_2Pin A1
#define bulb_3Pin A2
#define fanPin A3
#define buzzerPin A4

LiquidCrystal lcd(13,12,5,4,3,2);
SoftwareSerial gsm(10,11);

int i=0;
String incomingString="", device="";
char character,buf[100]=""; 
boolean bulb_1Status = false, bulb_2Status = false, bulb_3Status = false, fanStatus = false;
long previous_millis=0;

void setup()
{
 
 Serial.begin(9600);
 lcd.begin(16,2);
 gsm.begin(9600);

pinMode(bulb_1Pin,OUTPUT);
 pinMode(bulb_2Pin,OUTPUT);
 pinMode(bulb_3Pin,OUTPUT);
 pinMode(fanPin,OUTPUT);
 pinMode(buzzerPin,OUTPUT);

digitalWrite(bulb_1Pin,LOW);
 digitalWrite(bulb_2Pin,LOW);
 digitalWrite(bulb_3Pin,LOW);
 digitalWrite(fanPin,LOW);
 digitalWrite(buzzerPin,LOW);

lcd.clear();
 lcd.setCursor(0,0);
 lcd.print(" GSM BASED ");
 lcd.setCursor(0,1);
 lcd.print(" HOME AUTOMATION");
 delay(2000);
 
}

void send_deviceStatus()
{

gsm.println("AT+CMGF=1");
 buzzer(2);
 gsm.println("AT+CMGS=\"+918129613637\"\r");
 buzzer(2);
 
 gsm.print("BULB 1 ");
 if(bulb_1Status==true)
 gsm.println("ON \n");
 if(bulb_1Status==false)
 gsm.println("OFF \n");


gsm.print("BULB 2 ");
 if(bulb_2Status==true)
 gsm.println("ON \n");
 if(bulb_2Status==false)
 gsm.println("OFF \n");


gsm.print("BULB 3 ");
 if(bulb_3Status==true)
 gsm.println("ON \n");
 if(bulb_3Status==false)
 gsm.println("OFF \n");


gsm.print("FAN ");
 if(fanStatus==true)
 gsm.println("ON \n");
 if(fanStatus==false)
 gsm.println("OFF \n");
 buzzer(2);
 
 gsm.println((char)26);
 buzzer(2);

gsm.println("AT+CNMI=2,2,0,0,0");
 buzzer(2);
}

void buzzer(int k)
{
 int j;
 for(j=0;j<k;j++){
 digitalWrite(buzzerPin,HIGH);
 delay(500);
 digitalWrite(buzzerPin,LOW);
 delay(500);}
}

void loop()
{
 
top: 
 lcd.begin(16,2);
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print(" DEVICE STATUS ");

gsm.println("AT+CNMI=2,2,0,0,0");



 while(1)
 {

incomingString="";
 digitalWrite(bulb_1Pin,bulb_1Status);
 digitalWrite(bulb_2Pin,bulb_2Status);
 digitalWrite(bulb_3Pin,bulb_3Status);
 digitalWrite(fanPin,fanStatus);


 
 i=0;
 while(i<4){
 if(millis()-previous_millis > 2000)
 {
 previous_millis = millis();

if(i==0){
 lcd.setCursor(0,1);
 lcd.print(" BULB 1 ");
 if(bulb_1Status==true)
 lcd.print("ON ");
 if(bulb_1Status==false)
 lcd.print("OFF ");}

if(i==1){
 lcd.setCursor(0,1);
 lcd.print(" BULB 2 ");
 if(bulb_2Status==true)
 lcd.print("ON ");
 if(bulb_2Status==false)
 lcd.print("OFF ");}

if(i==2){
 lcd.setCursor(0,1);
 lcd.print(" BULB 3 ");
 if(bulb_3Status==true)
 lcd.print("ON ");
 if(bulb_3Status==false)
 lcd.print("OFF ");}

if(i==3){
 lcd.setCursor(0,1);
 lcd.print(" FAN ");
 if(fanStatus==true)
 lcd.print("ON ");
 if(fanStatus==false)
 lcd.print("OFF ");}
 i++;
 }
 


if(gsm.available())
 {
 if(gsm.read()=='*')
 {
 while(!gsm.available()){}
 
 incomingString += gsm.readStringUntil('#'); 
 // Serial.println(incomingString); 
 }
 
 }

if(incomingString.equals("B1ON")) { lcd.setCursor(0,0); lcd.print(" SMS RECEIVED "); lcd.setCursor(0,1); lcd.print(" BULB 1 ON "); buzzer(5); bulb_1Status = true; goto top;}
 if(incomingString.equals("B2ON")) { lcd.setCursor(0,0); lcd.print(" SMS RECEIVED "); lcd.setCursor(0,1); lcd.print(" BULB 2 ON "); buzzer(5); bulb_2Status = true; goto top;}
 if(incomingString.equals("B3ON")) { lcd.setCursor(0,0); lcd.print(" SMS RECEIVED "); lcd.setCursor(0,1); lcd.print(" BULB 3 ON "); buzzer(5); bulb_3Status = true; goto top;}
 if(incomingString.equals("FON")) { lcd.setCursor(0,0); lcd.print(" SMS RECEIVED "); lcd.setCursor(0,1); lcd.print(" FAN ON "); buzzer(5); fanStatus = true; goto top;}
 
 if(incomingString.equals("B1OFF")) { lcd.setCursor(0,0); lcd.print(" SMS RECEIVED "); lcd.setCursor(0,1); lcd.print(" BULB 1 OFF "); buzzer(5); bulb_1Status = false; goto top;}
 if(incomingString.equals("B2OFF")) { lcd.setCursor(0,0); lcd.print(" SMS RECEIVED "); lcd.setCursor(0,1); lcd.print(" BULB 1 OFF "); buzzer(5); bulb_2Status = false; goto top;}
 if(incomingString.equals("B3OFF")) { lcd.setCursor(0,0); lcd.print(" SMS RECEIVED "); lcd.setCursor(0,1); lcd.print(" BULB 1 OFF "); buzzer(5); bulb_3Status =false; goto top;}
 if(incomingString.equals("FOFF")) { lcd.setCursor(0,0); lcd.print(" SMS RECEIVED "); lcd.setCursor(0,1); lcd.print(" FAN OFF "); buzzer(5); fanStatus = false; goto top;}

if(incomingString.equals("STATUS")) { lcd.setCursor(0,0); lcd.print(" SMS RECEIVED "); lcd.setCursor(0,1);lcd.print(" SENDING STATUS ");send_deviceStatus(); goto top;}


}  } }

The program continuously checks the availability of serial data from GSM module. When a new message arrives, GSM module will pass it to the controller by using software serial method. The controller will decode the command from the arrived data and store it in a string variable named “incomingString”. The decoding is done by simply taking the string that comes between the characters ‘*’ and ‘#’. The sender must send all the commands in between these characters for getting a proper response from the controller. The “incomingString” variable is then compared with the saved strings(for eg. “B1ON”) by using an inbuilt function in Arduino (  incomingString.equals(“B1ON”); ). If the output of this function is true, then the status of the device mentioned in the message will be altered accordingly.

We can check the status of all devices from anywhere in the world by simply sending the word “*STATUS#”. When the controller receives the word, it will send the status of all the connected devices to a specified mobile number. A user-defined function namely “send_deviceStatus()” is declared in the program for doing this. Upon receiving the keyword, the program will call this function. The necessary AT commands for configuring GSM module were written in it along with the mobile number.

Comments
  • mary
    July 11, 2017

    what command can you use to turn on all the devices

  • mai
    May 7, 2017

    What would prevent others from sending commands? In there is no encryption, what prevents me from sending commands to your number? Just curios.

    • mai
      May 7, 2017

      By encryption, I mean security.

    • jojo
      May 7, 2017

      @mai – You are right.

    • Shone Vj
      May 9, 2017

      commands that alter the device status are set during the programming time.it is known only by the programmer.So in that way it is already secure. You increase it by making the device respond only to the messages from a particular number. This can be acheive by adding a few more steps .Firt you must store that num inside a string variable. Then compare it with the num from wich the command received.

Leave a Reply

Your email address will not be published. Required fields are marked *