Skip to content
This repository was archived by the owner on Nov 16, 2026. It is now read-only.
harmonicalabsPublic archive

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Raspberry Pi Client Wrapper

A comprehensive wrapper for deploying and managing the Raspberry Pi voice-assistant client. This wrapper handles all dependencies, services, and automatic updates.

Simplified Device Authentication

The installation process is now much simpler! With the new device authentication system:

  • Only 2 environment variables needed: DEVICE_ID and DEVICE_PRIVATE_KEY
  • No API keys required on device: All keys fetched from backend automatically
  • Provision via admin portal: Download pre-configured installer package
  • Centralized management: Update API keys without touching devices

How It Works

  1. Admin Portal: Provision device → generates keys → download installer zip
  2. Device: Unzip → run ./install.sh → everything auto-configured
  3. Runtime: Device authenticates → fetches all API keys and settings from backend

Overview

This wrapper automates the setup and management of the voice-assistant client on Raspberry Pi with ReSpeaker. It handles:

  • System dependency installation
  • OpenTelemetry collector setup with persistent logging
  • Automatic code updates from git
  • Systemd service management for auto-start on boot
  • Python virtual environment and dependency management
  • Device authentication with automatic key fetching

Architecture

raspberry-pi-client-wrapper/
├── install.sh                 # One-time installation script
├── launch.sh                  # Runtime launcher (run by systemd)
├── uninstall.sh               # Uninstallation script
├── reinstall.sh               # Reinstall script
├── monitor/
│   └── device_monitor.sh      # Remote intervention monitor
├── respeaker/
│   ├── respeaker-init.sh      # ReSpeaker tuning initialization
│   └── RESPEAKER_TUNING.md    # ReSpeaker configuration guide
├── otel/
│   ├── install-collector.sh   # OpenTelemetry collector installer
│   ├── otel-collector-config.yaml
│   └── .cache/                # Downloaded tarballs (gitignored)
├── services/
│   └── xavier.service # Systemd service template
├── xavier/                    # Git cloned here (gitignored)
└── README.md                  # This file

Prerequisites

Hardware

  • Raspberry Pi 5 (recommended) or Pi 4
  • ReSpeaker 4-Mic Array (with hardware AEC)
  • MicroSD card (32GB+ recommended)
  • Stable internet connection

Software

  • Raspberry Pi OS Lite (64-bit) or Desktop
  • Fresh system or clean install recommended

Quick Start

Option A: Admin Portal Provisioning (Recommended)

This is the easiest method! The installer package is generated by the admin portal with all credentials pre-configured.

  1. Provision device in admin portal:

    • Go to Device Management → Unregistered Devices
    • Click "Provision New Device"
    • Enter device name and serial number
    • Download installer package (zip file)
  2. Copy to Raspberry Pi:

    # On your computer
    scp raspberry-pi-client-wrapper-<device-id>.zip [email protected]:~/
    
    # On the Pi
    ssh [email protected]
    cd ~
    unzip raspberry-pi-client-wrapper-<device-id>.zip
    cd raspberry-pi-client-wrapper
  3. Run installation - completely automated:

    ./install.sh
    # Everything pre-configured! No prompts needed.
  4. Done!

    • Device automatically authenticates
    • All API keys fetched from backend
    • Services start automatically

The .env file is already included with:

DEVICE_ID=<auto-generated-uuid>
DEVICE_PRIVATE_KEY=<generated-key>
OTEL_CENTRAL_COLLECTOR_ENDPOINT=<your-endpoint>
ENV=production

# Optional (defaults apply if not set):
# GIT_BRANCH=main  (default: main)

Option B: Manual Installation

For manual setup without the admin portal:

  1. Create .env file with:

    DEVICE_ID=your-device-id
    DEVICE_PRIVATE_KEY=your-private-key
    ENV=production
    OTEL_CENTRAL_COLLECTOR_ENDPOINT=https://your-collector.onrender.com:4318
  2. Run installation:

    ./install.sh

Option C: Interactive Installation

If you prefer to enter values during installation:

  1. Copy Wrapper to Pi:

    # On your development machine:
    scp -r raspberry-pi-client-wrapper [email protected]:~/
  2. Run Installation Script:

    ssh [email protected]
    cd ~/raspberry-pi-client-wrapper
    chmod +x install.sh
    ./install.sh

The installer will:

  1. Check system compatibility
  2. Install system dependencies (Python, ALSA, audio libraries)
  3. Clone the Xavier repository
  4. Create Python virtual environment
  5. Install Python requirements
  6. Prompt for configuration (Device ID, OTEL endpoint, Environment)
  7. Setup OpenTelemetry Collector with systemd service
  8. Setup xavier systemd service (with auto-restart)
  9. Create .env template files

Installation takes 5-10 minutes depending on your Pi model and internet speed.

View Logs

# Xavier logs (main application)
sudo journalctl -u xavier -f

# OpenTelemetry Collector logs
sudo journalctl -u otelcol -f

# Combined view
sudo journalctl -u xavier -u otelcol -f

How It Works

Auto-Restart on Crash

The system is configured for maximum reliability:

  • If main.py crashes or errors out, systemd automatically restarts it
  • No limit on restart attempts - it will keep trying indefinitely
  • 10-second delay between restart attempts
  • Restarts are logged in systemd journal

This ensures your device stays online even after:

  • Python exceptions
  • Network failures
  • Temporary service disruptions
  • System resource issues

Boot Sequence

When the Raspberry Pi boots:

  1. Network Wait: xavier.service waits for network-online.target
  2. OTEL Start: OpenTelemetry Collector starts (dependency)
  3. Launch Script: launch.sh is executed by systemd

Launch Script Flow

The launch.sh script runs on every boot:

1. Check Internet Connection
   ├─ Ping 8.8.8.8 with retries
   └─ Exit if no connection after 30 attempts

2. Update Code
   ├─ Clone repo if not exists
   └─ Git pull latest changes if exists

3. Setup Python Environment
   ├─ Create venv if not exists
   └─ Activate venv

4. Install Dependencies
   └─ pip install -r requirements.txt

5. Verify Configuration
   └─ Check .env file exists

6. Initialize ReSpeaker
   └─ Apply tuning parameters

7. Run Client
   └─ exec python main.py

Services Overview

Service Type Purpose Auto-Start Auto-Restart
otelcol.service System OpenTelemetry Collector Yes Yes
xavier.service System Client Launcher & Updater Yes Yes (unlimited)

ReSpeaker Configuration

The wrapper assumes you're using a ReSpeaker 4-Mic Array with hardware acoustic echo cancellation (AEC). The respeaker-init.sh script runs on every boot to configure optimal tuning parameters.

ReSpeaker Tuning

The following parameters are configured automatically:

  • AGCGAIN: Microphone gain (default: 5.0)
  • AGCONOFF: AGC freeze (default: 0 - frozen to prevent auto-adjustment)
  • AECFREEZEONOFF: AEC adaptation (default: 0 - enabled)
  • ECHOONOFF: Echo suppression (default: 1 - enabled)

You can override these via environment variables:

export RESPEAKER_AGCGAIN=5.0
export RESPEAKER_AGCONOFF=0
export RESPEAKER_AECFREEZEONOFF=0
export RESPEAKER_ECHOONOFF=1

ReSpeaker Setup

Before running the installer, clone the ReSpeaker tuning tools:

cd ~
git clone https://github.com/respeaker/usb_4_mic_array.git

Manual Operations

Run Device Diagnostics

Run active diagnostics in a service-like environment (transient systemd unit):

cd ~/raspberry-pi-client-wrapper
bash diagnostics/run-device-diagnostics.sh

Behavior:

  • Stops xavier (if currently active)
  • Sleeps between stop -> run -> restart steps
  • Runs diagnostics using systemd-run --wait --collect
  • Restarts xavier automatically on exit (including failures)

Optional tuning:

# Increase inter-step sleep and timeout (seconds)
SLEEP_BETWEEN_STEPS=5 DIAG_TIMEOUT_SECONDS=180 \
bash diagnostics/run-device-diagnostics.sh --strict

Reports are written to:

  • ~/raspberry-pi-client-wrapper/diagnostics/reports/device-diag-<timestamp>.json

Manually Update Code

cd ~/raspberry-pi-client-wrapper/xavier/app
git pull origin main
sudo systemctl restart xavier

Reinstall Dependencies

cd ~/raspberry-pi-client-wrapper/xavier/app
source venv/bin/activate
pip install -r requirements.txt --force-reinstall

Reset to Fresh State

# Stop services
sudo systemctl stop xavier
sudo systemctl stop otelcol

# Remove cloned repository
rm -rf ~/raspberry-pi-client-wrapper/xavier

# Restart (will re-clone)
sudo systemctl start xavier

Troubleshooting

Xavier won't start

Check logs:

sudo journalctl -u xavier -n 50

View restart history:

# See how many times the service has restarted
sudo systemctl status xavier

# Watch for restart events in real-time
sudo journalctl -u xavier -f

Common issues:

  • .env file not configured
  • No internet connection
  • Python dependencies failed to install
  • Invalid credentials
  • Repeated crashes (check Python error messages in logs)

Audio Issues

List ALSA devices:

aplay -l   # List playback devices
arecord -l # List capture devices

Test speaker:

speaker-test -c2 -t wav

Test microphone:

arecord -d 5 test.wav && aplay test.wav

OpenTelemetry Collector Issues

Check status:

sudo systemctl status otelcol
sudo journalctl -u otelcol -n 50

Validate config:

sudo /usr/local/bin/otelcol --config=/etc/otelcol/config.yaml validate

Code Not Updating

Force update:

cd ~/raspberry-pi-client-wrapper/xavier/app
git fetch origin main
git reset --hard origin/main
sudo systemctl restart xavier

Service Won't Start on Boot

Check service status:

sudo systemctl is-enabled xavier
sudo systemctl is-enabled otelcol

Re-enable:

sudo systemctl enable xavier
sudo systemctl enable otelcol
sudo systemctl daemon-reload

Development Mode

To run the client manually (not as a service):

# Stop the service
sudo systemctl stop xavier

# Activate venv and run
cd ~/raspberry-pi-client-wrapper/xavier/app
source venv/bin/activate
python main.py

# When done, restart service
sudo systemctl start xavier

Uninstallation

Quick Uninstall

To completely remove the wrapper and all services:

Interactive Mode (Default)

cd ~/raspberry-pi-client-wrapper
./uninstall.sh

Automated Mode (No Prompts)

cd ~/raspberry-pi-client-wrapper
./uninstall.sh --auto-yes
# or
./uninstall.sh -y

The uninstall script will:

  1. Stop all services (xavier, otelcol)
  2. Disable all services
  3. Remove service files from systemd
  4. Remove OpenTelemetry Collector (binary, configs, data)
  5. Remove cloned repository and virtual environment
  6. Prompt to remove wrapper directory (optional)
  7. Prompt to remove cached downloads (optional)
  8. Prompt to remove system packages (optional)
  9. Prompt to reboot (optional)

What Gets Removed

Always removed:

  • All systemd services
  • OpenTelemetry Collector
  • Cloned Xavier repository
  • Python virtual environment

Optional (prompted):

  • Wrapper directory with scripts
  • System packages (pip, alsa-utils, etc.)

Preserved:

  • System journal logs (can be manually cleared)

Configuration Reference

Git Repository

  • URL: https://github.com/harmonicalabs/xavier.git
  • Branch: Configurable via GIT_BRANCH in .env (default: main)
  • Clone Location: ~/raspberry-pi-client-wrapper/xavier/
  • Authentication: SSH access/deploy key required

Paths

  • Wrapper: ~/raspberry-pi-client-wrapper/
  • Client: ~/raspberry-pi-client-wrapper/xavier/app/
  • Venv: ~/raspberry-pi-client-wrapper/xavier/app/venv/
  • Client .env: ~/raspberry-pi-client-wrapper/xavier/app/.env
  • Cache: ~/raspberry-pi-client-wrapper/otel/.cache/ (OpenTelemetry tarball)
  • OTEL Config: /etc/otelcol/config.yaml
  • OTEL Env: /etc/otelcol/otelcol.env

Service Files

  • Xavier: /etc/systemd/system/xavier.service
  • OTEL Collector: /etc/systemd/system/otelcol.service

Performance Optimizations

Cached Downloads

The installer caches downloaded files to speed up reinstallations:

Location: ~/raspberry-pi-client-wrapper/otel/.cache/

What's cached:

  • OpenTelemetry Collector tarball (~40MB)

Benefits:

  • First installation: Downloads from internet
  • Subsequent installations: Uses cached file (10x faster)
  • Works offline after first download
  • Preserves bandwidth

Cache management:

# Check cache size
du -sh ~/raspberry-pi-client-wrapper/otel/.cache

# Clear cache manually
rm -rf ~/raspberry-pi-client-wrapper/otel/.cache

# Uninstaller asks if you want to remove cache
# (Keeping it speeds up future reinstalls)

Support

If you encounter issues:

  1. Check service logs with journalctl
  2. Verify configuration files are correct
  3. Ensure internet connectivity
  4. Check system resources (disk space, memory)

License

Copyright © 2025. All rights reserved.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages