我有一个 IoT Edge 模块过滤器模块,它使用
send_message
(docs) 和 IoTHubModuleClient
每分钟发送遥测数据。
当我运行此模块时,门户中的 IoT 中心消息计数器会递增。另外,我在 IoT Edge Hub 配置中使用了此路由进行部署:
"routes": {
"filtermoduleToIoTHub": "FROM /messages/modules/filtermodule/outputs/* INTO $upstream"
}
此外,我在 IoT 中心创建了一个自定义路由和端点,用于将消息路由到存储帐户(基于密钥的身份验证)。路线查询是
true
,我使用该路线测试了相同的遥测数据,并且所有测试都通过了,因为它与查询匹配。
不知何故,数据并未最终存储在存储帐户中,并且没有真正直接的方法来调试为什么会出现这种情况。在我将
IoTHubDeviceClient
与 send_message
(无 IoT Edge)一起使用之前,具有相同的路由和存储帐户,并且数据确实显示在存储帐户中。这里缺少什么?我发现 IoT Edge 和 IoT Hub 上的文档以及它们如何协同工作有时非常令人困惑。
编辑:
我发送的遥测数据具有以下格式(与之前工作时的格式相同):
{"Measurements": [{"Temperature": 20.5}, {"Temperature": 20.5}]}
我正在使用 Terraform 进行部署,并且此端点和路由曾经有效:
# IoT Hub endpoint for bronze ingestion storage container
resource "azurerm_iothub_endpoint_storage_container" "bronze_ingestion_endpoint" {
resource_group_name = azurerm_resource_group.this.name
iothub_id = azurerm_iothub.this.id
name = "bronze-ingestion"
container_name = azurerm_storage_container.bronze_ingestion_avro.name
connection_string = azurerm_storage_account.bronze.primary_connection_string
file_name_format = "{iothub}/{YYYY}/{MM}/{DD}/{HH}/{mm}_{partition}.avro"
batch_frequency_in_seconds = 600
max_chunk_size_in_bytes = 10485760
encoding = "Avro"
}
# IoT Hub route for bronze ingestion storage container
resource "azurerm_iothub_route" "bronze_ingestion_route" {
resource_group_name = azurerm_resource_group.this.name
iothub_name = azurerm_iothub.this.name
name = "bronze-ingestion-route"
source = "DeviceMessages"
condition = "true"
endpoint_names = [azurerm_iothub_endpoint_storage_container.bronze_ingestion_endpoint.name]
enabled = true
}
编辑2:
我可以确认,如果我在本地计算机上使用
IoTHubModuleClient.create_from_connection_string(conn_string)
,路由有效并且数据显示在存储帐户中。当我在边缘环境中的 IoT 设备上使用 IoTHubModuleClient.create_from_edge_environment()
时,路由不起作用(但门户中的消息计数器会增加)。
Azure IoT Explorer 也是如此。如果我在本地计算机上使用
IoTHubModuleClient.create_from_connection_string(conn_string)
,我可以监控传入事件,但如果我在 IoT 设备上使用 IoTHubModuleClient.create_from_edge_environment()
,则不会显示任何内容。
以下是使用 IoT 中心消息路由将设备数据发送到 Azure 存储的步骤: 来自边缘环境:
使用
检查特定模块的日志。(0r) 使用
iotedge logs <module_name>
查看从模块发送的消息。请参阅此链接了解如何使用 Visual Studio Code开发 Azure IoT Edge 模块。
sudo iotedge logs <ModuleName> -f
创建一个天蓝色的存储和容器。使用连接字符串连接 IoT 中心的端点。
az iot hub message-endpoint create storage-container -n {iothub_name} --en {endpoint_name} -c {connection_string} --container {container_name}
使用以下命令创建路线:
az iot hub message-route create --route-name $routeName --hub-name $hubName --resource-group $resourceGroup --source-type devicemessages --endpoint-name $endpointName --enabled true --condition true
运行发送消息代码。请参阅此链接以获取发送消息代码。
现在,您可以使用 IoT 中心消息路由将设备数据发送到 Azure 存储。