我已经找到了许多关于如何从运行的IoT中心检索数据的示例。然而,在所有这些中,WebSockets的某些变体是必需的。
我需要一种方法来立即从IoT设备上获取最新消息.
场景
我使用4个设备运行一个小型气象站,这些设备将数据发送到IoT集线器。作为显示器,我想回收一个第一代iPad。它的浏览器不支持WebSockets,因此排除了所有现代方法。
我将每15分钟更新一次轮询值,最好使用简单的HTTP请求。
我有上面提到的示例运行(qrysweathertest.azurewebsites.net),但是它使用web套接字,因此不能在1 1stGen上工作。
发布于 2021-01-02 09:21:56
对于IoT集线器来说,这是不可能的。您必须将遥测值存储到存储(例如数据库),并构建一个小API来检索最新的值。您可以使用Azure函数存储和检索值,这将是一种低成本的方法来启用您的场景。
或者,IoT Central支持通过内置API检索最新的遥测值。而且,IoT Central的仪表板特性可能涵盖整个场景。
发布于 2021-01-05 10:39:13
您的业余爱好项目的另一种选择可以考虑使用设备双标记属性来存储最后一次遥测数据。
基本上,您可以使用EventGridTrigger订阅服务器将遥测数据存储到push方式中的设备孪生标记属性中,或者使用拉出方式(例如,IoTHubTrigger函数作为iothub流管道的使用者)。
下面的代码片段显示了IoTHubTrigger函数的一个示例:
run.csx:
#r "Microsoft.Azure.EventHubs"
#r "Newtonsoft.Json"
#r "..\\bin\\Microsoft.Azure.Devices.dll"
#r "..\\bin\\Microsoft.Azure.Devices.Shared.dll"
using Microsoft.Azure.Devices;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Microsoft.Azure.EventHubs;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;
static RegistryManager registryManager = RegistryManager.CreateFromConnectionString(Environment.GetEnvironmentVariable("AzureIoTHubShariedAccessPolicy"));
public static async Task Run(EventData message, ILogger log)
{
log.LogInformation($"\nSystemProperties:\n\t{string.Join(" | ", message.SystemProperties.Select(i => $"{i.Key}={i.Value}"))}");
if (message.SystemProperties["iothub-message-source"]?.ToString() == "Telemetry")
{
var connectionDeviceId = message.SystemProperties["iothub-connection-device-id"].ToString();
var msg = Encoding.UTF8.GetString(message.Body.Array);
log.LogInformation($"DeviceId = {connectionDeviceId}, Telemetry = {msg}");
var twinPatch = JsonConvert.SerializeObject(new { tags = new { telemetry = new { lastUpdated = message.SystemProperties["iothub-enqueuedtime"], data = JObject.Parse(msg) }}});
await registryManager.ReplaceTwinAsync(connectionDeviceId, twinPatch, "*");
}
}function.json:
{
"bindings": [
{
"name": "message",
"connection": "my_IOTHUB",
"eventHubName": "my_IOTHUB_NAME",
"consumerGroup": "function",
"cardinality": "one",
"direction": "in",
"type": "eventHubTrigger"
}
]
}一旦遥测数据存储在设备孪生标记属性中,我们就可以使用所有iothub内置特性来编程查询和检索设备双胞胎,或者使用蔚蓝门户。
下面的示例显示从所有设备获取遥测数据的查询字符串:
SELECT devices.id, devices.tags.telemetry FROM devices WHERE is_defined(devices.tags.telemetry)使用REST获取装置,我们可以获得查询字符串的结果,请参见下面的示例:

使用休息一下,去找双胞胎,我们可以获得特定设备的设备孪生点,请参见下面的示例:

备注:
https://stackoverflow.com/questions/65537175
复制相似问题