脚本波纹管每秒钟从温度计读取并且工作得很好。
我一直在尝试为此添加其他功能。因此,当达到某个温度时,它会执行一个脚本。在这种情况下,如果temp低于20,请运行heaton脚本。如果大于20,请运行散热脚本。
我遇到的问题是,当温度低于20时,它会很好地运行。一旦它超过20并且我想运行散热脚本,它就会运行它。一旦整个程序再次启动,它似乎运行heaton脚本,即使在20以上,然后运行加热。
这意味着当它超过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
答案 0 :(得分:1)
当我将< 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)