HC-SR501: Skirtumas tarp puslapio versijų

Iš Žinynas.
Jump to navigation Jump to search
2 eilutė: 2 eilutė:
  
 
[[Vaizdas:Introduction-to-HC-SR501.jpg|600px]]
 
[[Vaizdas:Introduction-to-HC-SR501.jpg|600px]]
 +
 +
Sensitivity reguliatorių patartina atsukti beveik iki max, time delay į minimum. Perjungti jumperį į kitą padėtį nei parodyta pav.
  
 
= RaspberryPI =
 
= RaspberryPI =
41 eilutė: 43 eilutė:
  
 
  VVC = PIN 2
 
  VVC = PIN 2
  OUTPUT = PIN 7
+
  OUTPUT = PIN 11
 
  GND = PIN 6
 
  GND = PIN 6
  
 
== Kodas ==
 
== Kodas ==
  
Sujudėjus bandytams, bus pranešta į telegramą.
+
Sujudėjus bandytams, bus įjungta šviesa ir pranešta į telegramą.  
  
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
54 eilutė: 56 eilutė:
 
from pyA20.gpio import port
 
from pyA20.gpio import port
 
import requests
 
import requests
 +
from time import gmtime, strftime
 
import time
 
import time
 +
import os
  
# nustatymai
+
# initialize GPIO
TG_BOTHSH = "" # boto tokenas
+
TG_BOTHSH = ""
TG_CHATID = "" # chat id (gausime per getUpdates)
+
TG_CHATID = ""
 
SENSOR_LOCATION = "Main room"
 
SENSOR_LOCATION = "Main room"
 
sensor = port.PA6
 
sensor = port.PA6
# kodas
 
 
gpio.init()
 
gpio.init()
 
gpio.setcfg(sensor, gpio.INPUT)
 
gpio.setcfg(sensor, gpio.INPUT)
 +
print "Sensor initializing..."
 +
time.sleep(2)
 +
print "Active"
 +
print "Press Ctrl+C to abort the program"
 
previous_state = False
 
previous_state = False
 
current_state = False
 
current_state = False
 
LOW = 1
 
LOW = 1
 
HIGH = 0
 
HIGH = 0
 +
LIGHT_PORT = 20
 
URL = "https://api.telegram.org/{}/".format(TG_BOTHSH)
 
URL = "https://api.telegram.org/{}/".format(TG_BOTHSH)
 +
 +
def switch_lights(state):
 +
  url = "http://192.168.13.102:1415/turn/{}/{}".format(LIGHT_PORT,state)
 +
  print "Turning lights: ",state
 +
  requests.get(url)
  
 
def send_message(text):
 
def send_message(text):
 
     url = URL + "sendMessage?text={}&chat_id={}".format(text, TG_CHATID)
 
     url = URL + "sendMessage?text={}&chat_id={}".format(text, TG_CHATID)
 
     requests.get(url)
 
     requests.get(url)
 +
 +
if os.getenv("TZ"):
 +
    os.unsetenv("TZ")
  
 
while True:
 
while True:
81 eilutė: 97 eilutė:
 
           print("GPIO pin %s is %s" % (sensor, new_state))
 
           print("GPIO pin %s is %s" % (sensor, new_state))
 
           if current_state == HIGH:
 
           if current_state == HIGH:
             print "Motion detected"
+
            d1 = strftime("%Y-%m-%d %H:%M:%S", gmtime())
 +
             print d1,"-> Motion detected"
 +
            #turn lights on for 10 seconds
 +
            switch_lights("on")
 +
            time.sleep(10)
 +
            #turn the lights off again
 +
            switch_lights("off")
 
             send_message("Motion detected on {}".format(SENSOR_LOCATION))
 
             send_message("Motion detected on {}".format(SENSOR_LOCATION))
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:RaspberryPI]]
 
[[Category:RaspberryPI]]

17:42, 7 rugsėjo 2019 versija

Judesio daviklis.

Introduction-to-HC-SR501.jpg

Sensitivity reguliatorių patartina atsukti beveik iki max, time delay į minimum. Perjungti jumperį į kitą padėtį nei parodyta pav.

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 11
GND = PIN 6

Kodas

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

#!/usr/bin/python

from pyA20.gpio import gpio
from pyA20.gpio import port
import requests
from time import gmtime, strftime
import time
import os

# initialize GPIO
TG_BOTHSH = ""
TG_CHATID = ""
SENSOR_LOCATION = "Main room"
sensor = port.PA6
gpio.init()
gpio.setcfg(sensor, gpio.INPUT)
print "Sensor initializing..."
time.sleep(2)
print "Active"
print "Press Ctrl+C to abort the program"
previous_state = False
current_state = False
LOW = 1
HIGH = 0
LIGHT_PORT = 20
URL = "https://api.telegram.org/{}/".format(TG_BOTHSH)

def switch_lights(state):
   url = "http://192.168.13.102:1415/turn/{}/{}".format(LIGHT_PORT,state)
   print "Turning lights: ",state
   requests.get(url)

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

if os.getenv("TZ"):
    os.unsetenv("TZ")

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:
            d1 = strftime("%Y-%m-%d %H:%M:%S", gmtime())
            print d1,"-> Motion detected"
            #turn lights on for 10 seconds
            switch_lights("on")
            time.sleep(10)
            #turn the lights off again
            switch_lights("off")
            send_message("Motion detected on {}".format(SENSOR_LOCATION))