Compare commits
17 Commits
v1.0.0
...
b467541eb5
| Author | SHA1 | Date | |
|---|---|---|---|
| b467541eb5 | |||
| 16c47e62f5 | |||
| 5850089de9 | |||
| c8e8afff67 | |||
| 9b1229a2ee | |||
| 9de5f82924 | |||
| e3bec0d16e | |||
| 675c39eab3 | |||
| c1519b3eb5 | |||
| f36257226f | |||
| b740372d88 | |||
| d0ba2c5a52 | |||
| b2f9bff765 | |||
| f54c0a0f35 | |||
| c9b68dd8e2 | |||
| 426f1d3813 | |||
| 8cc2c41acf |
211
INSTALL.md
Normal file
211
INSTALL.md
Normal file
@@ -0,0 +1,211 @@
|
|||||||
|
# Installation Guide
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
- Raspberry Pi with Raspberry Pi OS (Debian-based)
|
||||||
|
- Python 3.9+
|
||||||
|
- Bluetooth adapter
|
||||||
|
- MQTT broker accessible on network
|
||||||
|
- Xiaomi Mijia LYWSD03MMC sensors with ATC firmware
|
||||||
|
|
||||||
|
## Quick Install
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Clone repository
|
||||||
|
git clone <repo-url> ~/sensorpajen
|
||||||
|
cd ~/sensorpajen
|
||||||
|
|
||||||
|
# Create virtual environment and install
|
||||||
|
python3 -m venv .venv
|
||||||
|
source .venv/bin/activate
|
||||||
|
pip install -e .
|
||||||
|
|
||||||
|
# Configure
|
||||||
|
cp config/sensorpajen.env.example config/sensorpajen.env
|
||||||
|
cp config/sensors.json.example config/sensors.json
|
||||||
|
nano config/sensorpajen.env # Set MQTT_HOST, credentials
|
||||||
|
nano config/sensors.json # Add 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
|
||||||
|
|
||||||
|
# Enable and start
|
||||||
|
systemctl --user enable sensorpajen
|
||||||
|
systemctl --user start sensorpajen
|
||||||
|
```
|
||||||
|
|
||||||
|
## Verify Installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Check service status
|
||||||
|
systemctl --user status sensorpajen
|
||||||
|
|
||||||
|
# View logs
|
||||||
|
journalctl --user -u sensorpajen -f
|
||||||
|
|
||||||
|
# Test MQTT (adjust credentials)
|
||||||
|
mosquitto_sub -h MQTT_HOST -u USER -P PASSWORD -t "MiTemperature2/#" -v
|
||||||
|
```
|
||||||
|
|
||||||
|
## Configuration Files
|
||||||
|
|
||||||
|
### MQTT Settings (`config/sensorpajen.env`)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
MQTT_HOST=192.168.1.10 # Required
|
||||||
|
MQTT_PORT=1883
|
||||||
|
MQTT_USER=username
|
||||||
|
MQTT_PASSWORD=password
|
||||||
|
MQTT_CLIENT_ID=sensorpajen
|
||||||
|
MQTT_TOPIC_PREFIX=MiTemperature2
|
||||||
|
|
||||||
|
# Optional: ntfy notifications
|
||||||
|
NTFY_ENABLED=false
|
||||||
|
NTFY_URL=https://ntfy.sh
|
||||||
|
NTFY_TOPIC=sensorpajen
|
||||||
|
NTFY_TOKEN=
|
||||||
|
|
||||||
|
# Tuning
|
||||||
|
WATCHDOG_TIMEOUT=5 # BLE scan restart timeout
|
||||||
|
CONFIG_RELOAD_INTERVAL=900 # Auto-reload sensors (15 min)
|
||||||
|
LOG_LEVEL=INFO
|
||||||
|
```
|
||||||
|
|
||||||
|
### Sensors (`config/sensors.json`)
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"sensors": [
|
||||||
|
{
|
||||||
|
"mac": "A4:C1:38:12:34:56",
|
||||||
|
"name": "Living Room",
|
||||||
|
"comment": "Optional description"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Adding New Sensors
|
||||||
|
|
||||||
|
### Automatic Discovery (Recommended)
|
||||||
|
|
||||||
|
1. Power on new sensor near Raspberry Pi
|
||||||
|
2. Wait for ntfy notification (if enabled) or check logs
|
||||||
|
3. Run approval tool:
|
||||||
|
```bash
|
||||||
|
./scripts/approve-sensors.sh
|
||||||
|
```
|
||||||
|
4. Approve sensor with custom name
|
||||||
|
5. Sensor auto-loaded within 15 minutes
|
||||||
|
|
||||||
|
### Manual Addition
|
||||||
|
|
||||||
|
1. Add to `config/sensors.json`
|
||||||
|
2. Wait 15 minutes for auto-reload, or restart:
|
||||||
|
```bash
|
||||||
|
systemctl --user restart sensorpajen
|
||||||
|
```
|
||||||
|
|
||||||
|
## Service Management
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Start/stop
|
||||||
|
systemctl --user start sensorpajen
|
||||||
|
systemctl --user stop sensorpajen
|
||||||
|
systemctl --user restart sensorpajen
|
||||||
|
|
||||||
|
# Enable/disable autostart
|
||||||
|
systemctl --user enable sensorpajen
|
||||||
|
systemctl --user disable sensorpajen
|
||||||
|
|
||||||
|
# Status and logs
|
||||||
|
systemctl --user status sensorpajen
|
||||||
|
journalctl --user -u sensorpajen -f # Follow
|
||||||
|
journalctl --user -u sensorpajen -n 100 # Last 100 lines
|
||||||
|
journalctl --user -u sensorpajen --since today # Today's logs
|
||||||
|
```
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### Permission Denied on Bluetooth
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Verify capabilities
|
||||||
|
getcap $(readlink -f ~/sensorpajen/.venv/bin/python3)
|
||||||
|
# Should show: cap_net_admin,cap_net_raw+eip
|
||||||
|
|
||||||
|
# If missing, set them
|
||||||
|
sudo setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f ~/sensorpajen/.venv/bin/python3)
|
||||||
|
systemctl --user restart sensorpajen
|
||||||
|
```
|
||||||
|
|
||||||
|
### MQTT Connection Failed
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Test MQTT manually
|
||||||
|
mosquitto_sub -h MQTT_HOST -u USER -P PASSWORD -t "test" -v
|
||||||
|
|
||||||
|
# Check credentials in config/sensorpajen.env
|
||||||
|
# Check firewall on MQTT broker
|
||||||
|
```
|
||||||
|
|
||||||
|
### No Sensor Data
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Verify sensors visible
|
||||||
|
sudo hcitool lescan
|
||||||
|
|
||||||
|
# Check sensor has ATC firmware (should show as ATC_XXXXXX)
|
||||||
|
# Verify MAC addresses in sensors.json match actual sensors
|
||||||
|
# Check battery level (low battery = intermittent connection)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Service Won't Start
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Check logs for errors
|
||||||
|
journalctl --user -u sensorpajen -n 50
|
||||||
|
|
||||||
|
# Common issues:
|
||||||
|
# - MQTT_HOST not set in config/sensorpajen.env
|
||||||
|
# - sensors.json syntax error
|
||||||
|
# - Bluetooth service not running: sudo systemctl start bluetooth
|
||||||
|
```
|
||||||
|
|
||||||
|
## Updates
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd ~/sensorpajen
|
||||||
|
git pull
|
||||||
|
source .venv/bin/activate
|
||||||
|
pip install -e .
|
||||||
|
|
||||||
|
# Reinstall service if systemd file changed
|
||||||
|
cp systemd/sensorpajen.service ~/.config/systemd/user/
|
||||||
|
systemctl --user daemon-reload
|
||||||
|
systemctl --user restart sensorpajen
|
||||||
|
```
|
||||||
|
|
||||||
|
## Uninstall
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Stop and disable service
|
||||||
|
systemctl --user stop sensorpajen
|
||||||
|
systemctl --user disable sensorpajen
|
||||||
|
|
||||||
|
# Remove service file
|
||||||
|
rm ~/.config/systemd/user/sensorpajen.service
|
||||||
|
systemctl --user daemon-reload
|
||||||
|
|
||||||
|
# Remove capabilities
|
||||||
|
sudo setcap -r $(readlink -f ~/sensorpajen/.venv/bin/python3)
|
||||||
|
|
||||||
|
# Delete repository
|
||||||
|
rm -rf ~/sensorpajen
|
||||||
|
```
|
||||||
528
LYWSD03MMC.py
528
LYWSD03MMC.py
@@ -1,528 +0,0 @@
|
|||||||
#!/usr/bin/python3 -u
|
|
||||||
#!/home/openhabian/Python3/Python-3.7.4/python -u
|
|
||||||
#-u to unbuffer output. Otherwise when calling with nohup or redirecting output things are printed very lately or would even mixup
|
|
||||||
|
|
||||||
from bluepy import btle
|
|
||||||
import argparse
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
from dataclasses import dataclass
|
|
||||||
from collections import deque
|
|
||||||
import threading
|
|
||||||
import time
|
|
||||||
import signal
|
|
||||||
import traceback
|
|
||||||
import math
|
|
||||||
import logging
|
|
||||||
import paho.mqtt.client as mqtt
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Measurement:
|
|
||||||
temperature: float
|
|
||||||
humidity: int
|
|
||||||
voltage: float
|
|
||||||
calibratedHumidity: int = 0
|
|
||||||
battery: int = 0
|
|
||||||
timestamp: int = 0
|
|
||||||
sensorname: str = ""
|
|
||||||
rssi: int = 0
|
|
||||||
|
|
||||||
def __eq__(self, other): #rssi may be different
|
|
||||||
if self.temperature == other.temperature and self.humidity == other.humidity and self.calibratedHumidity == other.calibratedHumidity and self.battery == other.battery and self.voltage == other.voltage and self.sensorname == other.sensorname:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
measurements=deque()
|
|
||||||
#globalBatteryLevel=0
|
|
||||||
previousMeasurement=Measurement(0,0,0,0,0,0,0,0)
|
|
||||||
identicalCounter=0
|
|
||||||
|
|
||||||
def signal_handler(sig, frame):
|
|
||||||
if args.atc:
|
|
||||||
disable_le_scan(sock)
|
|
||||||
os._exit(0)
|
|
||||||
|
|
||||||
def watchDog_Thread():
|
|
||||||
global unconnectedTime
|
|
||||||
global connected
|
|
||||||
global pid
|
|
||||||
while True:
|
|
||||||
logging.debug("watchdog_Thread")
|
|
||||||
logging.debug("unconnectedTime : " + str(unconnectedTime))
|
|
||||||
logging.debug("connected : " + str(connected))
|
|
||||||
logging.debug("pid : " + str(pid))
|
|
||||||
now = int(time.time())
|
|
||||||
if (unconnectedTime is not None) and ((now - unconnectedTime) > 60): #could also check connected is False, but this is more fault proof
|
|
||||||
pstree=os.popen("pstree -p " + str(pid)).read() #we want to kill only bluepy from our own process tree, because other python scripts have there own bluepy-helper process
|
|
||||||
logging.debug("PSTree: " + pstree)
|
|
||||||
try:
|
|
||||||
bluepypid=re.findall(r'bluepy-helper\((.*)\)',pstree)[0] #Store the bluepypid, to kill it later
|
|
||||||
except IndexError: #Should not happen since we're now connected
|
|
||||||
logging.debug("Couldn't find pid of bluepy-helper")
|
|
||||||
os.system("kill " + bluepypid)
|
|
||||||
logging.debug("Killed bluepy with pid: " + str(bluepypid))
|
|
||||||
unconnectedTime = now #reset unconnectedTime to prevent multiple killings in a row
|
|
||||||
time.sleep(5)
|
|
||||||
|
|
||||||
|
|
||||||
def thread_SendingData():
|
|
||||||
global previousMeasurement
|
|
||||||
global measurements
|
|
||||||
path = os.path.dirname(os.path.abspath(__file__))
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
mea = measurements.popleft()
|
|
||||||
if (mea == previousMeasurement and identicalCounter < args.skipidentical): #only send data when it has changed or X identical data has been skipped, ~10 pakets per minute, 50 pakets --> writing at least every 5 minutes
|
|
||||||
print("Measurements are identical don't send data\n")
|
|
||||||
identicalCounter+=1
|
|
||||||
continue
|
|
||||||
identicalCounter=0
|
|
||||||
fmt = "sensorname,temperature,humidity,voltage" #don't try to seperate by semicolon ';' os.system will use that as command seperator
|
|
||||||
if ' ' in mea.sensorname:
|
|
||||||
sensorname = '"' + mea.sensorname + '"'
|
|
||||||
else:
|
|
||||||
sensorname = mea.sensorname
|
|
||||||
params = sensorname + " " + str(mea.temperature) + " " + str(mea.humidity) + " " + str(mea.voltage)
|
|
||||||
if (args.TwoPointCalibration or args.offset): #would be more efficient to generate fmt only once
|
|
||||||
fmt +=",humidityCalibrated"
|
|
||||||
params += " " + str(mea.calibratedHumidity)
|
|
||||||
if (args.battery):
|
|
||||||
fmt +=",batteryLevel"
|
|
||||||
params += " " + str(mea.battery)
|
|
||||||
if (args.rssi):
|
|
||||||
fmt +=",rssi"
|
|
||||||
params += " " + str(mea.rssi)
|
|
||||||
params += " " + str(mea.timestamp)
|
|
||||||
fmt +=",timestamp"
|
|
||||||
|
|
||||||
#""" cmd = path + "/" + args.callback + " " + fmt + " " + params
|
|
||||||
# print(cmd)
|
|
||||||
# ret = os.system(cmd)./L
|
|
||||||
# if (ret != 0):
|
|
||||||
# measurements.appendleft(mea) #put the measurement back
|
|
||||||
# print ("Data couln't be send to Callback, retrying...")
|
|
||||||
# time.sleep(5) #wait before trying again
|
|
||||||
# else: #data was sent
|
|
||||||
# previousMeasurement=Measurement(mea.temperature,mea.humidity,mea.voltage,mea.calibratedHumidity,mea.battery,0) #using copy or deepcopy requires implementation in the class definition
|
|
||||||
#"""
|
|
||||||
except IndexError:
|
|
||||||
#print("Keine Daten")
|
|
||||||
time.sleep(1)
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
print(traceback.format_exc())
|
|
||||||
|
|
||||||
sock = None #from ATC
|
|
||||||
lastBLEPaketReceived = 0
|
|
||||||
BLERestartCounter = 1
|
|
||||||
def keepingLEScanRunning(): #LE-Scanning gets disabled sometimes, especially if you have a lot of BLE connections, this thread periodically enables BLE scanning again
|
|
||||||
global BLERestartCounter
|
|
||||||
while True:
|
|
||||||
time.sleep(1)
|
|
||||||
now = time.time()
|
|
||||||
if now - lastBLEPaketReceived > args.watchdogtimer:
|
|
||||||
print("Watchdog: Did not receive any BLE Paket within", int(now - lastBLEPaketReceived), "s. Restarting BLE scan. Count:", BLERestartCounter)
|
|
||||||
disable_le_scan(sock)
|
|
||||||
enable_le_scan(sock, filter_duplicates=False)
|
|
||||||
BLERestartCounter += 1
|
|
||||||
print("")
|
|
||||||
time.sleep(5) #give some time to take effect
|
|
||||||
|
|
||||||
|
|
||||||
def calibrateHumidity2Points(humidity, offset1, offset2, calpoint1, calpoint2):
|
|
||||||
#offset1=args.offset1
|
|
||||||
#offset2=args.offset2
|
|
||||||
#p1y=args.calpoint1
|
|
||||||
#p2y=args.calpoint2
|
|
||||||
p1y=calpoint1
|
|
||||||
p2y=calpoint2
|
|
||||||
p1x=p1y - offset1
|
|
||||||
p2x=p2y - offset2
|
|
||||||
m = (p1y - p2y) * 1.0 / (p1x - p2x) # y=mx+b
|
|
||||||
#b = (p1x * p2y - p2x * p1y) * 1.0 / (p1y - p2y)
|
|
||||||
b = p2y - m * p2x #would be more efficient to do this calculations only once
|
|
||||||
humidityCalibrated=m*humidity + b
|
|
||||||
if (humidityCalibrated > 100 ): #with correct calibration this should not happen
|
|
||||||
humidityCalibrated = 100
|
|
||||||
elif (humidityCalibrated < 0):
|
|
||||||
humidityCalibrated = 0
|
|
||||||
humidityCalibrated=int(round(humidityCalibrated,0))
|
|
||||||
return humidityCalibrated
|
|
||||||
|
|
||||||
|
|
||||||
mode="round"
|
|
||||||
class MyDelegate(btle.DefaultDelegate):
|
|
||||||
def __init__(self, params):
|
|
||||||
btle.DefaultDelegate.__init__(self)
|
|
||||||
# ... initialise here
|
|
||||||
|
|
||||||
def handleNotification(self, cHandle, data):
|
|
||||||
global measurements
|
|
||||||
try:
|
|
||||||
measurement = Measurement(0,0,0,0,0,0,0,0)
|
|
||||||
if args.influxdb == 1:
|
|
||||||
measurement.timestamp = int((time.time() // 10) * 10)
|
|
||||||
else:
|
|
||||||
measurement.timestamp = int(time.time())
|
|
||||||
temp=int.from_bytes(data[0:2],byteorder='little',signed=True)/100
|
|
||||||
#print("Temp received: " + str(temp))
|
|
||||||
if args.round:
|
|
||||||
#print("Temperatur unrounded: " + str(temp
|
|
||||||
|
|
||||||
if args.debounce:
|
|
||||||
global mode
|
|
||||||
temp*=10
|
|
||||||
intpart = math.floor(temp)
|
|
||||||
fracpart = round(temp - intpart,1)
|
|
||||||
#print("Fracpart: " + str(fracpart))
|
|
||||||
if fracpart >= 0.7:
|
|
||||||
mode="ceil"
|
|
||||||
elif fracpart <= 0.2: #either 0.8 and 0.3 or 0.7 and 0.2 for best even distribution
|
|
||||||
mode="trunc"
|
|
||||||
#print("Modus: " + mode)
|
|
||||||
if mode=="trunc": #only a few times
|
|
||||||
temp=math.trunc(temp)
|
|
||||||
elif mode=="ceil":
|
|
||||||
temp=math.ceil(temp)
|
|
||||||
else:
|
|
||||||
temp=round(temp,0)
|
|
||||||
temp /=10.
|
|
||||||
#print("Debounced temp: " + str(temp))
|
|
||||||
else:
|
|
||||||
temp=round(temp,1)
|
|
||||||
humidity=int.from_bytes(data[2:3],byteorder='little')
|
|
||||||
print("Temperature: " + str(temp))
|
|
||||||
print("Humidity: " + str(humidity))
|
|
||||||
voltage=int.from_bytes(data[3:5],byteorder='little') / 1000.
|
|
||||||
print("Battery voltage:",voltage,"V")
|
|
||||||
measurement.temperature = temp
|
|
||||||
measurement.humidity = humidity
|
|
||||||
measurement.voltage = voltage
|
|
||||||
measurement.sensorname = args.name
|
|
||||||
if args.battery:
|
|
||||||
#measurement.battery = globalBatteryLevel
|
|
||||||
batteryLevel = min(int(round((voltage - 2.1),2) * 100), 100) #3.1 or above --> 100% 2.1 --> 0 %
|
|
||||||
measurement.battery = batteryLevel
|
|
||||||
print("Battery level:",batteryLevel)
|
|
||||||
|
|
||||||
|
|
||||||
if args.offset:
|
|
||||||
humidityCalibrated = humidity + args.offset
|
|
||||||
print("Calibrated humidity: " + str(humidityCalibrated))
|
|
||||||
measurement.calibratedHumidity = humidityCalibrated
|
|
||||||
|
|
||||||
if args.TwoPointCalibration:
|
|
||||||
humidityCalibrated= calibrateHumidity2Points(humidity,args.offset1,args.offset2, args.calpoint1, args.calpoint2)
|
|
||||||
print("Calibrated humidity: " + str(humidityCalibrated))
|
|
||||||
measurement.calibratedHumidity = humidityCalibrated
|
|
||||||
|
|
||||||
if(args.callback):
|
|
||||||
measurements.append(measurement)
|
|
||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print("Fehler")
|
|
||||||
print(e)
|
|
||||||
print(traceback.format_exc())
|
|
||||||
|
|
||||||
# Initialisation -------
|
|
||||||
|
|
||||||
def connect():
|
|
||||||
#print("Interface: " + str(args.interface))
|
|
||||||
p = btle.Peripheral(adress,iface=args.interface)
|
|
||||||
val=b'\x01\x00'
|
|
||||||
p.writeCharacteristic(0x0038,val,True) #enable notifications of Temperature, Humidity and Battery voltage
|
|
||||||
p.writeCharacteristic(0x0046,b'\xf4\x01\x00',True)
|
|
||||||
p.withDelegate(MyDelegate("abc"))
|
|
||||||
return p
|
|
||||||
|
|
||||||
# Main loop --------
|
|
||||||
parser=argparse.ArgumentParser(allow_abbrev=False)
|
|
||||||
parser.add_argument("--device","-d", help="Set the device MAC-Address in format AA:BB:CC:DD:EE:FF",metavar='AA:BB:CC:DD:EE:FF')
|
|
||||||
parser.add_argument("--battery","-b", help="Get estimated battery level", metavar='', type=int, nargs='?', const=1)
|
|
||||||
parser.add_argument("--count","-c", help="Read/Receive N measurements and then exit script", metavar='N', type=int)
|
|
||||||
parser.add_argument("--interface","-i", help="Specifiy the interface number to use, e.g. 1 for hci1", metavar='N', type=int, default=0)
|
|
||||||
parser.add_argument("--unreachable-count","-urc", help="Exit after N unsuccessful connection tries", metavar='N', type=int, default=0)
|
|
||||||
|
|
||||||
|
|
||||||
rounding = parser.add_argument_group("Rounding and debouncing")
|
|
||||||
rounding.add_argument("--round","-r", help="Round temperature to one decimal place",action='store_true')
|
|
||||||
rounding.add_argument("--debounce","-deb", help="Enable this option to get more stable temperature values, requires -r option",action='store_true')
|
|
||||||
|
|
||||||
offsetgroup = parser.add_argument_group("Offset calibration mode")
|
|
||||||
offsetgroup.add_argument("--offset","-o", help="Enter an offset to the reported humidity value",type=int)
|
|
||||||
|
|
||||||
complexCalibrationGroup=parser.add_argument_group("2 Point Calibration")
|
|
||||||
complexCalibrationGroup.add_argument("--TwoPointCalibration","-2p", help="Use complex calibration mode. All arguments below are required",action='store_true')
|
|
||||||
complexCalibrationGroup.add_argument("--calpoint1","-p1", help="Enter the first calibration point",type=int)
|
|
||||||
complexCalibrationGroup.add_argument("--offset1","-o1", help="Enter the offset for the first calibration point",type=int)
|
|
||||||
complexCalibrationGroup.add_argument("--calpoint2","-p2", help="Enter the second calibration point",type=int)
|
|
||||||
complexCalibrationGroup.add_argument("--offset2","-o2", help="Enter the offset for the second calibration point",type=int)
|
|
||||||
|
|
||||||
callbackgroup = parser.add_argument_group("Callback related arguments")
|
|
||||||
callbackgroup.add_argument("--callback","-call", help="Pass the path to a program/script that will be called on each new measurement")
|
|
||||||
callbackgroup.add_argument("--name","-n", help="Give this sensor a name reported to the callback script")
|
|
||||||
callbackgroup.add_argument("--skipidentical","-skip", help="N consecutive identical measurements won't be reported to callbackfunction",metavar='N', type=int, default=0)
|
|
||||||
callbackgroup.add_argument("--influxdb","-infl", help="Optimize for writing data to influxdb,1 timestamp optimization, 2 integer optimization",metavar='N', type=int, default=0)
|
|
||||||
|
|
||||||
atcgroup = parser.add_argument_group("ATC mode related arguments")
|
|
||||||
atcgroup.add_argument("--atc","-a", help="Read the data of devices with custom ATC firmware flashed",action='store_true')
|
|
||||||
atcgroup.add_argument("--watchdogtimer","-wdt",metavar='X', type=int, help="Re-enable scanning after not receiving any BLE packet after X seconds")
|
|
||||||
atcgroup.add_argument("--devicelistfile","-df",help="Specify a device list file giving further details to devices")
|
|
||||||
atcgroup.add_argument("--onlydevicelist","-odl", help="Only read devices which are in the device list file",action='store_true')
|
|
||||||
atcgroup.add_argument("--rssi","-rs", help="Report RSSI via callback",action='store_true')
|
|
||||||
|
|
||||||
|
|
||||||
args=parser.parse_args()
|
|
||||||
if args.device:
|
|
||||||
if re.match("[0-9a-fA-F]{2}([:]?)[0-9a-fA-F]{2}(\\1[0-9a-fA-F]{2}){4}$",args.device):
|
|
||||||
adress=args.device
|
|
||||||
else:
|
|
||||||
print("Please specify device MAC-Address in format AA:BB:CC:DD:EE:FF")
|
|
||||||
os._exit(1)
|
|
||||||
elif not args.atc:
|
|
||||||
parser.print_help()
|
|
||||||
os._exit(1)
|
|
||||||
|
|
||||||
if args.TwoPointCalibration:
|
|
||||||
if(not(args.calpoint1 and args.offset1 and args.calpoint2 and args.offset2)):
|
|
||||||
print("In 2 Point calibration you have to enter 4 points")
|
|
||||||
os._exit(1)
|
|
||||||
elif(args.offset):
|
|
||||||
print("Offset calibration and 2 Point calibration can't be used together")
|
|
||||||
os._exit(1)
|
|
||||||
if not args.name:
|
|
||||||
args.name = args.device
|
|
||||||
|
|
||||||
if args.callback:
|
|
||||||
dataThread = threading.Thread(target=thread_SendingData)
|
|
||||||
dataThread.start()
|
|
||||||
|
|
||||||
signal.signal(signal.SIGINT, signal_handler)
|
|
||||||
|
|
||||||
if args.device:
|
|
||||||
|
|
||||||
p=btle.Peripheral()
|
|
||||||
cnt=0
|
|
||||||
|
|
||||||
|
|
||||||
connected=False
|
|
||||||
#logging.basicConfig(level=logging.DEBUG)
|
|
||||||
logging.basicConfig(level=logging.ERROR)
|
|
||||||
logging.debug("Debug: Starting script...")
|
|
||||||
pid=os.getpid()
|
|
||||||
bluepypid=None
|
|
||||||
unconnectedTime=None
|
|
||||||
connectionLostCounter=0
|
|
||||||
|
|
||||||
watchdogThread = threading.Thread(target=watchDog_Thread)
|
|
||||||
watchdogThread.start()
|
|
||||||
logging.debug("watchdogThread started")
|
|
||||||
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
if not connected:
|
|
||||||
#Bluepy sometimes hangs and makes it even impossible to connect with gatttool as long it is running
|
|
||||||
#on every new connection a new bluepy-helper is called
|
|
||||||
#we now make sure that the old one is really terminated. Even if it hangs a simple kill signal was sufficient to terminate it
|
|
||||||
# if bluepypid is not None:
|
|
||||||
# os.system("kill " + bluepypid)
|
|
||||||
# print("Killed possibly remaining bluepy-helper")
|
|
||||||
# else:
|
|
||||||
# print("bluepy-helper couldn't be determined, killing not allowed")
|
|
||||||
|
|
||||||
print("Trying to connect to " + adress)
|
|
||||||
p=connect()
|
|
||||||
# logging.debug("Own PID: " + str(pid))
|
|
||||||
# pstree=os.popen("pstree -p " + str(pid)).read() #we want to kill only bluepy from our own process tree, because other python scripts have there own bluepy-helper process
|
|
||||||
# logging.debug("PSTree: " + pstree)
|
|
||||||
# try:
|
|
||||||
# bluepypid=re.findall(r'bluepy-helper\((.*)\)',pstree)[0] #Store the bluepypid, to kill it later
|
|
||||||
# except IndexError: #Should not happen since we're now connected
|
|
||||||
# logging.debug("Couldn't find pid of bluepy-helper")
|
|
||||||
connected=True
|
|
||||||
unconnectedTime=None
|
|
||||||
|
|
||||||
# if args.battery:
|
|
||||||
# if(cnt % args.battery == 0):
|
|
||||||
# print("Warning the battery option is deprecated, Aqara device always reports 99 % battery")
|
|
||||||
# batt=p.readCharacteristic(0x001b)
|
|
||||||
# batt=int.from_bytes(batt,byteorder="little")
|
|
||||||
# print("Battery-Level: " + str(batt))
|
|
||||||
# globalBatteryLevel = batt
|
|
||||||
|
|
||||||
|
|
||||||
if p.waitForNotifications(2000):
|
|
||||||
# handleNotification() was called
|
|
||||||
|
|
||||||
cnt += 1
|
|
||||||
if args.count is not None and cnt >= args.count:
|
|
||||||
print(str(args.count) + " measurements collected. Exiting in a moment.")
|
|
||||||
p.disconnect()
|
|
||||||
time.sleep(5)
|
|
||||||
#It seems that sometimes bluepy-helper remains and thus prevents a reconnection, so we try killing our own bluepy-helper
|
|
||||||
pstree=os.popen("pstree -p " + str(pid)).read() #we want to kill only bluepy from our own process tree, because other python scripts have there own bluepy-helper process
|
|
||||||
bluepypid=0
|
|
||||||
try:
|
|
||||||
bluepypid=re.findall(r'bluepy-helper\((.*)\)',pstree)[0] #Store the bluepypid, to kill it later
|
|
||||||
except IndexError: #Should normally occur because we're disconnected
|
|
||||||
logging.debug("Couldn't find pid of bluepy-helper")
|
|
||||||
if bluepypid != 0:
|
|
||||||
os.system("kill " + bluepypid)
|
|
||||||
logging.debug("Killed bluepy with pid: " + str(bluepypid))
|
|
||||||
os._exit(0)
|
|
||||||
print("")
|
|
||||||
continue
|
|
||||||
except Exception as e:
|
|
||||||
print("Connection lost")
|
|
||||||
connectionLostCounter +=1
|
|
||||||
if connected is True: #First connection abort after connected
|
|
||||||
unconnectedTime=int(time.time())
|
|
||||||
connected=False
|
|
||||||
if args.unreachable_count != 0 and connectionLostCounter >= args.unreachable_count:
|
|
||||||
print("Maximum numbers of unsuccessful connections reaches, exiting")
|
|
||||||
os._exit(0)
|
|
||||||
time.sleep(1)
|
|
||||||
logging.debug(e)
|
|
||||||
logging.debug(traceback.format_exc())
|
|
||||||
|
|
||||||
print ("Waiting...")
|
|
||||||
# Perhaps do something else here
|
|
||||||
|
|
||||||
elif args.atc:
|
|
||||||
print("Script started in ATC Mode")
|
|
||||||
print("----------------------------")
|
|
||||||
print("In this mode all devices within reach are read out, unless a namefile and --namefileonlydevices is specified.")
|
|
||||||
print("Also --name Argument is ignored, if you require names, please use --namefile.")
|
|
||||||
print("In this mode rounding and debouncing are not available, since ATC firmware sends out only one decimal place.")
|
|
||||||
print("ATC mode usually requires root rights. If you want to use it with normal user rights, \nplease execute \"sudo setcap cap_net_raw,cap_net_admin+eip $(eval readlink -f `which python3`)\"")
|
|
||||||
print("You have to redo this step if you upgrade your python version.")
|
|
||||||
print("----------------------------")
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import bluetooth._bluetooth as bluez
|
|
||||||
|
|
||||||
from bluetooth_utils import (toggle_device,
|
|
||||||
enable_le_scan, parse_le_advertising_events,
|
|
||||||
disable_le_scan, raw_packet_to_str)
|
|
||||||
mqttserver = "192.168.0.114"
|
|
||||||
mqttclient = mqtt.Client("MiTemperature2")
|
|
||||||
|
|
||||||
advCounter=dict()
|
|
||||||
sensors = dict()
|
|
||||||
if args.devicelistfile:
|
|
||||||
import configparser
|
|
||||||
if not os.path.exists(args.devicelistfile):
|
|
||||||
print ("Error specified device list file '",args.devicelistfile,"' not found")
|
|
||||||
os._exit(1)
|
|
||||||
sensors = configparser.ConfigParser()
|
|
||||||
sensors.read(args.devicelistfile)
|
|
||||||
|
|
||||||
if args.onlydevicelist and not args.devicelistfile:
|
|
||||||
print("Error: --onlydevicelist requires --devicelistfile <devicelistfile>")
|
|
||||||
os._exit(1)
|
|
||||||
|
|
||||||
dev_id = args.interface # the bluetooth device is hci0
|
|
||||||
toggle_device(dev_id, True)
|
|
||||||
|
|
||||||
try:
|
|
||||||
sock = bluez.hci_open_dev(dev_id)
|
|
||||||
except:
|
|
||||||
print("Cannot open bluetooth device %i" % dev_id)
|
|
||||||
raise
|
|
||||||
|
|
||||||
enable_le_scan(sock, filter_duplicates=False)
|
|
||||||
|
|
||||||
try:
|
|
||||||
prev_data = None
|
|
||||||
|
|
||||||
def le_advertise_packet_handler(mac, adv_type, data, rssi):
|
|
||||||
global lastBLEPaketReceived
|
|
||||||
if args.watchdogtimer:
|
|
||||||
lastBLEPaketReceived = time.time()
|
|
||||||
lastBLEPaketReceived = time.time()
|
|
||||||
#print("reveived BLE packet")
|
|
||||||
data_str = raw_packet_to_str(data)
|
|
||||||
ATCPaketMAC = data_str[10:22].upper()
|
|
||||||
macStr = mac.replace(":","").upper()
|
|
||||||
atcIdentifier = data_str[6:10].upper()
|
|
||||||
if(atcIdentifier == "1A18" and ATCPaketMAC == macStr) and not args.onlydevicelist or (atcIdentifier == "1A18" and mac in sensors): #only Data from ATC devices, double checked
|
|
||||||
advNumber = data_str[-2:]
|
|
||||||
if macStr in advCounter:
|
|
||||||
lastAdvNumber = advCounter[macStr]
|
|
||||||
else:
|
|
||||||
lastAdvNumber = None
|
|
||||||
if lastAdvNumber == None or lastAdvNumber != advNumber:
|
|
||||||
advCounter[macStr] = advNumber
|
|
||||||
print("BLE packet: %s %02x %s %d" % (mac, adv_type, data_str, rssi))
|
|
||||||
#print("AdvNumber: ", advNumber)
|
|
||||||
#temp = data_str[22:26].encode('utf-8')
|
|
||||||
#temperature = int.from_bytes(bytearray.fromhex(data_str[22:26]),byteorder='big') / 10.
|
|
||||||
global measurements
|
|
||||||
measurement = Measurement(0,0,0,0,0,0,0,0)
|
|
||||||
if args.influxdb == 1:
|
|
||||||
measurement.timestamp = int((time.time() // 10) * 10)
|
|
||||||
else:
|
|
||||||
measurement.timestamp = int(time.time())
|
|
||||||
|
|
||||||
|
|
||||||
#temperature = int(data_str[22:26],16) / 10.
|
|
||||||
temperature = int.from_bytes(bytearray.fromhex(data_str[22:26]),byteorder='big',signed=True) / 10.
|
|
||||||
print("Temperature: ", temperature)
|
|
||||||
humidity = int(data_str[26:28], 16)
|
|
||||||
print("Humidity: ", humidity)
|
|
||||||
batteryVoltage = int(data_str[30:34], 16) / 1000
|
|
||||||
print ("Battery voltage:", batteryVoltage,"V")
|
|
||||||
print ("RSSI:", rssi, "dBm")
|
|
||||||
|
|
||||||
if args.battery:
|
|
||||||
batteryPercent = int(data_str[28:30], 16)
|
|
||||||
print ("Battery:", batteryPercent,"%")
|
|
||||||
measurement.battery = batteryPercent
|
|
||||||
measurement.humidity = humidity
|
|
||||||
measurement.temperature = temperature
|
|
||||||
measurement.voltage = batteryVoltage
|
|
||||||
measurement.rssi = rssi
|
|
||||||
|
|
||||||
if mac in sensors:
|
|
||||||
try:
|
|
||||||
measurement.sensorname = sensors[mac]["sensorname"]
|
|
||||||
except:
|
|
||||||
measurement.sensorname = mac
|
|
||||||
if "offset1" in sensors[mac] and "offset2" in sensors[mac] and "calpoint1" in sensors[mac] and "calpoint2" in sensors[mac]:
|
|
||||||
measurement.humidity = calibrateHumidity2Points(humidity,int(sensors[mac]["offset1"]),int(sensors[mac]["offset2"]),int(sensors[mac]["calpoint1"]),int(sensors[mac]["calpoint2"]))
|
|
||||||
print ("Humidity calibrated (2 points calibration): ", measurement.humidity)
|
|
||||||
elif "humidityOffset" in sensors[mac]:
|
|
||||||
measurement.humidity = humidity + int(sensors[mac]["humidityOffset"])
|
|
||||||
print ("Humidity calibrated (offset calibration): ", measurement.humidity)
|
|
||||||
else:
|
|
||||||
measurement.sensorname = mac
|
|
||||||
|
|
||||||
print ("MQTT publishing")
|
|
||||||
mqttclient.connect(mqttserver)
|
|
||||||
mqttclient.publish(("MiTemperature2/%s/temp" % measurement.sensorname), measurement.temperature)
|
|
||||||
mqttclient.publish(("MiTemperature2/%s/humidity" % measurement.sensorname), measurement.humidity)
|
|
||||||
mqttclient.publish(("MiTemperature2/%s/batterylevel" % measurement.sensorname), measurement.battery)
|
|
||||||
print ("MQTT done")
|
|
||||||
|
|
||||||
|
|
||||||
if(args.callback):
|
|
||||||
measurements.append(measurement)
|
|
||||||
#print("Length:", len(measurements))
|
|
||||||
print("")
|
|
||||||
|
|
||||||
if args.watchdogtimer:
|
|
||||||
keepingLEScanRunningThread = threading.Thread(target=keepingLEScanRunning)
|
|
||||||
keepingLEScanRunningThread.start()
|
|
||||||
logging.debug("keepingLEScanRunningThread started")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Blocking call (the given handler will be called each time a new LE
|
|
||||||
# advertisement packet is detected)
|
|
||||||
parse_le_advertising_events(sock,
|
|
||||||
handler=le_advertise_packet_handler,
|
|
||||||
debug=False)
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
disable_le_scan(sock)
|
|
||||||
364
ROADMAP.md
364
ROADMAP.md
@@ -102,138 +102,70 @@ Using relative paths for portability across systems:
|
|||||||
|
|
||||||
## Migration Phases
|
## Migration Phases
|
||||||
|
|
||||||
### Phase 1: Preparation & Cleanup ✓ TODO
|
### Phase 1: Preparation & Cleanup ✅ DONE (2025-12-27)
|
||||||
**Goal**: Reorganize repository without breaking existing functionality
|
**Goal**: Reorganize repository without breaking existing functionality
|
||||||
|
|
||||||
|
**Notes**:
|
||||||
|
- Created modern Python package structure with src/ layout
|
||||||
|
- Converted INI sensor config to JSON format (sensors.json.example)
|
||||||
|
- Environment-based configuration instead of hardcoded values
|
||||||
|
- DHT11 sensor functionality removed as planned
|
||||||
|
- Legacy scripts preserved in legacy/ folder
|
||||||
|
|
||||||
#### Tasks:
|
#### Tasks:
|
||||||
1. Create new directory structure
|
- ✅ Create new directory structure
|
||||||
```bash
|
- ✅ Create pyproject.toml with dependencies
|
||||||
mkdir -p src/sensorpajen
|
- ✅ Remove DHT11 functionality
|
||||||
mkdir -p config
|
- ✅ Move legacy scripts to legacy/ folder
|
||||||
mkdir -p legacy
|
- ✅ Create config file templates (sensors.json.example, sensorpajen.env.example)
|
||||||
mkdir -p systemd
|
- ✅ Preserve requirements.txt for backward compatibility
|
||||||
mkdir -p debian
|
|
||||||
```
|
|
||||||
|
|
||||||
2. Create pyproject.toml with dependencies:
|
|
||||||
- bluepy
|
|
||||||
- paho-mqtt
|
|
||||||
|
|
||||||
3. Remove DHT11 functionality:
|
|
||||||
- Delete temperatur_koksfonstret.py
|
|
||||||
- Remove DHT11 cron job from documentation
|
|
||||||
- Update README.md
|
|
||||||
|
|
||||||
4. Move legacy scripts to legacy/ folder:
|
|
||||||
- LYWSD03MMC.py
|
|
||||||
- sendToMQTT.sh
|
|
||||||
- startup.sh
|
|
||||||
- sensorer.sh
|
|
||||||
- sensorer.ini
|
|
||||||
- bluetooth_utils.py
|
|
||||||
|
|
||||||
5. Verify existing system still works with legacy scripts
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### Phase 2: Python Package Structure ✓ TODO
|
### Phase 2: Python Package Structure ✅ DONE (2025-12-27)
|
||||||
**Goal**: Create modern Python package with proper entry point
|
**Goal**: Create modern Python package with proper entry point
|
||||||
|
|
||||||
|
**Notes**:
|
||||||
|
- Used src/ layout for better packaging practices
|
||||||
|
- Direct Python MQTT integration (no shell script callbacks)
|
||||||
|
- ATC firmware BLE advertisement reading (passive scanning)
|
||||||
|
- Watchdog thread for BLE connection recovery
|
||||||
|
- Clean separation of concerns (config, MQTT, sensors, main)
|
||||||
|
|
||||||
#### Tasks:
|
#### Tasks:
|
||||||
1. Create `src/sensorpajen/__init__.py`
|
- ✅ Created src/sensorpajen/__init__.py with version info
|
||||||
- Package initialization
|
- ✅ Created src/sensorpajen/config.py
|
||||||
- Version information
|
- Environment variable loading with validation
|
||||||
|
- SensorConfig class for JSON sensor mapping
|
||||||
2. Create `src/sensorpajen/config.py`
|
- Relative path resolution (PROJECT_ROOT)
|
||||||
- Environment variable loading
|
- Configuration validation and logging
|
||||||
- Configuration validation
|
- ✅ Created src/sensorpajen/utils.py
|
||||||
- Default values
|
- Ported bluetooth_utils.py (MIT licensed, Colin GUYON)
|
||||||
- Fail-fast on missing required config
|
- BLE scanning and advertisement parsing
|
||||||
```python
|
- ✅ Created src/sensorpajen/mqtt_publisher.py
|
||||||
import os
|
- MQTTPublisher class with connection management
|
||||||
import json
|
- Direct publishing (replaces sendToMQTT.sh)
|
||||||
from pathlib import Path
|
- Automatic reconnection support
|
||||||
|
- Battery data publishing (optional)
|
||||||
# MQTT Configuration from environment
|
- ✅ Created src/sensorpajen/sensor_reader.py
|
||||||
MQTT_HOST = os.environ.get("MQTT_HOST")
|
- SensorReader class for BLE scanning
|
||||||
MQTT_PORT = int(os.environ.get("MQTT_PORT", "1883"))
|
- ATC packet parsing
|
||||||
MQTT_USER = os.environ.get("MQTT_USER")
|
- Duplicate packet filtering
|
||||||
MQTT_PASSWORD = os.environ.get("MQTT_PASSWORD")
|
- Watchdog for BLE recovery
|
||||||
MQTT_CLIENT_ID = os.environ.get("MQTT_CLIENT_ID", "sensorpajen")
|
- Measurement dataclass
|
||||||
|
- ✅ Created src/sensorpajen/main.py
|
||||||
# Validate required config
|
- Application entry point
|
||||||
if not MQTT_HOST:
|
- Signal handling (SIGTERM, SIGINT)
|
||||||
raise RuntimeError("MQTT_HOST environment variable must be set")
|
- Graceful shutdown
|
||||||
(relative to project root)
|
- Logging to stdout for journald
|
||||||
PROJECT_ROOT = Path(__file__).parent.parent.parent
|
|
||||||
SENSOR_CONFIG_FILE = os.environ.get(
|
|
||||||
"SENSOR_CONFIG_FILE",
|
|
||||||
str(PROJECT_ROOT / "config
|
|
||||||
str(Path.home() / ".config/sensorpajen/sensors.json")
|
|
||||||
)
|
|
||||||
|
|
||||||
# Bluetooth settings
|
|
||||||
WATCHDOG_TIMEOUT = int(os.environ.get("WATCHDOG_TIMEOUT", "5"))
|
|
||||||
ENABLE_BATTERY = os.environ.get("ENABLE_BATTERY", "true").lower() == "true"
|
|
||||||
```
|
|
||||||
|
|
||||||
3. Create `src/sensorpajen/utils.py`
|
|
||||||
- Port bluetooth_utils.py functionality
|
|
||||||
- Clean up and modernize
|
|
||||||
|
|
||||||
4. Create `src/sensorpajen/sensor_reader.py`
|
|
||||||
- Extract sensor reading logic from LYWSD03MMC.py
|
|
||||||
- Remove callback/shell script dependency
|
|
||||||
- Direct Python MQTT integration
|
|
||||||
|
|
||||||
5. Create `src/sensorpajen/mqtt_publisher.py`
|
|
||||||
- MQTT client setup and connection
|
|
||||||
- Publishing logic (replacing sendToMQTT.sh)
|
|
||||||
- Error handling and reconnection
|
|
||||||
|
|
||||||
6. Create `src/sensorpajen/main.py`
|
|
||||||
- Entry point for the application
|
|
||||||
- Signal handling (SIGTERM, SIGINT)
|
|
||||||
- Logging setup (to stdout for journald)
|
|
||||||
- Main loop
|
|
||||||
```python
|
|
||||||
#!/usr/bin/env python3
|
|
||||||
import logging
|
|
||||||
import signal
|
|
||||||
import sys
|
|
||||||
from . import config
|
|
||||||
from .sensor_reader import SensorReader
|
|
||||||
from .mqtt_publisher import MQTTPublisher
|
|
||||||
|
|
||||||
def main():
|
|
||||||
logging.basicConfig(
|
|
||||||
level=logging.INFO,
|
|
||||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
||||||
stream=sys.stdout
|
|
||||||
)
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
logger.info("Starting sensorpajen service")
|
|
||||||
|
|
||||||
# Setup signal handlers
|
|
||||||
def signal_handler(sig, frame):
|
|
||||||
logger.info("Received shutdown signal")
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
signal.signal(signal.SIGTERM, signal_handler)
|
|
||||||
signal.signal(signal.SIGINT, signal_handler)
|
|
||||||
|
|
||||||
# Main application logic here
|
|
||||||
# ...
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### Phase 3: Configuration Migration ✓ TODO
|
### Phase 3: Configuration Migration ✅ DONE (2025-12-27)
|
||||||
**Goal**: Replace .ini file with JSON and environment variables
|
**Goal**: Replace .ini file with JSON and environment variables
|
||||||
|
|
||||||
|
**Notes**: Templates created in Phase 1, successfully tested on Raspberry Pi
|
||||||
|
|
||||||
#### Tasks:
|
#### Tasks:
|
||||||
1. Create sensor mapping converter script
|
1. Create sensor mapping converter script
|
||||||
- Read sensorer.ini
|
- Read sensorer.ini
|
||||||
@@ -304,9 +236,11 @@ config/sensorpajen.env
|
|||||||
debian/files
|
debian/files
|
||||||
debian/*.log
|
debian/*.log
|
||||||
debian/*.substvars
|
debian/*.substvars
|
||||||
### Phase 4: Virtual Environment & Dependencies ✓ TODO
|
### Phase 4: Virtual Environment & Dependencies ✅ DONE (2025-12-27)
|
||||||
**Goal**: Set up isolated Python environment
|
**Goal**: Set up isolated Python environment
|
||||||
|
|
||||||
|
**Notes**: Tested on Raspberry Pi, paho-mqtt v2.x compatibility fixed
|
||||||
|
|
||||||
#### Tasks:
|
#### Tasks:
|
||||||
1. Create virtual environment:
|
1. Create virtual environment:
|
||||||
```bash
|
```bash
|
||||||
@@ -332,97 +266,62 @@ config/sensorpajen.env
|
|||||||
|
|
||||||
4. Document virtual environment usage in README
|
4. Document virtual environment usage in README
|
||||||
|
|
||||||
---
|
---✅ DONE (2025-12-27)
|
||||||
|
|
||||||
### Phase 5: Bluetooth Permissions ✓ TODO
|
|
||||||
**Goal**: Allow non-root user to access Bluetooth
|
**Goal**: Allow non-root user to access Bluetooth
|
||||||
|
|
||||||
|
**Notes**: Tested on Raspberry Pi with setcap on actual Python binary
|
||||||
|
### Phase 5: Bluetooth Permissions ✅ DONE (2025-12-27)
|
||||||
|
**Goal**: Allow non-root user to access Bluetooth
|
||||||
|
|
||||||
|
**Notes**: Tested on Raspberry Pi with setcap on actual Python binary
|
||||||
|
|
||||||
#### Tasks:
|
#### Tasks:
|
||||||
1. Add user to bluetooth group:
|
- ✅ Bluetooth capabilities set with setcap
|
||||||
```bash
|
- ✅ Documented in SETUP_ON_PI.md with correct readlink -f usage
|
||||||
sudo usermod -a -G bluetooth fredrik
|
- ✅ Tested successfully on Raspberry Pi
|
||||||
```
|
|
||||||
|
|
||||||
2. Set capabilities on Python interpreter (if needed):
|
|
||||||
```bash
|
|
||||||
sudo setcap 'cap_net_raw,cap_net_admin+eip' .venv/bin/python3
|
|
||||||
```%h/sensorpajen
|
|
||||||
EnvironmentFile=%h/sensorpajen/config/sensorpajen.env
|
|
||||||
ExecStart=%h/sensorpajen/.venv/bin/python -m sensorpajen.main
|
|
||||||
Restart=always
|
|
||||||
RestartSec=10
|
|
||||||
|
|
||||||
# Bluetooth capabilities
|
|
||||||
AmbientCapabilities=CAP_NET_RAW CAP_NET_ADMIN
|
|
||||||
|
|
||||||
# Logging
|
|
||||||
StandardOutput=journal
|
|
||||||
StandardError=journal
|
|
||||||
SyslogIdentifier=sensorpajen
|
|
||||||
|
|
||||||
[Install]
|
|
||||||
WantedBy=default.target
|
|
||||||
```
|
|
||||||
|
|
||||||
Note: `%h` expands to the user's home directorycription=Sensorpajen - Bluetooth Temperature Sensor Monitor
|
|
||||||
After=network.target bluetooth.target
|
|
||||||
Wants=bluetooth.target
|
|
||||||
|
|
||||||
[Service]
|
|
||||||
Type=simple
|
|
||||||
WorkingDirectory=/home/fredrik/dev/sensorpajen
|
|
||||||
EnvironmentFile=/home/fredrik/.config/sensorpajen/sensorpajen.env
|
|
||||||
ExecStart=/home/fredrik/dev/sensorpajen/.venv/bin/python -m sensorpajen.main
|
|
||||||
Restart=always
|
|
||||||
RestartSec=10
|
|
||||||
|
|
||||||
# Bluetooth capabilities
|
|
||||||
AmbientCapabilities=CAP_NET_RAW CAP_NET_ADMIN
|
|
||||||
|
|
||||||
# Logging
|
|
||||||
StandardOutput=journal
|
|
||||||
StandardError=journal
|
|
||||||
SyslogIdentifier=sensorpajen
|
|
||||||
|
|
||||||
[Install]
|
|
||||||
WantedBy=default.target
|
|
||||||
```
|
|
||||||
|
|
||||||
2. Install service (user service):
|
|
||||||
```bash
|
|
||||||
mkdir -p ~/.config/systemd/user/
|
|
||||||
cp systemd/sensorpajen.service ~/.config/systemd/user/
|
|
||||||
systemctl --user daemon-reload
|
|
||||||
```
|
|
||||||
|
|
||||||
3. Enable lingering (service runs without login):
|
|
||||||
```bash
|
|
||||||
sudo loginctl enable-linger fredrik
|
|
||||||
```
|
|
||||||
|
|
||||||
4. Document systemd commands in README
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### Phase 7: Testing & Validation ✓ TODO
|
### Phase 6: Systemd Service Creation ✅ DONE (2025-12-27)
|
||||||
**Goal**: Verify new service works before removing legacy
|
**Goal**: Create and configure systemd user service
|
||||||
|
|
||||||
|
**Notes**:
|
||||||
|
- User service for easier management (no sudo required)
|
||||||
|
- Service ready for installation on Raspberry Pi
|
||||||
|
- Comprehensive documentation provided
|
||||||
|
- **Important discoveries**:
|
||||||
|
- `AmbientCapabilities` does NOT work in user services (only system services)
|
||||||
|
- Must use `setcap` on the Python binary instead
|
||||||
|
- `NoNewPrivileges=true` prevents file capabilities from working - must be disabled
|
||||||
|
- Capabilities must be set on actual binary, not symlinks: `setcap ... $(readlink -f python3)`
|
||||||
|
|
||||||
#### Tasks:
|
#### Tasks:
|
||||||
1. Stop legacy cron/tmux processes:
|
- ✅ Created systemd/sensorpajen.service
|
||||||
```bash
|
- ✅ Created systemd/README.md with full documentation
|
||||||
crontab -e # Comment out sensorpajen entries
|
- ✅ Service management and troubleshooting guides included
|
||||||
tmux kill-session -t sensorer
|
- ✅ Tested and verified working on Raspberry Pi
|
||||||
```
|
|
||||||
|
|
||||||
2. Start new service:
|
---
|
||||||
```bash
|
|
||||||
systemctl --user start sensorpajen
|
|
||||||
```
|
|
||||||
|
|
||||||
3. Monitor logs:
|
### Phase 7: Testing & Validation ✅ DONE (2025-12-27)
|
||||||
```bash
|
**Goal**: Verify new service works before removing legacy
|
||||||
journalctl --user -u sensorpajen -f
|
|
||||||
```APT Package Creation ✓ TODO
|
**Notes**:
|
||||||
|
- Service tested and running successfully
|
||||||
|
- Legacy cron/tmux system stopped
|
||||||
|
- All sensors reporting correctly via systemd service
|
||||||
|
|
||||||
|
#### Tasks:
|
||||||
|
- ✅ Stopped legacy cron/tmux processes
|
||||||
|
- ✅ Started new systemd service
|
||||||
|
- ✅ Monitored logs - no errors
|
||||||
|
- ✅ Verified all 8 sensors reporting
|
||||||
|
- ✅ Confirmed MQTT publishing working
|
||||||
|
- ✅ Tested service restart and auto-recovery
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Phase 8: APT Package Creation ✓ TODO
|
||||||
**Goal**: Create Debian package for easy installation on Raspberry Pi
|
**Goal**: Create Debian package for easy installation on Raspberry Pi
|
||||||
|
|
||||||
#### Tasks:
|
#### Tasks:
|
||||||
@@ -561,57 +460,36 @@ When installed via .deb package:
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### Phase 9:
|
### Phase 9: Cleanup & Documentation ✅ DONE (2025-12-27)
|
||||||
|
**Goal**: Remove legacy code and finalize documentation
|
||||||
|
|
||||||
4. Verify MQTT messages:
|
**Notes**:
|
||||||
```bash
|
- Legacy cron/tmux scripts removed
|
||||||
mosquitto_sub -h 192.168.0.114 -u hasse -P casablanca -t "MiTemperature2/#" -v
|
- Documentation focused on practical usage
|
||||||
```
|
- INSTALL.md created for sysadmins
|
||||||
|
|
||||||
5. Test service restart:
|
#### Tasks:
|
||||||
```bash
|
- ✅ Deleted legacy/ folder (old cron/tmux scripts)
|
||||||
systemctl --user restart sensorpajen
|
- ✅ Created INSTALL.md with concise installation guide
|
||||||
```
|
- ✅ Updated README.md troubleshooting section
|
||||||
|
- ✅ Documentation assumes sysadmin familiarity
|
||||||
6. Test crash recovery (kill process, verify auto-restart)
|
|
||||||
|
|
||||||
7. Test boot behavior:
|
|
||||||
```bash
|
|
||||||
systemctl --user enable sensorpajen
|
|
||||||
sudo reboot
|
|
||||||
# After reboot, verify service is running
|
|
||||||
systemctl --user status sensorpajen
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### Phase 8: Cleanup & Document
|
## Migration Complete! 🎉
|
||||||
- [ ] Publish APT package to personal repository
|
|
||||||
- [ ] Create automated build pipeline for .deb packages
|
|
||||||
- [ ] Add support for multiple MQTT brokers
|
|
||||||
- [ ] Implement configuration validation toolation ✓ TODO
|
|
||||||
**Goal**: Remove legacy code and finalize documentation
|
|
||||||
|
|
||||||
#### Tasks:
|
All phases completed. The system has been successfully migrated from a legacy cron/tmux-based system to a modern systemd service with:
|
||||||
1. Once new service is stable (run for 1-2 weeks):
|
|
||||||
- Delete legacy/ folder
|
|
||||||
- Remove cron jobs completely
|
|
||||||
- Remove tmux session references
|
|
||||||
|
|
||||||
2. Update README.md:
|
- ✅ Python package structure
|
||||||
- Installation instructions
|
- ✅ Environment-based configuration (no .ini files)
|
||||||
- Configuration guide
|
- ✅ Systemd user service with auto-restart
|
||||||
- Service management commands
|
- ✅ Automatic sensor discovery with approval workflow
|
||||||
- Troubleshooting section
|
- ✅ Configuration auto-reload (no restart needed)
|
||||||
- Remove DHT11 references
|
- ✅ ntfy notifications for new sensors
|
||||||
- Remove pirate_audio references
|
- ✅ Comprehensive documentation
|
||||||
|
|
||||||
3. Create INSTALL.md:
|
**Version**: 2.0.0-dev
|
||||||
- Fresh installation steps
|
**Status**: Production-ready
|
||||||
- Configuration examples
|
|
||||||
- First-time setup guide
|
|
||||||
|
|
||||||
4. Document in README:
|
|
||||||
```markdown
|
```markdown
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
|||||||
180
SETUP_ON_PI.md
Normal file
180
SETUP_ON_PI.md
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
# Setup on Raspberry Pi - Testing Guide
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
- Raspberry Pi with Bluetooth support
|
||||||
|
- Raspberry Pi OS (Debian-based)
|
||||||
|
- Git repository access
|
||||||
|
- MQTT broker accessible from the Pi
|
||||||
|
|
||||||
|
## Quick Setup Steps
|
||||||
|
|
||||||
|
### 1. Pull Latest Changes
|
||||||
|
```bash
|
||||||
|
cd ~/sensorpajen # or wherever your repo is
|
||||||
|
git pull origin master
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Install System Dependencies
|
||||||
|
```bash
|
||||||
|
# Install Bluetooth and build tools
|
||||||
|
sudo apt update
|
||||||
|
sudo apt install -y bluetooth bluez libbluetooth-dev python3-dev python3-pip python3-venv
|
||||||
|
|
||||||
|
# Verify Bluetooth is working
|
||||||
|
sudo systemctl status bluetooth
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Create Virtual Environment
|
||||||
|
```bash
|
||||||
|
cd ~/sensorpajen
|
||||||
|
python3 -m venv .venv
|
||||||
|
source .venv/bin/activate
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Install Python Dependencies
|
||||||
|
```bash
|
||||||
|
pip install --upgrade pip
|
||||||
|
pip install pybluez bluepy paho-mqtt
|
||||||
|
|
||||||
|
# Or install the package in development mode
|
||||||
|
pip install -e .
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5. Set Bluetooth Capabilities
|
||||||
|
This allows Python to access Bluetooth without sudo:
|
||||||
|
```bash
|
||||||
|
# Set capabilities on the actual Python binary (not the symlink)
|
||||||
|
sudo setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f .venv/bin/python3)
|
||||||
|
|
||||||
|
# Verify it was set correctly
|
||||||
|
getcap $(readlink -f .venv/bin/python3)
|
||||||
|
# Should show: cap_net_raw,cap_net_admin+eip
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6. Configure the Application
|
||||||
|
```bash
|
||||||
|
# Copy configuration templates
|
||||||
|
cp config/sensorpajen.env.example config/sensorpajen.env
|
||||||
|
cp config/sensors.json.example config/sensors.json
|
||||||
|
|
||||||
|
# Edit MQTT settings
|
||||||
|
nano config/sensorpajen.env
|
||||||
|
# Update MQTT_HOST, MQTT_USER, MQTT_PASSWORD
|
||||||
|
|
||||||
|
# Verify/edit sensor list
|
||||||
|
nano config/sensors.json
|
||||||
|
# Should already have your 8 sensors from legacy config
|
||||||
|
```
|
||||||
|
|
||||||
|
### 7. Test Run
|
||||||
|
```bash
|
||||||
|
# Make sure virtual environment is activated
|
||||||
|
source .venv/bin/activate
|
||||||
|
|
||||||
|
# Load environment variables
|
||||||
|
export $(cat config/sensorpajen.env | grep -v '^#' | xargs)
|
||||||
|
|
||||||
|
# Run the application
|
||||||
|
python -m sensorpajen.main
|
||||||
|
```
|
||||||
|
|
||||||
|
You should see:
|
||||||
|
- Configuration being loaded
|
||||||
|
- MQTT connection established
|
||||||
|
- BLE scanning started
|
||||||
|
- Sensor readings as they come in
|
||||||
|
|
||||||
|
Press Ctrl+C to stop.
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### Bluetooth Permission Issues
|
||||||
|
If you get permission errors:
|
||||||
|
```bash
|
||||||
|
# Check if capabilities are set
|
||||||
|
getcap $(readlink -f .venv/bin/python3)
|
||||||
|
|
||||||
|
# If not set, run:
|
||||||
|
sudo setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f .venv/bin/python3)
|
||||||
|
|
||||||
|
# Verify Bluetooth device is up
|
||||||
|
sudo hciconfig hci0 up
|
||||||
|
```
|
||||||
|
|
||||||
|
### MQTT Connection Issues
|
||||||
|
```bash
|
||||||
|
# Test MQTT connection
|
||||||
|
mosquitto_sub -h 10.0.0.114 -u hasse -P casablanca -t "MiTemperature2/#" -v
|
||||||
|
|
||||||
|
# Check if broker is accessible
|
||||||
|
ping 192.168.0.114 # or your MQTT broker IP
|
||||||
|
```
|
||||||
|
|
||||||
|
### No Sensor Data
|
||||||
|
```bash
|
||||||
|
# Check if sensors are in range and broadcasting
|
||||||
|
sudo hcitool lescan
|
||||||
|
|
||||||
|
# Check logs for specific errors
|
||||||
|
python -m sensorpajen.main 2>&1 | tee test.log
|
||||||
|
```
|
||||||
|
|
||||||
|
### BluePy Installation Issues
|
||||||
|
If bluepy fails to install:
|
||||||
|
```bash
|
||||||
|
sudo apt install -y libglib2.0-dev
|
||||||
|
pip install --no-cache-dir bluepy
|
||||||
|
```
|
||||||
|
|
||||||
|
## Environment Variables Reference
|
||||||
|
|
||||||
|
Copy from `config/sensorpajen.env.example` and modify:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Required
|
||||||
|
MQTT_HOST=192.168.0.114 # Your MQTT broker IP
|
||||||
|
MQTT_USER=hasse # MQTT username
|
||||||
|
MQTT_PASSWORD=casablanca # MQTT password
|
||||||
|
|
||||||
|
# Optional
|
||||||
|
MQTT_PORT=1883 # Default MQTT port
|
||||||
|
MQTT_CLIENT_ID=mibridge # Client identifier
|
||||||
|
MQTT_TOPIC_PREFIX=MiTemperature2 # MQTT topic prefix
|
||||||
|
|
||||||
|
# Application settings
|
||||||
|
SENSOR_CONFIG_FILE=config/sensors.json # Sensor config file
|
||||||
|
WATCHDOG_TIMEOUT=5 # BLE watchdog timeout (seconds)
|
||||||
|
ENABLE_BATTERY=true # Include battery data
|
||||||
|
LOG_LEVEL=INFO # DEBUG, INFO, WARNING, ERROR
|
||||||
|
```
|
||||||
|
|
||||||
|
## Viewing Logs
|
||||||
|
```bash
|
||||||
|
# While running in terminal, logs go to stdout
|
||||||
|
python -m sensorpajen.main
|
||||||
|
|
||||||
|
# To save logs to file
|
||||||
|
python -m sensorpajen.main 2>&1 | tee sensorpajen.log
|
||||||
|
```
|
||||||
|
|
||||||
|
## Next Steps
|
||||||
|
|
||||||
|
Once testing is successful:
|
||||||
|
1. Continue to Phase 4-6 in ROADMAP.md to set up systemd service
|
||||||
|
2. Service will run automatically on boot
|
||||||
|
3. Logs will be available via `journalctl --user -u sensorpajen`
|
||||||
|
|
||||||
|
## Returning to Development Machine
|
||||||
|
|
||||||
|
All changes can be committed on the Pi and pushed back:
|
||||||
|
```bash
|
||||||
|
# On Raspberry Pi
|
||||||
|
git add -A
|
||||||
|
git commit -m "Your changes"
|
||||||
|
git push origin master
|
||||||
|
|
||||||
|
# On development machine
|
||||||
|
git pull origin master
|
||||||
|
```
|
||||||
|
|
||||||
|
The development workflow works seamlessly from either machine!
|
||||||
271
TASKS.md
Normal file
271
TASKS.md
Normal file
@@ -0,0 +1,271 @@
|
|||||||
|
# Tasks
|
||||||
|
## Task: Add Auto-Discovery and Approval Flow for Sensors
|
||||||
|
|
||||||
|
### Problem Statement
|
||||||
|
|
||||||
|
Adding new sensors currently requires manually editing `sensors.json`, which is error-prone and inconvenient.
|
||||||
|
The system should automatically detect new sensors and provide a controlled way for users to approve or ignore them.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Goal
|
||||||
|
|
||||||
|
Implement **automatic sensor discovery** with a **user approval workflow** that:
|
||||||
|
|
||||||
|
* Detects new sensors automatically
|
||||||
|
* Notifies the user when new sensors are discovered
|
||||||
|
* Allows the user to approve or ignore sensors via a script
|
||||||
|
* Automatically updates `sensors.json` for approved sensors
|
||||||
|
* Restarts the service after configuration changes
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Scope
|
||||||
|
|
||||||
|
### In Scope
|
||||||
|
|
||||||
|
* Sensor auto-discovery
|
||||||
|
* Tracking newly discovered sensors
|
||||||
|
* Notification via `ntfy`
|
||||||
|
* Interactive user script for approving/ignoring sensors
|
||||||
|
* Updating `sensors.json`
|
||||||
|
* Restarting the service via systemd
|
||||||
|
|
||||||
|
### Out of Scope
|
||||||
|
|
||||||
|
* Web UI
|
||||||
|
* Authentication mechanisms beyond existing system access
|
||||||
|
* Changes to sensor hardware or firmware
|
||||||
|
* Long-term sensor management (removal, editing, etc.)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Functional Requirements
|
||||||
|
|
||||||
|
### 1. Sensor Auto-Discovery
|
||||||
|
|
||||||
|
* The service must detect sensors that are not present in `sensors.json`
|
||||||
|
* Each newly discovered sensor must have a stable unique identifier
|
||||||
|
* Discovered-but-unapproved sensors must **not** be added automatically
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2. Discovered Sensor Storage
|
||||||
|
|
||||||
|
* Newly discovered sensors must be stored in `config/discovered_sensors.json`
|
||||||
|
* Stored data must include:
|
||||||
|
* `mac` - MAC address (unique identifier)
|
||||||
|
* `name` - Advertised device name (e.g., "ATC_1234AB")
|
||||||
|
* `rssi` - Signal strength in dBm
|
||||||
|
* `first_seen` - ISO timestamp of first discovery
|
||||||
|
* `last_seen` - ISO timestamp of most recent advertisement
|
||||||
|
* `sample_reading` - One example reading with temperature, humidity, battery data
|
||||||
|
* `status` - One of: "pending", "approved", "ignored"
|
||||||
|
* `ignored_at` - ISO timestamp when ignored (if status is "ignored")
|
||||||
|
* `ignore_reason` - Optional user-provided reason for ignoring
|
||||||
|
* Approved sensors must have their status updated to "approved"
|
||||||
|
* Ignored sensors must remain in the file with status "ignored"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3. Notification via ntfy
|
||||||
|
|
||||||
|
* When a new sensor is discovered:
|
||||||
|
* Send a notification to the configured `ntfy` topic via curl
|
||||||
|
* Include at least:
|
||||||
|
* Sensor MAC address
|
||||||
|
* Sensor name
|
||||||
|
* Last seen timestamp
|
||||||
|
* Instruction that user action is required
|
||||||
|
* Configuration (in `config/sensorpajen.env`):
|
||||||
|
* `NTFY_ENABLED` - true/false to enable/disable notifications
|
||||||
|
* `NTFY_URL` - ntfy server URL (e.g., "https://ntfy.sh")
|
||||||
|
* `NTFY_TOPIC` - Topic to publish to
|
||||||
|
* `NTFY_TOKEN` - Authentication token (sent in header)
|
||||||
|
* ntfy is optional - system must work without it:
|
||||||
|
* If `NTFY_ENABLED=false`, skip notifications
|
||||||
|
* If ntfy is unreachable, log error and continue
|
||||||
|
* Discovery and approval must work even if ntfy fails
|
||||||
|
* The user must only be notified once per discovered sensor
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4. User Approval Script
|
||||||
|
|
||||||
|
Provide a CLI command `sensorpajen approve-sensors` that:
|
||||||
|
|
||||||
|
* Lists all sensors with status "pending" or "ignored"
|
||||||
|
* For each sensor, displays:
|
||||||
|
* MAC address
|
||||||
|
* Advertised name (e.g., "ATC_1234AB")
|
||||||
|
* Last seen timestamp
|
||||||
|
* Sample reading (temperature, humidity, battery)
|
||||||
|
* Current status (pending/ignored)
|
||||||
|
* For each sensor, allows the user to:
|
||||||
|
* Approve the sensor (add to `sensors.json`)
|
||||||
|
* Ignore the sensor (mark as ignored)
|
||||||
|
* Skip (leave as pending for later)
|
||||||
|
* If approving:
|
||||||
|
* Prompt for a sensor name (required, human-readable)
|
||||||
|
* Pre-fill comment field with extended metadata (MAC, device name, last seen, sample reading)
|
||||||
|
* Allow user to edit or keep the pre-filled comment (optional)
|
||||||
|
* If ignoring:
|
||||||
|
* Prompt for optional reason
|
||||||
|
* Update status to "ignored" with timestamp
|
||||||
|
* Interactive mode only (no batch/automated approval)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5. Updating sensors.json
|
||||||
|
|
||||||
|
* When a sensor is approved:
|
||||||
|
* Add it to `sensors.json` (only if MAC doesn't already exist)
|
||||||
|
* Include:
|
||||||
|
* `mac` - MAC address from discovery
|
||||||
|
* `name` - User-provided human-readable name
|
||||||
|
* `comment` - User-edited comment (pre-filled with metadata)
|
||||||
|
* The file must remain valid JSON
|
||||||
|
* Existing sensors must not be modified
|
||||||
|
* If MAC already exists in `sensors.json`, skip adding (renaming is done manually in the file)
|
||||||
|
* Update status to "approved" in `discovered_sensors.json`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 6. Configuration Reload
|
||||||
|
|
||||||
|
* The service must automatically reload `sensors.json` every 15 minutes
|
||||||
|
* No service restart required after approval
|
||||||
|
* If `sensors.json` is modified:
|
||||||
|
* Load new sensor list
|
||||||
|
* Start monitoring newly added sensors
|
||||||
|
* Continue monitoring existing sensors without interruption
|
||||||
|
* Log configuration reload events
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Non-Functional Requirements
|
||||||
|
|
||||||
|
* Must be safe to run on a Raspberry Pi
|
||||||
|
* Must not require a GUI
|
||||||
|
* Must fail gracefully if:
|
||||||
|
|
||||||
|
* `ntfy` is unreachable
|
||||||
|
* The user aborts the approval script
|
||||||
|
* Logging must clearly indicate:
|
||||||
|
|
||||||
|
* Discovery events
|
||||||
|
* Notifications sent
|
||||||
|
* Approval or ignore decisions
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Acceptance Criteria
|
||||||
|
|
||||||
|
* A new sensor is automatically detected and added to `discovered_sensors.json` with status "pending"
|
||||||
|
* Extended metadata (MAC, name, RSSI, timestamps, sample reading) is stored
|
||||||
|
* A notification is sent via `ntfy` when a sensor is discovered (if enabled)
|
||||||
|
* The approval CLI command (`sensorpajen approve-sensors`) lists pending and ignored sensors
|
||||||
|
* The CLI displays MAC, name, last seen, and sample reading for each sensor
|
||||||
|
* The user can approve a sensor with a custom name
|
||||||
|
* The comment field is pre-filled with metadata and user can edit it
|
||||||
|
* The user can ignore a sensor with an optional reason
|
||||||
|
* Previously ignored sensors can be approved in a later CLI run
|
||||||
|
* Approved sensors appear correctly in `sensors.json` (mac + name + comment only)
|
||||||
|
* Sensors already in `sensors.json` are not added again (no duplicates)
|
||||||
|
* The service automatically reloads `sensors.json` every 15 minutes
|
||||||
|
* New sensors are monitored without service restart
|
||||||
|
* Ignored sensors are stored with `ignored_at` timestamp and optional `ignore_reason`
|
||||||
|
* ntfy failures do not prevent discovery or approval workflow
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Notes for Implementation
|
||||||
|
|
||||||
|
* Prefer environment-based configuration (no `.ini` files)
|
||||||
|
* Keep the discovery logic separate from user interaction logic
|
||||||
|
* Avoid race conditions between discovery and approval
|
||||||
|
* Assume multiple sensors may be discovered before user action
|
||||||
|
* Use MAC address as unique identifier for sensors
|
||||||
|
* ntfy notification format: `curl -H "Authorization: Bearer $NTFY_TOKEN" -d "message" $NTFY_URL/$NTFY_TOPIC`
|
||||||
|
* Config reload: Use a timer thread that checks file mtime or reloads every 15 minutes
|
||||||
|
* Pre-filled comment example: `"MAC: A4:C1:38:12:34:56, Name: ATC_1234AB, Last seen: 2025-12-27T14:30:00, Temp: 21.5°C, Humidity: 45%, Battery: 87%"`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Implementation Details
|
||||||
|
|
||||||
|
### File Locations
|
||||||
|
* Discovered sensors: `config/discovered_sensors.json`
|
||||||
|
* Known sensors: `config/sensors.json` (existing)
|
||||||
|
* Configuration: `config/sensorpajen.env` (add ntfy settings)
|
||||||
|
|
||||||
|
### New CLI Command
|
||||||
|
* Entry point: `sensorpajen approve-sensors`
|
||||||
|
* Add to `pyproject.toml` under `[project.scripts]`
|
||||||
|
|
||||||
|
### Configuration Variables (add to sensorpajen.env)
|
||||||
|
```bash
|
||||||
|
# ntfy notifications (optional)
|
||||||
|
NTFY_ENABLED=true
|
||||||
|
NTFY_URL=https://ntfy.sh
|
||||||
|
NTFY_TOPIC=sensorpajen
|
||||||
|
NTFY_TOKEN=tk_xxxxxxxxxxxxx
|
||||||
|
|
||||||
|
# Config reload interval (seconds)
|
||||||
|
CONFIG_RELOAD_INTERVAL=900 # 15 minutes
|
||||||
|
```
|
||||||
|
|
||||||
|
### discovered_sensors.json Structure
|
||||||
|
```json
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"mac": "A4:C1:38:12:34:56",
|
||||||
|
"name": "ATC_1234AB",
|
||||||
|
"rssi": -65,
|
||||||
|
"first_seen": "2025-12-27T14:30:15",
|
||||||
|
"last_seen": "2025-12-27T14:35:42",
|
||||||
|
"sample_reading": {
|
||||||
|
"temperature": 21.5,
|
||||||
|
"humidity": 45,
|
||||||
|
"battery_percent": 87,
|
||||||
|
"battery_voltage": 2950
|
||||||
|
},
|
||||||
|
"status": "pending"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mac": "A4:C1:38:AB:CD:EF",
|
||||||
|
"name": "ATC_ABCDEF",
|
||||||
|
"rssi": -72,
|
||||||
|
"first_seen": "2025-12-27T15:00:00",
|
||||||
|
"last_seen": "2025-12-27T15:10:00",
|
||||||
|
"sample_reading": {
|
||||||
|
"temperature": 19.8,
|
||||||
|
"humidity": 52,
|
||||||
|
"battery_percent": 65,
|
||||||
|
"battery_voltage": 2800
|
||||||
|
},
|
||||||
|
"status": "ignored",
|
||||||
|
"ignored_at": "2025-12-27T15:15:00",
|
||||||
|
"ignore_reason": "Test sensor, not needed"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
### sensors.json Entry (after approval)
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mac": "A4:C1:38:12:34:56",
|
||||||
|
"name": "Living Room",
|
||||||
|
"comment": "MAC: A4:C1:38:12:34:56, Name: ATC_1234AB, Last seen: 2025-12-27T14:35:42, Temp: 21.5°C, Humidity: 45%, Battery: 87%"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
If you want, I can also:
|
||||||
|
|
||||||
|
* Split this into **multiple smaller tasks**
|
||||||
|
* Add a **definition of done** section
|
||||||
|
* Provide a **suggested file/module structure**
|
||||||
|
* Write a **follow-up roadmap entry** for sensor management
|
||||||
|
|
||||||
|
Just tell me how you want to evolve it next.
|
||||||
32
config/discovered_sensors.json.example
Normal file
32
config/discovered_sensors.json.example
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"mac": "A4:C1:38:12:34:56",
|
||||||
|
"name": "ATC_123456",
|
||||||
|
"rssi": -65,
|
||||||
|
"first_seen": "2025-12-27T10:30:15",
|
||||||
|
"last_seen": "2025-12-27T10:35:42",
|
||||||
|
"sample_reading": {
|
||||||
|
"temperature": 21.5,
|
||||||
|
"humidity": 45,
|
||||||
|
"battery_percent": 87,
|
||||||
|
"battery_voltage": 2950
|
||||||
|
},
|
||||||
|
"status": "pending"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mac": "A4:C1:38:AB:CD:EF",
|
||||||
|
"name": "ATC_ABCDEF",
|
||||||
|
"rssi": -72,
|
||||||
|
"first_seen": "2025-12-27T11:00:00",
|
||||||
|
"last_seen": "2025-12-27T11:10:00",
|
||||||
|
"sample_reading": {
|
||||||
|
"temperature": 19.8,
|
||||||
|
"humidity": 52,
|
||||||
|
"battery_percent": 65,
|
||||||
|
"battery_voltage": 2800
|
||||||
|
},
|
||||||
|
"status": "ignored",
|
||||||
|
"ignored_at": "2025-12-27T11:15:00",
|
||||||
|
"ignore_reason": "Test sensor, not needed"
|
||||||
|
}
|
||||||
|
]
|
||||||
22
config/sensorpajen.env.example
Normal file
22
config/sensorpajen.env.example
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
# MQTT Configuration
|
||||||
|
MQTT_HOST=192.168.0.114
|
||||||
|
MQTT_PORT=1883
|
||||||
|
MQTT_USER=hasse
|
||||||
|
MQTT_PASSWORD=casablanca
|
||||||
|
MQTT_CLIENT_ID=mibridge
|
||||||
|
|
||||||
|
# Sensor Configuration (relative to project root)
|
||||||
|
SENSOR_CONFIG_FILE=config/sensors.json
|
||||||
|
DISCOVERED_SENSORS_FILE=config/discovered_sensors.json
|
||||||
|
|
||||||
|
# Application Settings
|
||||||
|
WATCHDOG_TIMEOUT=5
|
||||||
|
ENABLE_BATTERY=true
|
||||||
|
LOG_LEVEL=INFO
|
||||||
|
CONFIG_RELOAD_INTERVAL=900 # 15 minutes in seconds
|
||||||
|
|
||||||
|
# ntfy Notifications (optional)
|
||||||
|
NTFY_ENABLED=false
|
||||||
|
NTFY_URL=https://ntfy.sh
|
||||||
|
NTFY_TOPIC=sensorpajen
|
||||||
|
NTFY_TOKEN=
|
||||||
37
config/sensors.json.example
Normal file
37
config/sensors.json.example
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
{
|
||||||
|
"sensors": [
|
||||||
|
{
|
||||||
|
"mac": "A4:C1:38:98:7B:B6",
|
||||||
|
"name": "mi_temp_1",
|
||||||
|
"comment": "Example sensor - replace with your sensors"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mac": "A4:C1:38:29:03:0D",
|
||||||
|
"name": "mi_temp_2"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mac": "A4:C1:38:62:CA:83",
|
||||||
|
"name": "mi_temp_3"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mac": "A4:C1:38:D5:EA:63",
|
||||||
|
"name": "mi_temp_4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mac": "A4:C1:38:7C:9C:63",
|
||||||
|
"name": "mi_temp_5"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mac": "A4:C1:38:68:2C:DA",
|
||||||
|
"name": "mi_temp_6"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mac": "A4:C1:38:AD:74:2B",
|
||||||
|
"name": "mi_temp_7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mac": "A4:C1:38:46:9F:D1",
|
||||||
|
"name": "mi_temp_8"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
64
pyproject.toml
Normal file
64
pyproject.toml
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
[build-system]
|
||||||
|
requires = ["setuptools>=61.0", "wheel"]
|
||||||
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
|
[project]
|
||||||
|
name = "sensorpajen"
|
||||||
|
version = "2.0.0-dev"
|
||||||
|
description = "Bluetooth temperature sensor monitor for Xiaomi Mijia LYWSD03MMC"
|
||||||
|
readme = "README.md"
|
||||||
|
requires-python = ">=3.9"
|
||||||
|
license = {text = "MIT"}
|
||||||
|
authors = [
|
||||||
|
{name = "Fredrik", email = "your@email.com"}
|
||||||
|
]
|
||||||
|
keywords = ["bluetooth", "temperature", "sensor", "mqtt", "raspberry-pi"]
|
||||||
|
classifiers = [
|
||||||
|
"Development Status :: 4 - Beta",
|
||||||
|
"Intended Audience :: Developers",
|
||||||
|
"License :: OSI Approved :: MIT License",
|
||||||
|
"Programming Language :: Python :: 3",
|
||||||
|
"Programming Language :: Python :: 3.9",
|
||||||
|
"Programming Language :: Python :: 3.10",
|
||||||
|
"Programming Language :: Python :: 3.11",
|
||||||
|
"Operating System :: POSIX :: Linux",
|
||||||
|
"Topic :: Home Automation",
|
||||||
|
]
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
"bluepy>=1.3.0",
|
||||||
|
"paho-mqtt>=1.6.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[project.optional-dependencies]
|
||||||
|
dev = [
|
||||||
|
"pytest>=7.0",
|
||||||
|
"pytest-cov>=4.0",
|
||||||
|
"black>=23.0",
|
||||||
|
"ruff>=0.1.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[project.urls]
|
||||||
|
Homepage = "https://github.com/yourusername/sensorpajen"
|
||||||
|
Repository = "https://github.com/yourusername/sensorpajen"
|
||||||
|
|
||||||
|
[project.scripts]
|
||||||
|
sensorpajen = "sensorpajen.main:main"
|
||||||
|
sensorpajen-approve-sensors = "sensorpajen.approve_sensors:main"
|
||||||
|
|
||||||
|
[tool.setuptools.packages.find]
|
||||||
|
where = ["src"]
|
||||||
|
|
||||||
|
[tool.black]
|
||||||
|
line-length = 100
|
||||||
|
target-version = ["py39", "py310", "py311"]
|
||||||
|
|
||||||
|
[tool.ruff]
|
||||||
|
line-length = 100
|
||||||
|
target-version = "py39"
|
||||||
|
|
||||||
|
[tool.pytest.ini_options]
|
||||||
|
testpaths = ["tests"]
|
||||||
|
python_files = ["test_*.py"]
|
||||||
|
python_classes = ["Test*"]
|
||||||
|
python_functions = ["test_*"]
|
||||||
227
readme.md
227
readme.md
@@ -1,33 +1,210 @@
|
|||||||
# Sensorpajen
|
# Sensorpajen
|
||||||
Raspberry Pi Zero W som läser av diverse mätere och rapporterar data till MQTT.
|
|
||||||
Använder Xiaomi Mijia thermometer 2 samt inbyggd DHT11 sensor
|
|
||||||
|
|
||||||
## Bluetooth termometrarna
|
Raspberry Pi service that monitors Xiaomi Mijia LYWSD03MMC Bluetooth temperature sensors and publishes data to MQTT.
|
||||||
- LYWSD03MMC.py - script för att läsa av Mi Mijia Thermometer 2
|
|
||||||
- sensorer.ini - config till LYWSD03MMC
|
|
||||||
- sendToMQTT.sh - stödscript till LYWSD03MMC
|
|
||||||
- bluetooth_utils.py - stödscript till LYWSD03MMC
|
|
||||||
|
|
||||||
## Sensorer i pajen
|
## Features
|
||||||
- temperatur_koksfonstret.py - Läser av DHT 11 sensor kopplad till Pi, används av cron
|
|
||||||
- sensorer.sh - script för att starta tmux vid boot, används av cron
|
|
||||||
|
|
||||||
## Cronjobb
|
- 🌡️ Monitors 8 Xiaomi Mijia thermometers via Bluetooth (ATC firmware)
|
||||||
```
|
- 📡 Publishes temperature, humidity, and battery data to MQTT
|
||||||
@reboot /home/pi/sensorer.sh
|
- 🔄 Automatic restart on failure
|
||||||
*/1 * * * * /home/pi/temperatur_koksfonstret.py 11 4
|
- 📊 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](INSTALL.md) for complete installation instructions.
|
||||||
|
|
||||||
|
### Quick Start
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 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
|
||||||
```
|
```
|
||||||
|
|
||||||
## Flasha nya termometrar
|
## Configuration
|
||||||
|
|
||||||
- Öppna och flasha bara en termometer i taget!!
|
### MQTT Settings
|
||||||
- Ha en permanent marker tillgänglig
|
|
||||||
- Flashning måste göras från en mobil
|
|
||||||
|
|
||||||
1. Ta reda på termometerns mac-adress med någon bra app för Bluetooth BLE . Den heter något i stil med LYWSD03MMC (Om du bara startat en termometer så är det lätt att hitta)
|
Edit `config/sensorpajen.env`:
|
||||||
2. Ladda ned senaste releasen av firmware här: https://github.com/atc1441/ATC_MiThermometer
|
|
||||||
3. Öppna den här webbsidan: https://atc1441.github.io/TelinkFlasher.html
|
```bash
|
||||||
4. Tryck på Connect och sök upp den aktiva termometern. Den heter något i stil med LYWSD03MMC
|
MQTT_HOST=192.168.1.10
|
||||||
5. Gör Do Activation och när den hittat Select Firmarware Start Flashing
|
MQTT_PORT=1883
|
||||||
6. Anteckna mac-adressen från (1) och sätt ett id på termometern. Det ska senare in i filen sensorer.ini
|
MQTT_USERNAME=username
|
||||||
7. Verifiera i din BLE läsare att termometern nu heter något i stil med ATC_XXXXXX. Om du missade att anteckna mac-adressen så är första delen alltid A4:C1:38 och XXXXXX i namnet är de sista tre delarna
|
MQTT_PASSWORD=password
|
||||||
|
MQTT_CLIENT_ID=sensorpajen
|
||||||
|
MQTT_TOPIC_PREFIX=MiTemperature2
|
||||||
|
```
|
||||||
|
|
||||||
|
### Sensors
|
||||||
|
|
||||||
|
Edit `config/sensors.json`:
|
||||||
|
|
||||||
|
```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](systemd/README.md) for detailed service management instructions.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 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**:
|
||||||
|
- Open https://atc1441.github.io/TelinkFlasher.html on your phone
|
||||||
|
- Click "Connect" and select your thermometer (LYWSD03MMC)
|
||||||
|
- Click "Do Activation"
|
||||||
|
- When found, select firmware and click "Start Flashing"
|
||||||
|
|
||||||
|
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](INSTALL.md#troubleshooting) for detailed troubleshooting steps.
|
||||||
|
|
||||||
|
### Quick Checks
|
||||||
|
|
||||||
|
**Permission errors:**
|
||||||
|
```bash
|
||||||
|
sudo setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f ~/sensorpajen/.venv/bin/python3)
|
||||||
|
systemctl --user restart sensorpajen
|
||||||
|
```
|
||||||
|
|
||||||
|
**Service status:**
|
||||||
|
```bash
|
||||||
|
systemctl --user status sensorpajen
|
||||||
|
journalctl --user -u sensorpajen -f
|
||||||
|
```
|
||||||
|
|
||||||
|
**MQTT test:**
|
||||||
|
```bash
|
||||||
|
mosquitto_sub -h <MQTT_HOST> -u <USER> -P <PASSWORD> -t "MiTemperature2/#" -v
|
||||||
|
```
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 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](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
|
||||||
|
|
||||||
|
- Bluetooth utilities based on work by Colin GUYON (MIT License)
|
||||||
|
- ATC firmware by atc1441: https://github.com/atc1441/ATC_MiThermometer
|
||||||
23
scripts/approve-sensors.sh
Executable file
23
scripts/approve-sensors.sh
Executable file
@@ -0,0 +1,23 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Wrapper script for approve-sensors that sets minimal required env vars
|
||||||
|
|
||||||
|
# Get script directory
|
||||||
|
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
|
PROJECT_ROOT="$( cd "$SCRIPT_DIR/.." && pwd )"
|
||||||
|
|
||||||
|
# Set minimal required environment variables
|
||||||
|
export MQTT_HOST="${MQTT_HOST:-localhost}"
|
||||||
|
export MQTT_PORT="${MQTT_PORT:-1883}"
|
||||||
|
|
||||||
|
# Load actual config if it exists (will override defaults)
|
||||||
|
if [ -f "$PROJECT_ROOT/config/sensorpajen.env" ]; then
|
||||||
|
set -a
|
||||||
|
source "$PROJECT_ROOT/config/sensorpajen.env"
|
||||||
|
set +a
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Activate virtual environment
|
||||||
|
source "$PROJECT_ROOT/.venv/bin/activate"
|
||||||
|
|
||||||
|
# Run the approve-sensors command
|
||||||
|
python -m sensorpajen.approve_sensors "$@"
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
#This script is provided by Chiunownow https://github.com/Chiunownow
|
|
||||||
#Thank you very much for providing this script
|
|
||||||
#This script is
|
|
||||||
|
|
||||||
#use e.g with that script: MySensor.sh
|
|
||||||
#!/bin/bash
|
|
||||||
#DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
|
|
||||||
#$DIR/LYWSD03MMC.py -d <device> -b 1000 -r --debounce --skipidentical 50 --name MySensor --callback sendToMQTT
|
|
||||||
|
|
||||||
#mosquitto_pub -h mqtt.host -t "MiTemperature2/$2/temp" -u mqtt.username -P mqtt.passwd -i "mibridge" -m "$3"
|
|
||||||
#mosquitto_pub -h mqtt.host -t "MiTemperature2/$2/humidity" -u mqtt.username -P mqtt.passwd -i "mibridge" -m "$4"
|
|
||||||
#mosquitto_pub -h mqtt.host -t "MiTemperature2/$2/batteryvoltage" -u mqtt.username -P mqtt.passwd -i "mibridge" -m "$5"
|
|
||||||
#mosquitto_pub -h mqtt.host -t "MiTemperature2/$2/batterylevel" -u mqtt.username -P mqtt.passwd -i "mibridge" -m "$6"
|
|
||||||
|
|
||||||
mosquitto_pub -h 192.168.0.114 -t "MiTemperature2/$2/temp" -u hasse -P casablanca -i "mibridge" -m "$3"
|
|
||||||
mosquitto_pub -h 192.168.0.114 -t "MiTemperature2/$2/humidity" -u hasse -P casablanca -i "mibridge" -m "$4"
|
|
||||||
mosquitto_pub -h 192.168.0.114 -t "MiTemperature2/$2/batteryvoltage" -u hasse -P casablanca -i "mibridge" -m "$5"
|
|
||||||
mosquitto_pub -h 192.168.0.114 -t "MiTemperature2/$2/batterylevel" -u hasse -P casablanca -i "mibridge" -m "$6"
|
|
||||||
26
sensorer.ini
26
sensorer.ini
@@ -1,26 +0,0 @@
|
|||||||
[A4:C1:38:98:7B:B6]
|
|
||||||
sensorname=mi_temp_1
|
|
||||||
|
|
||||||
[A4:C1:38:29:03:0D]
|
|
||||||
sensorname=mi_temp_2
|
|
||||||
|
|
||||||
[A4:C1:38:62:CA:83]
|
|
||||||
sensorname=mi_temp_3
|
|
||||||
|
|
||||||
[A4:C1:38:D5:EA:63]
|
|
||||||
sensorname=mi_temp_4
|
|
||||||
|
|
||||||
[A4:C1:38:7C:9C:63]
|
|
||||||
sensorname=mi_temp_5
|
|
||||||
|
|
||||||
[A4:C1:38:68:2C:DA]
|
|
||||||
sensorname=mi_temp_6
|
|
||||||
|
|
||||||
[A4:C1:38:AD:74:2B]
|
|
||||||
sensorname=mi_temp_7
|
|
||||||
|
|
||||||
[A4:C1:38:46:9F:D1]
|
|
||||||
sensorname=mi_temp_8
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
# körs vid boot
|
|
||||||
|
|
||||||
tmux new-session -d -s sensorer '/home/pi/sensorpajen/LYWSD03MMC.py -a -wdt 5 --devicelistfile sensorer.ini --callback yes --battery'
|
|
||||||
tmux detach -s sensorer
|
|
||||||
10
src/sensorpajen/__init__.py
Normal file
10
src/sensorpajen/__init__.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
"""
|
||||||
|
Sensorpajen - Bluetooth Temperature Sensor Monitor
|
||||||
|
|
||||||
|
Monitors Xiaomi Mijia LYWSD03MMC Bluetooth temperature sensors
|
||||||
|
and publishes data to MQTT broker.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__version__ = "2.0.0-dev"
|
||||||
|
__author__ = "Fredrik"
|
||||||
|
__license__ = "MIT"
|
||||||
303
src/sensorpajen/approve_sensors.py
Normal file
303
src/sensorpajen/approve_sensors.py
Normal file
@@ -0,0 +1,303 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
CLI tool for approving or ignoring discovered sensors.
|
||||||
|
|
||||||
|
Interactive tool to manage pending and ignored sensors.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import argparse
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from . import config
|
||||||
|
from .discovery_manager import DiscoveryManager, DiscoveredSensor
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def format_metadata_comment(sensor: DiscoveredSensor) -> str:
|
||||||
|
"""
|
||||||
|
Format sensor metadata as a comment string.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
sensor: Discovered sensor
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Formatted comment string
|
||||||
|
"""
|
||||||
|
return (
|
||||||
|
f"MAC: {sensor.mac}, "
|
||||||
|
f"Name: {sensor.name}, "
|
||||||
|
f"Last seen: {sensor.last_seen}, "
|
||||||
|
f"Temp: {sensor.sample_reading.get('temperature', 'N/A')}°C, "
|
||||||
|
f"Humidity: {sensor.sample_reading.get('humidity', 'N/A')}%, "
|
||||||
|
f"Battery: {sensor.sample_reading.get('battery_percent', 'N/A')}%"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def display_sensor(sensor: DiscoveredSensor, index: int, total: int):
|
||||||
|
"""
|
||||||
|
Display sensor information to the user.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
sensor: Discovered sensor to display
|
||||||
|
index: Current sensor number (1-based)
|
||||||
|
total: Total number of sensors
|
||||||
|
"""
|
||||||
|
print(f"\n{'='*70}")
|
||||||
|
print(f"Sensor {index}/{total}")
|
||||||
|
print(f"{'='*70}")
|
||||||
|
print(f"MAC Address: {sensor.mac}")
|
||||||
|
print(f"Device Name: {sensor.name}")
|
||||||
|
print(f"Last Seen: {sensor.last_seen}")
|
||||||
|
print(f"Status: {sensor.status}")
|
||||||
|
|
||||||
|
if sensor.status == "ignored" and sensor.ignored_at:
|
||||||
|
print(f"Ignored At: {sensor.ignored_at}")
|
||||||
|
if sensor.ignore_reason:
|
||||||
|
print(f"Reason: {sensor.ignore_reason}")
|
||||||
|
|
||||||
|
# Display sample reading
|
||||||
|
reading = sensor.sample_reading
|
||||||
|
print(f"\nSample Reading:")
|
||||||
|
print(f" Temperature: {reading.get('temperature', 'N/A')}°C")
|
||||||
|
print(f" Humidity: {reading.get('humidity', 'N/A')}%")
|
||||||
|
print(f" Battery: {reading.get('battery_percent', 'N/A')}%")
|
||||||
|
print(f" Voltage: {reading.get('battery_voltage', 'N/A')}mV")
|
||||||
|
print(f"{'='*70}")
|
||||||
|
|
||||||
|
|
||||||
|
def get_user_choice() -> str:
|
||||||
|
"""
|
||||||
|
Get user's choice for what to do with the sensor.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
User choice: 'a' (approve), 'i' (ignore), 's' (skip)
|
||||||
|
"""
|
||||||
|
while True:
|
||||||
|
choice = input("\n[A]pprove, [I]gnore, [S]kip, [Q]uit? ").strip().lower()
|
||||||
|
if choice in ['a', 'i', 's', 'q']:
|
||||||
|
return choice
|
||||||
|
print("Invalid choice. Please enter A, I, S, or Q.")
|
||||||
|
|
||||||
|
|
||||||
|
def approve_sensor(sensor: DiscoveredSensor, manager: DiscoveryManager):
|
||||||
|
"""
|
||||||
|
Approve a sensor and add it to sensors.json.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
sensor: Sensor to approve
|
||||||
|
manager: Discovery manager
|
||||||
|
"""
|
||||||
|
# Check if sensor already exists in sensors.json
|
||||||
|
sensor_config_path = Path(config.SENSOR_CONFIG_FILE)
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(sensor_config_path, 'r') as f:
|
||||||
|
data = json.load(f)
|
||||||
|
|
||||||
|
# Check for duplicates
|
||||||
|
for existing_sensor in data.get('sensors', []):
|
||||||
|
if existing_sensor.get('mac', '').upper() == sensor.mac:
|
||||||
|
print(f"\n⚠️ Sensor {sensor.mac} already exists in sensors.json")
|
||||||
|
print(" Renaming must be done manually in the file.")
|
||||||
|
return
|
||||||
|
|
||||||
|
except FileNotFoundError:
|
||||||
|
# File doesn't exist yet, create with empty sensors list
|
||||||
|
data = {'sensors': []}
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
print(f"\n❌ Error: Invalid JSON in {sensor_config_path}: {e}")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Get sensor name from user
|
||||||
|
while True:
|
||||||
|
name = input("\nEnter sensor name (required): ").strip()
|
||||||
|
if name:
|
||||||
|
break
|
||||||
|
print("Sensor name cannot be empty.")
|
||||||
|
|
||||||
|
# Pre-fill comment with metadata
|
||||||
|
default_comment = format_metadata_comment(sensor)
|
||||||
|
print(f"\nDefault comment:")
|
||||||
|
print(f" {default_comment}")
|
||||||
|
|
||||||
|
edit = input("\nEdit comment? [y/N]: ").strip().lower()
|
||||||
|
if edit == 'y':
|
||||||
|
print("\nEnter comment (or press Enter to keep default):")
|
||||||
|
comment = input("> ").strip()
|
||||||
|
if not comment:
|
||||||
|
comment = default_comment
|
||||||
|
else:
|
||||||
|
comment = default_comment
|
||||||
|
|
||||||
|
# Add to sensors.json
|
||||||
|
new_sensor = {
|
||||||
|
"mac": sensor.mac,
|
||||||
|
"name": name
|
||||||
|
}
|
||||||
|
|
||||||
|
if comment:
|
||||||
|
new_sensor["comment"] = comment
|
||||||
|
|
||||||
|
data.setdefault('sensors', []).append(new_sensor)
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(sensor_config_path, 'w') as f:
|
||||||
|
json.dump(data, f, indent=2)
|
||||||
|
|
||||||
|
print(f"\n✅ Sensor approved and added to sensors.json")
|
||||||
|
print(f" Name: {name}")
|
||||||
|
print(f" Configuration will be reloaded automatically within 15 minutes")
|
||||||
|
|
||||||
|
# Mark as approved in discovery manager
|
||||||
|
manager.approve(sensor.mac)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ Error saving to sensors.json: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def ignore_sensor(sensor: DiscoveredSensor, manager: DiscoveryManager):
|
||||||
|
"""
|
||||||
|
Ignore a sensor.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
sensor: Sensor to ignore
|
||||||
|
manager: Discovery manager
|
||||||
|
"""
|
||||||
|
reason = input("\nReason for ignoring (optional): ").strip()
|
||||||
|
|
||||||
|
manager.ignore(sensor.mac, reason if reason else None)
|
||||||
|
|
||||||
|
print(f"\n✅ Sensor ignored")
|
||||||
|
if reason:
|
||||||
|
print(f" Reason: {reason}")
|
||||||
|
|
||||||
|
|
||||||
|
def process_sensors(sensors: List[DiscoveredSensor], manager: DiscoveryManager):
|
||||||
|
"""
|
||||||
|
Process list of sensors interactively.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
sensors: List of sensors to process
|
||||||
|
manager: Discovery manager
|
||||||
|
"""
|
||||||
|
if not sensors:
|
||||||
|
print("\n✅ No sensors to process")
|
||||||
|
return
|
||||||
|
|
||||||
|
print(f"\nFound {len(sensors)} sensor(s) to review")
|
||||||
|
|
||||||
|
for i, sensor in enumerate(sensors, 1):
|
||||||
|
# Mark as reviewed when shown
|
||||||
|
manager.mark_reviewed(sensor.mac)
|
||||||
|
|
||||||
|
display_sensor(sensor, i, len(sensors))
|
||||||
|
|
||||||
|
choice = get_user_choice()
|
||||||
|
|
||||||
|
if choice == 'q':
|
||||||
|
print("\n👋 Exiting...")
|
||||||
|
break
|
||||||
|
elif choice == 'a':
|
||||||
|
approve_sensor(sensor, manager)
|
||||||
|
elif choice == 'i':
|
||||||
|
ignore_sensor(sensor, manager)
|
||||||
|
elif choice == 's':
|
||||||
|
print("\n⏭️ Skipped")
|
||||||
|
continue
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Main entry point for approve-sensors CLI."""
|
||||||
|
# Parse command line arguments
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Approve or ignore discovered Bluetooth sensors",
|
||||||
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||||
|
epilog="""
|
||||||
|
Examples:
|
||||||
|
%(prog)s # Show only new pending sensors
|
||||||
|
%(prog)s --all # Show all pending sensors (including reviewed)
|
||||||
|
%(prog)s --ignored # Show only ignored sensors
|
||||||
|
%(prog)s --all --ignored # Show all sensors
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--all', '-a',
|
||||||
|
action='store_true',
|
||||||
|
help='Show all pending sensors, including previously reviewed ones'
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--ignored', '-i',
|
||||||
|
action='store_true',
|
||||||
|
help='Show ignored sensors'
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
# Setup logging
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.WARNING,
|
||||||
|
format='%(levelname)s: %(message)s'
|
||||||
|
)
|
||||||
|
|
||||||
|
print("=" * 70)
|
||||||
|
print("Sensorpajen - Approve Sensors")
|
||||||
|
print("=" * 70)
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Load discovery manager
|
||||||
|
manager = DiscoveryManager()
|
||||||
|
|
||||||
|
# Get sensors based on flags
|
||||||
|
if args.all:
|
||||||
|
pending = manager.get_pending()
|
||||||
|
pending_label = "all pending"
|
||||||
|
else:
|
||||||
|
pending = manager.get_new_pending()
|
||||||
|
pending_label = "new pending"
|
||||||
|
|
||||||
|
ignored = manager.get_ignored() if args.ignored else []
|
||||||
|
|
||||||
|
if not pending and not ignored:
|
||||||
|
if args.all or args.ignored:
|
||||||
|
print(f"\n✅ No {pending_label if pending else 'ignored'} sensors found")
|
||||||
|
else:
|
||||||
|
print("\n✅ No new sensors to review")
|
||||||
|
all_pending = manager.get_pending()
|
||||||
|
if all_pending:
|
||||||
|
print(f"\nThere are {len(all_pending)} previously reviewed pending sensor(s).")
|
||||||
|
print("Run with --all to review them again.")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
# Process pending sensors
|
||||||
|
if pending:
|
||||||
|
print(f"\n📋 Processing {len(pending)} {pending_label} sensor(s)...")
|
||||||
|
process_sensors(pending, manager)
|
||||||
|
|
||||||
|
# Process ignored sensors if requested
|
||||||
|
if ignored:
|
||||||
|
if pending:
|
||||||
|
print("\n" + "=" * 70)
|
||||||
|
print(f"\n📋 Processing {len(ignored)} ignored sensor(s)...")
|
||||||
|
process_sensors(ignored, manager)
|
||||||
|
|
||||||
|
print("\n" + "=" * 70)
|
||||||
|
print("Done!")
|
||||||
|
print("=" * 70)
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\n\n👋 Interrupted by user")
|
||||||
|
return 1
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error: {e}", exc_info=True)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main())
|
||||||
142
src/sensorpajen/config.py
Normal file
142
src/sensorpajen/config.py
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
"""
|
||||||
|
Configuration management for Sensorpajen.
|
||||||
|
|
||||||
|
Loads configuration from environment variables with sensible defaults.
|
||||||
|
Configuration files are loaded relative to the project root.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Dict, List
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# Determine project root (3 levels up from this file: src/sensorpajen/config.py)
|
||||||
|
PROJECT_ROOT = Path(__file__).parent.parent.parent
|
||||||
|
|
||||||
|
# MQTT Configuration from environment
|
||||||
|
MQTT_HOST = os.environ.get("MQTT_HOST")
|
||||||
|
MQTT_PORT = int(os.environ.get("MQTT_PORT", "1883"))
|
||||||
|
MQTT_USER = os.environ.get("MQTT_USER")
|
||||||
|
MQTT_PASSWORD = os.environ.get("MQTT_PASSWORD")
|
||||||
|
MQTT_CLIENT_ID = os.environ.get("MQTT_CLIENT_ID", "sensorpajen")
|
||||||
|
MQTT_TOPIC_PREFIX = os.environ.get("MQTT_TOPIC_PREFIX", "MiTemperature2")
|
||||||
|
|
||||||
|
# Validate required MQTT configuration
|
||||||
|
if not MQTT_HOST:
|
||||||
|
raise RuntimeError(
|
||||||
|
"MQTT_HOST environment variable must be set. "
|
||||||
|
"Please configure config/sensorpajen.env"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Sensor configuration file (relative to project root)
|
||||||
|
SENSOR_CONFIG_FILE = os.environ.get(
|
||||||
|
"SENSOR_CONFIG_FILE",
|
||||||
|
str(PROJECT_ROOT / "config/sensors.json")
|
||||||
|
)
|
||||||
|
|
||||||
|
# Application settings
|
||||||
|
WATCHDOG_TIMEOUT = int(os.environ.get("WATCHDOG_TIMEOUT", "5"))
|
||||||
|
ENABLE_BATTERY = os.environ.get("ENABLE_BATTERY", "true").lower() == "true"
|
||||||
|
LOG_LEVEL = os.environ.get("LOG_LEVEL", "INFO").upper()
|
||||||
|
|
||||||
|
# Bluetooth settings
|
||||||
|
SKIP_IDENTICAL = int(os.environ.get("SKIP_IDENTICAL", "50"))
|
||||||
|
DEBOUNCE = os.environ.get("DEBOUNCE", "true").lower() == "true"
|
||||||
|
|
||||||
|
# ntfy notification settings (optional)
|
||||||
|
NTFY_ENABLED = os.environ.get("NTFY_ENABLED", "false").lower() == "true"
|
||||||
|
NTFY_URL = os.environ.get("NTFY_URL", "https://ntfy.sh")
|
||||||
|
NTFY_TOPIC = os.environ.get("NTFY_TOPIC", "sensorpajen")
|
||||||
|
NTFY_TOKEN = os.environ.get("NTFY_TOKEN", "")
|
||||||
|
|
||||||
|
# Discovery settings
|
||||||
|
DISCOVERED_SENSORS_FILE = os.environ.get(
|
||||||
|
"DISCOVERED_SENSORS_FILE",
|
||||||
|
str(PROJECT_ROOT / "config/discovered_sensors.json")
|
||||||
|
)
|
||||||
|
CONFIG_RELOAD_INTERVAL = int(os.environ.get("CONFIG_RELOAD_INTERVAL", "900")) # 15 minutes
|
||||||
|
|
||||||
|
|
||||||
|
class SensorConfig:
|
||||||
|
"""Manages sensor configuration from JSON file."""
|
||||||
|
|
||||||
|
def __init__(self, config_file: str = SENSOR_CONFIG_FILE):
|
||||||
|
"""
|
||||||
|
Initialize sensor configuration.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
config_file: Path to sensors JSON configuration file
|
||||||
|
"""
|
||||||
|
self.config_file = Path(config_file)
|
||||||
|
self.sensors: Dict[str, str] = {}
|
||||||
|
self.load()
|
||||||
|
|
||||||
|
def load(self):
|
||||||
|
"""Load sensor configuration from JSON file."""
|
||||||
|
if not self.config_file.exists():
|
||||||
|
raise FileNotFoundError(
|
||||||
|
f"Sensor configuration file not found: {self.config_file}\n"
|
||||||
|
f"Please copy config/sensors.json.example to config/sensors.json "
|
||||||
|
f"and configure your sensors."
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(self.config_file, 'r') as f:
|
||||||
|
data = json.load(f)
|
||||||
|
|
||||||
|
# Convert sensors list to MAC -> name mapping
|
||||||
|
for sensor in data.get('sensors', []):
|
||||||
|
mac = sensor.get('mac', '').upper()
|
||||||
|
name = sensor.get('name')
|
||||||
|
|
||||||
|
if mac and name:
|
||||||
|
self.sensors[mac] = name
|
||||||
|
logger.debug(f"Loaded sensor: {mac} -> {name}")
|
||||||
|
|
||||||
|
logger.info(f"Loaded {len(self.sensors)} sensors from {self.config_file}")
|
||||||
|
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
raise RuntimeError(f"Invalid JSON in {self.config_file}: {e}")
|
||||||
|
except Exception as e:
|
||||||
|
raise RuntimeError(f"Error loading sensor config: {e}")
|
||||||
|
|
||||||
|
def get_name(self, mac: str) -> str:
|
||||||
|
"""
|
||||||
|
Get sensor name by MAC address.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
mac: MAC address (any case)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Sensor name or the MAC address if not found
|
||||||
|
"""
|
||||||
|
return self.sensors.get(mac.upper(), mac)
|
||||||
|
|
||||||
|
def get_all_macs(self) -> List[str]:
|
||||||
|
"""Get list of all configured MAC addresses."""
|
||||||
|
return list(self.sensors.keys())
|
||||||
|
|
||||||
|
|
||||||
|
def validate_config():
|
||||||
|
"""
|
||||||
|
Validate configuration and log settings.
|
||||||
|
Should be called at application startup.
|
||||||
|
"""
|
||||||
|
logger.info("=== Sensorpajen Configuration ===")
|
||||||
|
logger.info(f"MQTT Host: {MQTT_HOST}:{MQTT_PORT}")
|
||||||
|
logger.info(f"MQTT User: {MQTT_USER}")
|
||||||
|
logger.info(f"MQTT Client ID: {MQTT_CLIENT_ID}")
|
||||||
|
logger.info(f"MQTT Topic Prefix: {MQTT_TOPIC_PREFIX}")
|
||||||
|
logger.info(f"Sensor Config: {SENSOR_CONFIG_FILE}")
|
||||||
|
logger.info(f"Discovered Sensors: {DISCOVERED_SENSORS_FILE}")
|
||||||
|
logger.info(f"Watchdog Timeout: {WATCHDOG_TIMEOUT}s")
|
||||||
|
logger.info(f"Battery Monitoring: {ENABLE_BATTERY}")
|
||||||
|
logger.info(f"Config Reload Interval: {CONFIG_RELOAD_INTERVAL}s")
|
||||||
|
logger.info(f"ntfy Enabled: {NTFY_ENABLED}")
|
||||||
|
if NTFY_ENABLED:
|
||||||
|
logger.info(f"ntfy URL: {NTFY_URL}/{NTFY_TOPIC}")
|
||||||
|
logger.info(f"Log Level: {LOG_LEVEL}")
|
||||||
|
logger.info("================================")
|
||||||
263
src/sensorpajen/discovery_manager.py
Normal file
263
src/sensorpajen/discovery_manager.py
Normal file
@@ -0,0 +1,263 @@
|
|||||||
|
"""
|
||||||
|
Discovery manager for tracking and managing discovered sensors.
|
||||||
|
|
||||||
|
Maintains a database of discovered sensors with their metadata and status.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import subprocess
|
||||||
|
from datetime import datetime
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Dict, List, Optional
|
||||||
|
from dataclasses import dataclass, asdict
|
||||||
|
|
||||||
|
from . import config
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class DiscoveredSensor:
|
||||||
|
"""Represents a discovered sensor with metadata."""
|
||||||
|
mac: str
|
||||||
|
name: str
|
||||||
|
rssi: int
|
||||||
|
first_seen: str
|
||||||
|
last_seen: str
|
||||||
|
sample_reading: Dict[str, float]
|
||||||
|
status: str = "pending" # pending, approved, ignored
|
||||||
|
reviewed: bool = False # Has been shown in approval CLI
|
||||||
|
ignored_at: Optional[str] = None
|
||||||
|
ignore_reason: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
class DiscoveryManager:
|
||||||
|
"""Manages discovered sensors and their approval status."""
|
||||||
|
|
||||||
|
def __init__(self, discovery_file: str = config.DISCOVERED_SENSORS_FILE):
|
||||||
|
"""
|
||||||
|
Initialize discovery manager.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
discovery_file: Path to discovered sensors JSON file
|
||||||
|
"""
|
||||||
|
self.discovery_file = Path(discovery_file)
|
||||||
|
self.sensors: Dict[str, DiscoveredSensor] = {}
|
||||||
|
self.load()
|
||||||
|
|
||||||
|
def load(self):
|
||||||
|
"""Load discovered sensors from JSON file."""
|
||||||
|
if not self.discovery_file.exists():
|
||||||
|
logger.info(f"Creating new discovered sensors file: {self.discovery_file}")
|
||||||
|
self.discovery_file.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
self.save()
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(self.discovery_file, 'r') as f:
|
||||||
|
data = json.load(f)
|
||||||
|
|
||||||
|
for sensor_data in data:
|
||||||
|
sensor = DiscoveredSensor(**sensor_data)
|
||||||
|
self.sensors[sensor.mac.upper()] = sensor
|
||||||
|
|
||||||
|
logger.info(f"Loaded {len(self.sensors)} discovered sensors")
|
||||||
|
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
logger.error(f"Invalid JSON in {self.discovery_file}: {e}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error loading discovered sensors: {e}")
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
"""Save discovered sensors to JSON file."""
|
||||||
|
try:
|
||||||
|
# Ensure directory exists
|
||||||
|
self.discovery_file.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
# Convert sensors to list of dicts
|
||||||
|
data = [asdict(sensor) for sensor in self.sensors.values()]
|
||||||
|
|
||||||
|
with open(self.discovery_file, 'w') as f:
|
||||||
|
json.dump(data, f, indent=2)
|
||||||
|
|
||||||
|
logger.debug(f"Saved {len(self.sensors)} discovered sensors")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error saving discovered sensors: {e}")
|
||||||
|
|
||||||
|
def add_or_update(self, mac: str, name: str, rssi: int,
|
||||||
|
temperature: float, humidity: float,
|
||||||
|
battery_percent: int, battery_voltage: int) -> bool:
|
||||||
|
"""
|
||||||
|
Add or update a discovered sensor.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
mac: MAC address
|
||||||
|
name: Advertised device name
|
||||||
|
rssi: Signal strength
|
||||||
|
temperature: Temperature reading
|
||||||
|
humidity: Humidity reading
|
||||||
|
battery_percent: Battery percentage
|
||||||
|
battery_voltage: Battery voltage in mV
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if this is a newly discovered sensor, False if updated existing
|
||||||
|
"""
|
||||||
|
mac = mac.upper()
|
||||||
|
now = datetime.now().isoformat()
|
||||||
|
|
||||||
|
sample_reading = {
|
||||||
|
"temperature": temperature,
|
||||||
|
"humidity": humidity,
|
||||||
|
"battery_percent": battery_percent,
|
||||||
|
"battery_voltage": battery_voltage
|
||||||
|
}
|
||||||
|
|
||||||
|
if mac in self.sensors:
|
||||||
|
# Update existing sensor
|
||||||
|
sensor = self.sensors[mac]
|
||||||
|
sensor.last_seen = now
|
||||||
|
sensor.rssi = rssi
|
||||||
|
sensor.sample_reading = sample_reading
|
||||||
|
self.save()
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
# New sensor discovered
|
||||||
|
sensor = DiscoveredSensor(
|
||||||
|
mac=mac,
|
||||||
|
name=name,
|
||||||
|
rssi=rssi,
|
||||||
|
first_seen=now,
|
||||||
|
last_seen=now,
|
||||||
|
sample_reading=sample_reading,
|
||||||
|
status="pending"
|
||||||
|
)
|
||||||
|
self.sensors[mac] = sensor
|
||||||
|
self.save()
|
||||||
|
logger.info(f"New sensor discovered: {mac} ({name})")
|
||||||
|
return True
|
||||||
|
|
||||||
|
def is_known(self, mac: str) -> bool:
|
||||||
|
"""
|
||||||
|
Check if a sensor has been discovered before.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
mac: MAC address
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if sensor is in discovered list
|
||||||
|
"""
|
||||||
|
return mac.upper() in self.sensors
|
||||||
|
|
||||||
|
def get_status(self, mac: str) -> Optional[str]:
|
||||||
|
"""
|
||||||
|
Get status of a discovered sensor.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
mac: MAC address
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Status string or None if not found
|
||||||
|
"""
|
||||||
|
sensor = self.sensors.get(mac.upper())
|
||||||
|
return sensor.status if sensor else None
|
||||||
|
|
||||||
|
def approve(self, mac: str):
|
||||||
|
"""
|
||||||
|
Mark a sensor as approved.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
mac: MAC address
|
||||||
|
"""
|
||||||
|
mac = mac.upper()
|
||||||
|
if mac in self.sensors:
|
||||||
|
self.sensors[mac].status = "approved"
|
||||||
|
self.save()
|
||||||
|
logger.info(f"Sensor approved: {mac}")
|
||||||
|
|
||||||
|
def ignore(self, mac: str, reason: Optional[str] = None):
|
||||||
|
"""
|
||||||
|
Mark a sensor as ignored.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
mac: MAC address
|
||||||
|
reason: Optional reason for ignoring
|
||||||
|
"""
|
||||||
|
mac = mac.upper()
|
||||||
|
if mac in self.sensors:
|
||||||
|
self.sensors[mac].status = "ignored"
|
||||||
|
self.sensors[mac].ignored_at = datetime.now().isoformat()
|
||||||
|
self.sensors[mac].ignore_reason = reason
|
||||||
|
self.save()
|
||||||
|
logger.info(f"Sensor ignored: {mac}")
|
||||||
|
|
||||||
|
def get_pending(self) -> List[DiscoveredSensor]:
|
||||||
|
"""Get list of sensors with status 'pending'."""
|
||||||
|
return [s for s in self.sensors.values() if s.status == "pending"]
|
||||||
|
|
||||||
|
def get_new_pending(self) -> List[DiscoveredSensor]:
|
||||||
|
"""Get list of pending sensors that haven't been reviewed yet."""
|
||||||
|
return [s for s in self.sensors.values() if s.status == "pending" and not s.reviewed]
|
||||||
|
|
||||||
|
def get_ignored(self) -> List[DiscoveredSensor]:
|
||||||
|
"""Get list of sensors with status 'ignored'."""
|
||||||
|
return [s for s in self.sensors.values() if s.status == "ignored"]
|
||||||
|
|
||||||
|
def mark_reviewed(self, mac: str):
|
||||||
|
"""
|
||||||
|
Mark a sensor as reviewed (shown in approval CLI).
|
||||||
|
|
||||||
|
Args:
|
||||||
|
mac: MAC address
|
||||||
|
"""
|
||||||
|
mac = mac.upper()
|
||||||
|
if mac in self.sensors:
|
||||||
|
self.sensors[mac].reviewed = True
|
||||||
|
self.save()
|
||||||
|
|
||||||
|
def send_ntfy_notification(self, sensor: DiscoveredSensor):
|
||||||
|
"""
|
||||||
|
Send ntfy notification for a newly discovered sensor.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
sensor: Discovered sensor to notify about
|
||||||
|
"""
|
||||||
|
if not config.NTFY_ENABLED:
|
||||||
|
logger.debug("ntfy notifications disabled")
|
||||||
|
return
|
||||||
|
|
||||||
|
if not config.NTFY_TOKEN:
|
||||||
|
logger.warning("ntfy enabled but NTFY_TOKEN not set")
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
message = (
|
||||||
|
f"🆕 New sensor discovered!\n\n"
|
||||||
|
f"MAC: {sensor.mac}\n"
|
||||||
|
f"Name: {sensor.name}\n"
|
||||||
|
f"Last seen: {sensor.last_seen}\n"
|
||||||
|
f"Temp: {sensor.sample_reading.get('temperature', 'N/A')}°C\n"
|
||||||
|
f"Humidity: {sensor.sample_reading.get('humidity', 'N/A')}%\n"
|
||||||
|
f"Battery: {sensor.sample_reading.get('battery_percent', 'N/A')}%\n\n"
|
||||||
|
f"Run 'sensorpajen approve-sensors' to approve or ignore."
|
||||||
|
)
|
||||||
|
|
||||||
|
url = f"{config.NTFY_URL}/{config.NTFY_TOPIC}"
|
||||||
|
|
||||||
|
result = subprocess.run(
|
||||||
|
["curl", "-H", f"Authorization: Bearer {config.NTFY_TOKEN}",
|
||||||
|
"-d", message, url],
|
||||||
|
capture_output=True,
|
||||||
|
timeout=10
|
||||||
|
)
|
||||||
|
|
||||||
|
if result.returncode == 0:
|
||||||
|
logger.info(f"Sent ntfy notification for {sensor.mac}")
|
||||||
|
else:
|
||||||
|
logger.warning(f"ntfy notification failed: {result.stderr.decode()}")
|
||||||
|
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
logger.warning("ntfy notification timed out")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error sending ntfy notification: {e}")
|
||||||
226
src/sensorpajen/main.py
Normal file
226
src/sensorpajen/main.py
Normal file
@@ -0,0 +1,226 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Sensorpajen - Main entry point
|
||||||
|
|
||||||
|
Bluetooth temperature sensor monitor for Xiaomi Mijia LYWSD03MMC sensors.
|
||||||
|
Publishes sensor data to MQTT broker.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import signal
|
||||||
|
import logging
|
||||||
|
import time
|
||||||
|
import threading
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from . import __version__
|
||||||
|
from . import config
|
||||||
|
from .mqtt_publisher import MQTTPublisher
|
||||||
|
from .sensor_reader import SensorReader, Measurement
|
||||||
|
from .discovery_manager import DiscoveryManager
|
||||||
|
|
||||||
|
|
||||||
|
class Sensorpajen:
|
||||||
|
"""Main application class."""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
"""Initialize the application."""
|
||||||
|
self.mqtt_publisher: MQTTPublisher = None
|
||||||
|
self.sensor_reader: SensorReader = None
|
||||||
|
self.sensor_config: config.SensorConfig = None
|
||||||
|
self.discovery_manager: DiscoveryManager = None
|
||||||
|
self.running = False
|
||||||
|
self.config_reload_timer: threading.Timer = None
|
||||||
|
|
||||||
|
# Setup logging
|
||||||
|
self._setup_logging()
|
||||||
|
|
||||||
|
# Setup signal handlers
|
||||||
|
signal.signal(signal.SIGTERM, self._signal_handler)
|
||||||
|
signal.signal(signal.SIGINT, self._signal_handler)
|
||||||
|
|
||||||
|
def _setup_logging(self):
|
||||||
|
"""Configure logging to stdout for journald."""
|
||||||
|
log_level = getattr(logging, config.LOG_LEVEL, logging.INFO)
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=log_level,
|
||||||
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||||
|
stream=sys.stdout
|
||||||
|
)
|
||||||
|
|
||||||
|
# Set our logger
|
||||||
|
self.logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def _signal_handler(self, sig, frame):
|
||||||
|
"""Handle shutdown signals."""
|
||||||
|
signal_name = "SIGTERM" if sig == signal.SIGTERM else "SIGINT"
|
||||||
|
self.logger.info(f"Received {signal_name}, shutting down gracefully...")
|
||||||
|
self.shutdown()
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
def _on_measurement(self, measurement: Measurement):
|
||||||
|
"""
|
||||||
|
Callback for new sensor measurements.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
measurement: Sensor measurement data
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# Publish to MQTT
|
||||||
|
self.mqtt_publisher.publish_measurement(
|
||||||
|
sensor_name=measurement.sensor_name,
|
||||||
|
temperature=measurement.temperature,
|
||||||
|
humidity=measurement.humidity,
|
||||||
|
battery_voltage=measurement.voltage,
|
||||||
|
battery_level=measurement.battery
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f"Error handling measurement: {e}")
|
||||||
|
|
||||||
|
def _reload_config(self):
|
||||||
|
"""Reload sensor configuration periodically."""
|
||||||
|
if not self.running:
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.logger.info("Reloading sensor configuration...")
|
||||||
|
old_sensors = set(self.sensor_config.sensors.keys())
|
||||||
|
self.sensor_config.load()
|
||||||
|
new_sensors = set(self.sensor_config.sensors.keys())
|
||||||
|
|
||||||
|
added = new_sensors - old_sensors
|
||||||
|
removed = old_sensors - new_sensors
|
||||||
|
|
||||||
|
if added:
|
||||||
|
self.logger.info(f"Added sensors: {', '.join(added)}")
|
||||||
|
if removed:
|
||||||
|
self.logger.info(f"Removed sensors: {', '.join(removed)}")
|
||||||
|
if not added and not removed:
|
||||||
|
self.logger.debug("No sensor configuration changes")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f"Error reloading configuration: {e}")
|
||||||
|
finally:
|
||||||
|
# Schedule next reload
|
||||||
|
if self.running:
|
||||||
|
self.config_reload_timer = threading.Timer(
|
||||||
|
config.CONFIG_RELOAD_INTERVAL,
|
||||||
|
self._reload_config
|
||||||
|
)
|
||||||
|
self.config_reload_timer.daemon = True
|
||||||
|
self.config_reload_timer.start()
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
"""Start the application."""
|
||||||
|
try:
|
||||||
|
self.logger.info("=" * 50)
|
||||||
|
self.logger.info(f"Starting Sensorpajen v{__version__}")
|
||||||
|
self.logger.info("=" * 50)
|
||||||
|
|
||||||
|
# Validate and log configuration
|
||||||
|
config.validate_config()
|
||||||
|
|
||||||
|
# Load sensor configuration
|
||||||
|
self.sensor_config = config.SensorConfig()
|
||||||
|
|
||||||
|
if len(self.sensor_config.sensors) == 0:
|
||||||
|
self.logger.error("No sensors configured!")
|
||||||
|
self.logger.error("Please configure sensors in config/sensors.json")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Initialize discovery manager
|
||||||
|
self.logger.info("Initializing discovery manager...")
|
||||||
|
self.discovery_manager = DiscoveryManager()
|
||||||
|
|
||||||
|
# Initialize MQTT publisher
|
||||||
|
self.logger.info("Initializing MQTT publisher...")
|
||||||
|
self.mqtt_publisher = MQTTPublisher()
|
||||||
|
self.mqtt_publisher.connect()
|
||||||
|
|
||||||
|
# Wait a moment for MQTT connection
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
if not self.mqtt_publisher.is_connected():
|
||||||
|
self.logger.warning("MQTT connection not established yet, continuing anyway...")
|
||||||
|
|
||||||
|
# Initialize sensor reader
|
||||||
|
self.logger.info("Initializing Bluetooth sensor reader...")
|
||||||
|
self.sensor_reader = SensorReader(
|
||||||
|
sensor_config=self.sensor_config,
|
||||||
|
discovery_manager=self.discovery_manager,
|
||||||
|
on_measurement=self._on_measurement,
|
||||||
|
interface=0 # hci0
|
||||||
|
)
|
||||||
|
|
||||||
|
# Start config reload timer
|
||||||
|
self.config_reload_timer = threading.Timer(
|
||||||
|
config.CONFIG_RELOAD_INTERVAL,
|
||||||
|
self._reload_config
|
||||||
|
)
|
||||||
|
self.config_reload_timer.daemon = True
|
||||||
|
self.config_reload_timer.start()
|
||||||
|
self.logger.info(f"Config reload scheduled every {config.CONFIG_RELOAD_INTERVAL}s")
|
||||||
|
|
||||||
|
# Start reading sensors (blocking call)
|
||||||
|
self.logger.info("=" * 50)
|
||||||
|
self.logger.info("Sensorpajen is now running")
|
||||||
|
self.logger.info("Monitoring sensors via Bluetooth...")
|
||||||
|
self.logger.info("Publishing to MQTT...")
|
||||||
|
self.logger.info("Press Ctrl+C to stop")
|
||||||
|
self.logger.info("=" * 50)
|
||||||
|
|
||||||
|
self.running = True
|
||||||
|
self.sensor_reader.start()
|
||||||
|
|
||||||
|
except FileNotFoundError as e:
|
||||||
|
self.logger.error(f"Configuration error: {e}")
|
||||||
|
sys.exit(1)
|
||||||
|
except RuntimeError as e:
|
||||||
|
self.logger.error(f"Configuration error: {e}")
|
||||||
|
sys.exit(1)
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f"Failed to start application: {e}", exc_info=True)
|
||||||
|
self.shutdown()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
def shutdown(self):
|
||||||
|
"""Shutdown the application gracefully."""
|
||||||
|
if not self.running:
|
||||||
|
return
|
||||||
|
|
||||||
|
self.running = False
|
||||||
|
self.logger.info("Shutting down...")
|
||||||
|
|
||||||
|
# Cancel config reload timer
|
||||||
|
if self.config_reload_timer:
|
||||||
|
try:
|
||||||
|
self.config_reload_timer.cancel()
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f"Error canceling reload timer: {e}")
|
||||||
|
|
||||||
|
# Stop sensor reader
|
||||||
|
if self.sensor_reader:
|
||||||
|
try:
|
||||||
|
self.sensor_reader.stop()
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f"Error stopping sensor reader: {e}")
|
||||||
|
|
||||||
|
# Disconnect MQTT
|
||||||
|
if self.mqtt_publisher:
|
||||||
|
try:
|
||||||
|
self.mqtt_publisher.disconnect()
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f"Error disconnecting MQTT: {e}")
|
||||||
|
|
||||||
|
self.logger.info("Shutdown complete")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Main entry point."""
|
||||||
|
app = Sensorpajen()
|
||||||
|
app.start()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
131
src/sensorpajen/mqtt_publisher.py
Normal file
131
src/sensorpajen/mqtt_publisher.py
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
"""
|
||||||
|
MQTT Publisher for sensor data.
|
||||||
|
|
||||||
|
Handles connection to MQTT broker and publishing of sensor measurements.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
from typing import Optional
|
||||||
|
from . import config
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class MQTTPublisher:
|
||||||
|
"""Manages MQTT connection and publishing of sensor data."""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
"""Initialize MQTT publisher with configuration."""
|
||||||
|
self.client: Optional[mqtt.Client] = None
|
||||||
|
self.connected = False
|
||||||
|
self._setup_client()
|
||||||
|
|
||||||
|
def _setup_client(self):
|
||||||
|
"""Setup MQTT client with callbacks."""
|
||||||
|
# Handle both paho-mqtt v1.x and v2.x
|
||||||
|
try:
|
||||||
|
# Try v2.x format (with callback_api_version)
|
||||||
|
self.client = mqtt.Client(
|
||||||
|
callback_api_version=mqtt.CallbackAPIVersion.VERSION1,
|
||||||
|
client_id=config.MQTT_CLIENT_ID
|
||||||
|
)
|
||||||
|
except (TypeError, AttributeError):
|
||||||
|
# Fall back to v1.x format
|
||||||
|
self.client = mqtt.Client(config.MQTT_CLIENT_ID)
|
||||||
|
|
||||||
|
# Set credentials if provided
|
||||||
|
if config.MQTT_USER and config.MQTT_PASSWORD:
|
||||||
|
self.client.username_pw_set(config.MQTT_USER, config.MQTT_PASSWORD)
|
||||||
|
|
||||||
|
# Setup callbacks
|
||||||
|
self.client.on_connect = self._on_connect
|
||||||
|
self.client.on_disconnect = self._on_disconnect
|
||||||
|
self.client.on_publish = self._on_publish
|
||||||
|
|
||||||
|
logger.info(f"MQTT client configured for {config.MQTT_HOST}:{config.MQTT_PORT}")
|
||||||
|
|
||||||
|
def _on_connect(self, client, userdata, flags, rc):
|
||||||
|
"""Callback for when client connects to broker."""
|
||||||
|
if rc == 0:
|
||||||
|
self.connected = True
|
||||||
|
logger.info(f"Connected to MQTT broker at {config.MQTT_HOST}:{config.MQTT_PORT}")
|
||||||
|
else:
|
||||||
|
self.connected = False
|
||||||
|
logger.error(f"Failed to connect to MQTT broker. Return code: {rc}")
|
||||||
|
|
||||||
|
def _on_disconnect(self, client, userdata, rc):
|
||||||
|
"""Callback for when client disconnects from broker."""
|
||||||
|
self.connected = False
|
||||||
|
if rc != 0:
|
||||||
|
logger.warning(f"Unexpected disconnection from MQTT broker. Return code: {rc}")
|
||||||
|
else:
|
||||||
|
logger.info("Disconnected from MQTT broker")
|
||||||
|
|
||||||
|
def _on_publish(self, client, userdata, mid):
|
||||||
|
"""Callback for when message is published."""
|
||||||
|
logger.debug(f"Message published: {mid}")
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
|
"""Connect to MQTT broker."""
|
||||||
|
try:
|
||||||
|
logger.info(f"Connecting to MQTT broker at {config.MQTT_HOST}:{config.MQTT_PORT}")
|
||||||
|
self.client.connect(config.MQTT_HOST, config.MQTT_PORT, keepalive=60)
|
||||||
|
self.client.loop_start() # Start network loop in background thread
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to connect to MQTT broker: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
def disconnect(self):
|
||||||
|
"""Disconnect from MQTT broker."""
|
||||||
|
if self.client:
|
||||||
|
self.client.loop_stop()
|
||||||
|
self.client.disconnect()
|
||||||
|
logger.info("Disconnected from MQTT broker")
|
||||||
|
|
||||||
|
def publish_measurement(self, sensor_name: str, temperature: float,
|
||||||
|
humidity: int, battery_voltage: float = None,
|
||||||
|
battery_level: int = None):
|
||||||
|
"""
|
||||||
|
Publish sensor measurement to MQTT.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
sensor_name: Name of the sensor
|
||||||
|
temperature: Temperature in Celsius
|
||||||
|
humidity: Humidity percentage
|
||||||
|
battery_voltage: Battery voltage (optional)
|
||||||
|
battery_level: Battery level percentage (optional)
|
||||||
|
"""
|
||||||
|
if not self.connected:
|
||||||
|
logger.warning("Not connected to MQTT broker, skipping publish")
|
||||||
|
return
|
||||||
|
|
||||||
|
topic_prefix = f"{config.MQTT_TOPIC_PREFIX}/{sensor_name}"
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Publish temperature
|
||||||
|
self.client.publish(f"{topic_prefix}/temp", f"{temperature:.1f}")
|
||||||
|
logger.debug(f"{sensor_name}: temp={temperature:.1f}°C")
|
||||||
|
|
||||||
|
# Publish humidity
|
||||||
|
self.client.publish(f"{topic_prefix}/humidity", f"{humidity}")
|
||||||
|
logger.debug(f"{sensor_name}: humidity={humidity}%")
|
||||||
|
|
||||||
|
# Publish battery info if enabled and available
|
||||||
|
if config.ENABLE_BATTERY:
|
||||||
|
if battery_voltage is not None:
|
||||||
|
self.client.publish(f"{topic_prefix}/batteryvoltage", f"{battery_voltage:.3f}")
|
||||||
|
logger.debug(f"{sensor_name}: battery_voltage={battery_voltage:.3f}V")
|
||||||
|
|
||||||
|
if battery_level is not None:
|
||||||
|
self.client.publish(f"{topic_prefix}/batterylevel", f"{battery_level}")
|
||||||
|
logger.debug(f"{sensor_name}: battery_level={battery_level}%")
|
||||||
|
|
||||||
|
logger.info(f"Published: {sensor_name} - {temperature:.1f}°C, {humidity}%")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error publishing to MQTT: {e}")
|
||||||
|
|
||||||
|
def is_connected(self) -> bool:
|
||||||
|
"""Check if connected to MQTT broker."""
|
||||||
|
return self.connected
|
||||||
292
src/sensorpajen/sensor_reader.py
Normal file
292
src/sensorpajen/sensor_reader.py
Normal file
@@ -0,0 +1,292 @@
|
|||||||
|
"""
|
||||||
|
Bluetooth sensor reader for Xiaomi Mijia LYWSD03MMC sensors with ATC firmware.
|
||||||
|
|
||||||
|
Reads temperature, humidity, and battery data from BLE advertisements.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import time
|
||||||
|
import threading
|
||||||
|
import bluetooth._bluetooth as bluez
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Optional, Callable, Dict
|
||||||
|
|
||||||
|
from . import config
|
||||||
|
from .utils import (enable_le_scan, disable_le_scan,
|
||||||
|
parse_le_advertising_events, raw_packet_to_str, toggle_device)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Measurement:
|
||||||
|
"""Sensor measurement data."""
|
||||||
|
temperature: float
|
||||||
|
humidity: int
|
||||||
|
voltage: float
|
||||||
|
battery: int = 0
|
||||||
|
rssi: int = 0
|
||||||
|
sensor_name: str = ""
|
||||||
|
timestamp: int = 0
|
||||||
|
|
||||||
|
|
||||||
|
class SensorReader:
|
||||||
|
"""Reads Xiaomi LYWSD03MMC sensors with ATC firmware via BLE."""
|
||||||
|
|
||||||
|
def __init__(self, sensor_config: config.SensorConfig,
|
||||||
|
discovery_manager,
|
||||||
|
on_measurement: Callable[[Measurement], None],
|
||||||
|
interface: int = 0):
|
||||||
|
"""
|
||||||
|
Initialize sensor reader.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
sensor_config: Sensor configuration mapping
|
||||||
|
discovery_manager: Discovery manager for tracking new sensors
|
||||||
|
on_measurement: Callback function for new measurements
|
||||||
|
interface: Bluetooth interface number (default 0 for hci0)
|
||||||
|
"""
|
||||||
|
self.sensor_config = sensor_config
|
||||||
|
self.discovery_manager = discovery_manager
|
||||||
|
self.on_measurement = on_measurement
|
||||||
|
self.interface = interface
|
||||||
|
self.sock: Optional[int] = None
|
||||||
|
self.running = False
|
||||||
|
self.last_ble_packet = time.time()
|
||||||
|
self.adv_counter: Dict[str, str] = {} # Track advertisement numbers to avoid duplicates
|
||||||
|
self.watchdog_thread: Optional[threading.Thread] = None
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
"""Start BLE scanning for sensors."""
|
||||||
|
try:
|
||||||
|
logger.info(f"Starting BLE scan on hci{self.interface}")
|
||||||
|
|
||||||
|
# Enable bluetooth device
|
||||||
|
toggle_device(self.interface, True)
|
||||||
|
|
||||||
|
# Open bluetooth socket
|
||||||
|
try:
|
||||||
|
self.sock = bluez.hci_open_dev(self.interface)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Cannot open bluetooth device hci{self.interface}: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
# Enable LE scanning without filtering duplicates
|
||||||
|
enable_le_scan(self.sock, filter_duplicates=False)
|
||||||
|
|
||||||
|
# Start watchdog if configured
|
||||||
|
if config.WATCHDOG_TIMEOUT > 0:
|
||||||
|
self.running = True
|
||||||
|
self.watchdog_thread = threading.Thread(target=self._watchdog_loop, daemon=True)
|
||||||
|
self.watchdog_thread.start()
|
||||||
|
logger.info(f"Watchdog started with {config.WATCHDOG_TIMEOUT}s timeout")
|
||||||
|
|
||||||
|
logger.info("BLE scanning enabled")
|
||||||
|
logger.info(f"Monitoring {len(self.sensor_config.sensors)} sensors")
|
||||||
|
|
||||||
|
# Start parsing advertisements (blocking call)
|
||||||
|
parse_le_advertising_events(
|
||||||
|
self.sock,
|
||||||
|
handler=self._handle_ble_packet,
|
||||||
|
debug=False
|
||||||
|
)
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
logger.info("Received keyboard interrupt")
|
||||||
|
self.stop()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error in sensor reader: {e}")
|
||||||
|
self.stop()
|
||||||
|
raise
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
"""Stop BLE scanning."""
|
||||||
|
self.running = False
|
||||||
|
|
||||||
|
if self.sock:
|
||||||
|
try:
|
||||||
|
disable_le_scan(self.sock)
|
||||||
|
logger.info("BLE scanning disabled")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error disabling BLE scan: {e}")
|
||||||
|
|
||||||
|
if self.watchdog_thread and self.watchdog_thread.is_alive():
|
||||||
|
self.watchdog_thread.join(timeout=2)
|
||||||
|
|
||||||
|
def _watchdog_loop(self):
|
||||||
|
"""Watchdog thread to restart BLE scanning if no packets received."""
|
||||||
|
restart_counter = 1
|
||||||
|
|
||||||
|
while self.running:
|
||||||
|
time.sleep(1)
|
||||||
|
now = time.time()
|
||||||
|
elapsed = now - self.last_ble_packet
|
||||||
|
|
||||||
|
if elapsed > config.WATCHDOG_TIMEOUT:
|
||||||
|
logger.warning(
|
||||||
|
f"Watchdog: No BLE packet within {int(elapsed)}s. "
|
||||||
|
f"Restarting BLE scan (count: {restart_counter})"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
disable_le_scan(self.sock)
|
||||||
|
time.sleep(1)
|
||||||
|
enable_le_scan(self.sock, filter_duplicates=False)
|
||||||
|
restart_counter += 1
|
||||||
|
self.last_ble_packet = now # Reset timer
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error restarting BLE scan: {e}")
|
||||||
|
|
||||||
|
def _handle_ble_packet(self, mac: str, adv_type: int, data: bytes, rssi: int):
|
||||||
|
"""
|
||||||
|
Handle incoming BLE advertisement packet.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
mac: MAC address of the device
|
||||||
|
adv_type: Advertisement type
|
||||||
|
data: Advertisement data
|
||||||
|
rssi: Signal strength
|
||||||
|
"""
|
||||||
|
# Update last packet time for watchdog
|
||||||
|
self.last_ble_packet = time.time()
|
||||||
|
|
||||||
|
# Convert data to hex string
|
||||||
|
data_str = raw_packet_to_str(data)
|
||||||
|
|
||||||
|
# Check if this is an ATC packet
|
||||||
|
# ATC format: [... service UUID 0x181A ... MAC ... data ...]
|
||||||
|
atc_identifier = data_str[6:10].upper()
|
||||||
|
if atc_identifier != "1A18":
|
||||||
|
return # Not an ATC packet
|
||||||
|
|
||||||
|
# Extract MAC from packet and verify it matches
|
||||||
|
packet_mac = data_str[10:22].upper()
|
||||||
|
mac_str = mac.replace(":", "").upper()
|
||||||
|
|
||||||
|
if packet_mac != mac_str:
|
||||||
|
return # MAC mismatch
|
||||||
|
|
||||||
|
mac_with_colons = mac.upper()
|
||||||
|
|
||||||
|
# Parse ATC data packet first to get sensor data
|
||||||
|
try:
|
||||||
|
parsed_data = self._parse_atc_data(data_str)
|
||||||
|
if not parsed_data:
|
||||||
|
return
|
||||||
|
|
||||||
|
temperature, humidity, battery_percent, battery_voltage, adv_number = parsed_data
|
||||||
|
|
||||||
|
# Check if this is a known sensor
|
||||||
|
if mac_with_colons not in self.sensor_config.sensors:
|
||||||
|
# Unknown sensor - check if we should discover it
|
||||||
|
self._handle_unknown_sensor(
|
||||||
|
mac_with_colons,
|
||||||
|
rssi,
|
||||||
|
temperature,
|
||||||
|
humidity,
|
||||||
|
battery_percent,
|
||||||
|
battery_voltage
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# Check advertisement number to avoid duplicates
|
||||||
|
if mac_str in self.adv_counter:
|
||||||
|
if self.adv_counter[mac_str] == adv_number:
|
||||||
|
return # Duplicate packet
|
||||||
|
self.adv_counter[mac_str] = adv_number
|
||||||
|
|
||||||
|
# Create measurement for known sensor
|
||||||
|
sensor_name = self.sensor_config.get_name(mac_with_colons)
|
||||||
|
measurement = Measurement(
|
||||||
|
temperature=temperature,
|
||||||
|
humidity=humidity,
|
||||||
|
voltage=battery_voltage / 1000.0,
|
||||||
|
battery=battery_percent,
|
||||||
|
rssi=rssi,
|
||||||
|
sensor_name=sensor_name,
|
||||||
|
timestamp=int(time.time())
|
||||||
|
)
|
||||||
|
|
||||||
|
# Log the measurement
|
||||||
|
logger.info(
|
||||||
|
f"{measurement.sensor_name}: {measurement.temperature}°C, "
|
||||||
|
f"{measurement.humidity}%, {measurement.voltage}V, "
|
||||||
|
f"battery {measurement.battery}%, RSSI {rssi}dBm"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Call measurement callback
|
||||||
|
if self.on_measurement:
|
||||||
|
self.on_measurement(measurement)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error parsing ATC packet from {mac}: {e}")
|
||||||
|
|
||||||
|
def _handle_unknown_sensor(self, mac: str, rssi: int, temperature: float,
|
||||||
|
humidity: int, battery_percent: int, battery_voltage: int):
|
||||||
|
"""
|
||||||
|
Handle discovery of unknown sensor.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
mac: MAC address with colons
|
||||||
|
rssi: Signal strength
|
||||||
|
temperature: Temperature reading
|
||||||
|
humidity: Humidity reading
|
||||||
|
battery_percent: Battery percentage
|
||||||
|
battery_voltage: Battery voltage in mV
|
||||||
|
"""
|
||||||
|
# Get or construct device name from MAC
|
||||||
|
# ATC sensors advertise as ATC_XXXXXX where XXXXXX is last 3 bytes
|
||||||
|
mac_suffix = mac.replace(":", "")[-6:]
|
||||||
|
device_name = f"ATC_{mac_suffix}"
|
||||||
|
|
||||||
|
# Check if already discovered
|
||||||
|
if self.discovery_manager.is_known(mac):
|
||||||
|
# Just update the discovery record
|
||||||
|
self.discovery_manager.add_or_update(
|
||||||
|
mac, device_name, rssi, temperature, humidity,
|
||||||
|
battery_percent, battery_voltage
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# New sensor - discover and notify
|
||||||
|
is_new = self.discovery_manager.add_or_update(
|
||||||
|
mac, device_name, rssi, temperature, humidity,
|
||||||
|
battery_percent, battery_voltage
|
||||||
|
)
|
||||||
|
|
||||||
|
if is_new:
|
||||||
|
logger.info(f"New sensor discovered: {mac} ({device_name})")
|
||||||
|
sensor = self.discovery_manager.sensors[mac]
|
||||||
|
self.discovery_manager.send_ntfy_notification(sensor)
|
||||||
|
|
||||||
|
def _parse_atc_data(self, data_str: str) -> Optional[tuple]:
|
||||||
|
"""
|
||||||
|
Parse ATC advertisement data.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple of (temperature, humidity, battery_percent, battery_voltage, adv_number) or None
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# Temperature: bytes 22-26, signed int16, big endian, /10
|
||||||
|
temp_hex = data_str[22:26]
|
||||||
|
temp_raw = int(temp_hex, 16)
|
||||||
|
if temp_raw & 0x8000: # Check sign bit
|
||||||
|
temp_raw = temp_raw - 0x10000
|
||||||
|
temperature = temp_raw / 10.0
|
||||||
|
|
||||||
|
# Humidity: bytes 26-28, uint8
|
||||||
|
humidity = int(data_str[26:28], 16)
|
||||||
|
|
||||||
|
# Battery: bytes 28-30, uint8
|
||||||
|
battery_percent = int(data_str[28:30], 16)
|
||||||
|
|
||||||
|
# Battery voltage: bytes 30-34, uint16, big endian, mV
|
||||||
|
battery_voltage = int(data_str[30:34], 16)
|
||||||
|
|
||||||
|
# Advertisement number: last 2 bytes
|
||||||
|
adv_number = data_str[-2:]
|
||||||
|
|
||||||
|
return (temperature, humidity, battery_percent, battery_voltage, adv_number)
|
||||||
|
|
||||||
|
except (ValueError, IndexError) as e:
|
||||||
|
logger.debug(f"Error parsing ATC data: {e}")
|
||||||
|
return None
|
||||||
10
startup.sh
10
startup.sh
@@ -1,10 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
tmux start-server
|
|
||||||
|
|
||||||
tmux new-session -d -s sensorer -n sensorer -d 'cd /home/pi/sensorpajen; ./LYWSD03MMC.py -a -wdt 5 --devicelistfile sensorer.ini --callback yes --battery'
|
|
||||||
tmux split-window -t sensorer:0 'cd /home/pi/pirate_audio; ./loop.sh'
|
|
||||||
tmux split-window -t sensorer:0 'cd /home/pi/pirate_audio; ./buttons.py'
|
|
||||||
|
|
||||||
tmux select-layout -t sensorer:0 tiled
|
|
||||||
|
|
||||||
273
systemd/README.md
Normal file
273
systemd/README.md
Normal file
@@ -0,0 +1,273 @@
|
|||||||
|
# Systemd Service Installation
|
||||||
|
|
||||||
|
## Installing Sensorpajen as a User Service
|
||||||
|
|
||||||
|
This allows sensorpajen to run automatically on boot as your user (no sudo required for management).
|
||||||
|
|
||||||
|
### Installation Steps
|
||||||
|
|
||||||
|
#### 1. Install the Service File
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd ~/sensorpajen
|
||||||
|
|
||||||
|
# Create user systemd directory if it doesn't exist
|
||||||
|
mkdir -p ~/.config/systemd/user/
|
||||||
|
|
||||||
|
# Copy service file
|
||||||
|
cp systemd/sensorpajen.service ~/.config/systemd/user/
|
||||||
|
|
||||||
|
# Reload systemd to recognize the new service
|
||||||
|
systemctl --user daemon-reload
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. Enable Lingering (Run Without Login)
|
||||||
|
|
||||||
|
This allows your user services to run even when you're not logged in:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Enable lingering for your user
|
||||||
|
sudo loginctl enable-linger $USER
|
||||||
|
|
||||||
|
# Verify it's enabled
|
||||||
|
loginctl show-user $USER | grep Linger
|
||||||
|
# Should show: Linger=yes
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. Start and Enable the Service
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Start the service now
|
||||||
|
systemctl --user start sensorpajen
|
||||||
|
|
||||||
|
# Enable it to start on boot
|
||||||
|
systemctl --user enable sensorpajen
|
||||||
|
|
||||||
|
# Check status
|
||||||
|
systemctl --user status sensorpajen
|
||||||
|
```
|
||||||
|
|
||||||
|
### Service Management Commands
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Start the service
|
||||||
|
systemctl --user start sensorpajen
|
||||||
|
|
||||||
|
# Stop the service
|
||||||
|
systemctl --user stop sensorpajen
|
||||||
|
|
||||||
|
# Restart the service
|
||||||
|
systemctl --user restart sensorpajen
|
||||||
|
|
||||||
|
# Check status
|
||||||
|
systemctl --user status sensorpajen
|
||||||
|
|
||||||
|
# View logs (all)
|
||||||
|
journalctl --user -u sensorpajen
|
||||||
|
|
||||||
|
# View logs (follow/tail)
|
||||||
|
journalctl --user -u sensorpajen -f
|
||||||
|
|
||||||
|
# View logs (last 100 lines)
|
||||||
|
journalctl --user -u sensorpajen -n 100
|
||||||
|
|
||||||
|
# View logs (since specific time)
|
||||||
|
journalctl --user -u sensorpajen --since "1 hour ago"
|
||||||
|
journalctl --user -u sensorpajen --since "2025-12-27 10:00"
|
||||||
|
|
||||||
|
# Enable service (start on boot)
|
||||||
|
systemctl --user enable sensorpajen
|
||||||
|
|
||||||
|
# Disable service (don't start on boot)
|
||||||
|
systemctl --user disable sensorpajen
|
||||||
|
```
|
||||||
|
|
||||||
|
### Viewing Logs
|
||||||
|
|
||||||
|
The service logs to systemd journal. View them with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Live view (like tail -f)
|
||||||
|
journalctl --user -u sensorpajen -f
|
||||||
|
|
||||||
|
# With timestamps
|
||||||
|
journalctl --user -u sensorpajen -f -o short-iso
|
||||||
|
|
||||||
|
# Just today's logs
|
||||||
|
journalctl --user -u sensorpajen --since today
|
||||||
|
```
|
||||||
|
|
||||||
|
### Updating the Service
|
||||||
|
|
||||||
|
After making changes to the code:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Pull latest changes
|
||||||
|
cd ~/sensorpajen
|
||||||
|
git pull origin master
|
||||||
|
|
||||||
|
# Restart the service to apply changes
|
||||||
|
systemctl --user restart sensorpajen
|
||||||
|
|
||||||
|
# Check it started correctly
|
||||||
|
systemctl --user status sensorpajen
|
||||||
|
```
|
||||||
|
|
||||||
|
After editing `sensorpajen.service`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Copy updated service file
|
||||||
|
cp systemd/sensorpajen.service ~/.config/systemd/user/
|
||||||
|
|
||||||
|
# Reload systemd configuration
|
||||||
|
systemctl --user daemon-reload
|
||||||
|
|
||||||
|
# Restart the service
|
||||||
|
systemctl --user restart sensorpajen
|
||||||
|
```
|
||||||
|
|
||||||
|
After editing configuration files:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Edit config
|
||||||
|
nano ~/sensorpajen/config/sensorpajen.env
|
||||||
|
# or
|
||||||
|
nano ~/sensorpajen/config/sensors.json
|
||||||
|
|
||||||
|
# Restart service to reload config
|
||||||
|
systemctl --user restart sensorpajen
|
||||||
|
```
|
||||||
|
|
||||||
|
### Troubleshooting
|
||||||
|
### Permission Denied Errors
|
||||||
|
|
||||||
|
If you see `PermissionError: [Errno 1] Operation not permitted` in the logs:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Verify Bluetooth capabilities are set on Python binary
|
||||||
|
getcap ~/.local/share/virtualenvs/*/bin/python3.*
|
||||||
|
|
||||||
|
# If not set, apply capabilities (adjust path to your venv):
|
||||||
|
sudo setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f ~/sensorpajen/.venv/bin/python3)
|
||||||
|
|
||||||
|
# Verify it was set:
|
||||||
|
getcap $(readlink -f ~/sensorpajen/.venv/bin/python3)
|
||||||
|
# Should show: cap_net_admin,cap_net_raw+eip
|
||||||
|
|
||||||
|
# Restart the service
|
||||||
|
systemctl --user restart sensorpajen
|
||||||
|
```
|
||||||
|
|
||||||
|
**Important**: Capabilities must be set on the **actual Python binary**, not symlinks. Use `readlink -f` to resolve the real path.
|
||||||
|
#### Service Won't Start
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Check detailed status
|
||||||
|
systemctl --user status sensorpajen
|
||||||
|
|
||||||
|
# Check logs for errors
|
||||||
|
journalctl --user -u sensorpajen -n 50
|
||||||
|
|
||||||
|
# Test the command manually
|
||||||
|
cd ~/sensorpajen
|
||||||
|
source .venv/bin/activate
|
||||||
|
export $(cat config/sensorpajen.env | grep -v '^#' | xargs)
|
||||||
|
python -m sensorpajen.main
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Bluetooth Permission Errors
|
||||||
|
|
||||||
|
Make sure capabilities are set on the Python binary:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
getcap $(readlink -f ~/.venv/bin/python3)
|
||||||
|
# Should show: cap_net_raw,cap_net_admin+eip
|
||||||
|
|
||||||
|
# If not set:
|
||||||
|
sudo setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f ~/sensorpajen/.venv/bin/python3)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Service Doesn't Start on Boot
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Check if service is enabled
|
||||||
|
systemctl --user is-enabled sensorpajen
|
||||||
|
# Should show: enabled
|
||||||
|
|
||||||
|
# Check if lingering is enabled
|
||||||
|
loginctl show-user $USER | grep Linger
|
||||||
|
# Should show: Linger=yes
|
||||||
|
|
||||||
|
# If not enabled:
|
||||||
|
systemctl --user enable sensorpajen
|
||||||
|
sudo loginctl enable-linger $USER
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Environment Variables Not Loading
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Verify environment file exists and is readable
|
||||||
|
cat ~/sensorpajen/config/sensorpajen.env
|
||||||
|
|
||||||
|
# Check file permissions
|
||||||
|
ls -la ~/sensorpajen/config/sensorpajen.env
|
||||||
|
|
||||||
|
# Test loading manually
|
||||||
|
export $(cat ~/sensorpajen/config/sensorpajen.env | grep -v '^#' | xargs)
|
||||||
|
env | grep MQTT
|
||||||
|
```
|
||||||
|
|
||||||
|
### Verifying Everything Works
|
||||||
|
|
||||||
|
After installation:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 1. Check service is running
|
||||||
|
systemctl --user status sensorpajen
|
||||||
|
|
||||||
|
# 2. Check logs show sensor data
|
||||||
|
journalctl --user -u sensorpajen -f
|
||||||
|
|
||||||
|
# 3. Check MQTT messages are being published
|
||||||
|
mosquitto_sub -h 192.168.0.114 -u hasse -P casablanca -t "MiTemperature2/#" -v
|
||||||
|
|
||||||
|
# 4. Reboot and verify it starts automatically
|
||||||
|
sudo reboot
|
||||||
|
# After reboot:
|
||||||
|
systemctl --user status sensorpajen
|
||||||
|
```
|
||||||
|
|
||||||
|
### Uninstalling the Service
|
||||||
|
|
||||||
|
If you need to remove the service:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Stop and disable
|
||||||
|
systemctl --user stop sensorpajen
|
||||||
|
systemctl --user disable sensorpajen
|
||||||
|
|
||||||
|
# Remove service file
|
||||||
|
rm ~/.config/systemd/user/sensorpajen.service
|
||||||
|
|
||||||
|
# Reload systemd
|
||||||
|
systemctl --user daemon-reload
|
||||||
|
|
||||||
|
# Optionally disable lingering
|
||||||
|
sudo loginctl disable-linger $USER
|
||||||
|
```
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
- **User Service**: Runs as your user, not root - more secure and easier to manage
|
||||||
|
- **Lingering**: Required for services to run when not logged in
|
||||||
|
- **Logs**: All output goes to systemd journal (journalctl)
|
||||||
|
- **Auto-restart**: Service restarts automatically on crashes
|
||||||
|
- **Environment**: Config loaded from `config/sensorpajen.env`
|
||||||
|
- **Working Directory**: Service runs from `~/sensorpajen`
|
||||||
|
|
||||||
|
## Next Steps
|
||||||
|
|
||||||
|
Once the service is working:
|
||||||
|
1. Monitor for a few days to ensure stability
|
||||||
|
2. Check logs occasionally: `journalctl --user -u sensorpajen --since yesterday`
|
||||||
|
3. Service will survive reboots and automatically restart on failures
|
||||||
32
systemd/sensorpajen.service
Normal file
32
systemd/sensorpajen.service
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Sensorpajen - Bluetooth Temperature Sensor Monitor
|
||||||
|
Documentation=https://github.com/yourusername/sensorpajen
|
||||||
|
After=network.target bluetooth.target
|
||||||
|
Wants=bluetooth.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
WorkingDirectory=%h/sensorpajen
|
||||||
|
EnvironmentFile=%h/sensorpajen/config/sensorpajen.env
|
||||||
|
ExecStart=%h/sensorpajen/.venv/bin/python -m sensorpajen.main
|
||||||
|
Restart=always
|
||||||
|
RestartSec=10
|
||||||
|
|
||||||
|
# Bluetooth capabilities (alternative to setcap)
|
||||||
|
# Note: This requires systemd to be run with proper permissions
|
||||||
|
# If this doesn't work, use setcap on the Python binary instead
|
||||||
|
#AmbientCapabilities=CAP_NET_RAW CAP_NET_ADMIN
|
||||||
|
|
||||||
|
# Logging
|
||||||
|
StandardOutput=journal
|
||||||
|
StandardError=journal
|
||||||
|
SyslogIdentifier=sensorpajen
|
||||||
|
|
||||||
|
# Security
|
||||||
|
# Note: NoNewPrivileges=true can prevent file capabilities from working
|
||||||
|
# We need capabilities for Bluetooth access, so we can't use it
|
||||||
|
#NoNewPrivileges=true
|
||||||
|
PrivateTmp=true
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=default.target
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
# Copyright (c) 2014 Adafruit Industries
|
|
||||||
# Author: Tony DiCola
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import Adafruit_DHT
|
|
||||||
import paho.mqtt.client as mqtt
|
|
||||||
|
|
||||||
mqttserver = "192.168.0.114"
|
|
||||||
client = mqtt.Client("koksfonstret")
|
|
||||||
client.connect(mqttserver)
|
|
||||||
|
|
||||||
# Parse command line parameters.
|
|
||||||
sensor_args = { '11': Adafruit_DHT.DHT11,
|
|
||||||
'22': Adafruit_DHT.DHT22,
|
|
||||||
'2302': Adafruit_DHT.AM2302 }
|
|
||||||
if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
|
|
||||||
sensor = sensor_args[sys.argv[1]]
|
|
||||||
pin = sys.argv[2]
|
|
||||||
else:
|
|
||||||
print('Usage: sudo ./Adafruit_DHT.py [11|22|2302] <GPIO pin number>')
|
|
||||||
print('Example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO pin #4')
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
# Try to grab a sensor reading. Use the read_retry method which will retry up
|
|
||||||
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
|
|
||||||
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
|
|
||||||
|
|
||||||
# Un-comment the line below to convert the temperature to Fahrenheit.
|
|
||||||
# temperature = temperature * 9/5.0 + 32
|
|
||||||
|
|
||||||
# Note that sometimes you won't get a reading and
|
|
||||||
# the results will be null (because Linux can't
|
|
||||||
# guarantee the timing of calls to read the sensor).
|
|
||||||
# If this happens try again!
|
|
||||||
|
|
||||||
if humidity is not None and temperature is not None:
|
|
||||||
print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity))
|
|
||||||
client.publish("casablanca/koksfonstret/temperature", temperature)
|
|
||||||
client.publish("casablanca/koksfonstret/humidity", humidity)
|
|
||||||
|
|
||||||
else:
|
|
||||||
print('Failed to get reading. Try again!')
|
|
||||||
sys.exit(1)
|
|
||||||
Reference in New Issue
Block a user