import RPi.GPIO as g
import time
g.setmode(g.BOARD)
g.setup(33, g.OUT)
while True:
tempfile = open("/sys/devices/w1_bus_master1/28-00152c2631ee/w1_slave")
thetext = tempfile.read()
tempfile.close()
tempdata = thetext.split("\n") [1].split(" ")[9]
temperature = float(tempdata[2:])
finaltemp = temperature / 1000
time.sleep(1)
if finaltemp > 22:
g.output(33,g.LOW)
time.sleep(0.3)
g.output(33,g.HIGH)
else:
g.output(33,g.LOW)
time.sleep(0.3)
g.output(33,g.HIGH)
我搜索了许多站点,包括这一站点,但从未找到我的解决方案。
如您所见,该代码当前从系统文件中获取温度并将温度存储在变量“ finaltemp”中。
我进行硬件设置的方式是将继电器开关连接到AC遥控器上的按钮,这就是为什么我将GPIO设置为非常快(0.3秒)打开和关闭以模拟按下按钮的原因。遥控器上的按钮。
我的目标是在温度根据条件变化时仅“闪烁”(按一下按钮)GPIO一次(!)。
例如:
室内温度为20,此时交流电源已关闭。因此,温度正在缓慢上升,当温度超过22时,我想运行3行代码来运行。但是,发生的事情是每次都会对其进行检查。由于条件在每次while循环重新开始时都满足,因此它会不断地重复运行代码,因此本质上发生的是我的AC不断地打开和关闭以及打开和关闭。
开启/关闭逻辑的伪代码:
LIMIT_LOW = 21.5
LIMIT_HIGH = 22.5
AC_running = False # False or True, you need to know exactly
while True:
temp = ....
if temp < LIMIT_LOW and AC_running:
# turn AC off
AC_running = False
elif temp > LIMIT_HIGH and not AC_running:
# turn AC on
AC_running = True
sleep(...)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句