本案例适用于开发者入门理解Azure Functions/ IoT Hub / Service Bus / Power BI等几款产品。

    设备数据通过Azure Functions 推送到 Power BI 数据大屏进行展示(1.准备工作) - 图1
    主要实战的内容为:

    1. 将设备遥测数据上传到物联网中心,
    2. 将遥测数据路由到消息中间件的Topic中,
    3. 使用Azure Function解析消息中间件Topic中的消息并推送到大屏 。

    本文主要是本案例的准备工作,即(第1条和第2条的内容):

    1.创建IoT Hub:

    创建IOT hub.mp4 (14.4MB) 2.创建Service Bus:

    创建sb.mp4 (10.09MB)

    1. 创建IoT Hub 消息路由,将遥测消息路由到Service Bus Topic

    iot 路由到 sb.mp4 (20.74MB) 本示例中的Python Device 代码来自于微软官网,请参照:
    https://docs.azure.cn/zh-cn/iot-hub/quickstart-send-telemetry-python

    1. # Copyright (c) Microsoft. All rights reserved.
    2. # Licensed under the MIT license. See LICENSE file in the project root for full license information.
    3. import random
    4. import time
    5. import sys
    6. # Using the Python Device SDK for IoT Hub:
    7. # https://github.com/Azure/azure-iot-sdk-python
    8. # The sample connects to a device-specific MQTT endpoint on your IoT Hub.
    9. import iothub_client
    10. # pylint: disable=E0611
    11. from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult
    12. from iothub_client import IoTHubMessage, IoTHubMessageDispositionResult, IoTHubError, DeviceMethodReturnValue
    13. # The device connection string to authenticate the device with your IoT hub.
    14. # Using the Azure CLI:
    15. # az iot hub device-identity show-connection-string --hub-name {YourIoTHubName} --device-id MyNodeDevice --output table
    16. CONNECTION_STRING = "your device conn string"
    17. # Using the MQTT protocol.
    18. PROTOCOL = IoTHubTransportProvider.MQTT
    19. MESSAGE_TIMEOUT = 10000
    20. # Define the JSON message to send to IoT Hub.
    21. TEMPERATURE = 20.0
    22. HUMIDITY = 60
    23. MSG_TXT = "{\"temperature\": %.2f,\"humidity\": %.2f}"
    24. def send_confirmation_callback(message, result, user_context):
    25. print ( "IoT Hub responded to message with status: %s" % (result) )
    26. def iothub_client_init():
    27. # Create an IoT Hub client
    28. client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
    29. return client
    30. def iothub_client_telemetry_sample_run():
    31. try:
    32. client = iothub_client_init()
    33. print ( "IoT Hub device sending periodic messages, press Ctrl-C to exit" )
    34. while True:
    35. # Build the message with simulated telemetry values.
    36. temperature = TEMPERATURE + (random.random() * 15)
    37. humidity = HUMIDITY + (random.random() * 20)
    38. msg_txt_formatted = MSG_TXT % (temperature, humidity)
    39. message = IoTHubMessage(msg_txt_formatted)
    40. # Add a custom application property to the message.
    41. # An IoT hub can filter on these properties without access to the message body.
    42. prop_map = message.properties()
    43. if temperature > 30:
    44. prop_map.add("temperatureAlert", "true")
    45. else:
    46. prop_map.add("temperatureAlert", "false")
    47. # Send the message.
    48. print( "Sending message: %s" % message.get_string() )
    49. client.send_event_async(message, send_confirmation_callback, None)
    50. time.sleep(3)
    51. except IoTHubError as iothub_error:
    52. print ( "Unexpected error %s from IoTHub" % iothub_error )
    53. return
    54. except KeyboardInterrupt:
    55. print ( "IoTHubClient sample stopped" )
    56. if __name__ == '__main__':
    57. print ( "IoT Hub Quickstart #1 - Simulated device" )
    58. print ( "Press Ctrl-C to exit" )
    59. iothub_client_telemetry_sample_run()

    image.png