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)
显示完整的追溯请 –
修复您的格式。你的错误很明显 - 你想在变量赋值之前使用变量'temperature',即温度='x'..这可能是由于你的try语句,但我们不知道是因为格式化 – AK47