Paso 4: código
Here is "my" code to query the sensors for values and update the LCD. It can be made better and I hope to do so in the future. You will want to save this as a file, and add it to your /etc/rc.local or create a startup script.
#!/usr/bin/pythonfrom Adafruit_CharLCD import Adafruit_CharLCD from subprocess import * from time import sleep, strftime from datetime import datetime import subprocess import re import sys import time #import MySQLdb as mdb # later feature #import time #import datetime
lcd = Adafruit_CharLCD()
lcd.begin(16,1)
while 1: output1 = subprocess.check_output(["./Adafruit_DHT", "2302", "4"]); matches = re.search("Temp =\s+([0-9.]+)", output1) if (not matches): time.sleep(10) continue ctemp = float(matches.group(1)) ftemp = ctemp * 9/5.0 + 32
# search for humidity printout matches = re.search("Hum =\s+([0-9.]+)", output1) if (not matches): time.sleep(10) continue humidity = float(matches.group(1)) #adjust value for accuracy humidity = humidity - 9
output2 = subprocess.check_output(["./Adafruit_DHT", "2302", "18"]); matches = re.search("Temp =\s+([0-9.]+)", output2) if (not matches): time.sleep(10) continue ctemp2 = float(matches.group(1)) ftemp2 = ctemp2 * 9/5.0 + 32
# search for humidity printout matches = re.search("Hum =\s+([0-9.]+)", output2) if (not matches): time.sleep(10) continue humidity2 = float(matches.group(1)) #adjust value for accuracy humidity2 = humidity2 - 15
lcd.clear() #lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n')) lcd.message('1 ' + 'T %s' % ( ftemp ) + ' H %s' % ( humidity ) + '\n' ) lcd.message('2 ' + 'T %s' % ( ftemp2 ) + ' H %s' % ( humidity2 ) )