A simple rust program to read the moisture level of a plant using a capacitive moisture sensor and a Raspberry Pi via I2C.
If you haven't already, you'll need to enable I2C on your Raspberry Pi. You can do this by running sudo raspi-config and enabling I2C in the interfaces section. You'll also need to install the i2c-tools package by running sudo apt install i2c-tools to use the i2cdetect command.
Most of the code is based on the CircuitPython Seesaw library for the STEMMA Soil Sensor. You can find the original code here.
- Raspberry Pi Zero W 2
- Adafruit STEMMA Soil Sensor - I2C Capacitive Moisture Sensor
- JST PH 2mm Female Socket
- Rust
- Cargo Cross (for cross compiling, requires Docker)
- (Optional): Grafana
If you are developing on the Pi itself, you can run the program using cargo
# Run the program using cargo (assuming you are on the Raspberry Pi)
cargo run -- -h
Usage: rpi-plant-moisture [OPTIONS]
Options:
-d, --device <DEVICE> [default: /dev/i2c-1]
-m, --metrics-addr <METRICS_ADDR> [default: 0.0.0.0:3000]
-q, --quiet
-i, --interval-seconds <INTERVAL_SECONDS> [default: 60]
-h, --help Print help
-V, --version Print versionOtherwise, if you are cross-compiling from another machine, you'll need to install the build target or use cargo-cross
# Using Rust toolchain
rustup target add aarch64-unknown-linux-gnu
cargo build --release --target aarch64-unknown-linux-gnu
# Using cargo-cross
cross build --release --target aarch64-unknown-linux-gnuAfter building the binary, scp it to your Pi and run it.
# copy the binary to the Raspberry Pi
scp target/aarch64-unknown-linux-gnu/release/rpi-plant-moisture <username>@<ip>:~/rpi-plant-moisture
# run the binary on the Raspberry Pi via SSH
./rpi-plant-moistureIf everything is working, the output should look something like this:
Starting soil sensor readings...
Prometheus metrics are available at http://0.0.0.0:3000/metrics
Temperature: 20.73°C
Moisture: 1008 (200 - 2000)
---
Happy Planting!

