58 lines
1.5 KiB
Python
58 lines
1.5 KiB
Python
|
import json
|
||
|
import sys
|
||
|
import os
|
||
|
|
||
|
# Import MQTT client.
|
||
|
import paho.mqtt.publish as publish
|
||
|
import paho.mqtt.client as paho
|
||
|
|
||
|
# MQTT credentials
|
||
|
#mqtt_username = 'homeassistant' # MQTT client username
|
||
|
#mqtt_password = '3355' # MQTT client password
|
||
|
#client_id = 'temperature' # unique client_id
|
||
|
mqtt_broker = "raspberrypi" # broker address, usually your HASS IP address
|
||
|
|
||
|
# create the MQTT client
|
||
|
client = paho.Client(protocol=paho.MQTTv31) # * set a random string (max 23 chars)
|
||
|
|
||
|
# Command topic
|
||
|
#topic = 'weathersensors/garden'
|
||
|
|
||
|
# client connection
|
||
|
#client.username_pw_set(mqtt_username, mqtt_password) # MQTT server credentials
|
||
|
client.connect(mqtt_broker, 1883, 60) # MQTT server address
|
||
|
|
||
|
#client.loop_start()
|
||
|
process = os.popen('/usr/local/bin/rtl_433 -R 42 -R 03 -F json -T 65 -M time:utc:usec')
|
||
|
|
||
|
processData = process.read().split('\n')
|
||
|
|
||
|
print("processData: ")
|
||
|
print(processData)
|
||
|
|
||
|
for i in processData:
|
||
|
print("i: ")
|
||
|
print(len(i))
|
||
|
print(i)
|
||
|
|
||
|
if len(i) > 0:
|
||
|
jsonData = (json.loads(i))
|
||
|
|
||
|
model = str(jsonData['model'])
|
||
|
if model == "Hideki-TS04":
|
||
|
topic = 'weathersensors/garden'
|
||
|
else:
|
||
|
topic = 'weathersensors/pool'
|
||
|
|
||
|
time = str(jsonData['time'])
|
||
|
battery = str(jsonData['battery_ok'])
|
||
|
temperature = str(jsonData['temperature_C'])
|
||
|
humidity = str(jsonData['humidity'])
|
||
|
|
||
|
client.publish(topic + "/time", "" + time)
|
||
|
client.publish(topic + "/battery", "" + battery)
|
||
|
client.publish(topic + "/temperature", "" + temperature)
|
||
|
client.publish(topic + "/humidity", "" + humidity)
|
||
|
|
||
|
process.close()
|