30分鍾連接樹莓派到微軟雲 Azure IoT Hub,並將數據進行可視化


更多內容,關注公眾號:

 

樹莓派是很多動手達人必備的小玩具,本節內容,讓我們拿出樹莓派,在30分鍾內,將樹莓派連接到微軟雲Azure的IoT Hub,然后將溫濕度曲線可視化。
(本節內容完整視頻在文章末尾。)

 

 

本節內容中,樹莓派發送的數據是模擬出來的,並沒有真實的連接到傳感器,您可以選購不同的傳感器來采集真實的環境信息。

Azure IoT Hub 為我們提供了設備與雲雙向通訊的能力,通過多種語言的SDK,我們能輕松快速的將樹莓派接入到雲。本案例使用微軟官方代碼,示例代碼一共約70行,非常簡單。

 

關於IoT Hub的更多內容,請參考:

https://mp.weixin.qq.com/s?__biz=Mzg2OTEyNzMzOQ==&mid=2247483659&idx=1&sn=68cdff986d7dcc9b6233e48ba820300c&chksm=cea084cff9d70dd9898cbec3b2fe3e06175f7288acd244af9cc6f1b5fe0e5aa0fc946da585ad&scene=21#wechat_redirect

 

時序見解(Azure Time Series Insights)用來存儲時間序列的值,同時提供UI,將數據可視化。

 

關於時序見解的更多內容,請參考:

https://mp.weixin.qq.com/s?__biz=Mzg2OTEyNzMzOQ==&mid=2247483703&idx=1&sn=a6ca6b60e5bd11359a3684e222fb2716&chksm=cea084f3f9d70de51626da3ca4816ae6510bc8c9418c3365184ce4936b3e7b708a3306ab982c&scene=21#wechat_redirect

 

時序見解和IoT Hub可以無縫連接,無需寫代碼即可將上傳到IoT Hub的數據進行可視化。

樹莓派上傳數據的代碼:

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 = "{your string}"

# 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 接入文檔,請參考:

 

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/

 

 完整實戰視頻如下:

https://v.qq.com/x/page/f3025q4e75x.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM