rkoshak / sensorReporter

A python based service that receives sensor inputs and publishes them in various ways.
Apache License 2.0
105 stars 41 forks source link
bluetooth gpio mqtt openhab rest-api

sensor_reporter

sensor_reporter is a Python 3 script that bridges sensors and actuators to MQTT or openHAB's REST API. It's a modular script that allows for relatively easy implementation of new capabilities with minimal effort. If you've used sensorReporter or mqttReporter before, this is a complete rewrite with many breaking changes. See the release notes below for details.

A number of connections, sensors, and actuators are currently supported.

Go into the subfolders for details in each subfolder's README.

Plug-in Type Purpose
bt.btle_sensor.BtleSensor Background Sensor Scans for BTLE advertisements from configured devices.
bt.btscan_sensor.SimpleBtSensor Polling Sensor Scans for the presence of BT devices with a given addresses.
bt.govee_sensor.GoveeSensor Background Sensor Receives BTLE packets from Govee H5075 temperature and humidity sensors.
energymeter.read_meter_values.Pafal20ec3gr Polling Sensor Periodically reads out an energymeter using serial device. Currently only Pafal 20ec3gr supported.
exec.exec_actuator.ExecActuator Actuator On command, executes the configured command line command.
exec.exec_sensor.ExecSensor Polling Sensor Executes the configured command line and publishes the result.
gpio.dht_sensor.DhtSensor Polling Sensor Publishes temperature and humidity readings from DHT11, DHT22, and AM2302 sensors connected to GPIO pins.
gpio.rpi_gpio.RpiGpioSensor Polling or Background Sensor Publsihes ON/OFF messages based on the state of a GPIO pin.
gpio.rpi_gpio.RpiGpioActuator Actuator Sets a GPIO pin to a given state on command.
gpio.gpio_led.GpioColorLED Actuator Commands 3 to 4 GPIO pins to control a RGB or RGBW LED
heartbeat.heartbeat.Heartbeat Polling Sensor Publishes the amount of time sensor_reporter has been up as number of msec and as DD:HH:MM:SS.
local.local_conn.LocalConnection Connection Allows sensors to call actuators.
local.local_logic.LogicOr Actuator Forwards commands from multiple inputs locally to an actuator.
mqtt.mqtt_conn.MqttConnection Connection Allows Actuators to subscribe and publish and Sensors to publish results to a MQTT server.
mqtt.homie_conn.HomieConnection Connection Subscribe and publish sensors and actuators to a MQTT server via Homie convention.
network.arp_sensor.ArpSensor Polling Sensor Periodically gets and parses the ARP table for given mac addresses.
network.dash_sensor.DashSensor Background Sensor Watches for Amazon Dash Button ARP packets.
one_wire.ds18x20_sensor.Ds18x20Sensor Polling Sensor Publishes temperature reading from DS18S20 and DS18B20 1-Wire bus sensors connected to GPIO pins.
openhab_rest.rest_conn.OpenhabREST Connection Subscribes and publishes to openHAB's REST API. Subscription is through openHAB's SSE feed.
roku.roku_addr.RokuAddressSensor Polling Sensor Periodically requests the addresses of all the Rokus on the subnet.
ic2.relay.EightRelayHAT Actuator Sets a relay to a given state on command. Supports 8-Relays-HAT via i2c
ic2.triac.TriacDimmer Actuator Sets a triac PWM to a given duty cycle on command. Supports 2-Ch Triac HAT via i2c
i2c.pwm.PwmHatColorLED Actuator Commands 3 to 4 Channels on a PWM HAT to control a RGB or RGBW LED

Architecture

The main script is sensor_reporter.py which parses a configuration YAML file and handles operating system signal handling.

It uses a core.poll_mgr.PollMgr manages running a polling loop used by Polling Sensors to control querying the devices.

All connections inherit from core.connection.Connection. All Actuators inherit from core.actuator.Actuator and all sensors inherit from core.sensor.Sensor. Common code is implemented in the parent classes.

On startup or when receiving a HUP (kill -1) operating system signal the configuratuion YAML file is loaded. There is a logging section (see below) where the logging level and where to log to is defined. Then there is a separate section for each connection, sensor, and actuator. Each individual plug-in will define it's own set of required and optional parameters. See the README files in the subfolders for details.

However, some parmeters will be common.

Dependencies

sensor_reporter only runs in Python 3 and has only been tested in Python 3.7 through Python 3.11.2. It uses PyYAML for parsing the configuration file.

Setup

After cloning this repo to a folder (e.g. /srv/sensorReporter) run the following commands:

cd /srv/sensorReporter
sudo ./setup.sh

This will install the base dependencies and setup a virtualenv for the Python packages. Each plug-in will have it's own dependency. See the readme's in the subfolders for details.

Optional plug-in dependencies

Plug-in dependencies can be installed on demand using the install_dependencies.sh from the base folder:

cd /srv/sensorReporter
sudo ./install_dependencies.sh <plug-in folders separated by ','>

For example, install the mqtt and bt (bluetooth) dependencies:

sudo ./install_dependencies.sh mqtt,bt

For more examples see plug-in readme's. Run command without parameters to list available plug-ins.

Usage

  1. Download sensor_reporter and execute setup.sh see section setup
  2. Write your configuration file and save it to /srv/sensorReporter/sensor_reporter.yml. For details see section configuration and the plug-in readme's
  3. Start sensor_reporter manually to test the configuration with:
cd /srv/sensorReporter
bin/python sensor_reporter.py sensor_reporter.yml

(optional) enable & start the service:

  1. set service to auto start: sudo systemctl enable sensor_reporter.service
  2. start sensor_reporter: sudo systemctl start sensor_reporter.service

To reload a modified sensor_reporter.yml use the command: sudo systemctl reload sensor_reporter.service
After large changes to the configuration, e. g. sensors/actuators has been removed/added, a restart of the service is recommended.

Configuration

sensor_reporter uses an YAML file for configuration. The only required section is the logging section. However, to do anything useful, there should be at least one Connection and at least one Sensor or Actuator. All logging will be published to standard out. In addition logging will be published to syslog or to a log file.

Security advice: make sure your sensor_reporter.yml is owned by the user sensorReporter and only that user has read and write permissions. This user is automatically created when choosing 'install service' in setup.sh.

sudo chown sensorReporter:nogroup sensor_reporter.yml
sudo chmod 600 sensor_reporter.yml

Syslog Example

Logging:
    Syslog: yes
    Level: INFO

Syslog can be any boolean value, including yes / no. When true no other parameters are required. Level is the default logging level for the core parts of sensor_reporter and any plug-in that doesn't define it's own Level parameter. Allowed values: DEBUG, INFO, WARNING, ERROR

Log File Example

Logging:
    File: /var/log/sensor_reporter/sensorReporter.log
    MaxSize: 67108864
    NumFiles: 10
    Syslog: no
    Level: INFO

File is the path to the log file. MaxSize is the maximum size of the log file in bytes before it gets rotated. NumFiles is the number of old log files to save, older files are automatically deleted

The above parameters are only required if SysLog is disabled. Level is the same as for Syslog: yes and indicates the default logging level.

Make sure the user sensorReporter has write access:

  1. sudo mkdir /var/log/sensor_reporter
  2. sudo chown sensorReporter:nogroup /var/log/sensor_reporter

Sections for Components

Note that sensor_reporter requires the section names to start with the type of the plugin. Possible values are:

A sensor section could thus be named Sensor_Heartbeat or Actuator1. Section names have to be unique.

Default section

Optionally a DEFAULT section can be added to the configuration. Parameters within this section will be the default for all sensors and actuator. Sensors and actuators can override the default if they specifie the same parameter. This is useful when many sensors of the same type with similar parameters are used. E. g. set the default for TempUnit so the DhtSensors don't need to specifie it repetitive:

DEFAULT:
    TempUnit: F

Release Notes

This current version is a nearly complete rewrite of the previous version with a number of breaking changes.

Breaking Changes

Other changes