【问题标题】:Retrieve sensor data from Azure EventHub Message with Azure Function (Python)使用 Azure Function (Python) 从 Azure EventHub 消息中检索传感器数据
【发布时间】:2020-10-05 21:01:01
【问题描述】:

我正在尝试将传感器数据从设备发送到 Azure IoT 中心,并在 Azure Function 中对其进行处理。当 Azure IoT Hub 使用 EventHubTrigger 接收数据时,我设法触发了该功能。

但是,我找不到任何方法来检索传感器数据。

从设备发送以下数据:

{"temperature": 27.5, "humidity": 63.0}

下面是我从 EventHubEvent 正文中得到的信息

{'properties': {}, 'systemProperties': {'iothub-connection-device-id': 'raspberrypi', 'iothub-connection-auth-method': '{"scope":"device","type":"sas","issuer":"iothub","acceptingIpFilterRule":null}', 'iothub-connection-auth-generation-id': '637374141338740275', 'iothub-enqueuedtime': '2020-10-04T16:21:15.0190000Z', 'iothub-message-source': 'Telemetry'}, 'body': 'eyJ0ZW1wZXJhdHVyZSI6IDI3LjUsICJodW1pZGl0eSI6IDYzLjB9'}

发送方和函数应用的源代码如下:

发送者/设备(仅相关部分)

# Define the JSON message to send to IoT Hub.
MSG_TXT = '{{"temperature": {temperature}, "humidity": {humidity}}}'

client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)

msg_txt_formatted = MSG_TXT.format(temperature=sensor_data["temperature"]["value"], humidity=sensor_data["humidity"]["value"])
message = Message(msg_txt_formatted)
client.send_message(message)

功能应用(__init__.py)

from typing import List
import logging
import json

import azure.functions as func

def main(event: List[func.EventHubEvent]):
    body = event.get_body()
    logging.info(body)
    my_json = body.decode('utf8').replace("'", '"')
    event_data_json = json.loads(my_json)
    logging.info(event_data_json[0]["data"])

路由 IoTHub 遥测数据是否需要任何其他设置?任何帮助将不胜感激。

【问题讨论】:

  • 您是否为 IoT 中心的设备遥测消息设置了消息路由?
  • 非常感谢!我没有注意到我需要添加自定义端点..
  • 感谢您的澄清。我已根据您的输入添加了答案。您可以接受。

标签: python azure


【解决方案1】:

您需要从IoT hub blade 添加message routing。如果使用内置端点事件中心,请将 DeviceTelemetry 消息路由到 events 端点,否则路由到自定义端点,即在这种情况下,它是您在事件中心命名空间中创建的事件中心。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-09
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    相关资源
    最近更新 更多