簡體   English   中英

使用 Azure 函數 (Python) 從 Azure EventHub 消息中檢索傳感器數據

[英]Retrieve sensor data from Azure EventHub Message with Azure Function (Python)

我正在嘗試將傳感器數據從設備發送到 Azure IoT 中心並在 Azure Function 中對其進行處理。 當 Azure IoT 中心使用 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 hub blade message routing 如果使用內置端點事件中心,則將 DeviceTelemetry 消息路由到events端點,否則路由到自定義端點,即在這種情況下,它是在事件中心命名空間中創建的事件中心。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM