HC-SR501

Iš Žinynas.
Jump to navigation Jump to search

Judesio daviklis.

Introduction-to-HC-SR501.jpg

RaspberryPI

Kodas

#!/usr/bin/python

import RPi.GPIO as GPIO
import time
import requests


sensor = 8

GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)

previous_state = False
current_state = False

while True:
  time.sleep(0.1)
  previous_state = current_state
  current_state = GPIO.input(sensor)
  if current_state != previous_state:
    new_state = "HIGH" if current_state else "LOW"
    print("GPIO pin %s is %s" % (sensor, new_state))
# turn the lights on :)
   requests.get(("http://192.168.254.102:1415/turn/20/on")

OrangePI

Sujungimas

Visi pinai aprašyti čia. Sujungimas:

VVC = PIN 2
OUTPUT = PIN 7
GND = PIN 6

Kodas

Sujudėjus bandytams, bus pranešta į telegramą.

#!/usr/bin/python

from pyA20.gpio import gpio
from pyA20.gpio import port
import requests
import time

# nustatymai
TG_BOTHSH = "" # boto tokenas
TG_CHATID = "" # chat id (gausime per getUpdates)
SENSOR_LOCATION = "Main room"
sensor = port.PA6
# kodas
gpio.init()
gpio.setcfg(sensor, gpio.INPUT)
previous_state = False
current_state = False
LOW = 1
HIGH = 0
URL = "https://api.telegram.org/{}/".format(TG_BOTHSH)

def send_message(text):
    url = URL + "sendMessage?text={}&chat_id={}".format(text, TG_CHATID)
    requests.get(url)

while True:
    previous_state = current_state
    current_state = gpio.input(sensor)
    if current_state != previous_state:
          new_state = "LOW" if current_state else "HIGH"
          print("GPIO pin %s is %s" % (sensor, new_state))
          if current_state == HIGH:
            print "Motion detected"
            send_message("Motion detected on {}".format(SENSOR_LOCATION))