Taisomas HC-SR501

Jump to navigation Jump to search

Dėmesio: Jūs nesate prisijungęs. Jūsų IP adresas bus viešai matomas, jei atliksite kokius nors keitimus. Jeigu prisijungsite arba sukursite paskyrą, jūsų keitimai bus priskirti jūsų naudotojo vardui; drauge įgysite naujų galimybių.

Keitimas gali būti atšauktas. Prašome patikrinti palyginimą, esantį žemiau, kad patvirtintumėte, kad jūs tai ir norite padaryti, ir tada išsaugokite pakeitimus, esančius žemiau, kad užbaigtumėte keitimo atšaukimą.

Dabartinė versija Jūsų tekstas
7 eilutė: 7 eilutė:
 
= RaspberryPI =
 
= RaspberryPI =
  
== Sujungimas ==
+
== Kodas ==
 
 
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
 +
import requests
  
GPIO.setmode(GPIO.BCM)
 
  
PIR_PIN = 4
+
sensor = 8
CNT = 0
 
  
GPIO.setup(PIR_PIN, GPIO.IN)
+
GPIO.setmode(GPIO.BCM)
 +
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)
  
def MOTION(PIR_PIN):
+
previous_state = False
  print "Motion Detected"
+
current_state = False
 
 
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();
 
  
 +
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")
 
</syntaxhighlight>
 
</syntaxhighlight>
  
251 eilutė: 108 eilutė:
  
 
[[Category:RaspberryPI]]
 
[[Category:RaspberryPI]]
[[Category:OrangePI]]
 

Primename, kad viskas, kas patenka į Žinynas, yra skelbiama pagal GNU Free Documentation License 1.2 (plačiau – Žinynas:Autorinės teisės). Jei nenorite, kad jūsų indėlis būtų be gailesčio kaitaliojamas ir platinamas, nerašykite čia.
Jūs taip pat pasižadate, kad tai jūsų pačių rašytas turinys arba kopijuotas iš viešų ar panašių nemokamų šaltinių. Nekopijuokite autorinėmis teisėmis apsaugotų darbų be leidimo!

Kad apsaugotume vikį nuo automatinio keitimų šlamšto, prašome išspręsti šį CAPTCHA:

Atšaukti Kaip redaguoti (atsidaro naujame lange)