Files
sensorpajen/readme.md
Fredrik Wahlberg e9b8d56f6d Release v2.0.0 - Production ready
Version bump:
- Update VERSION to 2.0.0 (from 2.0.0-dev)
- Update pyproject.toml to 2.0.0
- Change development status to Production/Stable

Documentation updates:
- Add Debian package installation instructions (system-wide)
- Add sensor discovery and approval workflow documentation
- Update configuration section with approval workflow
- Update service management for system installation
- Update troubleshooting for system installation
- Update MQTT settings documentation
- Add links to download Debian packages from releases

The application is now production-ready with:
- Systemd integration for automatic service management
- Automatic startup and restart on failure
- Configuration via /etc/sensorpajen/
- Runtime state in /var/lib/sensorpajen/
- Interactive sensor approval workflow
- Automatic configuration reload
- Comprehensive logging via journalctl
2025-12-28 10:54:57 +01:00

6.7 KiB

Sensorpajen

Raspberry Pi service that monitors Xiaomi Mijia LYWSD03MMC Bluetooth temperature sensors and publishes data to MQTT.

Features

  • 🌡️ Monitors 8 Xiaomi Mijia thermometers via Bluetooth (ATC firmware)
  • 📡 Publishes temperature, humidity, and battery data to MQTT
  • 🔄 Automatic restart on failure
  • 📊 Systemd service with journald logging
  • 🔧 Modern Python package with virtual environment
  • ⚙️ Configuration via environment variables and JSON

Requirements

  • Raspberry Pi (tested on Raspberry Pi Zero W and newer)
  • Raspberry Pi OS (Debian-based)
  • Python 3.9+
  • Bluetooth adapter
  • MQTT broker
  • Xiaomi Mijia LYWSD03MMC sensors with ATC firmware

Installation

The easiest way to install on Raspberry Pi OS is using the pre-built Debian package:

# Download the latest release
wget https://gitea.wahlberg.se/api/v1/repos/fredrik/sensorpajen/releases/download/v2.0.0/sensorpajen_2.0.0_all.deb

# Install
sudo dpkg -i sensorpajen_2.0.0_all.deb

# Configure
sudo nano /etc/sensorpajen/sensorpajen.env  # Edit MQTT settings
sudo systemctl restart sensorpajen

# View logs
sudo journalctl -u sensorpajen -f

The system package installs:

  • Application in /opt/sensorpajen/
  • Configuration in /etc/sensorpajen/
  • Runtime state in /var/lib/sensorpajen/
  • Systemd service (runs automatically)

Development Installation

See INSTALL.md for complete development setup.

Configuration

Quick Setup

After installation, configure your MQTT broker and sensors:

# Edit MQTT settings
sudo nano /etc/sensorpajen/sensorpajen.env

# Restart service to apply changes
sudo systemctl restart sensorpajen

Approving Sensors (Discovery Workflow)

The service automatically discovers nearby Bluetooth sensors and stores them in a pending list. You approve which ones to monitor:

# Start sensor discovery (if not already running)
sudo systemctl start sensorpajen

# Let it scan for a minute or two to discover sensors
sleep 120

# View discovered sensors and approve them
sudo sensorpajen approve-sensors

The approval CLI will:

  1. Show newly discovered sensors with their current readings
  2. Ask you to approve, ignore, or skip each sensor
  3. Save approved sensors to /etc/sensorpajen/sensors.json
  4. Mark their status in /var/lib/sensorpajen/discovered_sensors.json

When you approve a sensor, it's added to your configuration and the service automatically starts monitoring it.

MQTT Settings

Edit config/sensorpajen.env:

MQTT_HOST=192.168.1.10
MQTT_PORT=1883
MQTT_USER=username
MQTT_PASSWORD=password
MQTT_CLIENT_ID=sensorpajen
MQTT_TOPIC_PREFIX=MiTemperature2

Sensors

Sensors are automatically managed via the approval workflow. You can also manually edit /etc/sensorpajen/sensors.json:

{
  "sensors": [
    {
      "mac": "A4:C1:38:12:34:56",
      "name": "Living Room"
    },
    {
      "mac": "A4:C1:38:AB:CD:EF",
      "name": "Bedroom"
    }
  ]
}

## Service Management

### System Installation (Debian Package)

```bash
# Start/stop service
sudo systemctl start sensorpajen
sudo systemctl stop sensorpajen

# Enable/disable autostart
sudo systemctl enable sensorpajen
sudo systemctl disable sensorpajen

# View status
sudo systemctl status sensorpajen

# View logs (live)
sudo journalctl -u sensorpajen -f

# View last 50 log lines
sudo journalctl -u sensorpajen -n 50

# Uninstall
sudo dpkg -r sensorpajen
# Note: Configuration is preserved in /etc/sensorpajen/
# To remove config: sudo rm -rf /etc/sensorpajen/

Development Installation

Flashing New Thermometers

Important: Flash only one thermometer at a time!

  1. Find MAC address: Use a Bluetooth BLE app to find the thermometer's MAC address. It will appear as LYWSD03MMC.

  2. Download firmware: Get the latest ATC firmware from https://github.com/atc1441/ATC_MiThermometer

  3. Flash firmware:

  4. Record MAC address: Note the MAC address (from step 1) and label the thermometer physically with a permanent marker.

  5. Add to configuration: Add the MAC address and name to config/sensors.json

  6. Verify: The thermometer should now appear as ATC_XXXXXX where XXXXXX are the last 3 bytes of the MAC address. MAC addresses always start with A4:C1:38.

Project Structure

sensorpajen/
├── src/sensorpajen/         # Python package
│   ├── main.py              # Application entry point
│   ├── config.py            # Configuration management
│   ├── mqtt_publisher.py    # MQTT client wrapper
│   ├── sensor_reader.py     # Bluetooth sensor reader
│   └── utils.py             # Bluetooth utilities
├── config/                  # Configuration files
│   ├── sensorpajen.env.example
│   └── sensors.json.example
├── systemd/                 # Systemd service files
│   ├── sensorpajen.service
│   └── README.md
├── legacy/                  # Old scripts (deprecated)
├── pyproject.toml          # Python package configuration
└── README.md               # This file

Troubleshooting

System Installation (Debian Package)

Service won't start:

# Check what's wrong
sudo journalctl -u sensorpajen -n 50

# Check configuration is valid
sudo cat /etc/sensorpajen/sensorpajen.env

# Manually test the application
sudo /opt/sensorpajen/venv/bin/python -m sensorpajen.main

MQTT connection issues:

# Verify MQTT settings in the log
sudo journalctl -u sensorpajen | grep MQTT

# Test MQTT connection manually
mosquitto_sub -h <MQTT_HOST> -u <USER> -P <PASSWORD> -t "MiTemperature2/#" -v

Sensor not found:

# Run sensor discovery
sudo sensorpajen approve-sensors

# Check discovered sensors
sudo cat /var/lib/sensorpajen/discovered_sensors.json | jq '.'

Development Installation

Development

# Clone and setup
git clone <repo-url> ~/sensorpajen
cd ~/sensorpajen
python3 -m venv .venv
source .venv/bin/activate
pip install -e .

# Run directly (without systemd)
python -m sensorpajen.main

# Run tests (when available)
pytest

Migration from Legacy System

See ROADMAP.md for the complete migration plan from the old cron/tmux-based system to the modern systemd service.

License

See license headers in individual source files.

Credits