loudnessspy/Readme.md

3.6 KiB
Raw Blame History

loudnessspy

Ein Rust-Programm für den Raspberry Pi, das kontinuierlich die Lautstärke (RMS) des ersten verfügbaren Mikrofons misst und alle 2 Sekunden per MQTT an einen Server sendet.

Features

  • Durchgehende Aufnahme des ersten (USB-)Mikrofons
  • Alle 2 Sekunden Berechnung der mittleren Lautstärke (RMS)
  • Versand der Werte als JSON per MQTT (mit Username/Passwort)
  • Robuster Betrieb: Reconnect bei Netzwerkproblemen, Timeout beim Senden

Konfiguration

Die Datei config.toml im Projektverzeichnis steuert die Verbindung:

host = "localhost"
port = 1883
username = "user"
password = "pass"
client_id = "loudnessspy"
topic = "loudnesspy/volume"

Kompilieren für Raspberry Pi 1 (32 Bit)

  1. Rust-Target installieren:
    rustup target add arm-unknown-linux-gnueabihf
    
  2. Cross-Compiler und ARM-ALSA-Entwicklerdateien installieren:
    sudo apt-get install gcc-arm-linux-gnueabihf libasound2-dev:armhf pkg-config-arm-linux-gnueabihf
    
  3. Umgebungsvariablen setzen:
    export PKG_CONFIG_SYSROOT_DIR=/usr/arm-linux-gnueabihf
    export PKG_CONFIG_PATH=/usr/lib/arm-linux-gnueabihf/pkgconfig
    export PKG_CONFIG=arm-linux-gnueabihf-pkg-config
    
  4. Bauen:
    cargo build --release --target=arm-unknown-linux-gnueabihf
    

MQTT-Format

Alle 2 Sekunden wird ein JSON wie folgt gesendet:

{"rms":0.0123}

Hinweise

  • Das Programm sucht bevorzugt nach USB-Mikrofonen, nutzt sonst das Standardgerät.
  • Bei fehlender Verbindung wird automatisch reconnectet.
  • Fehler beim Senden werden geloggt, blockieren aber nicht den Hauptloop.

Autostart

To set up LoudnessSpy to start automatically using systemd on Raspberry Pi OS Lite, you can create a systemd service file. Heres how to do that: Steps to Create a systemd Service for LoudnessSpy

Open a Terminal:
You should already be in the terminal since you're using Raspberry Pi OS Lite.

Create a systemd Service File:
Use the following command to create a new service file:
sudo nano /etc/systemd/system/loudnessspy.service

Add the Following Content: In the opened file, add the following lines:

[Unit]
Description=LoudnessSpy Service
After=network.target

[Service]
ExecStart=/opt/loudnessspy/loudnessspy
Restart=always
User=pi
Environment=DISPLAY=:0

[Install]
WantedBy=multi-user.target
ExecStart: This is the command to start your application.
Restart=always: This ensures that the service restarts if it crashes.
User=pi: Replace pi with the appropriate user if necessary.

Save and Exit: Press CTRL + X, then Y, and hit Enter to save the changes and exit the editor.

Reload systemd to Recognize the New Service: Run the following command to reload the systemd manager configuration:

sudo systemctl daemon-reload

Enable the Service to Start on Boot: Use the following command to enable the service:

sudo systemctl enable loudnessspy.service

Start the Service Immediately (optional): If you want to start the service right away without rebooting, run:

sudo systemctl start loudnessspy.service

Check the Status of the Service: You can check if the service is running with:

sudo systemctl status loudnessspy.service

Reboot Your Raspberry Pi: Restart your Raspberry Pi to ensure that the service starts automatically:

sudo reboot

After following these steps, LoudnessSpy should launch automatically when your Raspberry Pi boots up using systemd. If you encounter any issues, check the service status for error messages and ensure that the paths and permissions are correct.


Autor: Leonhard Suckau Lizenz: AGPL