作为合作伙伴和客户,我能够遵循以下 URL 中有关传感器集成的每一步。
我已使用 IoThub 设备连接字符串发送传感器数据。发布此信息,如何使用 REST API 检查我的遥测数据,以及如何将此传感器数据路由到端点(可能是存储帐户或任何其他端点)。请推荐。
下面的Rest API用于检索Azure IoT Hub特定设备的云到设备消息。
GET https://fully-qualified-iothubname.azure-devices.net/devices/{id}/messages/deviceBound?api-version=2020-03-13
Authorization: <shared_access_token>
回复:
下面的代码是使用 Node.js 为 Azure IoT 中心设备创建共享访问签名 (SAS) 令牌。
const crypto = require('crypto');
// Function to generate SAS token
const generateSasToken = function(resourceUri, signingKey, policyName, expiresInMins) {
// URL encode the resource URI
resourceUri = encodeURIComponent(resourceUri);
// Set expiration in seconds
const expires = Math.ceil(Date.now() / 1000) + expiresInMins * 60;
// Construct the string to sign
const toSign = resourceUri + '\n' + expires;
// Use crypto to create HMAC-SHA256 signature
const hmac = crypto.createHmac('sha256', Buffer.from(signingKey, 'base64'));
hmac.update(toSign);
const base64UriEncoded = encodeURIComponent(hmac.digest('base64'));
// Construct the SAS token
let token = `SharedAccessSignature sr=${resourceUri}&sig=${base64UriEncoded}&se=${expires}`;
if (policyName) token += `&skn=${policyName}`;
return token;
};
// Example usage
const endpoint = "myhub.azure-devices.net/devices/device1";
const policyName = 'device'; // Replace with your policy name
const policyKey = "..."; // Replace with your policy key (or)Shared access policies device key give all persion for the device key
const token = generateSasToken(endpoint, policyKey, policyName, 60);
console.log("Generated SAS Token:", token);
您可以按照以下步骤配置从 IoT 设备到 IoT 中心内的存储帐户的消息路由:
选择您的 IoT 中心,然后单击导航窗格中的“消息路由”选项。
单击“+ 添加”按钮为消息路由创建新路由,然后在“端点”字段旁边单击“+ 添加”按钮指定路由的端点。
从可用选项中选择“Blob 存储”作为端点类型,然后输入端点的名称,例如“IoTStorageEndpoint”。
单击“选择容器”以选择要存储路由消息的存储容器。如果所需的容器不存在,您可以通过单击“+ 容器”按钮并指定名称和访问级别来创建一个新容器。
选择是使用 blob 的默认命名约定还是根据您的喜好进行自定义。配置完所有必要的详细信息后,单击“创建”以创建存储端点。输入路由名称,选择“设备遥测消息”作为数据源,启用路由,并指定路由查询,例如“level='storage'”/true。