千家信息网

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

发表于:2024-10-14 作者:千家信息网编辑
千家信息网最后更新 2024年10月14日,树莓派是很多动手达人必备的小玩具,本节内容,让我们拿出树莓派,在30分钟内,将树莓派连接到微软云Azure的IoT Hub,然后将温湿度曲线可视化。更多内容请关注公众号"云计算实战"本文完整的操作视频
千家信息网最后更新 2024年10月14日30分钟内将树莓派连接到微软云Azure IoT Hub并将

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


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


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


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

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


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


Azure 上的物联网服务介绍


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


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


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


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



树莓派上传数据的代码:

import randomimport timeimport 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=E0611from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResultfrom 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 tableCONNECTION_STRING = "HostName=iothubforsatest.azure-devices.cn;DeviceId=test001;SharedAccessKey=kev0eMtTv2UfUU+JD6WAQN2sSdNI9QnRbs4nv2n+1vg="# Using the MQTT protocol.PROTOCOL = IoTHubTransportProvider.MQTTMESSAGE_TIMEOUT = 10000# Define the JSON message to send to IoT Hub.TEMPERATURE = 100.0HUMIDITY = 60MSG_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 clientdef 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/


树莓 内容 代码 数据 时序 见解 更多 参考 可视化 微软 传感器 工具 派系 接入 不同 信息 公众 双向 同时 多种 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 公司服务器系统 锐起无盘 服务器 瑞金市点寰互联网科技 云服务器找回微信删掉的信息 网络安全等级测评工作流程 四川企业软件开发哪里好 web 3d软件开发 网络安全论坛永久会址最新消息 真正能接到单的软件开发 ibm服务器黑屏无法开机 计算机网络技术哪个方向最好 联想服务器不识别光驱 计算机网络技术基础考点 上门软件开发零售价格 网络安全的投诉电话是多少 大学文献检索数据库类型包括 河南放心软件开发服务品质保障 分组交换的网络技术 安卓的数据库是哪个公司的 用友t6如何指向服务器 世界服务器连接不上怎么办 银行网络安全知识宣传周 大米加工环节数据库字段 与网络安全的信息 川大网络安全研究生复试线 n11数据库充值 深圳电子软件开发工资多少 英雄联盟连不到服务器怎么办 托福单词软件开发 怎么检验数据库安装好了没
0