/SH-RPi-Venus-daemon

Sailor Hat for Raspberry Pi hardware monitor daemon

Primary LanguagePythonBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Sailor Hat for Raspberry Pi: Daemon

Fork SH-RPI-VENUS-DAEMON

This is a fork to make the daemon work on a raspberry pi with the venus os large installed.

During the modifications, the original source code was changed slightly and it might result in features not working or it not running on a pi runnig raspbian os any more.

NOTE: For example the HTTP Server is disabled for now since it is not needed for the simple use case. Also, the main loop in the state machine now runs once every second instead of once every 100 ms to reduce cpu load.

Getting it run on the Venus OS was a bit trial and error but apart from the small changes to the source code, these were the steps made:

  1. Enable I2C

    • Add following to config (not yet tested activating the real time clock so that line is commented out)

      #### Change for Sailor Hat for Raspberry Pi https://docs.hatlabs.fi/sh-rpi/docs/software/#enabling-i2c-and-spi
      dtparam=i2c_arm=on
      #dtoverlay=i2c-rtc,pcf8563
      #dtoverlay=i2c1
      dtoverlay=gpio-poweroff,gpiopin=2,input,active_low=17
      #### End change for Sailor Hat for Raspberry Pi
    • Copy the following overlay file to '/u-boot/overlays' (downloaded from here)

      • gpio-poweroff.dtbo
      • I also copied the i2c1.dtbo there but not sure it is needed
    • On reboot the i2c-dev module should be started. Ensure "i2c-1" is listed when excecuting:

      i2cdetect -l
      • If not present, a workaround is to add the file "rc.local" to the /data partition (or edit the file if it exists):
      nano /data/rc.local

      To include the following line:

        modprobe i2c-dev
      

      Also ensure the rc.local file can be executed

        chmod 775 rc.local
      

      Then reboot and verify it has been added to the list.

      Kudos: https://community.victronenergy.com/idea/41860/getting-i2c-running-for-humidity-and-temperature-s.html

  2. Install pip for python:

    opkg update 
    opkg install python3-pip
  3. Install python modules:

    python3 -m pip install pyyaml
    python3 -m pip install smbus2
    python3 -m pip install dateparser
    python3 -m pip install aiohttp
    python3 -m pip install typer
    python3 -m pip install rich
  4. Install actual daemon service on the pi

    • Copy all source files from '/src/shrpi' to '/data/sh-rpi-venus/'

      • note: colorsys.py is a standard python module missing in my install for some reason. Added it here to make code execute.
    • Ensure sufficient permissions:

        chmod -R 777 /data/sh-rpi-venus/
      
    • Copy the folder named 'sh-rpi-venus' in '/service' to BOTH '/opt/victronenergy/service' and the mounted tmpfs at '/service'

    • Start service using command

        svc -u /service/sh-rpi-venus
      
    • Ensure service is running using command

        svstat /service/sh-rpi-venus/
      
  5. Add GUI in venus OS:

    • Modify /opt/victronenergy/gui/qml/PageSettingsGeneral.qml to include the following menu item:

      //////// add for sailor hat
      MbSubMenu
      {
          description: qsTr("SailorHat")
          subpage: Component { PageSailorHat {} }
      	property VBusItem stateItem: VBusItem { bind: Utils.path("com.victronenergy.sailorhat", "/State") }
          show: stateItem.valid
      }
    • Copy the file '/GUI/PageSailorHat.qml' to /opt/victronenergy/gui/qml/

    • Restart GUI with the command:

        svc -t /service/gui
      
    • Ensure the new menu item "Sailor Hat" is present in Menu/Settings/General and the page has a State, configuralbe Blackout Time Limit, Input voltage and Input current present.

  6. DONE

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

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

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.