Publish sensor info as JSON on the same topic ⚡
This commit is contained in:
parent
db6b256853
commit
d999f6affa
2 changed files with 17 additions and 15 deletions
12
env_file
12
env_file
|
@ -6,15 +6,13 @@ 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
|
||||
|
||||
|
|
20
main.py
20
main.py
|
@ -2,6 +2,7 @@
|
|||
|
||||
"""MiJia GATT to MQTT"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
|
@ -15,10 +16,9 @@ load_dotenv() # Cargamos las variables de entorno necesarias
|
|||
|
||||
PING_URL = os.getenv('PING_URL')
|
||||
|
||||
MQTT_TOPIC_HUMIDITY = os.getenv('MQTT_TOPIC_HUMIDITY')
|
||||
MQTT_TOPIC_TEMPERATURE = os.getenv('MQTT_TOPIC_TEMPERATURE')
|
||||
MQTT_TOPIC_BATTERY = os.getenv('MQTT_TOPIC_BATTERY')
|
||||
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')
|
||||
|
@ -46,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):
|
||||
|
@ -60,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)
|
||||
|
@ -136,9 +136,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…
Reference in a new issue