是时候从 mqtt 负载打开 on/off 屏幕了
time to turn on/off screen from mqtt payload
我是 python 的新手,仍在学习中,所以请对我温柔点。我有我的 python 脚本,它订阅了一个 MQTT 主题并接收了一个代码。
如果有效载荷等于该代码,那么它将设置打开显示器的计时器。时间过去后,显示将关闭,直到有效负载再次等于之前描述的相同代码。
这是我的代码:
import paho.mqtt.client as mqttClient
import time
from subprocess import call
from time import sleep
def on_connect(client, userdata, flags, rc):
# print("Connected with result code "+str(rc))
client.subscribe("home/OpenMQTTGateway/433toMQTT")
#def on_message(client, userdata, msg):
# if msg.topic == 'home/OpenMQTTGateway/433toMQTT':
# print(str(msg.payload))
def on_message(client, userdata, msg):
global myGlobalMessagePayload
if msg.topic == 'home/OpenMQTTGateway/433toMQTT':
myGlobalMessagePayload = msg.payload
timeUntilDisplayOff = 240
timer = timeUntilDisplayOff
while True:
if msg.payload == '1381683':
timer = timeUntilDisplayOff
print ("Motion detected! Setting timer to " + str(timer) + " seconds.")
if timer > 0:
if timer % 10 == 0:
print ("Timer: " + str(timer) + " seconds")
timer -= 1
elif timer == 0:
call(['vcgencmd', 'display_power', '0'])
print ("Timer is 0. Display turned off. Waiting for motion...")
# display is now off. we wait for motion and turn it on
myGlobalMessagePayload == '1381683'
call(['vcgencmd', 'display_power', '1'])
timer = timeUntilDisplayOff
sleep(1)
问题:
当我 运行 我的代码时,我收到一条错误消息:
Traceback (most recent call last):
File "test2.py", line 26, in <module>
if msg.payload == '1381683':
NameError: name 'msg' is not defined
我还使用通用代码重复了该过程,即在 # display is now off. we wait for motion and turn it on
之后接近尾声时,它看起来正确吗?或者可以做得更好吗?
谢谢
更新:
感谢@blt,我整理了我的代码。但是,当 msg.payload 匹配“1381683”时,我的代码会继续循环。我得到以下信息:
Motion detected! Setting timer to 240 seconds.
Timer: 240 seconds
Motion detected! Setting timer to 240 seconds.
Timer: 240 seconds
Motion detected! Setting timer to 240 seconds.
Timer: 240 seconds
上面一直在循环...
缩进在 python 中很重要。
您需要将以下块缩进一级:
timeUntilDisplayOff = 1800
timer = timeUntilDisplayOff
while True:
否则本节后面的代码将不会在范围内包含 msg
变量(因此会出现错误)。
所以on_message
函数的前半部分是:
def on_message(client, userdata, msg):
global myGlobalMessagePayload
if msg.topic == 'home/OpenMQTTGateway/433toMQTT':
myGlobalMessagePayload = msg.payload
timeUntilDisplayOff = 1800
timer = timeUntilDisplayOff
while True:
if msg.payload == '1381683':
timer = timeUntilDisplayOff
print ("Motion detected! Setting timer to " + str(timer) + " seconds.")
我是 python 的新手,仍在学习中,所以请对我温柔点。我有我的 python 脚本,它订阅了一个 MQTT 主题并接收了一个代码。
如果有效载荷等于该代码,那么它将设置打开显示器的计时器。时间过去后,显示将关闭,直到有效负载再次等于之前描述的相同代码。
这是我的代码:
import paho.mqtt.client as mqttClient
import time
from subprocess import call
from time import sleep
def on_connect(client, userdata, flags, rc):
# print("Connected with result code "+str(rc))
client.subscribe("home/OpenMQTTGateway/433toMQTT")
#def on_message(client, userdata, msg):
# if msg.topic == 'home/OpenMQTTGateway/433toMQTT':
# print(str(msg.payload))
def on_message(client, userdata, msg):
global myGlobalMessagePayload
if msg.topic == 'home/OpenMQTTGateway/433toMQTT':
myGlobalMessagePayload = msg.payload
timeUntilDisplayOff = 240
timer = timeUntilDisplayOff
while True:
if msg.payload == '1381683':
timer = timeUntilDisplayOff
print ("Motion detected! Setting timer to " + str(timer) + " seconds.")
if timer > 0:
if timer % 10 == 0:
print ("Timer: " + str(timer) + " seconds")
timer -= 1
elif timer == 0:
call(['vcgencmd', 'display_power', '0'])
print ("Timer is 0. Display turned off. Waiting for motion...")
# display is now off. we wait for motion and turn it on
myGlobalMessagePayload == '1381683'
call(['vcgencmd', 'display_power', '1'])
timer = timeUntilDisplayOff
sleep(1)
问题:
当我 运行 我的代码时,我收到一条错误消息:
Traceback (most recent call last):
File "test2.py", line 26, in <module>
if msg.payload == '1381683':
NameError: name 'msg' is not defined
我还使用通用代码重复了该过程,即在 # display is now off. we wait for motion and turn it on
之后接近尾声时,它看起来正确吗?或者可以做得更好吗?
谢谢
更新:
感谢@blt,我整理了我的代码。但是,当 msg.payload 匹配“1381683”时,我的代码会继续循环。我得到以下信息:
Motion detected! Setting timer to 240 seconds.
Timer: 240 seconds
Motion detected! Setting timer to 240 seconds.
Timer: 240 seconds
Motion detected! Setting timer to 240 seconds.
Timer: 240 seconds
上面一直在循环...
缩进在 python 中很重要。
您需要将以下块缩进一级:
timeUntilDisplayOff = 1800
timer = timeUntilDisplayOff
while True:
否则本节后面的代码将不会在范围内包含 msg
变量(因此会出现错误)。
所以on_message
函数的前半部分是:
def on_message(client, userdata, msg):
global myGlobalMessagePayload
if msg.topic == 'home/OpenMQTTGateway/433toMQTT':
myGlobalMessagePayload = msg.payload
timeUntilDisplayOff = 1800
timer = timeUntilDisplayOff
while True:
if msg.payload == '1381683':
timer = timeUntilDisplayOff
print ("Motion detected! Setting timer to " + str(timer) + " seconds.")