从0到1学习Azure IoT:基于azure-mol-samples的物联网应用开发
【免费下载链接】azure-mol-samplesSupporting resources for "Learn Azure in a Month of Lunches" (Manning Publications)项目地址: https://gitcode.com/gh_mirrors/az/azure-mol-samples
azure-mol-samples是微软官方支持的物联网开发学习项目,提供了完整的Azure IoT Hub部署与设备连接示例。通过本指南,你将快速掌握如何使用Azure云服务构建稳定、安全的物联网应用,实现设备数据采集、实时分析与Web可视化。
🌐 什么是Azure IoT Hub?
Azure IoT Hub是微软提供的全托管服务,作为物联网设备与云平台之间的通信中枢。它支持:
- 双向安全通信(设备到云/云到设备)
- 设备身份管理与访问控制
- 消息路由与事件处理
- 高扩展性(支持数百万设备连接)
在**azure-mol-samples/20/**目录中,提供了完整的IoT Hub创建脚本与应用示例,帮助开发者快速上手。
🚀 快速部署IoT环境的3个步骤
1. 创建资源组与IoT Hub
使用Azure CLI一键创建基础环境:
# 创建资源组 az group create --name azuremolchapter20 --location eastus # 创建免费版IoT Hub(每天支持8000条消息) az iot hub create \ --resource-group azuremolchapter20 \ --name azuremol \ --sku f1代码来源:azure_cli_sample.sh
2. 注册IoT设备身份
为你的设备(如树莓派)创建唯一身份标识:
# 添加IoT CLI扩展 az extension add --name azure-cli-iot-ext # 创建设备身份 az iot hub device-identity create \ --hub-name azuremol \ --device-id raspberrypi # 获取设备连接字符串(用于设备端编程) az iot hub device-identity show-connection-string \ --hub-name azuremol \ --device-id raspberrypi代码来源:azure_cli_sample.sh
3. 部署实时数据可视化Web应用
项目提供了基于WebSocket的实时数据展示应用:
# 创建Web App服务计划 az appservice plan create \ --resource-group azuremolchapter20 \ --name azuremol \ --sku f1 # 部署应用代码 git clone https://gitcode.com/gh_mirrors/az/azure-mol-samples cd azure-mol-samples/20 git remote add molwebappiot $(az webapp deployment source config-local-git --resource-group azuremolchapter20 --name $webAppName -o tsv) git push molwebappiot master应用配置:server.js 通过WebSocket将IoT Hub数据推送到前端页面
💻 核心代码解析
IoT设备数据接收逻辑
IoTHub/iot-hub.js实现了从IoT Hub接收设备消息的核心功能:
// 连接到IoT Hub并读取设备消息 IoTHubReaderClient.prototype.startReadMessage = function(cb) { this.iotHubClient.open() .then(this.iotHubClient.getPartitionIds.bind(this.iotHubClient)) .then((partitionIds) => { return partitionIds.map((partitionId) => { return this.iotHubClient.createReceiver(this.consumerGroupName, partitionId, { 'startAfterTime': Date.now() }) .then((receiver) => { receiver.on('message', (message) => { cb(message.body.toString(), message.enqueuedTimeUtc); }); }); }); }); };温度数据分析处理
analyzeTemperature.js展示了如何处理设备上传的传感器数据:
// 从IoT设备数据中提取温度值 function analyzeTemperature(context, myEventHubMessages) { context.log(`JavaScript eventhub trigger function called for message array: ${myEventHubMessages}`); myEventHubMessages.forEach((message, index) => { context.log(`Processed message ${index}: ${message}`); var temperature = JSON.parse(message).temperature; // 温度异常检测逻辑 if (temperature > 30) { context.log(`High temperature detected: ${temperature}°C`); // 可在此处添加告警逻辑 } }); }📊 应用架构与工作流程
- 设备层:树莓派等IoT设备通过MQTT协议连接到IoT Hub
- 数据处理层:
- IoT Hub接收设备消息并存储到事件中心
- 消费者组
molwebapp[azure_cli_sample.sh#L80-L82] 负责将消息路由到Web应用
- 应用层:
- Web App通过server.js建立WebSocket连接
- 实时数据通过views/index.ejs渲染到前端页面
❓ 常见问题解决
Q: 免费版IoT Hub消息限制如何处理?
A: 免费层每天支持8000条消息,适合开发测试。生产环境可升级到S1/S2层,通过azure_cli_sample.sh#L49命令监控消息配额使用情况。
Q: 如何扩展到多设备场景?
A: 重复执行设备注册步骤为每个设备创建唯一ID,确保设备端代码使用正确的连接字符串。
🎯 下一步学习路径
- 尝试修改analyzeTemperature.js添加自定义数据处理逻辑
- 探索azure-mol-samples/21/目录中的Azure Functions集成示例
- 参考官方文档配置设备孪生(Device Twin)实现远程设备管理
通过azure-mol-samples项目,即使是物联网开发新手也能在短时间内构建起完整的Azure IoT解决方案。立即克隆项目开始你的物联网开发之旅吧!
【免费下载链接】azure-mol-samplesSupporting resources for "Learn Azure in a Month of Lunches" (Manning Publications)项目地址: https://gitcode.com/gh_mirrors/az/azure-mol-samples
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考