Click here to Skip to main content
13,863,642 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

21.1K views
1.9K downloads
24 bookmarked
Posted 7 Aug 2016
Licenced CPOL

HASSI- Home Automation, Safety And Security With IoT Using Arduino

Rate this:
Please Sign up or sign in to vote.
Face Recognition based secured home, ThingSpeak based monitoring, Mqtt based control, Gmail based Notification With Low Cost Arduino UNO and C#

Overview

They say "innovation starts at home" ( well, ok I changed charity with home. But then!)

Every DiY enthusiast at least try to create some solutions or product or application that he can use in his home. therefore home automation has been one of the popular areas of Embedded developers and DiY guys.

When the early IoT adoptation started, the companies reached out these DiY guys, commonly referred as the makers community. So, the makers commkunity was the one who got their hands into early IoT devices. Needless to say that home automation was again at the core of innovation, development and early solution. Being a DiY guy and my wife Moumita being a circuit makergirl it was not a hard choice for us to decide which project we would chose if we had to chose one in DiY category. It had to be home automation.

People with a liking for history may know that in last IoT tutorial held by codeproject, I worked mainly with Arduino, creating my own services and working with predominantly HTTP protocol for connectivity. This time, we have teamed up to create bigger and better solution to more critical problems.

Moumita has always been developing all the circuits I ever worked with. But this year she has started writing and coding. That has given me an immense amount of strength to take up more complicated projects.

So, when we decided to work on Home automation, we had to brainstrom about a good product which we would like to work on to. We first detailed out the aspects of home automation.They are:

  • Home Monitoring- deploying sensors around home for observing various parameters like light, temperature
  • Home Control- Controlling AC, Fan, Light remotely
  • Home Automation- Automating the devices, like switching on bulb when it is dark
  • Home Security- protecting the home from thieves and instruders
  • Home Safety- making the home safer by preventing it from fire

Instead of working with a single concept, we decided to build a solution that demonstrated each of these aspects. While writing a beginners tutorial, it is always a good idea to work into something that cover almost complete areas of a domain.

Therefore in this tutorial we are going to share with you a powerful framework of Home Automation, monitoring , Safety andSecurity from a single app.

In the past GSM based home control system been USED for remote monitoring and control. Generating a SMS for every control is not feasible as messages are costly. Also GSM doesn't provide any visualization support which means that the variation in data can not be observed in graphical format.

Internet of things has been of the recent growing trends in hardware technology. IoT enables the devices to connected to internet. Therefore controlling and monitoring can be performed over a large range of devices like mobiles, tablets, pc and so on. Also, as this tutorial was planned for Codeproject's IoT tutorial contest, IoT had to be the core part of it.

Therefore we extend our framework with a complete cloud integration with Google and ThingSpeak, a messaging gateway integration with Mqtt and call this framework HASSI.

Detailed Functionality

The project provides authentication using Face Recognition based on Eigen based PCA face recognition. Lock and Unlock commands of home are connected to face authentication. If an unathenticated user attempts to open the door without face authentication, a gmail notification is generated with his face photo. In case of a fire at home, the system also generates a gmail message which can be further channelized through IFTTT.

Therefore the work has three features

1)Home Monitoring:In the home monitoring system we have a temperature sensor, the temperature sensor value can be remotely checked with a think speak channel.So the values can be seen as a graph remotely.

2) Home Safety And Alarm system:When the temperature sensor value is exceeds above 40 then the notification is generated to your gmail and also locally. By receiving this mail you can take approprite decision, you can further forward this mail to many other channels using IFTTT.

3)Home Control:Where we implement Lighting and fan control system( small DC fan and small LED series light!) using our mobiles with Mqtt protocol

4) Home Security:

Fig: Block Diagram of the project

The HASSI framework offers opening of the door lock using your face. So, user needs facial verification in order to be able to open the lock. Any intruder trying to unlock the home without facial verification will be snapped and the picture along with alarm will be sent to owner's gmail account.

We are going to use C# as the frontend for user interaction with the physical world and physical world's gateway to the cloud. We will use Arduino C program in Arduino to communicate with this C# UI with serial communication.

Now these are the ways the software works

When you sit in front of the system,the software is going to authenticate user through face recognization.For that you need to register your face and you also require administrative password. You give the correct administrative password then only your face is registered. It shows that you are in the frame. So immidiately after adminsitrative authenticationyou can go with facial verification. Facial recognition is required for opening your home door lock.

If your face is not recognised then the software is displayed unknown message.The software is giving a voice message that some unknown person is trying to open the door.And the software also send a message to gmail id by adding the photo of the unauthorised person.Therefore that software not only generate a voice message but also take a photograph at the same time

Before you start any home control system through the software you need to give the Arduino port. Arduino is connected with the PC with the virtual port. Different virtual port will be there ,but the software will show the connected port.

When you start monitoring the temperature,the software will show the temperature with a simple linechart right on your C# UI form.

The data is also available in the ThingSpeak channel,which displays temperature values with a graph so that you can monitor it remotely from anywhere in the world.

If the sensed temperature cross 40 degree celcious mark (which can be simulated bytaking a candel stick and hold it nearer to the temperature sensor)then software displays the message locally on the software and also a message will be sentto the gmail account. A buzzer is triggered locally so that anyone in home knwos about the event.

We are communicating with the MQTT protocol.It is a publish-sucribe based protocol. You can publish a message from anywhere into a message broker, which is asyncronusly pushed to all the peers which have subscribed to the channel where message is published.

Our C# client isgoing to receive the message and based on that it sends a signal to Arduino about On and Off control of light, fan and open and close control for the door.

Hardware Fabrication

Our work is combination of hardware and software.The hardware component specifications are as follows:

  1. LED light array.It is 27 watt so it requires a battery connection to driving it.We use 12V 4.5 Amps battery to drive LED array.
  2. In the fan module we use CPU fan.
  3. One Gear motor for snthesizing open and close of the gate.
  4. To generate notification we use temperature sensor LM 35 and one buzzar.
  5. Set of relays to control light and fan.

Circuit Schematic

Figure: Complete Circuit Diagram (Click to enlarge)

You can learn about rigging up a 12v power supply from here

We use the Relay board that we had developed for Urobi.

The above diagram is the main circuit of the project. Arduino is connected to a relay module. A Bulb and a fan is controlled through relays. The electrical unit is connected to main line through relay. When relay is active, the electrical unit is switched on, drawing more current. When the relay is turned off, the electrical load draws no current. LM 35 is connected to A5 for sensing temperature value. The sensed value is sent to PC through serial port which is mitigated to ThingSpeak channel.

The first relay is for controlling the direction of the lock gate. It's input is +-12v. When this relay is turned off, it's output is +12 and when turned on, output is -12v. The output of this relay is connected to second relay. This relay's output is connected to lock. So if both relay 1 and relay 2 are active, gate will move anticlockwise, if only relay 2 is active, gate 2 will move clockwise.

relay3 and 4 drives a light and fan respectively. These realys are given input supply from 12v 4.5A battery.

As we have used 12v relay which can not be driven by Microcontroller's 5v supply, relays are coupled through MCTE2E optocoupler.

Hardware modules are as follows:

Figure: 12 volt power supply

Figure: Relay board

Figure :LED light array,LM 35 and buzzar

Coding

Arduino Code

let us first look at our Arduino sketch. bulbPin and fanPin are the pins with which the relays corresponding to bulb and fan are connected. tSensor is the analog pin where temperature sensor LM 35 is connected. lockPin is the relay connected to door lock. dpdt is the directional control relay which either outputs +12v or -12v depending upon it's state. When both dpdt and lockPin are made high, motor with door lock moves in clockwise direction. When dpdt is off and lockPin is high, lock motor rotates in anti clock wise direction, simulating lock close condition.

As Arduino would be receiving serial data from our C# client, Serial.begin() is called to initialize serial port communication. When C# sends data to Arduino, Serial.available() will be non zero, if so character values are converted to numbers. For example PC sends command '1' which is converted to 1 by Arduino by subtracting 48 from the Ascii value of the received character.

Command 1 and 2 corresponds to Bulb High and Low respectively.

Command 3 and 4 corresponds to Fan High and Low respectively.

command 5 and 6 corresponds to Lock and Unlock respectively. Note that when either of locking or unlocking operations are initiated, the motor rotates for about 300ms and then stops. This is to caliborate only a small movement of the motor for locking and unlocking.

Command 0 corresponds to switching off all the home equipments. It is helpful when the user is going outside. he can close all his electrical appliences with a single command.

int bulbPin=9;
int fanPin=11;
int tSensor=5;
int buzzPin=8;
int dpdt=13;
int lockPin=12;
int ref=0;

void setup() {
  analogReference(INTERNAL);
  // initialize digital pin 13 as an output.
  Serial.begin(19200);
  pinMode(bulbPin, OUTPUT);
  pinMode(fanPin,OUTPUT);
 pinMode(buzzPin,OUTPUT);
 pinMode(dpdt,OUTPUT);
 pinMode(lockPin,OUTPUT);
 digitalWrite(lockPin,LOW);
 digitalWrite(dpdt,LOW);
  digitalWrite(bulbPin,LOW);
  digitalWrite(fanPin, LOW);
  digitalWrite(buzzPin,LOW);
  Serial.println(ref);
}

// the loop function runs over and over again forever
bool on=false;
float avEn=0;
int count=0;
void loop() {

  int reading=analogRead(tSensor);
  float tempC = reading / 9.31;
 avEn=avEn+tempC;
 //Serial.println(tempC);
 count++;
 if(tempC>40)
 {
 // Serial.println("High");
   digitalWrite(buzzPin,HIGH);
 }
 else
 {
  digitalWrite(buzzPin,LOW);
 }
  if(Serial.available())
  {
   int r=Serial.read();
   r=r-48;
   //1= light on 2=light off 3 fan on 4 fan off 0 all off
   switch(r)
   {
    case 1: digitalWrite(bulbPin,HIGH);
    break;
    case 2: digitalWrite(bulbPin,LOW);
    break;
    case 3: digitalWrite(fanPin,HIGH);
    break;
    case 4: digitalWrite(fanPin,LOW);
    break;
    case 5: // Lock
    digitalWrite(dpdt,HIGH);
    digitalWrite(lockPin,HIGH);
    delay(3000);
    digitalWrite(lockPin,LOW);
    digitalWrite(dpdt,LOW);
    break;

    case 6: //unlock 
    digitalWrite(lockPin,HIGH);
    delay(5000);
    digitalWrite(lockPin,LOW);
    break;
    case 0: digitalWrite(fanPin, LOW);
            digitalWrite(bulbPin,LOW);
     break;
   }
  }
  
  delay(100);              // wait for 100 ms
}

C# Code

We have developed our code on the top of this awesomeRealTime Face recognition article of Sergio

Figure :Form Design ( Click to zoom)

When the form is loaded, our Mqtt client is connected with global mqtt broker iot.eclipse.org(or test.mosquitto.org). If the Mqtt client named mc is successfully connected to broker, then it subscribes to a channel by name rupam/MyHome (you must change the name as many reader may be tring to use the same channel simultanously, causing unexpected beheviour to your commands). So, whenever a command is received by the mqtt clientmc_MqttMsgPublishReceived event handler is called.

We provide manual buttons for all the operations: Bulb On, Bulb Off, Fan On, Fan Off, All Off, Lock and Unlock.

At the same time we loop through all the available serial ports and load the port names in the combobox in GroupBox with lable"connect to arduino".

MqttClient mc = null;
    Color ac = Color.Blue;
    string topic = "rupam/MyHome"; 
private void FrmPrincipal_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            for (int i = 0; i < ports.Length; i++)
            {
                cmbPort.Items.Add(ports[i]);
            }
            try
            {
                mc = new MqttClient("test.mosquitto.org");
                mc.Connect("RUPAM");
                mc.Subscribe(new string[] { topic }, new byte[] { (byte)0 });
                mc.MqttMsgPublishReceived += mc_MqttMsgPublishReceived;
                label5.Text = "Connected to test.mosquitto.org in channel rupam/MyHome";
                label5.BackColor = Color.Green;
            }
            catch
            {
                label5.Text = "No Connection with Server-Check Internet Connection";
                label5.BackColor = Color.Red;
            }
            ac = btnConnect.BackColor;
        }

Once the serial port is connected, you can test the operations by clicking these buttons before trying out Mqtt based remote control. When a particular button is clicked, it's color is changed to Green. When the other button is clicked corresponding to this operation, button's color is replaced with the original color. So when Bulb on is clicked, Bulb off becomes gray and bulb on is green, when bulb off is clicked, that button will be green and button with lable buld on will be back to it's actual color.

private void btnBulbOn_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("1");
            btnBulbOn.BackColor = Color.Green;
            btnBulbOff.BackColor = ac;
        }

        private void btnBulbOff_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("2");
            btnBulbOn.BackColor = ac;
            btnBulbOff.BackColor = Color.Green;
        }

        private void btnFanOn_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("3");
            btnFanOn.BackColor = Color.Green;
            btnFanOff.BackColor = ac;
        }

        private void btnFanOff_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("4");
            btnFanOn.BackColor = ac;
            btnFanOff.BackColor = Color.Green;
        }

        private void btnAllOff_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("0");
            btnFanOff.BackColor = ac;
            btnFanOn.BackColor = ac;
            btnBulbOn.BackColor = ac;
            btnBulbOff.BackColor = ac;
        }

For both Lock and unlock button user face verification is needed. Once user's face is verified, Label4 will have a tag name that of the verified username, else it will have a tag called Unknown. In case user's face is unknown, a photo will be captured and will be sent to an entered email along with the face in front of the camera. Gmail Message is sent with attachment using a method called SendMail in EmailSend class.

You need to set your mail id and password in EmailSend's SendMail method to be able to send a mail from your mail id.

You need to also follow the official gmail tutorial to allow less secured apps to access your mail id

public class EmailSend
   {
       static Stopwatch stTime = new Stopwatch();
       public static void SendMail(string emailTo, string subject,string body,string attachmentPath)
       {
           if (!stTime.IsRunning || stTime.Elapsed.TotalMilliseconds > 10000 || subject.Contains("Alarm"))
           {
               string emailFrom = "YourMailID@gmail.com";
               string password = "yourgmailPassword";
               string smtpAddress = "smtp.gmail.com";
               int portNumber = 587;
               bool enableSSL = true;

               using (MailMessage mail = new MailMessage())
               {
                   mail.From = new MailAddress(emailFrom);
                   mail.To.Add(emailTo);
                   mail.Subject = subject;
                   mail.Body = body;
                   mail.IsBodyHtml = true;
                   // Can set to false, if you are sending pure text.
                   if (attachmentPath.Length > 2)
                   {
                       mail.Attachments.Add(new Attachment(attachmentPath));
                   }

                   using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
                   {
                       smtp.Credentials = new NetworkCredential(emailFrom, password);
                       smtp.EnableSsl = enableSSL;
                       smtp.Send(mail);
                   }
               }
               if (!stTime.IsRunning)
               {
                   stTime.Start();
               }
               else
               {
                   stTime.Reset();
                   stTime.Restart();
               }
           }
       }
   }
private void btnLock_Click(object sender, EventArgs e)
       {
           if (!label4.Text.Contains("Unknown"))
           {
               serialPort1.WriteLine("5");
               btnUnlock.BackColor = ac;
               btnLock.BackColor = Color.Green;
               grpDoor.Enabled = false;

           }
           else
           {
               //// Send a Mail
               (new System.Speech.Synthesis.SpeechSynthesizer()).SpeakAsync("Unauthorized Attempt!!!");

               String fileName = ".\\Unknown\\Image_" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".jpg";//Save the filename first on

               imageBoxFrameGrabber.Image.Save(fileName);
               EmailSend.SendMail("receiversmailID@gmail.com", "Unknown Person", "Unknown person attempting unlock " + DateTime.Now, fileName);
               ///////
           }

       }

The serial port object by name serialPort1 is added with an event handler called serialPort1_DataReceived. Whenever there is a data in the port, it is captured through a delegate ( because it is an independent thread which can not use UI elements).

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
       {
           string s = serialPort1.ReadLine();
           this.BeginInvoke(MyDlg, s);
       }

Note that Arduino only sends Temperature data to C# application. Therefore only temperature data is received by this application. This data is sent to ThingSpeak channel and is analyzed for threshold >40. If the temperature exceeds the threshold an email is sent to user.

void Display(string s)
        {
           
           
            
            try
            {
                double en = double.Parse(s);
                chart1.Series[0].Points.AddY(en);
                if (en > 40)
                {
                    EmailSend.SendMail("receiversMailId@gmail.com", "Fire at home Alarm","Fire at Home Alarm "+DateTime.Now ,"");
                }
                //string s = UpdateThingSpeakData(new string[] { "40", "40" }, "C606GJW98AWMN9P9");
                string s1 = UpdateThingSpeakData(new string[] { en.ToString() }, "MB2RQE6KCT9D670V");
            }
            catch
            {
            }
        }

In Mqtt event handler we check for the command, convert the character command to number and then appropriately click corresponding button by calling button click event handlers.

void mc_MqttMsgPublishReceived(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e)
        {
            //throw new NotImplementedException();
            //MessageBox.Show(GetString(e.Message));
            string s = "";
            try
            {
                byte b = e.Message[0];
                //e.Retain = false;
                s = (b - 48).ToString();
            }
            catch
            {
                return;
            }
            this.Invoke((MethodInvoker)delegate
            {

                if (s.Equals("1"))
                {
                    btnBulbOn_Click(btnBulbOn, new EventArgs());
                }
                if (s.Equals("2"))
                {
                    btnBulbOff_Click(btnConnect, new EventArgs());
                }
                if (s.Equals("3"))
                {
                    btnFanOn_Click(btnFanOn, new EventArgs());
                }
                if (s.Equals("4"))
                {
                    btnFanOff_Click(btnFanOff, new EventArgs());
                }
                if (s.Equals("0"))
                {
                    btnAllOff_Click(btnAllOff, new EventArgs());
                }
                if (s.Equals("5"))
                {
                    if (grpDoor.Enabled)
                    {
                        btnLock_Click(btnAllOff, new EventArgs());
                    }
                    else
                    {
                        (new System.Speech.Synthesis.SpeechSynthesizer()).SpeakAsync("Unauthorized Attempt!!!");

                    }
                }
                if (s.Equals("6"))
                {
                    if (grpDoor.Enabled)
                    {
                        btnUnlock_Click(btnAllOff, new EventArgs());
                    }
                    else
                    {
                        (new System.Speech.Synthesis.SpeechSynthesizer()).SpeakAsync("Unauthorized Attempt!!!");

                        String fileName = ".\\Unknown\\Image_" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".jpg";//Save the filename first on 
                
                        
                        imageBoxFrameGrabber.Image.Save(fileName);
                        EmailSend.SendMail("maneeshanmc27@gmail.com", "Unknown Person", "Unknown person attempting unlock " + DateTime.Now, fileName);

                    }
                }

            });
        }

You can download MyMqtt App from Google play. Open settings in the app and give broker address as iot.eclipse.org or test.mosquitto.org ( whatever you have set in the form). Now publish to Channel rupam/MyHome.

You need to publish 1 for bulb on, 2 for bulb off and so on.

Results

First to become authentic user need to register his name on the software through face recognization.It requires a administrative permission.If permission granted then user is connsidered as authorised.

Figure: First step of user registration

Figure: admin password for user face registration

Figure :Admin permission granted

Figure : User registered

Now arduino is connected with the system with a virtual line.Select the port.

Figure : Selection of arduino port

Figure : Started home control temperature unit

Figure :Temperature data also availale in the thinkspeak

Now to increase the temperature sensor value you can hold a fire stick nearer to the LM-35(Temperature sensor).

Figure: Inceasing the temperature sensor value

Figure:Tmperature is increasing showing on the graph

When temperature will increase and if it crossed the threshold value then the notification will send to gmail id.

Figure: gmail notification for the temperature notification

Not if a unknown person wants to open gate then the software going to display unknown .Unknown person are those who are not registered their name.

Figure: display unknown to user

Not only display unknown to the software but also photo of that unknown person will send to the gmail address.So that user can take some action.

Figure: Email with unknown person photograph

Conclusion

Home automation has been one of the most popular areas of research for both hobby projects as well as for industrial IoT development. However, home automation has always been considered separately in comparison to home monitoring and security system. In this project we have combined safety, security, monitoring and control system with IoT. The safety system ensures that in case of a fire, user is always notified as early as possible. The security system ensures that unauthorized face can not break into the home, controlling system ensures that user can control lights and fans from his mobile and monitoring system ensuresthat user monitors his home temperature remotely which is helpful if he has plants at home which are sensitive to temperature. Results shows that the system meets all the prerequisites and generates real time data and alert.This system can be further improved by incorporating other features like automatic climate control based on temperature monitoring, automatic lighting control based on light value monitoring.

So, even though HASSI is simple, low cost, it is still extremely powerful. You can build more complicated home automation projects based on this framework.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Authors

Grasshopper.iics
CEO Integrated Ideas
India India
gasshopper.iics is a group of like minded programmers and learners in codeproject. The basic objective is to keep in touch and be notified while a member contributes an article, to check out with technology and share what we know. We are the "students" of codeproject.

This group is managed by Rupam Das, an active author here. Other Notable members include Ranjan who extends his helping hands to invaluable number of authors in their articles and writes some great articles himself.

Rupam Das is mentor of Grasshopper Network,founder and CEO of Integrated Ideas Consultancy Services, a research consultancy firm in India. He has been part of projects in several technologies including Matlab, C#, Android, OpenCV, Drupal, Omnet++, legacy C, vb, gcc, NS-2, Arduino, Raspberry-PI. Off late he has made peace with the fact that he loves C# more than anything else but is still struck in legacy style of coding.
Rupam loves algorithm and prefers Image processing, Artificial Intelligence and Bio-medical Engineering over other technologies.

He is frustrated with his poor writing and "grammer" skills but happy that coding polishes these frustrations.
Group type: Organisation

116 members


Moumita Das
Software Developer Integrated Ideas
India India
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
QuestionGood Day! Pin
Member 1393925930-Aug-18 23:55
memberMember 1393925930-Aug-18 23:55 
QuestionMessage Closed Pin
29-Dec-17 20:08
groupCompliance BD29-Dec-17 20:08 
QuestionPlease Some Help.. Pin
Member 1306255217-Mar-17 10:41
memberMember 1306255217-Mar-17 10:41 
QuestionHASSI Pin
Member 1265925019-Aug-16 3:10
memberMember 1265925019-Aug-16 3:10 
AnswerRe: HASSI Pin
Grasshopper.iics19-Aug-16 13:05
groupGrasshopper.iics19-Aug-16 13:05 
PraiseAwesome idea Pin
giri0018-Aug-16 6:42
membergiri0018-Aug-16 6:42 
GeneralRe: Awesome idea Pin
Moumita Das8-Aug-16 20:42
memberMoumita Das8-Aug-16 20:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190214.1 | Last Updated 7 Aug 2016
Article Copyright 2016 by Grasshopper.iics, Moumita Das
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid