[PYTHON] [For beginners] I made a motion sensor with Raspberry Pi and notified LINE!

Introduction

I connected the Raspberry Pi to the sensor module and made a motion sensor that notifies my LINE of the detection information.

Background

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.

What you need to make

・ 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/ )

OS installation

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.

Connection of Raspberry Pi and sensor module

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.

Initial setting

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.

Sensor module operation check

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

Issuing access tokens with LINE Notify

  1. Go to LINE Notify, log in with the LINE account you want to send notifications to, and open My Page.

  2. Click "__Issue Token __"

  3. Set the token name to any token name and select "Receive LINE Notify notification at _1: 1"

  4. Make a note of the issued token

Notify LINE of the detection information of the motion sensor

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

Raspberry Pi restart

I restarted the Raspberry Pi and held my hand over the sensor module and I got a notification!

20200629_095257000_iOS.png

Impressions of making

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

[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
I made a resource monitor for Raspberry Pi with a spreadsheet
I made a LINE BOT with Python and Heroku
I made a surveillance camera with my first Raspberry PI.
I made a Python program for Raspberry Pi that operates Omron's environmental sensor in the mode with data storage
I made a web server with Raspberry Pi to watch anime
Use PIR motion sensor with raspberry Pi
[Electronic work] I made a Suica touch sound detector with Raspberry Pi
I made a stamp substitute bot with line
I made a LINE Bot with Serverless Framework!
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
I tried to create a button for Slack with Raspberry Pi + Tact Switch
[AWS] I made a reminder BOT with LINE WORKS
Create a color sensor using a Raspberry Pi and a camera
I made a household account book bot with LINE Bot
I made a window for Log output with Tkinter
I made a LINE BOT that returns parrots with Go
Create a web surveillance camera with Raspberry Pi and OpenCV
I tried connecting Raspberry Pi and conect + with Web API
I tried using the DS18B20 temperature sensor with Raspberry Pi
I made a Chatbot using LINE Messaging API and Python
[AWS] I made a reminder BOT with LINE WORKS (implementation)
I tried to make a motion detection surveillance camera with OpenCV using a WEB camera with Raspberry Pi
I automatically collected my Qiita post data with Raspberry Pi and notified it regularly with Slack
I made a fortune with Python.
I made a daemon with Python
Using a webcam with Raspberry Pi
I made a simple circuit with Python (AND, OR, NOR, etc.)
I made a Nyanko tweet form with Python, Flask and Heroku
Christmas classic (?) Lighting a Christmas tree with Raspberry Pi and Philips Hue
I made a lot of files for RDP connection with Python
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Make a Kanji display compass with Raspberry Pi and Sense Hat
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
I made a chatbot with Tensor2Tensor and this time it worked
"2/2" I am making a simple web application for robot operation. "Raspberry Pi 3B + and Django Channels"
"1/2" I am making a simple web application for robot operation. "Raspberry Pi 3B + and Django Channels"
Pet monitoring with Rekognition and Raspberry pi
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
[Raspberry Pi] Add a thermometer and a hygrometer
Build a Tensorflow environment with Raspberry Pi [2020]
Note: I want to do home automation with Home Assistant + Raspberry Pi + sensor # 1
Make a wireless LAN Ethernet converter and simple router with Raspberry Pi
Try fishing for smelt with Raspberry Pi
Get GrovePi + sensor value with Raspberry Pi and store it in kintone
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I made a pet camera that is always connected with WebRTC (Nuxt.js + Python + Firebase + SkyWay + Raspberry Pi)
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
Make a wash-drying timer with a Raspberry Pi
I made a Hex map with Python
I made a life game with Numpy
I made a stamp generator with GAN
Operate an oscilloscope with a Raspberry Pi
Create a car meter with raspberry pi
I made a roguelike game with Python
I made an npm package to get the ID of the IC card with Raspberry Pi and PaSoRi
I made a simple blackjack with Python
I made a configuration file with Python
I made a library for actuarial science
I made a WEB application with Django