SmartHome

Always put the house in your pocket with your mobile phone and be a real owner even from a distance!

BeginnerShowcase (no instructions)8 hours1,183
SmartHome

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×2
Buzzer
Buzzer
×1
LED (generic)
LED (generic)
×10
Breadboard (generic)
Breadboard (generic)
×2
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1
Resistor 1k ohm
Resistor 1k ohm
×2
Male/Male Jumper Wires
×20
Male/Female Jumper Wires
Male/Female Jumper Wires
×5
Female/Female Jumper Wires
Female/Female Jumper Wires
×20
Camera Module
Raspberry Pi Camera Module
V3
×1
Through Hole Resistor, 150 ohm
Through Hole Resistor, 150 ohm
×10
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
Arduino UNO
Arduino UNO
×2

Software apps and online services

VNC Viewer
PuTTY - SSH Client
Python modules Rpi.GPIO, time, picamera, os, board, adafruit_dht, pigpio
Python 3.8 programming language
Apache Server
Raspberry Pi OS

Story

Read more

Schematics

diagrama_14pofe5JxH.jpg

Code

light_off.py

Python
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(13,GPIO.OUT)
GPIO.output(13,0) 
#GPIO.setwarnings(False)

SmartHome.php

PHP
<html>
<head>
<?php
$myfile = fopen("/var/www/html/temp/tmp.txt", "r") or die("Unable to open file!");
if (isset($_POST['refresh']))  
{
exec('sudo python /var/www/refresh_photo.py');
}
if (isset($_POST['lock_door']))  
{
exec('sudo python /var/www/lock_door.py');
}
if (isset($_POST['open_door']))   
{
exec('sudo python /var/www/open_door.py');
}
if (isset($_POST['light_on']))  
{
exec('sudo python /var/www/light_on.py');
}
if (isset($_POST['light_off']))   
{
exec('sudo python /var/www/light_off.py');
}
if (isset($_POST['light_on_rez']))  
{
exec('sudo python /var/www/light_on_rez.py');

}
if (isset($_POST['light_off_rez']))  
{
exec('sudo python /var/www/light_off_rez.py');
}
if (isset($_POST['temp_humid']))  
{
exec('sudo python3 /var/www/temp_humid.py');
}
if (isset($_POST['smartHome']))  
{
exec('sudo python /var/www/SmartHome.py');
}

?>

  <title>SmartHome PROJECT_SM_2020 </title>
</head>
<body bgcolor="#FFDAB9">
<center>
<table witdh="400" border="1" bgcolor="#FFE4B5" bordercolor="red">
<td>
<font face="impact" color="navy"><b>
<center>
<a href="https://ac.tuiasi.ro/">Faculty of Automatic Control & Computer Engineering</a> - Romania 

</font>


<center> <h1> <Font face="impact" color='navy'>
 IOT Smart Home Project  </font></h1>

<font color="black" face ="arial"size="3">
Control your home from anywhere in the world!</font>
</font><br><br>
<img src="/imag/imag1.jpg" width="300"" height="200"> </center>
<br>
<font color="black" face ="arial"size="3">Current Temperature & Humidity 
<span> <?php echo fread($myfile,filesize('/var/www/html/temp/tmp.txt')); fclose($myfile); ?></span> </font>
</font><br><br>
<center><form method="post">
  <table style="width: 50%; text-align: left; margin-left: auto; margin-right: auto;"border="2" cellpadding="2" cellspacing="2">
      <tr>
        <td style="text-align: center;"><button name="refresh"><font face="impact" color="blue">Refresh Camera Capture</button></td></font>
        <td style="text-align: center;"><button name="temp_humid"><font face="impact" color="blue">Temperature & Humidity </button></td> </font>
      </tr>
      <tr>
        <td  style="text-align: center;"><button name="lock_door"><font face="impact" color="blue">Lock & Close  Door</button></td></font>
        <td  style="text-align: center;"><button name="open_door"><font face="impact" color="blue">Unlock & Open Door</button></td></font>
      </tr>
      <tr>
        <td  style="text-align: center;"><button name="light_on"><font face="impact" color="blue">Light On</button></td></font>
        <td  style="text-align: center;"><button name="light_off"><font face="impact" color="blue">Light Off</button></td></font>
      </tr>
      <tr>
        <td  style="text-align: center;"><button name="light_on_rez"><font face="impact" color="blue">Light On REZ</button></td></font>
        <td  style="text-align: center;"><button name="light_off_rez"><font face="impact" color="blue">Light Off REZ</button></td></font>
      </tr>
      <button name="smartHome"><font face="impact" color="blue">Start Alarm</button><br></br>
    </tbody>
  </table>

</form><br></br>
<a href=""> More details </a> </center>
<center>
<br> <font face="arial" color="navy">
&copy 2020Andrei ABABEI Maria-Alexandra BOICU Beatris Zaharia<br>Automation and Computer Engineering Faculty
 <br>
</td>
</table>

</body>
</html>

light_off_rez.py

Python
#import picamera
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
#file=open("/var/www/html/imag/imag1.jpg","wb")
#camera=picamera.PiCamera()
#camera.resolution =(320,240)  # 1024,768
GPIO.setmode(GPIO.BOARD)
GPIO.setup(19,GPIO.OUT)
GPIO.output(19,0) #aprind LED
#GPIO.setwarnings(False)

#camera.start_preview()
#time.sleep(2)
#camera.capture(file)
#file.close()

light_on.py

Python
import RPi.GPIO as GPIO

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(13,GPIO.OUT)
GPIO.output(13,1) 
#GPIO.setwarnings(False)

light_on_rez.py

Python
#import picamera
import RPi.GPIO as GPIO

GPIO.setwarnings(False)
#file=open("/var/www/html/imag/imag1.jpg","wb")
#camera=picamera.PiCamera()
#camera.resolution =(320,240)  # 1024,768
GPIO.setmode(GPIO.BOARD)
GPIO.setup(19,GPIO.OUT)
GPIO.output(19,1) #aprind LED
#GPIO.setwarnings(False)

#camera.start_preview()
#time.sleep(2)
#camera.capture(file)
#file.close()

lock_door.py

Python
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)  #lock/unlock
p = GPIO.PWM(17, 50)
GPIO.setup(13, GPIO.OUT)  #open/close
p1 = GPIO.PWM(13, 50)
p.start(2.5) # 0 grade
p1.start(2.5) # 0 grade

try:
    for i in (1,10):
        p1.ChangeDutyCycle(9.5)  # turn towards 90 degree
        time.sleep(2) # sleep 1 second
        p.ChangeDutyCycle(8.67)  # turn towards 135 degree
 #       time.sleep(1) # sleep 1 second
except KeyboardInterrupt:
    p.stop()
    p1.stop()
    GPIO.cleanup()

open_door.py

Python
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)  #lock/unlock
p = GPIO.PWM(17, 50)
GPIO.setup(13, GPIO.OUT)  #open/close
p1 = GPIO.PWM(13, 50)
p.start(8.67) # 120 grade inc
p1.start(9.5) # 135 grade usa

try:
    for i in (1,10):
        p.ChangeDutyCycle(2.5)  # turn towards 0 degree
        time.sleep(3) # sleep 1 second
        p1.ChangeDutyCycle(2.5)  # turn towards 0 degree
      #  time.sleep(2) # sleep 1 second
except KeyboardInterrupt:
    p.stop()
    p1.stop()
    GPIO.cleanup()

refresh_photo.py

Python
import picamera
import time

file=open("/var/www/html/imag/imag1.jpg","wb")
camera=picamera.PiCamera()
camera.resolution =(320,240)  # 1024,768
camera.start_preview()
#time.sleep(2)
camera.capture(file)
file.close()

SmartHome.py

Python
import smtplib
import time  
import pigpio 
import RPi.GPIO as GPIO

pi = pigpio.pi()
buzzer = 18
pi.set_mode(buzzer, pigpio.OUTPUT) 
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
trig = 32 
echo = 38
GPIO.setup(trig, GPIO.OUT) 
GPIO.setup(echo, GPIO.IN) 
GPIO.setup(8, GPIO.OUT)
GPIO.setup(10, GPIO.OUT)
def calculate_distance():
    #set the trigger to HIGH
	GPIO.output(trig, GPIO.HIGH)
	time.sleep(0.00001) 
	GPIO.output(trig, GPIO.LOW)
    	start = time.time() 
	stop = time.time()
    	while GPIO.input(echo) == 0:
		start = time.time()
   	while GPIO.input(echo) == 1: 
		stop = time.time()
    	duration = stop - start
    	distance = 34300/2 * duration 
	if distance < 0.5 and distance > 400: 
        	return 0
    	else:
        	return distance 

GPIO.output(8, GPIO.LOW)
GPIO.output(10,GPIO.HIGH)
server=smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login("proiectef@gmail.com","Sazam123")
ai=True
try: 
	while True : 
	
		if calculate_distance() < 15:
			if ai==True :
				msg="You have a visitor!"
				server.sendmail("proiectef@gmail.com","beatris0117@yahoo.com",msg)
				GPIO.output(8, GPIO.HIGH)
				GPIO.output(10, GPIO.LOW)
				ai=False
			pi.hardware_PWM(buzzer, 500, 500000)
			time.sleep(0.05)
         #turn off the buzzer and wait 50 ms
 	  		pi.hardware_PWM(buzzer, 0, 0) 
			time.sleep(0.05)

		else:
			if ai==False:
				msg="The visitor left!"
				server.sendmail("proiectef@gmail.com","beatris0117@yahoo.com",msg)
				GPIO.output(8, GPIO.LOW)
				GPIO.output(10, GPIO.HIGH)
				ai=True
			pi.hardware_PWM(buzzer,0,0)
		
		time.sleep(0.1)

except KeyboardInterrupt: 
	pass
server.quit()
pi.write(buzzer, 0)
pi.stop()
GPIO.cleanup()

Temp_Humid.py

Python
import os
import time
import Adafruit_DHT

DHT_SENSOR =Adafruit_DHT.AM2302
DHT_PIN = 4
p=0
t=0
h=0

f = open('/var/www/html/temp/tmp.txt', 'w')
try:
        while p<10:
                humidity,temp=Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
                print("{0} {1}".format(humidity, temp))
                t=t+temp
                h=h+humidity
        #       time.sleep(1)
                p=p+1
except:
        print("Exceptie")
        pass

f.write('{0:0.2f} C {1:0.2f} %'.format (t/10,h/10))
print("Scrierea s-a realizat cu succes!")

Credits

Beatris Zaharia

Beatris Zaharia

2 projects • 3 followers
Andrei Ababei

Andrei Ababei

1 project • 4 followers
Maria-Alexandra Boicu

Maria-Alexandra Boicu

2 projects • 2 followers
Thanks to Ababei Andrei, Boicu Maria-Alexandra, and Zaharia Beatris.

Comments