rpirf

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2023 License: GPL-2.0 Imports: 6 Imported by: 1

README

rpirf

A library for sending 433mhz signals via a Raspberry Pi.

Protocol and logic ported from https://github.com/milaq/rpi-rf

Supported Hardware

Sender

Most generic 433/315MHz capable modules connected via GPIO to a Raspberry Pi will work with this library. The picture below displays a unit which has been tested.

433mhz sender

Outlets / Sockets

Most generic 433mhz remote controlled outlets do work. The picture below displays a outlet which works with the sender and library.

generic socket

The library has been tested on following outlets using a Raspberry Pi 3b and a Raspberry Pi 2b, both running Raspbian Lite.

Limitations

As of now (March 14 2022), the library does not support receiving (sniffing) signals in order to send them later.

However, a Python library by milaq/rpi-rf is recommended, which supports sending as well as receiving data.

However, the go rpirf library is only supposed to support sending.

Installation / Setup

To install the library in your current go project, go get it using following command:

go get github.com/smarthome-go/rpirf

You can then import the library in your project using following code

import "github.com/smarthome-go/rpirf"

Getting started

Creating a new instance

Before data can be sent, the physical device must be set up, and some basic parameters, for example pulselength or protocol must be set.

device := rpirf.NewRF(17, 1, 10, 180, 24)

The following parameters describe

  • the BCM pin number
  • Protocol to use
  • How often each code should be sent (for redundancy)
  • The pulselength
  • The content length
Sending codes

After the device has been set up, any int can be sent as a code.
The Send method encodes the provided code to binary and sends it using the previously configured hardware.

device.Send(5121438)
Cleaning up

After all data has been sent, it is recommended to clean the device. However, in a typical setup, this function is either defered or called on program exit.

device.Cleanup()

Make sure to implement proper error handling for the functions above.
For a complete reference, take a look at the Example

Example

package main

import (
	"github.com/smarthome-go/rpirf"
)

func main() {
	device, err := rpirf.NewRF(17, 1, 10, 180, 24)
	if err != nil {
		panic(err.Error())
	}
	if err := device.Send(123456); err != nil {
		panic(err.Error())
	}
	if err := device.Cleanup(); err != nil {
		panic(err.Error())
	}

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotInitialized     = errors.New("cannot send code: device is not initialized. make sure to initialize the device first")
	ErrCleanWOInitialized = errors.New("cannot cleanup a non-initialized device")
	ErrCleanup            = errors.New("failed to cleanup: could not close rpio")
	ErrInitialize         = errors.New("failed to initialize device: could not open rpio")
	ErrNonArm             = errors.New("unsupported architecture: this library only works on the raspberry pi (arm)")
)

Functions

This section is empty.

Types

type HardwareOutput added in v1.1.0

type HardwareOutput interface {
	Kind() HardwareOutputKind
	Low() error
	High() error
}

type HardwareOutputCharacterdev added in v1.1.0

type HardwareOutputCharacterdev struct {
	LineHandle *gpiod.Line
}

func NewCharacterDev added in v1.1.0

func NewCharacterDev(devicePath string, pinNumber int) (HardwareOutputCharacterdev, error)

func (HardwareOutputCharacterdev) High added in v1.1.0

func (self HardwareOutputCharacterdev) High() error

func (HardwareOutputCharacterdev) Kind added in v1.1.0

func (HardwareOutputCharacterdev) Low added in v1.1.0

func (self HardwareOutputCharacterdev) Low() error

type HardwareOutputKind added in v1.1.0

type HardwareOutputKind uint8
const (
	HardwareOutputRpi HardwareOutputKind = iota
	HardwareOutputCdev
)

type HardwareOutputRaspberryPi added in v1.1.0

type HardwareOutputRaspberryPi struct {
	Pin rpio.Pin
}

func NewRaspberryPi added in v1.1.0

func NewRaspberryPi(pinNumber uint) (HardwareOutputRaspberryPi, error)

func (HardwareOutputRaspberryPi) High added in v1.1.0

func (self HardwareOutputRaspberryPi) High() error

func (HardwareOutputRaspberryPi) Kind added in v1.1.0

func (HardwareOutputRaspberryPi) Low added in v1.1.0

func (self HardwareOutputRaspberryPi) Low() error

type Protocol

type Protocol struct {
	Pulselength uint16
	SyncHigh    uint8
	SyncLow     uint8
	ZeroHigh    uint8
	ZeroLow     uint8
	OneHigh     uint8
	OneLow      uint8
}

type RFDevice

type RFDevice struct {
	Output        HardwareOutput
	TxEnabled     bool
	TxProto       uint8
	TxRepeat      uint8
	TxLength      uint8
	TxPulseLength uint16
}

func NewRF

func NewRF(hardware HardwareOutput, protocolIndex uint8, repeat uint8, pulseLength uint16, length uint8) RFDevice

Initializes the GPIO device Provide a pin number, a protocol, how often the signal should be sent, the pulse length and the data length The pin number will be the `BCM / bcm2835` pin, not the physical one

func (*RFDevice) Cleanup

func (device *RFDevice) Cleanup() error

Disables the transmitter and frees the allocated GPIO pin

func (*RFDevice) Send

func (device *RFDevice) Send(code int) error

Sends the provided decimal number as a binary code

Jump to

Keyboard shortcuts

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