30分钟内将树莓派连接到微软云Azure IoT Hub并将数据展示成图表 - 图1
    树莓派是很多动手达人必备的小玩具,本节内容,让我们拿出树莓派,在30分钟内,将树莓派连接到微软云Azure的IoT Hub,然后将温湿度曲线可视化。
    (本节内容完整视频在文章末尾。)

    30分钟内将树莓派连接到微软云Azure IoT Hub并将数据展示成图表 - 图2

    本节内容中,树莓派发送的数据是模拟出来的,并没有真实的连接到传感器,您可以选购不同的传感器来采集真实的环境信息。

    Azure IoT Hub 为我们提供了设备与云双向通讯的能力,通过多种语言的SDK,我们能轻松快速的将树莓派接入到云。本案例使用微软官方代码,示例代码一共约70行,非常简单。
    30分钟内将树莓派连接到微软云Azure IoT Hub并将数据展示成图表 - 图3

    关于IoT Hub的更多内容,请参考:

    Azure 上的物联网服务介绍

    时序见解(Azure Time Series Insights)用来存储时间序列的值,同时提供UI,将数据可视化。
    30分钟内将树莓派连接到微软云Azure IoT Hub并将数据展示成图表 - 图4

    关于时序见解的更多内容,请参考:

    Azure Time Series Insights-时序见解(1)

    时序见解和IoT Hub可以无缝连接,无需写代码即可将上传到IoT Hub的数据进行可视化。

    树莓派上传数据的代码:

    1. import random
    2. import time
    3. # Using the Python Device SDK for IoT Hub:
    4. # https://github.com/Azure/azure-iot-sdk-python
    5. # The sample connects to a device-specific MQTT endpoint on your IoT Hub.
    6. from azure.iot.device import IoTHubDeviceClient, Message
    7. # The device connection string to authenticate the device with your IoT hub.
    8. # Using the Azure CLI:
    9. # az iot hub device-identity show-connection-string --hub-name {YourIoTHubName} --device-id MyNodeDevice --output table
    10. CONNECTION_STRING = "{your string}"
    11. # Define the JSON message to send to IoT Hub.
    12. TEMPERATURE = 20.0
    13. HUMIDITY = 60
    14. MSG_TXT = '{{"temperature": {temperature},"humidity": {humidity}}}'
    15. def iothub_client_init():
    16. # Create an IoT Hub client
    17. client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
    18. return client
    19. def iothub_client_telemetry_sample_run():
    20. try:
    21. client = iothub_client_init()
    22. print ( "IoT Hub device sending periodic messages, press Ctrl-C to exit" )
    23. while True:
    24. # Build the message with simulated telemetry values.
    25. temperature = TEMPERATURE + (random.random() * 15)
    26. humidity = HUMIDITY + (random.random() * 20)
    27. msg_txt_formatted = MSG_TXT.format(temperature=temperature, humidity=humidity)
    28. message = Message(msg_txt_formatted)
    29. # Add a custom application property to the message.
    30. # An IoT hub can filter on these properties without access to the message body.
    31. if temperature > 30:
    32. message.custom_properties["temperatureAlert"] = "true"
    33. else:
    34. message.custom_properties["temperatureAlert"] = "false"
    35. # Send the message.
    36. print( "Sending message: {}".format(message) )
    37. client.send_message(message)
    38. print ( "Message successfully sent" )
    39. time.sleep(1)
    40. except KeyboardInterrupt:
    41. print ( "IoTHubClient sample stopped" )
    42. if __name__ == '__main__':
    43. print ( "IoT Hub Quickstart #1 - Simulated device" )
    44. print ( "Press Ctrl-C to exit" )
    45. iothub_client_telemetry_sample_run()

    IoT Hub 接入文档,请参考:

    https://docs.azure.cn/zh-cn/iot-hub/quickstart-send-telemetry-python

    树莓派系统下载:

    https://www.raspberrypi.org/downloads/

    Micro SD卡格式化工具:

    https://www.sdcard.org/downloads/index.html

    树莓派系统写入Micro SD卡工具:

    https://sourceforge.net/projects/win32diskimager/

    完整视频如下:

    完整 树莓派链接IOT hub.mp4 (169.2MB) 未完待续…

    以下内容将在以后的文章中介绍:
    树莓派采集真实的传感器数据;
    使用流分析(Stream Analytics)对上传的数据进行时时分析。

    image.png