Niezbędnym elementem bezpieczeństwa inteligentnego domu jest czujnik ruchu informujący o niepożądanym ruchu w domu podczas naszej nieobecności. Jednym z takich sprawdzonych czujników ruchu jest czujnik ruchu PIR HC-SR501
Podłączenie do Raspberry Pi
- VCC czujnika (czerwony) do zasilania 5V Raspberry Pi (lub szyna + na płytce)
- OUT z czujnika (zielony) do Raspberry Pi GPIO 7 (pin 26)
- GND czujnika (czarny) do GND Raspberry Pi (lub szyna – na płytce)
HC-SR501SendEmail.py
Skrypt wysyła powiadomienia emailem jeśli ruch trwał łącznie dłużej niż minutę (sprawdzanie co 3 sekundy jeśli ruch się utrzymuje zmienna i jest zwiększana jeśli ruch zanika zmienna jest zmniejszana).
- PIR to numer pinu
- SMTserver,sender,destination,username,password należy zastąpić własnymi danymi logowania do skrzynki powiadamiającej
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
#!/usr/bin/env python import RPi.GPIO as GPIO import time from datetime import datetime import smtplib from smtplib import SMTP_SSL as SMTP # this invokes the secure SMTP protocol (port 465, uses SSL) from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText """InsertTempMysql.py: script to send email when the sensor detects motion """ __author__ = "zbiros" __copyright__ = "Copyright 2015, Malinowo.net.pl" __credits__ = ["maros"] __license__ = "GPL" __version__ = "1.0.1" __maintainer__ = "zbiros" __email__ = "zbiros@malinowo.secu.com.pl" __status__ = "Development" SMTPserver = 'xxx.home.pl' sender = 'alert@xxxx.com.pl' destination = ['janek.kowalski@xxxx.pl'] username = "login@xxx.home.pl" password = "HasloDoPoczty" text_subtype = 'plain' PIR = 26 # On-board pin number 7 (GPIO04) state = False val = False GPIO.setmode(GPIO.BOARD) # Change this if using GPIO numbering GPIO.setup(PIR, GPIO.IN) # Set PIR as input i = 0 def send_email(): content="""\ Test message """ subject="Sent from Python" from email.MIMEText import MIMEText try: msg = MIMEText(content, text_subtype) msg['Subject']= subject msg['From'] = sender # some SMTP servers will do this automatically, not all conn = SMTP(SMTPserver) conn.set_debuglevel(False) conn.login(username, password) try: conn.sendmail(sender, destination, msg.as_string()) finally: conn.close() except Exception, exc: sys.exit( "mail failed; %s" % str(exc) ) # give a error message try: while True: time.sleep(3) val = GPIO.input(PIR) # read input value if (val == True): # check if the input is HIGH print "Alert! detected suspicious movement" i = i + 1 if(i%2 == 0): print "traffic counter:" + `i` else: print "traffic counter:" + `i` if i > 20: # 3 x 20 including minute motion detected print "send e-mail alert notification !" send_email() i = 0 if (state == False): state = True else: print "not detected anything suspicious..." if i == 0: i = 0 else: i = i - 1 print "traffic counter:" + `i` if (state == True): state = False; except KeyboardInterrupt: GPIO.cleanup() |
wynik działania na ekranie:
Czujnik do kupienia w:
http://botland.com.pl/czujniki-ruchu/1655-czujnik-ruchu-pir-hc-sr501.html
Warto zobaczyć
https://github.com/antontsv/r2d2/blob/master/sensor_showcase.py