当前位置: 首页 > 知识库问答 >
问题:

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

庄文栋
2023-03-14

我试图使用Azure IoT中心和REST api(不使用Azure IoT中心python SDK)从云向我的设备发送消息。

我可以通过urihttps://成功地从设备向集线器发送消息(POST请求)

我还是尝试了以下方法

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"
    }
  }'

其中通过Azure CLIaz iot hub生成sas令牌-n生成

{"Message":"ErrorCode:ArgumentInvalid;Request must contain IoTHub custom 'To' header","ExceptionMessage":"Tracking ID:ec98ff8...

在那里我切断了末端。所以我尝试添加一个“To”头,不管我放什么,它都会返回相同的错误消息。

我还尝试了此处建议的云到设备Azure IoT REST API,即通过endpointhttps://main.iothub.ext.azure.com/api/Service/SendMessage/发送,但运气不佳。


共有2个答案

葛胜泫
2023-03-14

以下是如何做到这一点:

>

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

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()

保存并关闭SendCloudToDeviceMessage。py文件。

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

运行应用程序:python SendCloudToDeviceMessage。py

参考:https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/iot-hub/iot-hub-python-python-c2d.md

郎翔
2023-03-14

要使用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}'
 类似资料:
  • 在搜索文档之后,我找不到任何关于如何在没有使用外部服务器的情况下使用FCM向设备发送消息的信息。 例如,如果我正在创建一个聊天应用程序,我将需要向用户发送关于未读消息的推送通知,因为他们不会一直在线,而且我不可能在后台有一个始终连接到实时数据库的持久服务,因为这会占用太多资源。 那么当某个用户“B”向他/她发送聊天消息时,我将如何向用户“a”发送推送通知呢?我需要一个外部服务器来完成这个任务吗?还

  • null 当我确实从Firebase云消息发送给自己一条测试消息时,它确实工作正常,并且成功地向每个设备发送了一个推送通知。 例如,以下是 null

  • 我刚开始使用Firebase云消息。我建了一个IOS应用来接收推送通知。应用程序运行良好。我从Firebase控制台发送消息,它们会正确显示。 我的问题是:我可以向所有设备发送一条消息吗(就像我可以在控制台中做的那样)?我是的,怎么会这样? 提前道谢!

  • 我正在编写一个新的服务器应用程序(使用。net)向客户端(主要是android设备)发送通知,我正在使用firebase cloud messaging的新Http v1 api,我看到下面的Post请求主体发送主题通知 但是,我如何使用FCM Id向特定设备发送通知? 注意:我已经实现了使用旧版 API 向单个设备发送通知。

  • 我正在编写一个定制的Android Wear应用程序,该应用程序应该向连接的主机设备(手机)发送一次性消息。通过对API的深入研究,我发现以下教程应该可以很好地工作:http://developer.android.com/training/wearables/data-layer/messages.html 我的Android应用程序有一个WearableListenerService,我的An

  • 我需要使用< code>aws-sdk-go lib通过设备令牌数组向多个设备发送SNS推送通知。 目前我正在使用以下步骤向SNS发送推送消息: 创建endpoint: 将消息发送到endpoint: 我还没有见过一种方法,只使用一个请求就可以将一个推送消息发送到多个设备。可能吗? 像这个例子来说明: