Thanks for the script I like it and using it to monitor T&H on a DHT22 connected to an RPi.
For some reason I receive the T&H data in the csv output but not visible in MQTT - only the Temp data.
If you have time and willing to help can you please take a look at the script?
Thanks in advance
Tom
import Adafruit_DHT
import paho.mqtt.client as paho
import os
import time
Define constants
Sensor type (Adafruit_DHT.DHT11 or Adafruit_DHT.DHT22)
def on_publish(client,userdata,result): #create function for callback
print("data published \n")
pass
client1= paho.Client("control1") #create client object
client1.on_publish = on_publish #assign function to callback
client1.connect(MQTT_BROKER,MQTT_PORT) #establish connection
try:
f = open(LOGFILE, 'a+')
if os.stat(LOGFILE).st_size == 0:
f.write('Date,Time,Sensor,Temperature,Humidity\r\n')
except:
pass
if cupboardHumidity is not None and cupboardTemperature is not None:
ret= client1.publish("akna/temperature","{0:0.1f}".format(cupboardTemperature))
ret= client1.publish("akna/humidity","{0:0.1f}".format(cupboardHumidity))
f.write('{0},{1},akna,{2:0.1f}*C,{3:0.1f}%\r\n'.format(time.strftime('%y-%m-%d'), time.strftime('%H:%M'), cupboardTemperature, cupboardHumidity))
else:
ret= client1.publish("kitchen/cupboard/temperature","FAILED")
print("Failed to retrieve data from cupboard sensor")
if floorHumidity is not None and floorTemperature is not None:
Hi Jonathan,
Thanks for the script I like it and using it to monitor T&H on a DHT22 connected to an RPi. For some reason I receive the T&H data in the csv output but not visible in MQTT - only the Temp data. If you have time and willing to help can you please take a look at the script?
Thanks in advance Tom
import Adafruit_DHT import paho.mqtt.client as paho import os import time
Define constants
Sensor type (Adafruit_DHT.DHT11 or Adafruit_DHT.DHT22)
DHT_SENSOR = Adafruit_DHT.DHT22
Configure GPIO pins
CUPBOARD_PIN = 2
FLOOR_PIN = 4
WASHINGMACHINE_PIN = 27
MQTT details
MQTT_BROKER="192.168.1.131" MQTT_PORT=1883
Output file name
LOGFILE = "/home/pi/sensors.csv"
##########################################################
Only edit below if you know what you're doing!
##########################################################
def on_publish(client,userdata,result): #create function for callback print("data published \n") pass client1= paho.Client("control1") #create client object client1.on_publish = on_publish #assign function to callback client1.connect(MQTT_BROKER,MQTT_PORT) #establish connection
try: f = open(LOGFILE, 'a+') if os.stat(LOGFILE).st_size == 0: f.write('Date,Time,Sensor,Temperature,Humidity\r\n') except: pass
cupboardHumidity, cupboardTemperature = Adafruit_DHT.read_retry(DHT_SENSOR, CUPBOARD_PIN)
floorHumidity, floorTemperature = Adafruit_DHT.read_retry(DHT_SENSOR, FLOOR_PIN)
washingMachineHumidity, washingMachineTemperature = Adafruit_DHT.read_retry(DHT_SENSOR, WASHINGMACHINE_PIN)
if cupboardHumidity is not None and cupboardTemperature is not None: ret= client1.publish("akna/temperature","{0:0.1f}".format(cupboardTemperature)) ret= client1.publish("akna/humidity","{0:0.1f}".format(cupboardHumidity)) f.write('{0},{1},akna,{2:0.1f}*C,{3:0.1f}%\r\n'.format(time.strftime('%y-%m-%d'), time.strftime('%H:%M'), cupboardTemperature, cupboardHumidity)) else: ret= client1.publish("kitchen/cupboard/temperature","FAILED") print("Failed to retrieve data from cupboard sensor")
if floorHumidity is not None and floorTemperature is not None:
else:
if washingMachineHumidity is not None and washingMachineTemperature is not None:
else:
def on_disconnect(client, userdata, rc): print("client disconnected ok") client1.on_disconnect = on_disconnect client1.disconnect()