Commit 8827f2b8 authored by julric's avatar julric

check if temp-file exists

parent d7d2c9b1
......@@ -21,12 +21,14 @@ filename=dht22
enabled=on
measurement=humidity
pinNumber=4
tempfile=sensors/dht22.tmp
[DHT22-temp]
filename=dht22
enabled=on
measurement=temp
pinNumber=4
tempfile=sensors/dht22.tmp
[LDR]
filename=analogue
......
# original code by Tom Hartley http://airpi.es
# additional code by Peter Albeda http://www.albeda.net
import sensor
import dhtreader
import time
import ConfigParser
import os
class DHT22(sensor.Sensor):
requiredData = ["measurement","pinNumber"]
requiredData = ["measurement","pinNumber","tempfile"]
optionalData = ["unit"]
def __init__(self,data):
dhtreader.init()
dhtreader.tempfile = data["tempfile"]
if os.path.isfile(dhtreader.tempfile):
ini_file = ConfigParser.ConfigParser()
ini_file.read(dhtreader.tempfile)
t = float(ini_file.get('saved_values','last_temp'))
h = float(ini_file.get('saved_values','last_humi'))
dhtreader.lastData = (t,h)
dhtreader.lastDataTime = float(ini_file.get('saved_values','last_time'))
else:
dhtreader.lastDataTime = 0
dhtreader.lastData = (None,None)
self.sensorName = "DHT22"
self.pinNum = int(data["pinNumber"])
if "temp" in data["measurement"].lower():
......@@ -32,6 +48,16 @@ class DHT22(sensor.Sensor):
tim = time.time()
try:
t, h = dhtreader.read(22,self.pinNum)
ini_file = ConfigParser.RawConfigParser()
cfgfile = open(dhtreader.tempfile,'w')
ini_file.add_section('saved_values')
ini_file.set('saved_values','last_temp',str(t))
ini_file.set('saved_values','last_humi',str(h))
ini_file.set('saved_values','last_time',str(time.time()))
ini_file.write(cfgfile)
cfgfile.close()
except Exception:
t, h = dhtreader.lastData
dhtreader.lastData = (t,h)
......@@ -43,3 +69,4 @@ class DHT22(sensor.Sensor):
return temp
elif self.valName == "Relative_Humidity":
return h
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment