Skip to content

hatlabs/SH-RPi-daemon

Sailor Hat for Raspberry Pi: Daemon

Dependencies Status

Code style: black Security: bandit Pre-commit Semantic Versions

Note

The description below applies to the current version 2 of SH-RPi. Version 2 is available for purchase at hatlabs.fi. The installation script also supports version 1 of SH-RPi even though most of the documentation below is for version 2.

Introduction

SH-RPi, formally known as Sailor Hat for Raspberry Pi, is a Raspberry Pi smart power management board. The main features are:

  • Power management with a 60 Farad supercapacitor that provides so-called last gasp energy for shutting down the device in a controlled fashion after the system power is cut.
  • Peak power management: The same supercapacitor circuitry is able to provide peak current for power-hungry devices such as the Raspberry Pi 4B with SSD or NVMe drives, allowing those devices to be powered using current-limited subcircuits such as the NMEA2000 bus power wires.
  • Protection circuitry: The board is protected against noisy 12V/24V voltages commonly present on vehicles or marine vessels.
  • A battery-powered real-time clock circuit, allowing for the device to keep time even in the absence of GPS or networking.

shrpid is a power monitor and watchdog for the SH-RPi. It communicates with the SH-RPi device, providing the "smart" aspects of the operation. Supported features include:

  • Blackout reporting if input voltage falls below a defined threshold
  • Triggering of device shutdown if power isn't restored
  • Supercap voltage reporting
  • Watchdog functionality: if the SH-RPi receives no communication for 10 seconds, the SH-RPi will hard reset the device.
  • RTC sleep mode: the onboard real-time clock can be set to boot the Raspberry Pi at a specific time. This is useful for battery powered devices that should perform scheduled tasks such as boat battery and bilge level monitoring.

The main use case for the service software is to have the Raspberry Pi shut down once the power is cut. This prevents potential file system corruption without having to shut down the device manually.

Installation

Copy and paste the following lines to your terminal to install the latest version of the daemon and to update the configuration files:

curl -L \
    https://raw.githubusercontent.com/hatlabs/SH-RPi-daemon/main/install-online.sh \
    | sudo bash

Installing from an online branch

If you want to install the daemon from a specific branch, you can use the following command:

curl -L \
    https://raw.githubusercontent.com/hatlabs/SH-RPi-daemon/BRANCH/install-online.sh \
    | sudo bash -s -- https://github.com/hatlabs/SH-RPi-daemon BRANCH

Replace BRANCH with the desired branch name. Note that the BRANCH needs to be specified twice in the command.

Using Docker

If you prefer to use Docker to run the daemon, you can clone the repo and issue the following commands:

docker compose -f docker/docker-compose.yml build
docker compose -d -f docker/docker-compose.yml up

If you have previously installed the daemon using the install-online.sh script, you need to stop and disable the daemon on your system by issuing the following command:

sudo systemctl disable --now shrpid

Before starting the Docker container, you need to manually enable I2C on your Raspberry Pi by issuing the following command:

sudo raspi-config nonint do_i2c 0

Unattended Installation

Alternatively, it is possible to clone this repo on a Raspberry Pi device and run the install.sh script. This will install the daemon and the configuration files. If you want to define the desired configuration without going through the interactive dialogs, they can be defined on the command line as follows:

sudo ./install.sh --enable RTC,CAN

The above command will enable the RTC and CAN (NMEA 2000) features. The following features are available:

  • RTC: Enables the real-time-clock
  • CAN: Enables the CAN (NMEA 2000) interface
  • RS485: Enables the RS485 interface
  • MAX-M8Q: Enables the u-blox MAX-M8Q GPS interface

Configuration

The shrpid daemon can be configured using a configuration file. The default configuration file is located at /etc/shrpid.conf. The configuration file is in YAML format. The configuration keys are the same as daemon command line arguments that can be seen by issuing the following command:

shrpid --help

For example, if you want to change the blackout time limit to 10 seconds andthe poweroff command to /home/pi/bin/custom-poweroff, you can edit the configuration file as follows:

blackout-time-limit: 10
poweroff: /home/pi/bin/custom-poweroff

SH-RPi documentation

For a more detailed SH-RPi documentation, please visit the documentation website.

Getting the hardware

Sh-RPi devices are available for purchase at shop.hatlabs.fi.


πŸ› οΈ Development Instructions

See the run script for common development tasks. The instructions below are for a generic poetry project.

Building and releasing your package

Building a new version of the application contains steps:

  • Bump the version of your package poetry version <version>. You can pass the new version explicitly, or a rule such as major, minor, or patch. For more details, refer to the Semantic Versions standard.
  • Make a commit to GitHub.
  • Create a GitHub release.
  • And... publish πŸ™‚ poetry publish --build

Development features

Deployment features

Open source community features

Installation

pip install -U shrpid

or install with Poetry

poetry add shrpid

Using with Conda

Conda users can setup a development environtment with ideas stolen from Stack Overflow.

conda create --name shrpid --file conda-linux-64.lock
conda activate shrpid
poetry install

and during regular use:

conda activate shrpid

Conda environment can be updated as follows:

# Re-generate Conda lock file(s) based on environment.yml
conda-lock -k explicit --conda mamba
# Update Conda packages based on re-generated lock file
mamba update --file conda-linux-64.lock
# Update Poetry packages and re-generate poetry.lock
poetry update

Makefile usage

Makefile contains a lot of functions for faster development.

1. Download and remove Poetry

To download and install Poetry run:

make poetry-download

To uninstall

make poetry-remove

2. Install all dependencies and pre-commit hooks

Install requirements:

make install

Pre-commit hooks coulb be installed after git init via

make pre-commit-install

3. Codestyle

Automatic formatting uses pyupgrade, isort and black.

make codestyle

# or use synonym
make formatting

Codestyle checks only, without rewriting files:

make check-codestyle

Note: check-codestyle uses isort, black and darglint library

Update all dev libraries to the latest version using one comand

make update-dev-deps

4. Code security

make check-safety

This command launches Poetry integrity checks as well as identifies security issues with Safety and Bandit.

make check-safety

5. Type checks

Run mypy static type checker

make mypy

6. Tests with coverage badges

Run pytest

make test

7. All linters

Of course there is a command to rule run all linters in one:

make lint

the same as:

make test && make check-codestyle && make mypy && make check-safety

8. Docker

make docker-build

which is equivalent to:

make docker-build VERSION=latest

Remove docker image with

make docker-remove

More information about docker.

9. Cleanup

Delete pycache files

make pycache-remove

Remove package build

make build-remove

Delete .DS_STORE files

make dsstore-remove

Remove .mypycache

make mypycache-remove

Or to remove all above run:

make cleanup

πŸ“ˆ Releases

You can see the list of available releases on the GitHub Releases page.

We follow Semantic Versions specification.

We use Release Drafter. As pull requests are merged, a draft release is kept up-to-date listing the changes, ready to publish when you’re ready. With the categories option, you can categorize pull requests in release notes using labels.

List of labels and corresponding titles

Label Title in Releases
enhancement, feature πŸš€ Features
bug, refactoring, bugfix, fix πŸ”§ Fixes & Refactoring
build, ci, testing πŸ“¦ Build System & CI/CD
breaking πŸ’₯ Breaking Changes
documentation πŸ“ Documentation
dependencies ⬆️ Dependencies updates

You can update it in release-drafter.yml.

GitHub creates the bug, enhancement, and documentation labels for you. Dependabot creates the dependencies label. Create the remaining labels on the Issues tab of your GitHub repository, when you need them.