赋值前引用的变量:Python

时间:2017-08-16 12:46:28

标签: python python-2.7 raspberry-pi3 azure-iot-hub

我试图从raspberry pi 3获取数据到Azure

该脚本从Raspberry Pi3读取数据,Raspberry Pi3通过蓝牙连接到传感器并获取多个值 不幸的是,当我运行它时,我收到了错误

  

"局部变量'温度'在分配之前引用"

def iothub_client_sample_run():
    msgs=[]

    for address, name in list(devices.items()):
    try:
        client = iothub_client_init()

        if client.protocol == IoTHubTransportProvider.MQTT & (name == "Flower care"):
       msg_txt_formatted = MSG_TXT % (
          temperature,
            sunlight,
            moisture,
            fertility)

            message = IoTHubMessage(msg_txt_formatted)
                # optional: assign ids
       message.temperature_id = "%d" % temperature

       client.send_event_async(message, send_confirmation_callback, devices.items())
       print ( "IoTHubClient.send_event_async accepted message {} for transmission to IoT Hub.".format(devices.items()) )
       return msgs      
while 1:
msgs=iothub_client_sample_run()
for msg in msgs:
print msg['topic']
print msg['payload']
(result, mid)=mqttc.publish(msg['topic'],msg['payload'])
print ( "Send status: %s" % status )
        time.sleep(10)
mqttc.disconnect()

except IoTHubError as iothub_error:
        print ( "Unexpected error %s from IoTHub" % iothub_error )
       return
except KeyboardInterrupt:
        print ( "IoTHubClient sample stopped" )
    print_last_message_time(client)

1 个答案:

答案 0 :(得分:1)

这里的错误信息很清楚。

请记住,Python一次读取并执行一行代码,因此如果在使用它的函数之后有一个变量,那么它将抛出一个错误。在调用变量之前,请调整代码并将它们放在一起,并且不应再次遇到此错误。