Files
sensorpajen/readme.md
Fredrik Wahlberg b467541eb5 Phase 9 Complete: Cleanup & Documentation
Completed:
- Created INSTALL.md with concise sysadmin-focused guide
- Updated README.md troubleshooting to reference INSTALL.md
- Marked ROADMAP Phase 9 complete

Documentation Philosophy:
- Compact and practical
- Assumes sysadmin familiarity
- Focus on actual usage, not theory

All 9 phases of migration now complete!
2025-12-27 23:09:02 +01:00

5.3 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

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