Please prepare the following items [Infrared sensor](https://www.amazon.co.jp/SODIAL-R-267-%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3% 83% AB% E7% 84% A6% E9% 9B% BB% E5% 9E% 8B% E8% B5% A4% E5% A4% 96% E7% B7% 9A% E6% A4% 9C% E5% 87% BA% E5% 99% A8PIR% E3% 83% A2% E3% 83% BC% E3% 82% B7% E3% 83% A7% E3% 83% B3 / dp / B00L11K4RQ? Ie = UTF8 & ref_ = pe_1095802_173146692) Raspberry Pi and related products [Code](https://www.amazon.co.jp/%E3%82%B9%E3%82%A4%E3%83%83%E3%83%81%E3%82%B5%E3%82 % A4% E3% 82% A8% E3% 83% B3% E3% 82% B9-SFE-PRT-08430-% E3% 82% B8% E3% 83% A3% E3% 83% B3% E3% 83% 91% E3% 83% AF% E3% 82% A4% E3% 83% A4-% E3% 83% A1% E3% 82% B9% EF% BD% 9E% E3% 83% A1% E3% 82% B9 -10% E6% 9C% AC% E3% 82% BB% E3% 83% 83% E3% 83% 88 / dp / B0079AXJ0W? Ie = UTF8 & ref_ = pe_1095802_173146692) If possible, look for the cheapest one yourself.
sensor | raspbery pi |
---|---|
VCC | 5V |
OUT | GPIO25:22 |
GND | GND:25 |
pi@raspberrypi:~$ sudo su
root@raspberrypi:/home/pi#
Since GPIO uses 25 pins, export GPIO25 to user space. If you do not do this, you will not be able to access GPIO.
root@raspberrypi:/home/pi# echo 25 > /sys/class/gpio/export
Writing to an export file in the / sys / class / gpio directory with the echo command creates a new directory called / sys / class / gpio / gpio25 with the virtual files needed to control GPIO. Since the input from the sensor is judged this time, set in in the direction file.
root@raspberrypi:/home/pi# cd /sys/class/gpio/gpio25
root@raspberrypi:/sys/class/gpio/gpio25# echo in > direction
Now, if the sensor responds, 1 will be written to the value file, and if there is no response, 0 will be written. When the sensor is made to react and the following command is executed, 1 is written in value and the operation can be confirmed.
root@raspberrypi:/sys/class/gpio/gpio25# cat value
1
What to make this time ・ Send an email when the infrared sensor responds
sudo nano sensor_push.py
sensor_push.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import time
import RPi.GPIO as GPIO
import os.path
import datetime
import smtplib
from email import Encoders
from email.Utils import formatdate
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
INTAVAL = 3
SLEEPTIME = 5
SENSOR_PIN = 25
#Gmail account
ADDRESS = "Gmail address"
PASSWARD = "Gmail password"
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PIN, GPIO.IN)
st = time.time()-INTAVAL
while True:
print GPIO.input(SENSOR_PIN)
if(GPIO.input(SENSOR_PIN) == GPIO.HIGH) and (st + INTAVAL < time.time()):
st = time.time()
print("Detected a person")
def create_message(from_addr, to_addr, subject, body, mime=None, attach_file=None):
msg = MIMEMultipart()
msg["From"] = from_addr
msg["To"] = to_addr
msg["Date"] = formatdate()
msg["Subject"] = subject
body = MIMEText(body)
msg.attach(body)
#Attachment
if mime != None and attach_file != None:
attachment = MIMEBase(mime['type'],mime['subtype'])
file = open(attach_file['path'])
attachment.set_payload(file.read())
file.close()
Encoders.encode_base64(attachment)
msg.attach(attachment)
attachment.add_header("Content-Disposition","attachment", filename=attach_file['name'])
return msg
def send(from_addr, to_addrs, msg):
smtpobj = smtplib.SMTP(SMTP, PORT)
smtpobj.ehlo()
smtpobj.starttls()
smtpobj.ehlo()
smtpobj.login(ADDRESS, PASSWARD)
smtpobj.sendmail(from_addr, to_addrs, msg.as_string())
smtpobj.close()
if __name__ == '__main__':
#destination address
to_addr = "destination address"
#Subject and body
subject = "Thieves!Fire!Murder!"
body = "Text"
#Attachment settings(text.Attach txt file)
mime={'type':'text', 'subtype':'comma-separated-values'}
attach_file={'name':'test.txt', 'path':'./text.txt'}
#Compose a message(There is an attachment)
#msg = create_message(ADDRESS, to_addr, subject, body, mime, attach_file)
#Message composition(No attachments)
msg = create_message(ADDRESS, to_addr, subject, body)
#Send
send(ADDRESS, [to_addr], msg)
time.sleep(SLEEPTIME)
The rest
sudo python sensor_push.py
An email will be sent to you (if the infrared sensor responds)
The subject is "Thieves! Fire! Murder!"
~~ I have confirmed the operation, so if you want to be able to execute it automatically ~~
pi@raspberrypi:~$ crontab -e
#
#
@reboot python sensor_push.py
(Do not write "# @ reboot python sensor_push.py"!)
[* Raspberry Pi *] It's summer! Let's make a cicada sound using a motion sensor [Send email easily with Gmail](http://make.bcde.jp/python/gmail%E3%81%A7%E7%B0%A1%E5%8D%98%E3%81%AB%E3%83 % A1% E3% 83% BC% E3% 83% AB% E9% 80% 81% E4% BF% A1 /)
Recommended Posts