python - 使用 Dweet.io 时,循环卡在 Raspberry PI 上的 python 上
问题描述
我对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')
所以我要么需要弄清楚为什么循环会卡住,要么需要弄清楚为什么第二个版本不在后台运行。
在此先感谢,我很感激任何线索。
解决方案
如果您将命令设置为完成,则从那时起它将始终完成。因此循环。
推荐阅读
- python - 有没有办法用 matplotlib 绘制矩形和/或正方形?
- java - 确保此方法调用中使用的正则表达式不会导致拒绝服务 - 正则表达式
- javascript - 无法在使用 chrome.identity.launchWebAuthFlow() 打开的窗口中使用 prompt() 或 alert()
- python - 梯度下降得到不同的最终参数,用于矩阵形式的线性回归的不同初始参数
- http - HTTP 重定向是否健壮?
- ios - 如何在 ios 中制作 react-native 全屏模式
- javascript - Discord Bot 不可用 公会麻烦
- django - 使用复选框显示项目并仅发布已选中的项目
- linux - 基本 Linux Bash 脚本 while / for
- javascript - 尝试测试获取所有路由时出现“TypeError:无法读取 null 的属性 '1'”?