30分钟实战树莓派连接到微软云Azure IoT Hub并将数据可视化

更多内容,关注公众号:

在这里插入图片描述

树莓派是很多动手达人必备的小玩具,本节内容,让我们拿出树莓派,在30分钟内,将树莓派连接到微软云Azure的IoT Hub,然后将温湿度曲线可视化。

本实战完整视频:

树莓派连接到Azure IoT Hub 并用时序见解展示数据

在这里插入图片描述

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

Azure IoT Hub 为我们提供了设备与云双向通讯的能力,通过多种语言的SDK,我们能轻松快速的将树莓派接入到云。本案例使用微软官方代码,示例代码一共约70行,非常简单。
在这里插入图片描述

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

Azure IoT 产品介绍

时序见解(Azure Time Series Insights)用来存储时间序列的值,同时提供UI,将数据可视化。

在这里插入图片描述

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

Azure 时序见解

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

树莓派上传数据的代码:


```bash
import random
import time

# Using the Python Device SDK for IoT Hub:
#   https://github.com/Azure/azure-iot-sdk-python
# The sample connects to a device-specific MQTT endpoint on your IoT Hub.
from azure.iot.device import IoTHubDeviceClient, Message

# The device connection string to authenticate the device with your IoT hub.
# Using the Azure CLI:
# az iot hub device-identity show-connection-string --hub-name {YourIoTHubName} --device-id MyNodeDevice --output table
CONNECTION_STRING = "HostName=seanyupitest.azure-devices.cn;DeviceId=pi001;SharedAccessKey=iKPhil61uPFkJMtzg9wSL6OxSNcHPi71H7W3/BmzGbs="

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

def iothub_client_init():
    # Create an IoT Hub client
    client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
    return client

def iothub_client_telemetry_sample_run():

    try:
        client = iothub_client_init()
        print ( "IoT Hub device sending periodic messages, press Ctrl-C to exit" )

        while True:
            # Build the message with simulated telemetry values.
            temperature = TEMPERATURE + (random.random() * 15)
            humidity = HUMIDITY + (random.random() * 20)
            msg_txt_formatted = MSG_TXT.format(temperature=temperature, humidity=humidity)
            message = Message(msg_txt_formatted)

            # Add a custom application property to the message.
            # An IoT hub can filter on these properties without access to the message body.
            if temperature > 30:
              message.custom_properties["temperatureAlert"] = "true"
            else:
              message.custom_properties["temperatureAlert"] = "false"

            # Send the message.
            print( "Sending message: {}".format(message) )
            client.send_message(message)
            print ( "Message successfully sent" )
            time.sleep(1)

    except KeyboardInterrupt:
        print ( "IoTHubClient sample stopped" )

if __name__ == '__main__':
    print ( "IoT Hub Quickstart #1 - Simulated device" )
    print ( "Press Ctrl-C to exit" )
    iothub_client_telemetry_sample_run()```

IoT Hub 接入文档,请参考:

使用Azure IoT Device SDK

树莓派系统下载:
树莓派系统下载

Micro SD卡格式化工具:
SD卡格式化工具

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

发布了44 篇原创文章 · 获赞 4 · 访问量 1828

猜你喜欢

转载自blog.csdn.net/yushuzhen2008/article/details/103239131