30分钟内将树莓派连接到微软云Azure IoT Hub并将
admin
2023-02-02 03:19:54
0

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


更多内容请关注公众号”云计算实战“


本文完整的操作视频参见:https://v.qq.com/x/page/f3025q4e75x.html

30分钟内将树莓派连接到微软云Azure IoT Hub并将


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

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

30分钟内将树莓派连接到微软云Azure IoT Hub并将


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


Azure 上的物联网服务介绍


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

30分钟内将树莓派连接到微软云Azure IoT Hub并将


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


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


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



树莓派上传数据的代码:

import random
import time
import sys

# 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.
import iothub_client
# pylint: disable=E0611
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult
from iothub_client import IoTHubMessage, IoTHubMessageDispositionResult, IoTHubError, DeviceMethodReturnValue

# 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=iothubforsatest.azure-devices.cn;DeviceId=test001;SharedAccessKey=kev0eMtTv2UfUU+JD6WAQN2sSdNI9QnRbs4nv2n+1vg="

# Using the MQTT protocol.
PROTOCOL = IoTHubTransportProvider.MQTT
MESSAGE_TIMEOUT = 10000

# Define the JSON message to send to IoT Hub.
TEMPERATURE = 100.0
HUMIDITY = 60
MSG_TXT = "{\"temperature\": %.2f,\"humidity\": %.2f,\"deviceid\": 'test0001'}"

def send_confirmation_callback(message, result, user_context):
    print ( "IoT Hub responded to message with status: %s" % (result) )

def iothub_client_init():
    # Create an IoT Hub client
    client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
    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 % (temperature, humidity)
            message = IoTHubMessage(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.
            prop_map = message.properties()
            if temperature > 30:
              prop_map.add("temperatureAlert", "true")
            else:
              prop_map.add("temperatureAlert", "false")

            # Send the message.
            print( "Sending message: %s" % message.get_string() )
            client.send_event_async(message, send_confirmation_callback, None)
            time.sleep(3)

    except IoTHubError as iothub_error:
        print ( "Unexpected error %s from IoTHub" % iothub_error )
        return
    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/


相关内容

热门资讯

玩家最新攻略“新毛豆互娱有挂吗... 网上科普关于“新毛豆互娱有没有挂”话题很是火热,小编也是针对新毛豆互娱作*弊开挂的方法以及开挂对应的...
我来教教您“乐禧乾安麻将真的有... 家人们!今天小编来为大家解答乐禧乾安麻将透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里...
【今日要闻】“新老夫子拼三张怎... 您好:新老夫子拼三张这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这...
玩家最新攻略“开心麻花到底是不... 有 亲,根据资深记者爆料开心麻花是可以开挂的,确实有挂(咨询软件无需打开...
重磅消息“嘟嘟十三张是不是有挂... 有 亲,根据资深记者爆料嘟嘟十三张是可以开挂的,确实有挂(咨询软件无需打...
终于了解“嘟嘟十三张可以开挂吗... 家人们!今天小编来为大家解答嘟嘟十三张透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里买...
【第一消息】“麻友圈2挪来挪去... 【第一消息】“麻友圈2挪来挪去有挂吗?”(太坑了果然有挂)您好,麻友圈2挪来挪去这个游戏其实有挂的,...
玩家最新攻略“闽悦麻将开挂器?... 家人们!今天小编来为大家解答闽悦麻将透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里买很...
【第一消息】“一乐浙江有挂吗?... 【第一消息】“一乐浙江有挂吗?”(外卦神器下载)您好,一乐浙江这个游戏其实有挂的,确实是有挂的,需要...
玩家最新攻略“瓦力游戏开挂器?... 家人们!今天小编来为大家解答瓦力游戏透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里买很...