Skip to content

Instantly share code, notes, and snippets.

@lepinkainen
Last active June 8, 2020 06:19
Show Gist options
  • Save lepinkainen/d00e8ce5d74dfd563f97f1b9f70745ea to your computer and use it in GitHub Desktop.
Save lepinkainen/d00e8ce5d74dfd563f97f1b9f70745ea to your computer and use it in GitHub Desktop.
Ruuvitag to MQTT as JSON
# Installation
# sudo apt-get install libglib2.0-dev
# sudo pip install git+https://github.com/kipe/ruuvitag.git
# sudo pip install paho-mqtt
#
# Run with: sudo python3 tag_to_mqtt.py
#
# Run this script in a while loop or from cron
from ruuvitag import RuuviTag
from paho.mqtt.publish import single
import json
def scan():
# Scan for 30 seconds and report tags
for tag in RuuviTag.scan(timeout=30):
data = tag.as_dict()
# Last seen is a python datetime, skip it since it won't go in to MQTT
data.pop('last_seen')
# send MQTT message to topic ruuvitag with payload as the sensor data in JSON
single('ruuvitag', payload=json.dumps(data), hostname='mqtt.server.address')
if __name__ == "__main__":
print("Scanning...")
try:
scan()
except KeyboardInterrupt:
print("Stopped by keyboard interrupt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment