HC-SR501: Skirtumas tarp puslapio versijų

Iš Žinynas.
Jump to navigation Jump to search
(telegram supportas ant orangepi)
29 eilutė: 29 eilutė:
 
     new_state = "HIGH" if current_state else "LOW"
 
     new_state = "HIGH" if current_state else "LOW"
 
     print("GPIO pin %s is %s" % (sensor, new_state))
 
     print("GPIO pin %s is %s" % (sensor, new_state))
     httpresponse = urllib.urlopen ("http://xxx.xxx.xxx.xxx:8080/json.htm?type=command&param=switchlight&idx=XX&switchcmd=On")
+
# turn the lights on :)
 +
     httpresponse = urllib.urlopen ("http://192.168.254.102:1415/turn/20/on")
 
</syntaxhighlight>
 
</syntaxhighlight>
  
43 eilutė: 44 eilutė:
  
 
== Kodas ==
 
== Kodas ==
 +
 +
Sujudėjus bandytams, bus pranešta į telegramą.
  
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
from time import sleep
+
#!/usr/bin/python
 +
 
 
from pyA20.gpio import gpio
 
from pyA20.gpio import gpio
 
from pyA20.gpio import port
 
from pyA20.gpio import port
mq135 = port.PA6
+
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.init()
gpio.setcfg(mq135, gpio.INPUT)
+
gpio.setcfg(sensor, gpio.INPUT)
try:
+
previous_state = False
     while True:
+
current_state = False
        state = gpio.input(mq135)
+
LOW = 1
        sleep(0.2)
+
HIGH = 0
        print "Statusas: %d"%state
+
URL = "https://api.telegram.org/{}/".format(TG_BOTHSH)
except KeyboardInterrupt:
+
 
    print ("Goodbye.")
+
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))
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:RaspberryPI]]
 
[[Category:RaspberryPI]]

20:49, 8 liepos 2019 versija

Judesio daviklis.

Introduction-to-HC-SR501.jpg

RaspberryPI

Kodas

#!/usr/bin/python

import RPi.GPIO as GPIO
import time
import urllib

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 :)
    httpresponse = urllib.urlopen ("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))