Antonio Artimon
Published

Motion Detection Info System via Gmail

In this project we use ultrasonic sensor, and if the distance is less than 50cm, sensor will send and e-mail with temperature and humidity

BeginnerProtip550
Motion Detection Info System via Gmail

Things used in this project

Hardware components

Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1
Breadboard (generic)
Breadboard (generic)
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Through Hole Resistor, 5.1 kohm
Through Hole Resistor, 5.1 kohm
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×5
Jumper wires (generic)
Jumper wires (generic)
×5

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Story

Read more

Schematics

Circuit Diagram(RPi)

Code

Python 2.7.16

Python
#we import the RPi.GPIO library with the name of GPIO
import RPi.GPIO as GPIO
#we import the sleep module from the time library
import time
#we import SMTP protocol client
import smtplib
#To control the sensor we are going to use the Adafruit DHT11 Python library
import Adafruit_DHT
#GPIO.BCM option means that you are referring to the pins by the "Broadcom SOC channel" number
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#name the type of sensor used
type=Adafruit_DHT.DHT11
#declare the pin used by the sensor in GPIO form
dht11=25
trig=23
echo=24
#set the trigger pin as OUTPUT and the echo as INPUT
GPIO.setup(trig,GPIO.OUT)
GPIO.setup(echo,GPIO.IN)
#set the sensor as INPUT
GPIO.setup(dht11,GPIO.IN)
#create one SMTP object
server=smtplib.SMTP('smtp.gmail.com',587)
#start the server
server.starttls()
#login data for your account
server.login("username@gmail.com","password")

def calculate_distance():
  #set the trigger to HIGH
	GPIO.output(trig,GPIO.HIGH)
  #sleep 0.00001 s and the set the trigger to LOW
	time.sleep(0.00001)
	GPIO.output(trig,GPIO.LOW)
  #save the start and stop times
	start=time.time()
	stop=time.time()
  #modify the start time to be the last time until the echo become HIGH
	while GPIO.input(echo)==0:
		start=time.time()
  #modify the stop time to be the last time until the echo becomes LOW
	while GPIO.input(echo)==1:
		stop=time.time()
  #get the duration of the echo pin as HIGH
	duration=stop-start
  #calculate the distance
	distance=34300/2*duration
  #return the distance
	return distance
    
#we start a loop that never ends
try:
	while True:
    #make the reading
		humidity, temperature=Adafruit_DHT.read_retry(type,dht11) 
    #we will save the values only if they are not null
		if humidity is not None and temperature is not None:
    #msg is the message that will be sent 
    #msg has temperature and humidity values
            		msg="Temperature = {:.1f} , Humidity = {:.1f}" .format(temperature, humidity)
		if calculate_distance() <50:
      #send the message    
			server.sendmail("username@gmail.com","receiver@gmail.com",msg)
			print("E-mail sent !")
		time.sleep(1)
except KeyboardInterrupt:
	pass
#close the server    
server.quit()
##clean all the used ports
GPIO.cleanup()

Credits

Antonio Artimon

Antonio Artimon

1 project • 0 followers

Comments