Good Day All,
I am quite new to Python scripting and recently received my PiFace CAD which is working quite nicely. I am having problems creating a loop for updating information on the Display while still checking for an input from the buttons.
I have a RPi with PiFace CAD where I soldiered two DHT-11 Temperature and Humidity sensors on and all that is working perfect. I created a PiFace CAD menu that allows me to switch the Display from Temperature to Humidity and switch the Display Backlight Off and that works as well but I cannot get the Display to update the temperature or humidity reading say every 5 minutes or so. The moment I include a While loop, the script does not read button presses anymore.
If somebody can please help me out here - I tried to Google a lot but could not find anything. Here is my full code (Copied bits and peaces from all over :-) ):
#!/usr/bin/env python3
import sys
import threading
import subprocess
from time import sleep
import pifacecad
UPDATE_INTERVAL = 5 * 1 # 5 mins
TEMP_IN = "cat /root/sen1.out | awk '{print $7}'"
TEMP_OUT = "cat /root/sen2.out | awk '{print $7}'"
HUMID_IN = "cat /root/sen1.out | awk '{print $3}'"
HUMID_OUT = "cat /root/sen2.out | awk '{print $3}'"
DATE = "date"
temperature_symbol = pifacecad.LCDBitmap(
[0x4, 0x4, 0x4, 0x4, 0xe, 0xe, 0xe, 0x0])
temp_symbol_index = 0
#class Weather(object):
def run_cmd(cmd):
return subprocess.check_output(cmd, shell=True).decode('utf-8')
def get_my_temp1():
return run_cmd(TEMP_IN)[:-1]
def get_my_temp2():
return run_cmd(TEMP_OUT)[:-1]
def get_my_humid1():
return run_cmd(HUMID_IN)[:-1]
def get_my_humid2():
return run_cmd(HUMID_OUT)[:-1]
def get_date():
return run_cmd(DATE)[11:16]
def show_tempinfo(event):
event.chip.lcd.write(str(event.pin_num))
choice = event.pin_num
if choice ==6:
cad.lcd.clear()
cad.lcd.backlight_on()
cad.lcd.write_custom_bitmap(temp_symbol_index)
cad.lcd.write("In:{}C ".format(get_my_temp1()))
cad.lcd.write("{}\n".format(get_date()))
cad.lcd.write_custom_bitmap(temp_symbol_index)
cad.lcd.write("Out:{}C".format(get_my_temp2()))
if choice ==7:
cad.lcd.clear()
cad.lcd.backlight_on()
cad.lcd.write("In:{}% ".format(get_my_humid1()))
cad.lcd.write("{}\n".format(get_date()))
cad.lcd.write("Out:{}%".format(get_my_humid2()))
if choice ==5:
cad.lcd.backlight_off()
cad = pifacecad.PiFaceCAD()
cad.lcd.store_custom_bitmap(temp_symbol_index, temperature_symbol)
cad.lcd.blink_off()
cad.lcd.cursor_off()
cad.lcd.write("Waiting for Sensors..")
switchlistener = pifacecad.SwitchEventListener(chip=cad)
for i in range(8):
switchlistener.register(i, pifacecad.IODIR_FALLING_EDGE, show_tempinfo)
switchlistener.activate()




