diff --git a/cmd/hkcam/main.go b/cmd/hkcam/main.go index 5bfc2bb..753e4ec 100644 --- a/cmd/hkcam/main.go +++ b/cmd/hkcam/main.go @@ -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" ) @@ -21,6 +27,7 @@ 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") @@ -28,6 +35,7 @@ func main() { 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") @@ -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 diff --git a/go.mod b/go.mod index 7a97e74..2dd80a7 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index c372624..10428f6 100644 --- a/go.sum +++ b/go.sum @@ -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=