Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python NameError:未定义名称“消息”_Python_Undefined_Mqtt - Fatal编程技术网

Python NameError:未定义名称“消息”

Python NameError:未定义名称“消息”,python,undefined,mqtt,Python,Undefined,Mqtt,我对python没有任何经验。。。但我需要将其用于raspberry+mqtt+wiringpi+home_assistance集成,我想创建简单的操作,mqtt客户端正在侦听,并且在接收适当主题的适当信息时,他将更改wiringpi设置。。。部分起作用的。。。当我试图创建对信息的依赖时,问题出现了 import paho.mqtt.client as mqtt #import the client1 import wiringpi import time wiringpi.wiringPiS

我对python没有任何经验。。。但我需要将其用于raspberry+mqtt+wiringpi+home_assistance集成,我想创建简单的操作,mqtt客户端正在侦听,并且在接收适当主题的适当信息时,他将更改wiringpi设置。。。部分起作用的。。。当我试图创建对信息的依赖时,问题出现了

import paho.mqtt.client as mqtt #import the client1
import wiringpi
import time

wiringpi.wiringPiSetup()

############
def wiadomosc(client, userdata, message):
    global external_value1, added_value2
    print("message received " ,str(message.payload.decode("utf-8")))
    print("message topic=",message.topic)
    print("message qos=",message.qos)
    print("message retain flag=",message.retain)
external_value1 = str(message.payload.decode("utf-8"))
wiringpi.pinMode(29, 0)
########################################
broker_address="192.168.0.211"
print("creating new instance")
client = mqtt.Client("P1") #create new instance
client.on_message=wiadomosc #attach function to callback
print("connecting to broker")
client.connect(broker_address) #connect to broker
client.loop_start() #start the loop
print("Subscribing to topic","home/kitchen/output/lights/set")
client.subscribe("home/kitchen/output/lights/set")
time.sleep(40000) # wait
client.loop_stop() #stop the loop
我收到了

NameError: name 'message' is not defined

我知道当从mqtt收到消息时,它会显示出来。。。我试图创建空值,但它没有正常工作,上面的代码被简化,我已经删除了所有if,并且只留下了导致问题的部分

您的问题是缩进,消息超出了您的功能。您正在将message作为wiadomosc函数的一个参数传递,但在该函数减速之后,立即使用message.payload初始化外部_value1,这是以前未定义的

def wiadomosc(client, userdata, message):
    global external_value1, added_value2
    print("message received " ,str(message.payload.decode("utf-8")))
    print("message topic=",message.topic)
    print("message qos=",message.qos)
    print("message retain flag=",message.retain)
    external_value1 = str(message.payload.decode("utf-8"))
wiringpi.pinMode(29, 0)

external_value1=strmessage.payload.decodeutf-8消息未定义,您正在从不存在的函数调用某些内容。但当我从mqtt接收消息时,将创建该消息。。。如何创建对将要创建的内容的依赖关系?一行哈希并不表示函数的结束,缩进表示函数的结束。您可以将其创建为空,然后使用mqtt中的新数据填充它