I connected the Raspberry Pi to the sensor module and made a motion sensor that notifies my LINE of the detection information.
As part of creating an in-house prototype app, I will be in charge of building a motion sensor. What is Raspberry Pi? I've heard of Python, but what can I do? I started from the state and made something that will notify LINE when the sensor responds. I am posting an article as a work report to my company, but I would like to do electronic work using Raspberry Pi! I hope it will be helpful for those who say.
・ RaspBerry Pi Zero ・ Ren He HC-SR501 Human body infrared sensitive module ( URL:https://www.amazon.co.jp/gp/product/B07DCKZS5S ) ・ SD card ・ Breadboard / jumper wire (female-female) ( URL:https://www.amazon.co.jp/gp/product/B01A4DDUTA ) ・ USB hub ·monitor ・ USB keyboard ・ Plastic hammer ・ GPIO hammer header ( https://www.amazon.co.jp/gp/product/B0711MPHVF/ )
Download the OS from Raspberry Pi official website. This time I used NOOBS Lite. Unzip the downloaded OS Zip file and write it to the SD card.
Use the GPIO Hammer Header to hammer the GPIO pins to the Raspberry Pi with a plastic hammer, then connect them with jumper wires to the GPIO pins that play the same role as the sensor module. The RaspBerry Pi Zero used this time connects like this.
The detection time and detection range of the sensor module are set to this level.
Insert the SD card into the Raspberry Pi. Connect your monitor, USB keyboard, mouse and power supply to launch the Raspberry Pi Make the initial settings by referring to this article.
Make GPIO control settings for Raspberry Pi in Python and check the operation of the sensor module. Run the following program
sensor_test.py
import RPi.GPIO as GPIO
GPIO_PIN = 12
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_PIN,GPIO.IN)
while True:
if(GPIO.input(GPIO_PIN) == GPIO.HIGH):
print("1")
break
GPIO.cleanup()
After executing the program, hold your hand over the sensor module, and if the following results are obtained, the operation check is complete.
pi@raspberrypi:~ $ python /home/pi/work/sensor_test.py
1
Go to LINE Notify, log in with the LINE account you want to send notifications to, and open My Page.
Click "__Issue Token __"
Set the token name to any token name and select "Receive LINE Notify notification at _1: 1"
Make a note of the issued token
The program is finally implemented! Receive the detection information from the sensor module with Raspberry Pi and hit the API toward LINE Notify.
detection_LINE.py
import requests
import RPi.GPIO as GPIO
import time
SLEEPTIME = 30
GPIO_PIN = 12
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_PIN,GPIO.IN)
def main():
url = "https://notify-api.line.me/api/notify"
token = "The access token you noted earlier"
headers = {"Authorization" : "Bearer "+ token}
message = 'IN USE!!!!!'
payload = {"message" : message}
requests.post(url ,headers = headers ,params=payload)
try:
while True:
if(GPIO.input(GPIO_PIN) == GPIO.HIGH):
main()
time.sleep(SLEEPTIME)
finally:
GPIO.cleanup
Set Cron so that it will be executed automatically when Raspberry Pi starts.
crontab -e
When the cron config file opens, add the following to the last line:
@reboot python /home/pi/work/detection_LINE.py
I restarted the Raspberry Pi and held my hand over the sensor module and I got a notification!
I started from a sloppy state, but I was surprised to use force to install GPIO, and when the detection range of the sensor module was maximized + I executed the script without inserting the sleep time and the LINE notification sounded like a firecracker, I was a beginner There were many unique detours, but I enjoyed it as if I were studying freely during the summer vacation!
Next, I wish I could make something using AWS Lambda.
Recommended Posts