Azure IoT中心:没有SDK的HTTP设备到云的消息? [英] Azure IoT Hub: HTTP Device-to-Cloud Messages without SDK?

查看:53
本文介绍了Azure IoT中心:没有SDK的HTTP设备到云的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的32位微控制器(用C语言编写的程序代码)具有非常有限的闪存空间,并且正在与蜂窝模块进行通信.我有一个带IoT中心的Azure帐户设置,我想向IoT中心发送一些设备到云的消息.我已经测试并确认与其他服务器的HTTP和HTTPS通信.但是,我找不到任何地方指定发送设备到云消息所需的HTTP标头.谁能提供所需的HTTP消息格式的描述(HTTP起始行,所需的HTTP标头和HTTP标头值)?

I'm using a 32-bit microcontroller (program code written in C) with very limited flash space that is communicating with a cellular module. I have an Azure account setup with an IoT Hub and I would like to send some device-to-cloud messages to my IoT Hub. I have tested and confirmed both HTTP and HTTPS communication to other servers. However, I can't find anywhere that specifies what the required HTTP headers are for sending a device-to-cloud message. Can anyone provide a description of the required HTTP message format (the HTTP start line, required HTTP headers and the HTTP header values)?

我找到了C的Azure SDK,但是即使打开了所有优化选项,它也占用了我的微控制器太多的代码空间.我已经尝试按照SDK中的代码构建HTTP消息的方式,但是我似乎缺少了一些内容.我只发送两到三个设备到云的消息,所以我假设,如果我知道标头是什么,生成设备到云的消息将不需要太多代码.

I found the Azure SDK for C, but even with all the optimization options turned on it takes up too much codespace for my microcontroller. I've tried following the code in the SDK for how the HTTP message is built, but I seem to be missing some pieces. I only have two or three device-to-cloud messages that I am sending, so I assume that if I know what the headers are it won't take much code to generate the device-to-cloud messages.

我拥有的蜂窝模块正在处理X.509证书以进行相互TLS身份验证.假设正在工作.对于这个问题,我只关心为Azure IoT设备到云的消息查找所需的HTTP消息格式.

The cellular module I have is handling the X.509 certificates for mutual TLS authentication. Assume that is working. For this question I'm only concerned with finding the required HTTP message formatting for a Azure IoT device-to-cloud message.

编辑:在评论中提出建议后,我最终被带到了该页面: https://docs.microsoft.com/en-us/rest/api/iothub/device/senddeviceevent

EDIT: Following a suggestion from a comment, I was eventually led to this page: https://docs.microsoft.com/en-us/rest/api/iothub/device/senddeviceevent

使用建议的HTTP POST(已替换我的设备详细信息),我现在收到401错误(IotHubUnauthorizedAccess).我以为我了解身份验证应该如何工作,但是我想我错了.

Using the suggested HTTP POST (with my device specifics replaced), I am now getting a 401 error (IotHubUnauthorizedAccess). I thought I understood how the authentication was supposed to work, but I guess I was wrong.

我的IoT设备具有对称密钥.我以为我应该包含标头格式为:

My IoT device has a symmetric key. I thought I was supposed to include a header formatted as:

Authorization:SharedAccessKey=<my_primary_key>

但这不起作用.我的HTTP正文很简单:

but that doesn't work. My HTTP body is simply:

{"deviceID":<my_IoT_Device_ID>}

我应该在哪里包含对称密钥信息,它的格式是什么?

Where am I supposed to include the symmetric key information, and what is its format?

推荐答案

经过反复试验,我们发现它比我们想象的要容易得多.如果您使用X.509证书进行客户端身份验证,则无需在HTTP消息内容中包含任何内容即可指定身份验证.使用X.509证书进行身份验证的Azure设备到云的消息的最低要求是:

After some trial and error, we found out it's much easier than we thought. If you are using X.509 certificates for client authentication, you don't need to include anything in the HTTP message content to specify your authentication. The minimum required for an Azure device-to-cloud message using X.509 certificates for authentication is:

POST /devices/<id>/messages/events?api-version=2018-06-30 HTTP/1.1
Host:<fully-qualified-iothubname>.azure-devices.net
Content-Length:<number-of-bytes-in-the-JSON-body>

{"deviceID":"<id>",<your-JSON-formatted-custom-d2c-message-data>}

其中< id> 是Azure IoT中心上列出的设备ID,而< fully-qualified-iothubname> 是IoT中心名称.我相信,如果您想这样做,而不是使用 Content-Length 标头,Azure将支持分块编码.

where <id> is the device ID as listed on the Azure IoT Hub and <fully-qualified-iothubname> is the IoT Hub name. I believe Azure supports chunked encoding if you want to do that instead of using the Content-Length header.

这篇关于Azure IoT中心:没有SDK的HTTP设备到云的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆