joncojonathan / kitchensensors Goto Github PK
View Code? Open in Web Editor NEWScripts to obtain data from sensors connected to a Raspberry Pi Zero.
License: MIT License
Scripts to obtain data from sensors connected to a Raspberry Pi Zero.
License: MIT License
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
DHT_SENSOR = Adafruit_DHT.DHT22
CUPBOARD_PIN = 2
#FLOOR_PIN = 4
#WASHINGMACHINE_PIN = 27
MQTT_BROKER="192.168.1.131"
MQTT_PORT=1883
LOGFILE = "/home/pi/sensors.csv"
##########################################################
##########################################################
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:
ret= client1.publish("kitchen/floor/temperature","{0:0.1f}".format(floorTemperature))
ret= client1.publish("kitchen/floor/humidity","{0:0.1f}".format(floorHumidity))
f.write('{0},{1},Floor,{2:0.1f}*C,{3:0.1f}%\r\n'.format(time.strftime('%y-%m-%d'), time.strftime('%H:%M'), floorTemperature, floorHumidity))
#else:
ret= client1.publish("kitchen/floor/temperature","FAILED")
print("Failed to retrieve data from floor sensor")
#if washingMachineHumidity is not None and washingMachineTemperature is not None:
ret= client1.publish("kitchen/washing-machine/temperature","{0:0.1f}".format(washingMachineTemperature))
ret= client1.publish("kitchen/washing-machine/humidity","{0:0.1f}".format(washingMachineHumidity))
f.write('{0},{1},Washing-Machine,{2:0.1f}*C,{3:0.1f}%\r\n'.format(time.strftime('%y-%m-%d'), time.strftime('%H:%M'), washingMachineTemperature, washignMachineHumidity))
#else:
ret= client1.publish("kitchen/washing-machine/temperature","FAILED")
print("Failed to retrieve data from washing-machine sensor")
def on_disconnect(client, userdata, rc):
print("client disconnected ok")
client1.on_disconnect = on_disconnect
client1.disconnect()
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.