Hi Everybody,
I’m a brand new Ruuvitags user in Switzerland.
I use serveral other BLE devices (ordered from China mostly) and it’s a pleasure to finaly have some BLE equipements which works fine out of the box !
I’m not an IT, so please be indulgent with my small piece of code
I wanted to have a simple MQTT publisher of my Ruuvitags, so here it comes, in Python.
It’s not perfect, probably really dirty but it works, but If it could help some others persons.
import paho.mqtt.client as mqtt
import os
from ruuvitag_sensor.ruuvi import RuuviTagSensor
def publish_mqtt_ruuvi():
# Declare MQTT Broker
mqtt_topic_root="home/sensor/ruuvi/"
mqtt_client = mqtt.Client("MY_MQTT_CLIENT")
mqtt_client.username_pw_set("USERNAME","PASSWORD")
mqtt_client.connect("MY_MQTT_BROKER_IP")
mqtt_client.disconnect()
macs = ['AA:AA:AA:AA:AA','AA:AA:AA:AA:AB','AA:AA:AA:AA:AC','AA:AA:AA:AA:AD','AA:AA:AA:AA:AE','AA:AA:AA:AA:AF']
# get_data_for_sensors will look data for the duration of timeout_in_sec
timeout_in_sec = 15
datas = RuuviTagSensor.get_data_for_sensors(macs, timeout_in_sec)
# Dictionary will have lates data for each sensor
for mac, data in datas.items():
if mac == "AA:AA:AA:AA:AA":
topic = mqtt_topic_root + "room1"
elif mac == "AA:AA:AA:AA:AB":
topic = mqtt_topic_root + "room2"
elif mac == "AA:AA:AA:AA:AC":
topic = mqtt_topic_root + "room3"
elif mac == "AA:AA:AA:AA:AD":
topic = mqtt_topic_root + "room3"
elif mac == "AA:AA:AA:AA:AE":
topic = mqtt_topic_root + "room4"
elif mac == "AA:AA:AA:AA:AF":
topic = mqtt_topic_root + "room5"
for field, value in data.items():
if field == "temperature" or field == "humidity" or field == "pressure":
mqtt_client.reconnect()
mqtt_client.publish(topic + "/" + field, str(value), qos=1)
mqtt_client.disconnect()