2017-08-16 83 views
0

我想从树莓派3数据天青变量赋值之前引用:Python的

脚本树莓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) 
+0

显示完整的追溯请 –

+0

修复您的格式。你的错误很明显 - 你想在变量赋值之前使用变量'temperature',即温度='x'..这可能是由于你的try语句,但我们不知道是因为格式化 – AK47

回答

1

错误消息这里是相当清楚的。

请记住,Python一次读取并执行一行代码,所以如果您在使用函数之后声明了一个变量,那么它将引发错误。在调用你的代码之前把你的变量放在变量中,并且你不应该再次遇到这个错误。

+0

谢谢我明白了。 – PUser

相关问题