Compare commits
11 commits
Author | SHA1 | Date | |
---|---|---|---|
32b2ee190f | |||
c61718f811 | |||
d999f6affa | |||
db6b256853 | |||
022f9df89a | |||
abe1814825 | |||
3078a1ac51 | |||
9ac2f44c87 | |||
f5cdda5bfd | |||
1dd29d6658 | |||
d163faedcd |
4 changed files with 63 additions and 22 deletions
27
.drone.yml
27
.drone.yml
|
@ -1,4 +1,23 @@
|
|||
kind: pipeline
|
||||
name: build
|
||||
|
||||
steps:
|
||||
- name: Build
|
||||
image: registry.cuzo.dev/library/python:3
|
||||
commands:
|
||||
- pip install -r requirements.txt
|
||||
# - pytest
|
||||
|
||||
image_pull_secrets:
|
||||
- custom_mirror_registry
|
||||
|
||||
trigger:
|
||||
event:
|
||||
exclude:
|
||||
- pull_request
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: Create release from tag
|
||||
steps:
|
||||
|
@ -6,16 +25,14 @@ steps:
|
|||
image: registry.cuzo.dev/plugins/gitea-release
|
||||
settings:
|
||||
api_key:
|
||||
from_secret: DRONE_API_KEY
|
||||
base_url: https://git.cuzo.dev
|
||||
title:
|
||||
from_secret: DRONE_SEMVER
|
||||
from_secret: drone_api_key
|
||||
base_url: https://git.parravidales.es
|
||||
when:
|
||||
ref:
|
||||
- refs/tags/*
|
||||
|
||||
image_pull_secrets:
|
||||
- PRIVATE_DOCKER_REGISTRY
|
||||
- custom_mirror_registry
|
||||
|
||||
trigger:
|
||||
event:
|
||||
|
|
13
README.md
13
README.md
|
@ -1,5 +1,7 @@
|
|||
# Bluetooth LE to MQTT bridge for the Xiaomi Mijia Temperature & Humidity sensor
|
||||
|
||||
[](https://ci.cuzo.dev/parra/mijia-sensors)
|
||||
|
||||
## Create environment file
|
||||
|
||||
Copy the `env_file` to `.env` and open it. Complete all environment variables:
|
||||
|
@ -10,6 +12,7 @@ Copy the `env_file` to `.env` and open it. Complete all environment variables:
|
|||
- `MQTT_SENSOR_NAME` contains the sensor name, useful to split the telemetry data if you have more than one sensors.
|
||||
- `MQTT_PUBLISH_DELAY` specify, in seconds, how many time should wait since the script take the measurements to publish in the broker
|
||||
- `MIJIA_BTLE_ADDRESS` constant with the BLE address of your Mijia device.. This can be retrieved activating the pairing mode in the sensor and scanning the BT devices
|
||||
- `PING_URL` used to ping a monitoring service if everything was OK (Useful, for example, for monitor the script with Uptime Kuma)
|
||||
|
||||
## Install dependencies
|
||||
|
||||
|
@ -32,3 +35,13 @@ Or you can add a new entry in the `crontab`, like:
|
|||
```sh
|
||||
*/20 * * * * /usr/bin/python3 ~/scripts/mijia-temperature/main.py >~/scripts/mijia-temperature/last.log 2>&1
|
||||
```
|
||||
|
||||
The published message will have this structure:
|
||||
```json
|
||||
{
|
||||
"id": "mijia-sensor-1",
|
||||
"battery": 74,
|
||||
"temperature": 21.4,
|
||||
"humidity": 47.3
|
||||
}
|
||||
```
|
||||
|
|
17
env_file
17
env_file
|
@ -6,17 +6,18 @@ MQTT_PASSWORD=
|
|||
MQTT_CLIENT_ID=
|
||||
|
||||
# Topic configuration
|
||||
MQTT_TOPIC_PREFIX=home/sensor
|
||||
MQTT_TELE_PREFIX=home/tele
|
||||
MQTT_SENSOR_NAME=mijia-sensor-1
|
||||
|
||||
MQTT_SENSOR_NAME=mijia-salon
|
||||
MQTT_TOPIC_PREFIX=r2m
|
||||
MQTT_TOPIC=${MQTT_TOPIC_PREFIX}/${MQTT_SENSOR_NAME}
|
||||
|
||||
MQTT_TOPIC_HUMIDITY=${MQTT_TOPIC_PREFIX}/humedad
|
||||
MQTT_TOPIC_TEMPERATURE=${MQTT_TOPIC_PREFIX}/temperatura
|
||||
MQTT_TOPIC_BATTERY=${MQTT_TELE_PREFIX}/${MQTT_SENSOR_NAME}/bateria
|
||||
MQTT_TOPIC_STATE=${MQTT_TELE_PREFIX}/${MQTT_SENSOR_NAME}/event
|
||||
MQTT_TELE_PREFIX=home/sensors/tele
|
||||
MQTT_TOPIC_STATE=${MQTT_TELE_PREFIX}/${MQTT_SENSOR_NAME}/state
|
||||
|
||||
MQTT_PUBLISH_DELAY=5
|
||||
|
||||
# Sensor configuration
|
||||
MIJIA_BTLE_ADDRESS=
|
||||
MIJIA_BTLE_ADDRESS=
|
||||
|
||||
# Ping URL (Uptime Kuma, for example)
|
||||
PING_URL=
|
||||
|
|
28
main.py
28
main.py
|
@ -2,9 +2,11 @@
|
|||
|
||||
"""MiJia GATT to MQTT"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
from dotenv import load_dotenv
|
||||
import paho.mqtt.client as mqtt
|
||||
|
@ -12,10 +14,11 @@ from bluepy import btle
|
|||
|
||||
load_dotenv() # Cargamos las variables de entorno necesarias
|
||||
|
||||
MQTT_TOPIC_HUMIDITY = os.getenv('MQTT_TOPIC_HUMIDITY')
|
||||
MQTT_TOPIC_TEMPERATURE = os.getenv('MQTT_TOPIC_TEMPERATURE')
|
||||
MQTT_TOPIC_BATTERY = os.getenv('MQTT_TOPIC_BATTERY')
|
||||
PING_URL = os.getenv('PING_URL')
|
||||
|
||||
MQTT_SENSOR_NAME = os.getenv('MQTT_SENSOR_NAME')
|
||||
MQTT_TOPIC_STATE = os.getenv('MQTT_TOPIC_STATE')
|
||||
MQTT_TOPIC = os.getenv('MQTT_TOPIC')
|
||||
|
||||
MQTT_PUBLISH_DELAY = int(os.getenv('MQTT_PUBLISH_DELAY'))
|
||||
MQTT_CLIENT_ID = os.getenv('MQTT_CLIENT_ID')
|
||||
|
@ -43,7 +46,7 @@ humidity = None
|
|||
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
client.publish(MQTT_TOPIC_STATE, 'connected', 1, True)
|
||||
client.publish(MQTT_TOPIC_STATE, 'online', 1, True)
|
||||
|
||||
|
||||
class MyDelegate(btle.DefaultDelegate):
|
||||
|
@ -57,7 +60,7 @@ class MyDelegate(btle.DefaultDelegate):
|
|||
def main():
|
||||
mqttc = mqtt.Client(MQTT_CLIENT_ID)
|
||||
mqttc.username_pw_set(MQTT_USER, MQTT_PASSWORD)
|
||||
mqttc.will_set(MQTT_TOPIC_STATE, 'disconnected', 1, True)
|
||||
mqttc.will_set(MQTT_TOPIC_STATE, 'offline', 1, True)
|
||||
mqttc.on_connect = on_connect
|
||||
|
||||
mqttc.connect(MQTT_SERVER, MQTT_PORT, 60)
|
||||
|
@ -96,7 +99,10 @@ def main():
|
|||
publish_sensor_data(mqttc)
|
||||
last_msg_time = time.time()
|
||||
reset_variables()
|
||||
break
|
||||
if PING_URL:
|
||||
urllib.request.urlopen(PING_URL).read()
|
||||
print("Done.")
|
||||
return 0
|
||||
|
||||
except (btle.BTLEDisconnectError, IOError):
|
||||
print("Disconnected :(")
|
||||
|
@ -131,9 +137,13 @@ def fetch_sensor_data(temp_hum):
|
|||
|
||||
|
||||
def publish_sensor_data(mqttc):
|
||||
mqttc.publish(MQTT_TOPIC_TEMPERATURE, temperature, 1, True)
|
||||
mqttc.publish(MQTT_TOPIC_HUMIDITY, humidity, 1, True)
|
||||
mqttc.publish(MQTT_TOPIC_BATTERY, battery, 1, True)
|
||||
payload = {
|
||||
"id": MQTT_SENSOR_NAME,
|
||||
"battery": battery,
|
||||
"temperature": float(temperature),
|
||||
"humidity": float(humidity)
|
||||
}
|
||||
mqttc.publish(MQTT_TOPIC, json.dumps(payload), 1, True)
|
||||
time.sleep(MQTT_PUBLISH_DELAY)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue