在学校,我们目前正在与Raspberry Pi和GrovePi合作。对于一个小项目,我们正在尝试使其工作。我们正在使用包含的脚本作为基础,因为这是我们第一次使用Python。现在我们得到了这个错误;
Traceback (most recent call last):
File "/home/pi/GrovePi/Projects/Home_Weather_Display/Home_Weather_Display.py", line 74, in <module>
setText_norefresh("Temp:" + t + "C\n" + "Humidity :" + h + "%")
NameError: name 'setText_norefresh' is not defined
因此,我们试图使此脚本正常工作,它是一个将温度和湿度显示给Grove-LCD RGB背光的脚本。我希望有人能帮助我们。
from grovepi import *
from grove_rgb_lcd import *
from time import sleep
from math import isnan
def set_text(text):
e.delete(0,END)
e.insert(0,text)
return
dht_sensor_port = 7 # connect the DHt sensor to port 7
dht_sensor_type = 0 # use 0 for the blue-colored sensor and 1 for the white-colored sensor
# set green as backlight color
# we need to do it just once
# setting the backlight color once reduces the amount of data transfer over the I2C line
while True:
try:
# get the temperature and Humidity from the DHT sensor
[ temp,hum ] = dht(dht_sensor_port,dht_sensor_type)
print("temp =", temp, "C\thumidity =", hum,"%")
# check if we have nans
# if so, then raise a type error exception
if isnan(temp) is True or isnan(hum) is True:
raise TypeError('nan error')
t = str(temp)
h = str(hum)
# instead of inserting a bunch of whitespace, we can just insert a \n
# we're ensuring that if we get some strange strings on one line, the 2nd one won't be affected
setText_norefresh("Temp:" + t + "C\n" + "Humidity :" + h + "%")
except (IOError, TypeError) as e:
print(str(e))
# and since we got a type error
# then reset the LCD's text
setText("")
except KeyboardInterrupt as e:
print(str(e))
# since we're exiting the program
# it's better to leave the LCD with a blank text
setText("")
break
# wait some time before re-updating the LCD
sleep(0.05)