Skip to content

Commit

Permalink
Add support for attached motion sensor on GPIO
Browse files Browse the repository at this point in the history
This assumes running on Raspberry Pi, using an HC-SR501. This device
outputs a HIGH signal when motion is detected, or a low signal when
no motion is detected after a certain delay.

Can be set by specifying --motion-pin to the GPIO pin on the raspberry
to which the HC-SR501 data pin is connected.

Implements #5
  • Loading branch information
pieter committed Jul 17, 2019
1 parent 6a81223 commit 8a12572
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cmd/hkcam/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package main

import (
"flag"

"github.com/brutella/hc"
"github.com/brutella/hc/accessory"
"github.com/brutella/hc/log"
"github.com/brutella/hc/service"

"github.com/stianeikeland/go-rpio/v4"

"image"
"runtime"

"time"

"github.com/brutella/hkcam"
"github.com/brutella/hkcam/ffmpeg"
)
Expand All @@ -21,13 +27,15 @@ func main() {
var loopbackFilename *string
var h264Encoder *string
var h264Decoder *string
var motionPin int = -1

if runtime.GOOS == "linux" {
inputDevice = flag.String("input_device", "v4l2", "video input device")
inputFilename = flag.String("input_filename", "/dev/video0", "video input device filename")
loopbackFilename = flag.String("loopback_filename", "/dev/video1", "video loopback device filename")
h264Decoder = flag.String("h264_decoder", "", "h264 video decoder")
h264Encoder = flag.String("h264_encoder", "h264_omx", "h264 video encoder")
flag.IntVar(&motionPin, "motion_pin", -1, "GPIO pin of any attached motion sensor")
} else if runtime.GOOS == "darwin" { // macOS
inputDevice = flag.String("input_device", "avfoundation", "video input device")
inputFilename = flag.String("input_filename", "default", "video input device filename")
Expand Down Expand Up @@ -63,6 +71,25 @@ func main() {
MultiStream: *multiStream,
}

// Add Motion Sensor
if motionPin != -1 {
err := rpio.Open()
if err != nil {
log.Info.Panic(err)
}
pin := rpio.Pin(10)
pin.Input()

motionSensor := service.NewMotionSensor()
cam.AddService(motionSensor.Service)

go func() {
for range time.Tick(time.Second) {
motionSensor.MotionDetected.SetValue(pin.Read() == rpio.High)
}
}()
}

ffmpeg := hkcam.SetupFFMPEGStreaming(cam, cfg)

// Add a custom camera control service to record snapshots
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ require (
github.com/brutella/hc v1.2.0
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/radovskyb/watcher v1.0.6
github.com/stianeikeland/go-rpio/v4 v4.4.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/radovskyb/watcher v1.0.6 h1:8WIQ9UxEYMZjem1OwU7dVH94DXXk9mAIE1i8eqHD+IY=
github.com/radovskyb/watcher v1.0.6/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg=
github.com/stianeikeland/go-rpio/v4 v4.4.0 h1:LScvNyXHF412co42LG5t7bvBDbtDAhLF828ebaGqmjA=
github.com/stianeikeland/go-rpio/v4 v4.4.0/go.mod h1:BkK52zk+FRk8wCTDf88/86Sojc+NfUiCAHd1ZV3RuTM=
github.com/tadglines/go-pkgs v0.0.0-20140924210655-1f86682992f1 h1:ms/IQpkxq+t7hWpgKqCE5KjAUQWC24mqBrnL566SWgE=
github.com/tadglines/go-pkgs v0.0.0-20140924210655-1f86682992f1/go.mod h1:roo6cZ/uqpwKMuvPG0YmzI5+AmUiMWfjCBZpGXqbTxE=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 h1:mKdxBk7AujPs8kU4m80U72y/zjbZ3UcXC7dClwKbUI0=
Expand Down

0 comments on commit 8a12572

Please sign in to comment.