无法从python发送E2C消息到Azure IoTHub

时间:2019-09-09 09:01:32

标签: python azure azure-iot-hub

我在一个模拟器上创建,该模拟器将模拟边缘到云的消息从模拟设备发送到IoT中心。在遵循SDK的官方文档和GitHub页面中的代码时遇到了一些问题。

这是我的代码:

async def send_e2c_message(self):
   # ConnectionString get from IoT Hub
   conn_str = "HostName={IoTHubName};SharedAccessKeyName=service;SharedAccessKey={SomeAccessKey}"

   # The client object is used to interact with the Azure IoT hub.
   device_client = IoTHubDeviceClient.create_from_connection_string(conn_str)

   # Connect the client.
   await device_client.connect()

   # Send message
   print("Sending message...")
   await device_client.send_message('Hello World')
   print("Message successfully sent!")

   # Disconnect client
   await device_client.disconnect()


if __name__ == "__main__":
    asyncio.run(IoTHubAccessor.send_e2c_message(self))

错误显示如下:

Unexpected error in <azure.iot.device.iothub.pipeline.pipeline_stages_iothub.UseAuthProviderStage object at 0x03E31CF0>._execute_op() call
Traceback (most recent call last):
  File 
  ...
in _execute_op
    sas_token=self.auth_provider.get_current_sas_token(),
  File 
  ...
in get_current_sas_token
    self.generate_new_sas_token()
  File 
resource_uri = self.hostname + "/devices/" + self.device_id
TypeError: can only concatenate str (not "NoneType") to str
UseAuthProviderStage(SetAuthProviderOperation): completing with error can only concatenate str (not "NoneType") to str
Exception caught in background thread.  Unable to handle.

File 
"...\pipeline_stages_mqtt.py", line 37, in _cancel_pending_connection_op
    op = self._pending_connection_op
AttributeError: 'MQTTTransportStage' object has no attribute '_pending_connection_op'
MQTTTransportStage(ConnectOperation): completing with error 'MQTTTransportStage' object has no attribute '_pending_connection_op'
SerializeConnectOpsStage(ConnectOperation): completing with error 'MQTTTransportStage' object has no attribute '_pending_connection_op'
Unhandled exception in background thread
This may cause the background thread to abort and may result in system instability.

我得到的图片是我必须提供一些DeviceId,但是,连接字符串中的DeviceId将不起作用。我不知道该放在哪里。

我只是按照官方文档中的示例进行操作,所以我不知道这是什么问题。如果有人可以帮忙,我真的很感激。

谢谢。

1 个答案:

答案 0 :(得分:1)

您使用了错误的连接字符串。由于您要从docs发送消息,因此需要使用设备连接字符串:

device-side

enter image description here