2021-01-19 06:49:01 +00:00
|
|
|
import time
|
2021-01-24 22:20:52 +00:00
|
|
|
from datetime import datetime
|
2021-01-19 06:49:01 +00:00
|
|
|
import subprocess
|
2021-01-24 21:55:07 +00:00
|
|
|
import sys
|
2021-01-19 06:49:01 +00:00
|
|
|
|
|
|
|
# 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
|
2021-01-24 21:55:07 +00:00
|
|
|
client_id = sys.argv[1] # unique client_id
|
2021-01-19 06:49:01 +00:00
|
|
|
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
|
2021-01-24 21:55:07 +00:00
|
|
|
temperature_topic = 'weathersensors/' + client_id + '/temperature'
|
|
|
|
humidity_topic = 'weathersensors/' + client_id + '/humidity'
|
2021-01-24 22:28:22 +00:00
|
|
|
time_topic = 'weathersensors/' + client_id + '/time'
|
2021-01-19 06:49:01 +00:00
|
|
|
|
2021-01-24 21:55:07 +00:00
|
|
|
#client connection
|
2021-01-19 06:49:01 +00:00
|
|
|
#client.username_pw_set(mqtt_username, mqtt_password) # MQTT server credentials
|
|
|
|
client.connect(mqtt_broker) # MQTT server address
|
|
|
|
|
|
|
|
client.loop_start()
|
|
|
|
|
|
|
|
while True:
|
2021-02-15 22:17:09 +00:00
|
|
|
# DATETIME
|
|
|
|
now = datetime.now()
|
|
|
|
#print("now =", now)
|
|
|
|
#Output format: DD/MM/YYYY H:M:S
|
|
|
|
dt_string = now.strftime("%d-%m-%Y %H:%M:%S")
|
2021-01-19 06:49:01 +00:00
|
|
|
|
|
|
|
# Get Temperature from DHT
|
|
|
|
_, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
|
2021-02-15 22:17:09 +00:00
|
|
|
|
|
|
|
# cut temperature to xx.xx if more then tow digits after "."
|
|
|
|
t_split = temperature.split(".")
|
|
|
|
if len(t_split[1]) > 2:
|
|
|
|
t_cut = len(t_split[1]) - 2
|
|
|
|
t_split[1] = t_split[1][:-t_cut]
|
|
|
|
temperature = t_split[0] + "." + t_split[1]
|
2021-01-19 06:49:01 +00:00
|
|
|
# Get Humidity from DHT
|
|
|
|
humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
|
2021-02-15 22:17:09 +00:00
|
|
|
# cut humidity to xx.xx if more then tow digits after "."
|
|
|
|
h_split = humidity.split(".")
|
|
|
|
if len(h_split[1]) > 2:
|
|
|
|
h_cut = len(h_split[1]) - 2
|
|
|
|
h_split[1] = h_split[1][:-h_cut]
|
|
|
|
humidity = h_split[0] + "." + h_split[1]
|
2021-01-19 06:49:01 +00:00
|
|
|
# Publish our data
|
|
|
|
client.publish(temperature_topic, temperature)
|
|
|
|
client.publish(humidity_topic, humidity)
|
2021-01-24 22:11:08 +00:00
|
|
|
client.publish(time_topic, dt_string)
|
2021-01-19 06:49:01 +00:00
|
|
|
|
2021-02-14 21:01:24 +00:00
|
|
|
#Output data to screen
|
2021-02-14 21:24:28 +00:00
|
|
|
print(dt_string)
|
|
|
|
print(temperature)
|
|
|
|
print(humidity)
|
2021-02-14 21:06:48 +00:00
|
|
|
|
|
|
|
time.sleep(30)
|