Python 在不应该运行时运行脚本



下面的脚本每秒都会从温度计中获得读数,并且运行良好。

我一直在尝试添加其他功能。因此,当达到一定的温度时,它会执行一个脚本。在这种情况下,如果温度低于20,请运行heaton脚本。如果超过20,运行heatoff脚本。

我遇到的问题是,当温度低于20时,它会正常运行。一旦它达到20以上,我想运行heatoff脚本,它就会运行它。一旦整个程序再次启动,即使超过20,它似乎也会运行heaton脚本,然后运行heatooff。

这意味着当温度高于20时,它会循环加热,然后再加热,直到温度真正降至20以下,然后只加热。

import threading
import time
import os
def hot_temp():
    with open("/sys/bus/w1/devices/28-0216019bb8ff/w1_slave") as tfile:
        next(tfile)
        secondline = next(tfile)
    temperaturedata = secondline.split(" ")[9] 
    temperature = float(temperaturedata[2:]) 
    temperature = temperature / 1000 
    if temperature < 20.000:
        os.system("sudo python /var/www/html/scripts/heaton.py")
    else:
        os.system("sudo python /var/www/html/scripts/heatoff.py")
    return temperature
while True:
    output = hot_temp()    
    with open('/var/www/html/output/hottemp.html', 'w') as f:
        print >> f, output
    time.sleep(1)

我也尝试过使用else而不是elif,但我得到了完全相同的结果。

    temperature = temperature / 1000 
if temperature < 20.000:
    os.system("sudo python /var/www/html/scripts/heaton.py")
elif temperature > 20.000:
    os.system("sudo python /var/www/html/scripts/heatoff.py")
return temperature

我还尝试过改变温度范围,以便加热到20.000,并在20.000或20.001或20.010或20.100时关闭

当我将< 20.000> 20.000更改为<= 20.000>= 20.000时,此问题得到了解决。

import threading
import datetime
import time
import os
def hot_temp():
    with open("/sys/bus/w1/devices/28-0216019bb8ff/w1_slave") as tfile:
        next(tfile)
        secondline = next(tfile)
    temperaturedata = secondline.split(" ")[9] 
    temperature = float(temperaturedata[2:]) 
    temperature = temperature / 1000 
    if temperature <= 20.000:
        os.system("sudo python /var/www/html/scripts/heaton.py")
        output = temperature
        with open('/var/www/html/log.txt', 'a') as f:
            print >> f, datetime.datetime.now().time(), "Current Temp: ", output, " (HEATING)"
    elif temperature >= 20.000:
        os.system("sudo python /var/www/html/scripts/heatoff.py")
        output = temperature
        with open('/var/www/html/log.txt', 'a') as f:
            print >> f, datetime.datetime.now().time(), "Current Temp: ", output, "(COOLING)"
    elif temperature >= 30.000:
        os.system("sudo python /var/www/html/scripts/mailheaton.py")
    elif temperature <= 10.000:
        os.system("sudo python /var/www/html/scripts/mailheatoff.py")
    return temperature
while True:
    output = hot_temp()    
    with open('/var/www/html/output/hottemp.html', 'w') as f:
        print >> f, output
    time.sleep(1)

最新更新