此程序部分有效。我无法工作的部分是将类下的函数组合在一起。我正在寻找的是用户输入设置浮点变量并将其与温度传感器的原始浮点变量进行比较。我想设置一个温度(目标)值THEN在后台运行一个温度读数循环。
我非常喜欢编程。我一直在阅读很多,但不明白为什么变量temp_c在一个类下面仍然没有定义? 我如何组合这个程序,以便定义每个变量,并在设置温度目标后运行一个循环。
运行程序后,我看到:
Enter Temp:22 Traceback (most recent call last):
File "/home/pi/Desktop/Trial 1.py", line 16,
in <module> class HotTub:
File "/home/pi/Desktop/Trial 1.py", line 40, in HotTub
temp_c=read_temp()
File "/home/pi/Desktop/Trial 1.py", line 27, in read_temp
lines = read_temp_raw() NameError: name
'read_temp_raw' is not defined
第一个代码示例不起作用,第二个代码示例。 :
import os
import glob
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(22, GPIO.OUT, initial=GPIO.LOW)
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
class HotTub:
target=float(input("Enter Temp:" ))
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c =float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c
while True:
try:
temp_c=read_temp()
if temp_c>=target:
GPIO.setmode(GPIO.BOARD)
GPIO.output(22, GPIO.HIGH)
print ('Temp is higher than',target,temp_c)
else:
GPIO.setmode(GPIO.BOARD)
GPIO.output(22, GPIO.LOW)
print ('Temp is lower than',target,temp_c)
time.sleep(0.5)
except KeyboardInterrupt:
print("W: interrupt received, stopping…")
GPIO.cleanup()
print ("Exit")
import os
import glob
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(22, GPIO.OUT, initial=GPIO.LOW) # Set pin 16 to be an output pin and set initial value to low (off)
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c =float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c
def compare_temp():
temp_c=read_temp()
a=float(22)
if temp_c>a:
GPIO.setmode(GPIO.BOARD)
GPIO.output(22, GPIO.HIGH)
print ('Temp is higher than 22',temp_c)
else:
GPIO.setmode(GPIO.BOARD)
GPIO.output(22, GPIO.LOW)
print ('Temp is lower than 22',temp_c)
time.sleep(0.5)
while True:
try:
compare_temp()
except KeyboardInterrupt:
print("W: interrupt received, stopping…")
GPIO.cleanup()
print ("Exit")
答案 0 :(得分:0)
当您将模块(第2个样本)代码转换为类(第1个)时,您需要通过其类实例调用函数
所以read_temp_raw()调用在类体中变为self.read_temp_raw()或
#
myinstance = HotTub()
myinstance.read_temp_raw()
#
类是命名空间,可以容纳很多对象,你可以解决 变量的方式相同,例如:
self.temp_c = some_value
或
print(myinstance.temp_c)