Skip to content

การประยุกต์ใช้ LINE Notify กับ PIR Sensor

mrolarik edited this page Jul 31, 2018 · 9 revisions

จากบทความที่ได้เขียนไว้เกี่ยวกับ

บทความนี้จะแนะนำการประยุกต์ใช้ LINE Notify กับ PIR Sensor ซึ่งอาจนำไปปรับปรุงให้เป็นระบบการแจ้งเตือนการบุกรุกได้ในอนาคต

โปรแกรมการประยุกต์ใช้ LINE Notify กับ PIR Sensor

  • ตัวอย่างโปรแกรมสามารถดาวน์โหลดได้จาก rpi-pir-line-notify.py
#!/usr/local/bin/python
# -*- coding: utf-8 -*-

import requests
import RPi.GPIO as GPIO
import time
import datetime

# LINE notify
url = 'https://notify-api.line.me/api/notify'
token = 'MQxJL1wXd7e71PDUOUvFK3eYyqq4VySHQRVMfaobwiw'
headers = {'content-type':'application/x-www-form-urlencoded','Authorization':'Bearer '+token}

GPIO.setwarnings(False)
#GPIO.setmode(GPIO.BOARD)
GPIO.setmode(GPIO.BCM)

pir_sensor_pin = 17
led_pin = 4

GPIO.setup(pir_sensor_pin, GPIO.IN)    #Read output from PIR motion sensor
GPIO.setup(led_pin, GPIO.OUT)           #LED output pin

for i in range(0,3):
        GPIO.output(led_pin,True)
        #print("LED ON")
        time.sleep(1)

        GPIO.output(led_pin,False)
        #print("LED OFF")
        time.sleep(0.5)

print("Starting PIR motion sensor...")

while(True):
        if(GPIO.input(pir_sensor_pin)):
                for i in range(0,2):
                        GPIO.output(led_pin, True)
                        time.sleep(1)
                        GPIO.output(led_pin, False)
                        time.sleep(0.5)

                # MSG
                dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
                msg = 'Motion Detected: ' + dt

                r = requests.post(url, headers=headers, data = {'message':msg})
                print r.text

                print msg
                time.sleep(3)
        time.sleep(1) #loop delay

GPIO.cleanup()

การเรียกใช้งาน

จากตัวอย่างข้างต้นได้กำหนดชื่อไฟล์คือ rpi-pir-line-notify.py ดังนั้นสามารถ run โปรแกรมดังนี้

$ python rpi-pir-line-notify.py

ผลลัพธ์ที่ได้

Starting PIR motion sensor...
{"status":200,"message":"ok"}
Motion Detected: 2018-08-01 01:23
{"status":200,"message":"ok"}
Motion Detected: 2018-08-01 01:24

ผลลัพธ์ที่ส่ง LINE Notify Line Notify Mobile

อธิบายโปรแกรมเพิ่มเติม

กำหนดให้ระบบหยุดรอ ก่อนจะเริ่มทำงานใหม่อีกครั้ง

import time

time.sleep(1)

จากตัวอย่างข้างต้น time.sleep(1) สั่งให้ระบบหยุดรอ 1 วินาที

  • การกำหนดข้อความ (msg) ที่จะใช้ส่ง โดยจะแสดงข้อความ และวันเวลา
import datetime

dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
msg = 'Motion Detected: ' + dt
print msg

ผลลัพธ์ที่ได้คือ

Motion Detected: 2018-08-01 01:12

การสั่งให้หลอด LED กระพริบ

for i in range(0,3):
        GPIO.output(led_pin,True)
        #print("LED ON")
        time.sleep(1)

        GPIO.output(led_pin,False)
        #print("LED OFF")
        time.sleep(0.5)
  • จากตัวอย่างสั่งให้หลอด LED กระพริบ 3 ครั้ง จากคำสั่ง for i in range(0,3):
  • สั่งให้เปิดหลอด LED 1 วินาที
GPIO.output(led_pin,True)
time.sleep(1)
  • สั่งให้ปิดหลอด LED 0.5 วินาที
GPIO.output(led_pin,False)
time.sleep(0.5)
Clone this wiki locally