Mainthread:
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(Broker, 1883, 60)
client.on_log = on_log
client.loop_start()
global alarm
alarm = True
while True:
try:
if alarm == True:
...
并且在函数中有变量变量:
def on_message(client, userdata, msg):
message = str(msg.payload)
if message.lower() == 'alarm off': alarm = False
elif message.lower() == 'alarm on': alarm = True
它的功能就像功能中的单独变量而Mainthread我找不到原因。
答案 0 :(得分:0)
var应该在on_message函数中标记为全局,而不是你拥有它。
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(Broker, 1883, 60)
client.on_log = on_log
client.loop_start()
alarm = True
while True:
try:
if alarm == True:
...
和
def on_message(client, userdata, msg):
global alearm
message = str(msg.payload)
if message.lower() == 'alarm off': alarm = False
elif message.lower() == 'alarm on': alarm = True