Cloud-to-device Azure IoT REST API

2019-07-14 19:57发布

问题:

I explored on sending data from Device to Cloud using Azure REST Apis. It is working seamlessly without any issues. I'm not finding good articles on sending Cloud-to-Device messages to Arduino board using "Azure IoT Hub REST Apis". Could some one provide suggestions on this

回答1:

As Peter Pan said, there isn't a RESTful API for send C2D messages currently. However, you still have some other chooses.

  1. Use Azure Function App. You can create a HTTP trigger Azure Function App to use as RESTful proxy, and run IoT Hub SDK on Azure Function App to send C2D messages.

  2. Use AMQP over WebSockets with 443 port if you cannot use 5671 port for AMQP to connect to IoT Hub on your service side. We have developed a web based IoT Hub devtool based on Rhea, and you can reference our code.



回答2:

You could also send the request as the azure portal does it. All C2D from AZ Portal are sent through this Endpoint https://main.iothub.ext.azure.com/api/Service/SendMessage/ and the payload is a json that look like this:

{
  "hostName": "iothub-hostname",
  "owner": "twinUpdate",
  "key": "key for the iothub",
  "deviceID": "your device on that hub",
  "body": "{\"test\": \"This is a test over postman\"}",
  "properties": "[]"
}

Keep in mind that you need to add an Authorization Header with valid Bearer token. You can get this when you log in to AZ Portal.



回答3:

According to the offical document Send and receive messages with IoT Hub, and after I reviewed the source codes of Azure IoT Hub for sending cloud-to-device message using different languages, there is no REST API to support on sending Cloud-to-Device messages. To send c2d message from application to Azure IoT Hub, the recommended protocol is AMQP and the simple way is using Azure IoT Hub SDK. That you can refer to the section Communication protocol, as below.

However, if you want to receive the c2d message from Arduino, you can refer to the section Cloud-to-device messages and use the REST API Receive Device Bound Notification on Arduino.