HC-SR501: Skirtumas tarp puslapio versijų
Jump to navigation
Jump to search
(telegram supportas ant orangepi) |
|||
| (nerodoma 5 tarpinės versijos, sukurtos to paties naudotojo) | |||
| 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 = | ||
| − | == Kodas == | + | == Sujungimas == |
| + | |||
| + | Visi pinai aprašyti [[RaspberryPI|čia]]. Sujungimas: | ||
| + | |||
| + | VVC = PIN 2 | ||
| + | OUTPUT = PIN 7 | ||
| + | GND = PIN 6 | ||
| + | |||
| + | == Golang kodas == | ||
| + | |||
| + | <syntaxhighlight lang="go"> | ||
| + | package main | ||
| + | |||
| + | import ( | ||
| + | . "github.com/cyoung/rpi" | ||
| + | "fmt" | ||
| + | "time" | ||
| + | ) | ||
| + | |||
| + | func main() { | ||
| + | WiringPiSetup() | ||
| + | pin := 7 | ||
| + | go func() { | ||
| + | last_time := time.Now().UnixNano() / 1000000 | ||
| + | motion_detected := 0 | ||
| + | for pinas := range WiringPiISR(BoardToPin(pin), INT_EDGE_FALLING) { | ||
| + | if pinas > -1 { | ||
| + | n := time.Now().UnixNano() / 1000000 | ||
| + | delta := n - last_time | ||
| + | if delta > 800 { //software debouncing | ||
| + | fmt.Printf("Motion detected (Total detected %d times)\n",motion_detected) | ||
| + | last_time = n | ||
| + | motion_detected++ | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | }() | ||
| + | |||
| + | for { | ||
| + | // empty cycle | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | == Golang HomeKit integracija == | ||
| + | [[Vaizdas:Pirdeactivated pav01.png|400px]] | ||
| + | [[Vaizdas:Piractivated pav02.png|400px]] | ||
| + | |||
| + | <syntaxhighlight lang="go"> | ||
| + | /* | ||
| + | Copyright (c) 2020 e1z0 | ||
| + | Licensed under BSD License | ||
| + | To make it work under OrangePI devices please use WiringOP branch of wiring pi | ||
| + | git clone https://github.com/zhaolei/WiringOP.git -b h3 | ||
| + | ./build | ||
| + | make install | ||
| + | */ | ||
| + | package main | ||
| + | |||
| + | import ( | ||
| + | "github.com/brutella/hc" | ||
| + | "github.com/brutella/hc/accessory" | ||
| + | "github.com/brutella/hc/service" | ||
| + | "github.com/brutella/hc/log" | ||
| + | "fmt" | ||
| + | . "github.com/cyoung/rpi" | ||
| + | "time" | ||
| + | "os" | ||
| + | |||
| + | ) | ||
| + | |||
| + | func main() { | ||
| + | WiringPiSetup() | ||
| + | log.Debug.Enable() | ||
| + | |||
| + | info := accessory.Info{Name: "Pir Sensor", FirmwareRevision: "v1.0", Manufacturer: "e1z0", Model: "HC-SR501", SerialNumber: "iddqd"} | ||
| + | |||
| + | |||
| + | PirSensor := NewMotionSensor(info) | ||
| + | t, err := hc.NewIPTransport(hc.Config{StoragePath: "data", Pin: "00102003"}, PirSensor.Accessory) | ||
| + | if err != nil { | ||
| + | fmt.Printf("eruoras: %s\n",err) | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | pin := 7 | ||
| + | triggered := false | ||
| + | go func() { | ||
| + | last_time := time.Now().UnixNano() / 1000000 | ||
| + | motion_detected := 0 | ||
| + | for pinas := range WiringPiISR(BoardToPin(pin), INT_EDGE_FALLING) { | ||
| + | if pinas > -1 && triggered == false { | ||
| + | n := time.Now().UnixNano() / 1000000 | ||
| + | delta := n - last_time | ||
| + | if delta > 800 { //software debouncing | ||
| + | fmt.Printf("Motion detected (Total detected %d times)\n",motion_detected) | ||
| + | PirSensor.MotionSensor.MotionDetected.SetValue(true) | ||
| + | last_time = n | ||
| + | motion_detected++ | ||
| + | triggered = true | ||
| + | timer1 := time.NewTimer(time.Second*5) | ||
| + | go func() { | ||
| + | <-timer1.C | ||
| + | triggered = false | ||
| + | PirSensor.MotionSensor.MotionDetected.SetValue(false) | ||
| + | }() | ||
| + | |||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | }() | ||
| + | hc.OnTermination(func() { | ||
| + | t.Stop() | ||
| + | time.Sleep(time.Second*5) | ||
| + | os.Exit(0) | ||
| + | }) | ||
| + | t.Start() | ||
| + | } | ||
| + | |||
| + | |||
| + | type MotionSensor struct { | ||
| + | *accessory.Accessory | ||
| + | MotionSensor *service.MotionSensor | ||
| + | } | ||
| + | |||
| + | func NewMotionSensor(info accessory.Info) *MotionSensor { | ||
| + | acc := MotionSensor{} | ||
| + | acc.Accessory = accessory.New(info, 10) | ||
| + | acc.MotionSensor = service.NewMotionSensor() | ||
| + | acc.AddService(acc.MotionSensor.Service) | ||
| + | return &acc | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | == Python Kodas == | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
#!/usr/bin/python | #!/usr/bin/python | ||
| − | |||
import RPi.GPIO as GPIO | import RPi.GPIO as GPIO | ||
import time | import time | ||
| − | |||
| − | + | GPIO.setmode(GPIO.BCM) | |
| + | |||
| + | PIR_PIN = 4 | ||
| + | CNT = 0 | ||
| + | |||
| + | GPIO.setup(PIR_PIN, GPIO.IN) | ||
| + | |||
| + | def MOTION(PIR_PIN): | ||
| + | print "Motion Detected" | ||
| + | |||
| + | print "PIR Module Test (CTRL+C to exit)" | ||
| + | time.sleep(2) | ||
| + | print "Ready" | ||
| − | |||
| − | |||
| − | + | try: | |
| − | + | GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION) | |
| + | while 1: | ||
| + | time.sleep(100) | ||
| + | except KeyboardInterrupt: | ||
| + | print "Quit" | ||
| + | GPIO.cleanup(); | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| 40 eilutė: | 186 eilutė: | ||
VVC = PIN 2 | VVC = PIN 2 | ||
| − | OUTPUT = PIN | + | 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"> | ||
| 53 eilutė: | 199 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 | ||
| − | # | + | # initialize GPIO |
| − | TG_BOTHSH = "" | + | TG_BOTHSH = "" |
| − | TG_CHATID = "" | + | TG_CHATID = "" |
SENSOR_LOCATION = "Main room" | SENSOR_LOCATION = "Main room" | ||
sensor = port.PA6 | sensor = port.PA6 | ||
| − | |||
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: | ||
| 80 eilutė: | 240 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]] | ||
| + | [[Category:OrangePI]] | ||
Dabartinė 13:15, 15 rugsėjo 2020 versija
Judesio daviklis.
Sensitivity reguliatorių patartina atsukti beveik iki max, time delay į minimum. Perjungti jumperį į kitą padėtį nei parodyta pav.
RaspberryPI[keisti]
Sujungimas[keisti]
Visi pinai aprašyti čia. Sujungimas:
VVC = PIN 2 OUTPUT = PIN 7 GND = PIN 6
Golang kodas[keisti]
package main
import (
. "github.com/cyoung/rpi"
"fmt"
"time"
)
func main() {
WiringPiSetup()
pin := 7
go func() {
last_time := time.Now().UnixNano() / 1000000
motion_detected := 0
for pinas := range WiringPiISR(BoardToPin(pin), INT_EDGE_FALLING) {
if pinas > -1 {
n := time.Now().UnixNano() / 1000000
delta := n - last_time
if delta > 800 { //software debouncing
fmt.Printf("Motion detected (Total detected %d times)\n",motion_detected)
last_time = n
motion_detected++
}
}
}
}()
for {
// empty cycle
}
}
Golang HomeKit integracija[keisti]
/*
Copyright (c) 2020 e1z0
Licensed under BSD License
To make it work under OrangePI devices please use WiringOP branch of wiring pi
git clone https://github.com/zhaolei/WiringOP.git -b h3
./build
make install
*/
package main
import (
"github.com/brutella/hc"
"github.com/brutella/hc/accessory"
"github.com/brutella/hc/service"
"github.com/brutella/hc/log"
"fmt"
. "github.com/cyoung/rpi"
"time"
"os"
)
func main() {
WiringPiSetup()
log.Debug.Enable()
info := accessory.Info{Name: "Pir Sensor", FirmwareRevision: "v1.0", Manufacturer: "e1z0", Model: "HC-SR501", SerialNumber: "iddqd"}
PirSensor := NewMotionSensor(info)
t, err := hc.NewIPTransport(hc.Config{StoragePath: "data", Pin: "00102003"}, PirSensor.Accessory)
if err != nil {
fmt.Printf("eruoras: %s\n",err)
}
pin := 7
triggered := false
go func() {
last_time := time.Now().UnixNano() / 1000000
motion_detected := 0
for pinas := range WiringPiISR(BoardToPin(pin), INT_EDGE_FALLING) {
if pinas > -1 && triggered == false {
n := time.Now().UnixNano() / 1000000
delta := n - last_time
if delta > 800 { //software debouncing
fmt.Printf("Motion detected (Total detected %d times)\n",motion_detected)
PirSensor.MotionSensor.MotionDetected.SetValue(true)
last_time = n
motion_detected++
triggered = true
timer1 := time.NewTimer(time.Second*5)
go func() {
<-timer1.C
triggered = false
PirSensor.MotionSensor.MotionDetected.SetValue(false)
}()
}
}
}
}()
hc.OnTermination(func() {
t.Stop()
time.Sleep(time.Second*5)
os.Exit(0)
})
t.Start()
}
type MotionSensor struct {
*accessory.Accessory
MotionSensor *service.MotionSensor
}
func NewMotionSensor(info accessory.Info) *MotionSensor {
acc := MotionSensor{}
acc.Accessory = accessory.New(info, 10)
acc.MotionSensor = service.NewMotionSensor()
acc.AddService(acc.MotionSensor.Service)
return &acc
}
Python Kodas[keisti]
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
PIR_PIN = 4
CNT = 0
GPIO.setup(PIR_PIN, GPIO.IN)
def MOTION(PIR_PIN):
print "Motion Detected"
print "PIR Module Test (CTRL+C to exit)"
time.sleep(2)
print "Ready"
try:
GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)
while 1:
time.sleep(100)
except KeyboardInterrupt:
print "Quit"
GPIO.cleanup();
OrangePI[keisti]
Sujungimas[keisti]
Visi pinai aprašyti čia. Sujungimas:
VVC = PIN 2 OUTPUT = PIN 11 GND = PIN 6
Kodas[keisti]
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))