Publish sensor info as JSON on the same topic
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
parra 2023-07-19 18:18:33 +02:00
parent db6b256853
commit d999f6affa
2 changed files with 17 additions and 15 deletions

View file

@ -6,15 +6,13 @@ MQTT_PASSWORD=
MQTT_CLIENT_ID= MQTT_CLIENT_ID=
# Topic configuration # Topic configuration
MQTT_TOPIC_PREFIX=home/sensor MQTT_SENSOR_NAME=mijia-sensor-1
MQTT_TELE_PREFIX=home/tele
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_TELE_PREFIX=home/sensors/tele
MQTT_TOPIC_TEMPERATURE=${MQTT_TOPIC_PREFIX}/temperatura MQTT_TOPIC_STATE=${MQTT_TELE_PREFIX}/${MQTT_SENSOR_NAME}/state
MQTT_TOPIC_BATTERY=${MQTT_TELE_PREFIX}/${MQTT_SENSOR_NAME}/bateria
MQTT_TOPIC_STATE=${MQTT_TELE_PREFIX}/${MQTT_SENSOR_NAME}/event
MQTT_PUBLISH_DELAY=5 MQTT_PUBLISH_DELAY=5

20
main.py
View file

@ -2,6 +2,7 @@
"""MiJia GATT to MQTT""" """MiJia GATT to MQTT"""
import json
import os import os
import re import re
import time import time
@ -15,10 +16,9 @@ load_dotenv() # Cargamos las variables de entorno necesarias
PING_URL = os.getenv('PING_URL') PING_URL = os.getenv('PING_URL')
MQTT_TOPIC_HUMIDITY = os.getenv('MQTT_TOPIC_HUMIDITY') MQTT_SENSOR_NAME = os.getenv('MQTT_SENSOR_NAME')
MQTT_TOPIC_TEMPERATURE = os.getenv('MQTT_TOPIC_TEMPERATURE')
MQTT_TOPIC_BATTERY = os.getenv('MQTT_TOPIC_BATTERY')
MQTT_TOPIC_STATE = os.getenv('MQTT_TOPIC_STATE') MQTT_TOPIC_STATE = os.getenv('MQTT_TOPIC_STATE')
MQTT_TOPIC = os.getenv('MQTT_TOPIC')
MQTT_PUBLISH_DELAY = int(os.getenv('MQTT_PUBLISH_DELAY')) MQTT_PUBLISH_DELAY = int(os.getenv('MQTT_PUBLISH_DELAY'))
MQTT_CLIENT_ID = os.getenv('MQTT_CLIENT_ID') MQTT_CLIENT_ID = os.getenv('MQTT_CLIENT_ID')
@ -46,7 +46,7 @@ humidity = None
def on_connect(client, userdata, flags, rc): 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): class MyDelegate(btle.DefaultDelegate):
@ -60,7 +60,7 @@ class MyDelegate(btle.DefaultDelegate):
def main(): def main():
mqttc = mqtt.Client(MQTT_CLIENT_ID) mqttc = mqtt.Client(MQTT_CLIENT_ID)
mqttc.username_pw_set(MQTT_USER, MQTT_PASSWORD) 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.on_connect = on_connect
mqttc.connect(MQTT_SERVER, MQTT_PORT, 60) mqttc.connect(MQTT_SERVER, MQTT_PORT, 60)
@ -136,9 +136,13 @@ def fetch_sensor_data(temp_hum):
def publish_sensor_data(mqttc): def publish_sensor_data(mqttc):
mqttc.publish(MQTT_TOPIC_TEMPERATURE, temperature, 1, True) payload = {
mqttc.publish(MQTT_TOPIC_HUMIDITY, humidity, 1, True) "id": MQTT_SENSOR_NAME,
mqttc.publish(MQTT_TOPIC_BATTERY, battery, 1, True) "battery": battery,
"temperature": float(temperature),
"humidity": float(humidity)
}
mqttc.publish(MQTT_TOPIC, json.dumps(payload), 1, True)
time.sleep(MQTT_PUBLISH_DELAY) time.sleep(MQTT_PUBLISH_DELAY)