Python less than not working?



为我的树莓派制作了一个小脚本,在cpu温度达到某个点时切换风扇,代码为:

import os
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
R = 7
GPIO.setup(R, GPIO.OUT)
GPIO.output(R, GPIO.HIGH)
while True:
    def getCPUtemperature():
        res = os.popen('vcgencmd measure_temp').readline()
        return res.replace("temp=", "").replace("'Cn", "")
    CPU = getCPUtemperature()
    print(CPU)
    if CPU > 36.0:
        GPIO.output(R, GPIO.LOW)
    elif CPU < 30.0:
        GPIO.output(R, GPIO.HIGH)

首先,是的,我使用的是一个继电器,所以高和低应该是这样的。工作,但问题是,尽管控制台输出的值像29.3,风扇仍然运行,程序仍然认为值高于36,我不知道为什么它没有意识到值小于27

有人知道为什么它不起作用吗?

感谢

getCPUtemperature返回的值看起来应该转换为浮点值,例如:

def getCPUtemperature():
    res = os.popen('vcgencmd measure_temp').readline()
    return float(res.replace("temp=","").replace("'Cn",""))

最新更新