sr501

package module
v0.0.0-...-d344d15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2020 License: MIT Imports: 2 Imported by: 0

README

HC-SR501 Sensor Go API

GoDoc

Provides a simple, easy-to-use API for interfacing with the HC-SR501 IR motion sensor on the Raspberry Pi platform using GPIO pins.

Features
  • Simple API for easily detecting and responding to motion events
  • Leverages [Periph.io]'s GPIO library for low-level communication on Pi hardware
  • Customizable pin selection for binding event listeners to
Installation
go get github.com/littlehawk93/go-sr501
Getting started

Basic working example

wg := sync.WaitGroup{}
wg.Add(1)

// Bind the sensor signal pin to GPIO Pin #17
sensor, err := sr501.NewSensor("17", func() {

    // Print that motion was detected and unlock the wait group
    log.Println("MOTION DETECTED!")
    wg.Done()
})

if err != nil {
    log.Fatalf("Error initializing motion sensor: %s", err.Error())
}

defer sensor.Close()

sensor.Begin()

// Wait until motion has been detected before proceeding
wg.Wait()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DetectHandler

type DetectHandler func()

DetectHandler event handler whenever the motion sensor detects motion

type Sensor

type Sensor struct {
	// contains filtered or unexported fields
}

Sensor handles interfacing with the HC-SR501 PIR motion sensor

func NewSensor

func NewSensor(pin string, handler DetectHandler) (*Sensor, error)

NewSensor creates and initializes a new Motion Sensor

func (*Sensor) Begin

func (me *Sensor) Begin()

Begin begin listening for motion using the motion sensor. Any subsequent 'Begin' calls are ignored

func (*Sensor) Close

func (me *Sensor) Close()

Close closes this sensor and stops firing any event listeners for detected motion Any subsequent calls to 'Close' after the first are ignored. The 'Close' method call is ignored if 'Begin' has not yet been called

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL