initial commit with a working version

This commit is contained in:
robert 2021-01-19 06:49:01 +00:00
parent 641e140099
commit f5b06bcafb

56
mqtt_publish_temp.py Normal file
View File

@ -0,0 +1,56 @@
import time
import datetime
import subprocess
# Import Adafruit_DHT
import Adafruit_DHT
# 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
# DHT config
DHT_PIN = 4 # GPIO nr
DHT_SENSOR = Adafruit_DHT.DHT22
# connection event
def on_connect(client, data, flags, rc):
print('Connected: ' + str(rc))
# create the MQTT client
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'
# client connection
#client.username_pw_set(mqtt_username, mqtt_password) # MQTT server credentials
client.connect(mqtt_broker) # MQTT server address
client.loop_start()
while True:
# Get Temperature from DHT
_, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
# Get Humidity from DHT
humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
# Publish our data
client.publish(temperature_topic, temperature)
client.publish(humidity_topic, humidity)
time.sleep(30)
# Output data to screen
# print temperature
# print humidity