From 02e6c84a442d0904923625782a6d69a075605c16 Mon Sep 17 00:00:00 2001 From: robert Date: Sun, 24 Jan 2021 22:55:07 +0100 Subject: [PATCH] upgrade to use parameters add round function --- README.md | 1 + mqtt_publish_temp.py | 15 ++++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ac65096..1c0ba6b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # raspberrypi-temp-sensors +run the script with "/usr/bin/python /home/pi/mqtt/mqtt_publish_temp.py \ No newline at end of file diff --git a/mqtt_publish_temp.py b/mqtt_publish_temp.py index a9aacce..d82a6da 100644 --- a/mqtt_publish_temp.py +++ b/mqtt_publish_temp.py @@ -1,6 +1,7 @@ import time import datetime import subprocess +import sys # Import Adafruit_DHT import Adafruit_DHT @@ -12,7 +13,7 @@ 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 +client_id = sys.argv[1] # unique client_id mqtt_broker = "raspberrypi" # broker address, usually your HASS IP address # DHT config @@ -27,10 +28,10 @@ def on_connect(client, data, flags, rc): client = paho.Client(protocol=paho.MQTTv31) # * set a random string (max 23 chars) # Command topic -temperature_topic = 'weathersensors/emily/temperature' -humidity_topic = 'weathersensors/emily/humidity' +temperature_topic = 'weathersensors/' + client_id + '/temperature' +humidity_topic = 'weathersensors/' + client_id + '/humidity' -# client connection +#client connection #client.username_pw_set(mqtt_username, mqtt_password) # MQTT server credentials client.connect(mqtt_broker) # MQTT server address @@ -40,9 +41,10 @@ while True: # Get Temperature from DHT _, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN) - + temperature = round(temperature, 2) # Get Humidity from DHT humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN) + humidity = round(humidity, 2) # Publish our data client.publish(temperature_topic, temperature) @@ -52,5 +54,4 @@ while True: # Output data to screen # print temperature - # print humidity - + # print humidity \ No newline at end of file