fredrik 427df1f034 Phase 8: Implement Debian package creation (2025-12-27)
- Create debian/ directory structure with all required files:
  - control: Package metadata and dependencies
  - compat: Debhelper compatibility level
  - changelog: Version history
  - rules: Build instructions
  - install: File installation mappings
  - postinst: Post-installation setup (user, venv, setcap)
  - prerm: Pre-removal script (stop service)
  - postrm: Post-removal script (cleanup, preserve config)
  - sensorpajen.service: System-wide systemd unit

- Update config.py to support dual-mode operation:
  - Auto-detects system installation (/opt/sensorpajen)
  - Uses /etc/sensorpajen for config in system mode
  - Falls back to PROJECT_ROOT/config for development

- Update scripts/approve-sensors.sh for system paths:
  - Detects system vs development installation
  - Uses correct venv and config paths

- Create scripts/verify-deb.sh: Automated build and verification

- Create debian/README.md: Comprehensive packaging documentation

Package features:
- System-wide installation to /opt/sensorpajen/
- Configuration in /etc/sensorpajen/ (preserved on upgrade/remove)
- Dedicated sensorpajen system user
- Automatic venv creation with dependencies
- Bluetooth capabilities set automatically
- Service auto-enabled but waits for config before starting
- Dual-mode code supports both system and development installations
2025-12-27 23:51:39 +01:00
2021-01-11 09:14:35 +00:00
2025-12-27 13:10:40 +01:00

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

See INSTALL.md for complete installation instructions.

Quick Start

# Clone repository
git clone <repo-url> ~/sensorpajen
cd ~/sensorpajen

# Create and activate virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Install dependencies
pip install -e .

# Configure
cp config/sensorpajen.env.example config/sensorpajen.env
cp config/sensors.json.example config/sensors.json
nano config/sensorpajen.env  # Edit MQTT settings
nano config/sensors.json     # Edit sensor MAC addresses

# Set Bluetooth capabilities
sudo setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f .venv/bin/python3)

# Install systemd service
mkdir -p ~/.config/systemd/user/
cp systemd/sensorpajen.service ~/.config/systemd/user/
systemctl --user daemon-reload
sudo loginctl enable-linger $USER

# Start service
systemctl --user enable sensorpajen
systemctl --user start sensorpajen

# Check status
systemctl --user status sensorpajen
journalctl --user -u sensorpajen -f

Configuration

MQTT Settings

Edit config/sensorpajen.env:

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

Sensors

Edit config/sensors.json:

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

Service Management

See systemd/README.md for detailed service management instructions.

# Start/stop service
systemctl --user start sensorpajen
systemctl --user stop sensorpajen

# Enable/disable autostart
systemctl --user enable sensorpajen
systemctl --user disable sensorpajen

# View status
systemctl --user status sensorpajen

# View logs
journalctl --user -u sensorpajen -f
journalctl --user -u sensorpajen -n 100

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

See INSTALL.md for detailed troubleshooting steps.

Quick Checks

Permission errors:

sudo setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f ~/sensorpajen/.venv/bin/python3)
systemctl --user restart sensorpajen

Service status:

systemctl --user status sensorpajen
journalctl --user -u sensorpajen -f

MQTT test:

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

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

S
Description
Script på Raspberry Pi sensorpajern. Den mäter temperatur och luftfuktighet och skickar till mqtt.
Readme
1 MiB
2025-12-29 15:35:53 +01:00
Languages
Python 90.8%
Shell 9.2%