#!/usr/bin/python
#
import sys
from Adafruit_CharLCD import Adafruit_CharLCD
import Adafruit_DHT
import time
import threading
def readSensor():
''' Read DHT sensor and return formated result string '''
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 4)
if humidity is not None and temperature is not None:
str = ('Temp: {0:0.1f}*c#Humidity: {1:0.1f}%'.format(temperature, humidity))
return str.split('#')
else:
print('Failed to get reading. Try again!')
return None, None
def saveData(mydata):
''' Save my results append to file '''
fo = open('myresults.txt', 'a')
fo.write(mydata)
fo.close()
def myLoop():
'''Main thread Loop, replace while and sleep with kivvy Clock calls from within your script'''
while True:
t, h = readSensor()
lcd.clear()
lcd.message( t + "\n" + h)
saveData(t + " : " + h + "\r\n")
time.sleep(1)
def runSensors():
''' Starting a loop using threading '''
proc = threading.Thread( name = "Run Loop", target = myLoop )
proc.daemon = True
proc.start()
if __name__ == '__main__':
lcd = Adafruit_CharLCD.Adafruit_CharLCD()
lcd.clear()
runSensors()
count = 0
while True: # A loop to keep the thread alive
count +=1
print "Just counting... ", count
time.sleep(0.5)