0

我对python很陌生,所以请耐心等待。我正在尝试更改 GPIO 引脚的状态以根据 dweet.io 变量的状态控制继电器。变量是命令,它可以是 on、off 或 done。程序检查 dweet 直到它不等于完成,这意味着它是打开或关闭,并相应地更改继电器的状态。然后,它将 dweet 改回,使命令等于完成,然后重新执行。但是,它只能工作一次。之后,它只是不断地将 dweet 改回完成。

import RPi.GPIO as GPIO
import time
import dweepy
import os

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)

dweepy.dweet_for('my_thing', {'command': 'done'})
url = dweepy.get_latest_dweet_for("my_thing")
dict = url[0]
command = dict["content"][str("command")]


while True:
        1 == 1

        while command == "done":
                time.sleep(2)
                url = dweepy.get_latest_dweet_for("my_thing")
                dict = url[0]
                command = dict["content"][str("command")]


        else:
                if command == "on":
                        GPIO.output(7,GPIO.LOW)
                        time.sleep(2)
                        dweepy.dweet_for('my_thing', {'command': 'done'})

                else:
                        GPIO.output(7,GPIO.HIGH)
                        time.sleep(2)
                        dweepy.dweet_for('my_thing', {'command': 'done'})

我认为既然它工作一次,我应该能够在将 dweet 更改回 done 并摆脱无限循环之后再次启动程序。这可行,但是当 Pi 启动时我无法运行它。

import RPi.GPIO as GPIO
import time
import dweepy
import os

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)

dweepy.dweet_for('my_thing', {'command': 'done'})
url = dweepy.get_latest_dweet_for("my_thing")
dict = url[0]
command = dict["content"][str("command")]




while command == "done":
        time.sleep(2)
        url = dweepy.get_latest_dweet_for("my_thing")
        dict = url[0]
        command = dict["content"][str("command")]


else:
        if command == "on":
                GPIO.output(7,GPIO.LOW)
                time.sleep(2)
                dweepy.dweet_for('my_thing', {'command': 'done'})
                execfile('dweetrelay.py')

        else:
                GPIO.output(7,GPIO.HIGH)
                time.sleep(2)
                dweepy.dweet_for('my_thing', {'command': 'done'})
                execfile('dweetrelay.py')

所以我要么需要弄清楚为什么循环会卡住,要么需要弄清楚为什么第二个版本不在后台运行。

在此先感谢,我很感激任何线索。

4

1 回答 1

0

如果您将命令设置为完成,则从那时起它将始终完成。因此循环。

于 2020-04-10T14:24:26.083 回答