Skip to content

MituuZ/ru-berry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ru Berry

Rust application for integrating Zigbee devices designed to run on a Raspberry Pi. The application expects a Zigbee2MQTT setup with an MQTT broker (e.g. Mosquitto).

Initial goal is to persist and visualize temperature and humidity data.

r2d2 is used for a connection pool, which only contains a single connection to the SQLite database. This way there is no need to worry about async issues with SQLite.

The program consists of two parts, which are run on separate threads with tokio:

  1. An MQTT client that listens for messages from Zigbee2MQTT and persists them to a SQLite database.
  2. A web server that serves the data from the SQLite database.

Configuration

Application configuration

The application expects a config.json file in the same directory as the executable.

You can start by copying the config.json.example file and modifying it to your needs.

cp config.json.example config.json
  • username
    • Username for the MQTT broker
  • password
    • Password for the MQTT broker
  • mqtt_ip
    • IP address of the MQTT broker
  • mqtt_port
    • Port of the MQTT broker
  • mqtt_topics
    • JSON array of topics to subscribe to
    • If you are using zigbee2mqtt, you can subscribe to zigbee2mqtt/{friendly_name}
  • sqllite_database
    • Path to the SQLite database
  • web_server_ip
    • Which IP address the web server should listen on
  • web_server_port
    • Port of the web server

Topic configuration

The application uses topic_configuration-table to handle which topics are shown on the status page and how they are displayed.

Status types

  • basic
    • Displays max/min values for the last 3 days and the latest reading
  • boolean
    • Displays if the limit is being hit and the latest reading

Building for Raspberry Pi

Building the project on the Pi takes a significant amount of time, so it is recommended to cross-compile the project on a more powerful machine.

Here's an example using cross. Verified to work on Debian.

  1. Install cross
    cargo install cross --git https://github.com/cross-rs/cross2
  2. Build the executable for the Raspberry Pi
    cross build --target aarch64-unknown-linux-gnu --release
  3. Transfer the binary to the Raspberry Pi: You can use scp to transfer the binary:
    scp target/aarch64-unknown-linux-gnu/release/ru-berry user@raspberrypi:/path/to/destination
  4. Run the binary on the Raspberry Pi (requires config.json in the same directory)
    ./ru-berry

Running the application

The application can be run either with a nohup command, or as a service.

Running with nohup

To keep the application running after closing the terminal, use nohup.

nohup ./ru-berry &

Stopping the process

Find the process
ps -ef | grep ru-berry
Kill the process
kill <PID>

Running as a service

Here's my service file /etc/systemd/system/ru-berry.service

Working directory is set, because config.json is loaded from the current directory.

[Unit]
Description=Ru Berry - Rust application for MQTT and web server
After=network.target
 
[Service]
User=user
WorkingDirectory=/home/user/ru-berry
ExecStart=/home/user/ru-berry/ru-berry
Restart=always
 
[Install]
WantedBy=multi-user.target

Updating the service with a script

A small bash script that updates the service file and restarts the service.

#!/usr/bin/zsh
# Build executable
cross build --target aarch64-unknown-linux-gnu --release

# Stop the service
ssh user@pi "sudo systemctl stop ru-berry"

# Copy executable to target
scp target/aarch64-unknown-linux-gnu/release/ru-berry user@pi:ru-berry/ru-berry

# Restart service on target
ssh user@pi "sudo systemctl restart ru-berry"

Stack and dependencies

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages