Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.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 使用REST api向设备发送云消息Azure IoT Hub,端点_Python_Azure_Rest_Cloud_Azure Iot Hub - Fatal编程技术网

Python 使用REST api向设备发送云消息Azure IoT Hub,端点

Python 使用REST api向设备发送云消息Azure IoT Hub,端点,python,azure,rest,cloud,azure-iot-hub,Python,Azure,Rest,Cloud,Azure Iot Hub,我正在尝试使用Azure IoT Hub和REST api(不使用Azure IoT Hub python SDK)从云向我的设备发送消息 我可以使用uri0从设备向集线器成功发送消息(POST请求)https://.azure-devices.net/devices//messages/events?api-版本=2018-06-30。在它的文档中说,/messages/devicebound上有一个服务端端点。但是,它们并没有显示一个完整的示例,因此我不能完全确定我应该使用的完整端点是什么,

我正在尝试使用Azure IoT Hub和REST api(不使用Azure IoT Hub python SDK)从云向我的设备发送消息

我可以使用uri
0从设备向集线器成功发送消息(POST请求)https://.azure-devices.net/devices//messages/events?api-版本=2018-06-30
。在它的文档中说,
/messages/devicebound
上有一个服务端端点。但是,它们并没有显示一个完整的示例,因此我不能完全确定我应该使用的完整端点是什么,以及如何/在哪里指定要发送到哪个设备

无论如何,我尝试了以下方法:

curl -v POST \
  https://<myhub>.azure-devices.net/messages/devicebound?api-version=2018-06-30 \
  -H 'Authorization: SharedAccessSignature <sas>' \
  -H 'Content-Type: application/json' \
  -d '{
    "payload": {
      "key": "value"
    }
  }'
在那里我切断了末端。所以我尝试添加一个“To”头,不管我放什么,它都会返回相同的错误消息


我也尝试了这里的建议,即通过端点发送,但没有运气

要使用IoT Hub API从设备端接收云到设备消息,您必须执行以下请求-

curl -X GET \
  'https://{your hub}.azure-devices.net/devices/{your device id}/messages/deviceBound?api-version=2018-06-30' \
  -H 'Authorization: {your sas token}'
以下是如何做到这一点:

  • 使用文本编辑器,创建SendCloudToDeviceMessage.py文件

  • 在SendCloudToDeviceMessage.py文件的开头添加以下导入语句和变量:

  • 将以下代码添加到SendCloudToDeviceMessage.py文件中。将“{iot hub connection string}”和“{device id}”占位符值替换为前面提到的iot hub connection string和device id:
  • 添加以下功能以将反馈消息打印到控制台:
  • 添加以下代码以向设备发送消息,并在设备确认云到设备消息时处理反馈消息:
  • 添加以下主要功能:
  • 保存并关闭SendCloudToDeviceMessage.py文件

  • 安装依赖项库:
    pip安装azure iothub服务客户端

  • 运行应用程序:
    python SendCloudToDeviceMessage.py


  • 参考:

    我不是在问如何接收消息,我是在问如何将它们从云端发送到设备。嘿,我正在尝试实现同样的目标,你找到了一种方法吗???我已经跟踪了你收到的错误消息,并将
    -H'iothub添加到:'\
    标题,但我得到了错误
    {“消息”:“ErrorCode:ArgumentInvalid;请求必须在IoTHub custom'To'标头中包含设备Id”,“ExceptionMessage”
    ,尽管我已在标头中放置了正确的设备Id。
    curl -X GET \
      'https://{your hub}.azure-devices.net/devices/{your device id}/messages/deviceBound?api-version=2018-06-30' \
      -H 'Authorization: {your sas token}'
    
    import random
    import sys
    import iothub_service_client
    from iothub_service_client import IoTHubMessaging, IoTHubMessage, IoTHubError
    
    OPEN_CONTEXT = 0
    FEEDBACK_CONTEXT = 1
    MESSAGE_COUNT = 1
    AVG_WIND_SPEED = 10.0
    MSG_TXT = "{\"service client sent a message\": %.2f}"
    
    CONNECTION_STRING = "{IoTHubConnectionString}"
    DEVICE_ID = "{deviceId}"
    
    def open_complete_callback(context):
        print ( 'open_complete_callback called with context: {0}'.format(context) )
    
    def send_complete_callback(context, messaging_result):
        context = 0
        print ( 'send_complete_callback called with context : {0}'.format(context) )
        print ( 'messagingResult : {0}'.format(messaging_result) )
    
    def iothub_messaging_sample_run():
        try:
            iothub_messaging = IoTHubMessaging(CONNECTION_STRING)
    
            iothub_messaging.open(open_complete_callback, OPEN_CONTEXT)
    
            for i in range(0, MESSAGE_COUNT):
                print ( 'Sending message: {0}'.format(i) )
                msg_txt_formatted = MSG_TXT % (AVG_WIND_SPEED + (random.random() * 4 + 2))
                message = IoTHubMessage(bytearray(msg_txt_formatted, 'utf8'))
    
                # optional: assign ids
                message.message_id = "message_%d" % i
                message.correlation_id = "correlation_%d" % i
                # optional: assign properties
                prop_map = message.properties()
                prop_text = "PropMsg_%d" % i
                prop_map.add("Property", prop_text)
    
                iothub_messaging.send_async(DEVICE_ID, message, send_complete_callback, i)
    
            try:
                # Try Python 2.xx first
                raw_input("Press Enter to continue...\n")
            except:
                pass
                # Use Python 3.xx in the case of exception
                input("Press Enter to continue...\n")
    
            iothub_messaging.close()
    
        except IoTHubError as iothub_error:
            print ( "Unexpected error {0}" % iothub_error )
            return
        except KeyboardInterrupt:
            print ( "IoTHubMessaging sample stopped" )
    
    if __name__ == '__main__':
        print ( "Starting the IoT Hub Service Client Messaging Python sample..." )
        print ( "    Connection string = {0}".format(CONNECTION_STRING) )
        print ( "    Device ID         = {0}".format(DEVICE_ID) )
    
        iothub_messaging_sample_run()