Compare commits

...

10 Commits
0.0.2 ... main

Author SHA1 Message Date
parra 32b2ee190f Fix loop bug when publishing data 🐛
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2024-01-20 13:05:22 +01:00
parra c61718f811 Update README to add an example of a published message in JSON format 📝
continuous-integration/drone/push Build is passing Details
2023-10-05 11:10:35 +02:00
parra d999f6affa Publish sensor info as JSON on the same topic
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2023-07-19 18:18:33 +02:00
parra db6b256853 Added URL ping to notify when scripts finish OK
continuous-integration/drone Build is passing Details
continuous-integration/drone/tag Build is passing Details
2022-10-07 18:01:50 +02:00
parra 022f9df89a Removed trigger filter in build pipeline
continuous-integration/drone Build is passing Details
continuous-integration/drone/push Build is passing Details
2022-10-07 12:07:12 +02:00
parra abe1814825 Updated CI badge in README.md 2022-10-07 12:07:12 +02:00
parra 3078a1ac51 Update Drone secrets 2022-10-07 12:07:12 +02:00
parra 9ac2f44c87 Updated drone badge links 2022-10-07 12:07:12 +02:00
parra f5cdda5bfd Added Drone badge 2022-10-07 12:07:12 +02:00
parra 1dd29d6658 Added mirror registry to Python plugin 2022-10-07 12:07:12 +02:00
4 changed files with 50 additions and 24 deletions

View File

@ -3,14 +3,18 @@ name: build
steps:
- name: Build
image: python:3
image: registry.cuzo.dev/library/python:3
commands:
- pip install -r requirements.txt
# - pytest
image_pull_secrets:
- custom_mirror_registry
trigger:
event:
- push
exclude:
- pull_request
---
kind: pipeline
@ -21,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:

View File

@ -1,5 +1,7 @@
# Bluetooth LE to MQTT bridge for the Xiaomi Mijia Temperature & Humidity sensor
[![Build Status](https://ci.cuzo.dev/api/badges/parra/mijia-sensors/status.svg)](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
}
```

View 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
View File

@ -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)