我是靠谱客的博主 鲜艳草丛,这篇文章主要介绍【IoT 毕业设计】Ruff硬件+阿里云IoT+微信小程序构建环境监控系统,现在分享给大家,希望可以做个参考。

   0.技术架构   

 IoT 物联网毕业设计实战采用 Ruff 开发板,串口连接温湿度传感器DHT11和气质量传感器SDS011,每5分钟采集一次数据,通过MQTT协议发送到阿里云 IoT 物联网平台,写入云端的设备影子中。微信小程序调用阿里云函数计算FC,读取 IoT 物联网平台的设备影子中的数据,通过ECharts的仪表盘来展示空气质量 PM2.5 指数,温度和湿度值。

整体架构如下图所示:

e8f15cece5e96aa31a54ee6d572a706a.png

   1.硬件设备   

本次开发实战硬件清单如下:

5ec3ab8fd752fd85ecc5172774e33b1c.png

万能的淘宝有售

   2.阿里云开发   

2.1 物联网平台开发

2.1.1.免费开通阿里云 IoT物联网云服务:

https://www.aliyun.com/product/iot

67a48c5db7481c78d5315c3453eb8348.png

2.1.2.创建产品家居环境监测器,选择自定义品类,直连设备:

76ab11e5a20b0f775d89e2c893663662.png

2.1.3.注册设备,此时设备为未激活状态。

74867cbd9c644bbaf1ebe508559fd7c7.png

点击DeviceSecret的查看,获取设备身份三元组。

921ca318c085b57a2d3cc2224f20d9c7.png

2.1.4.设备更新最新传感器数据到设备影子中,对应的通信Topic和Payload格式如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
通信 Topic: /shadow/update/${ProductKey}/${DeviceName} 数据 Payload: { "method":"update", "state":{ "reported":{ "temperature":27, "humidity":45, "pm25":23, "pm10":36 } }, "version":156768564324 }

2.2 函数计算FC开发

2.2.1.免费开通阿里云 函数计算FC 云服务:

https://www.aliyun.com/product/fc

4a78b75efe9e2278ca616ad289f41ecb.png

2.2.2.创建基于HTTP触发器的函数服务:

6c3b15bb4bcff8952375ac500aa9ff41.png

2.2.3.采用 Nodejs 语言来编写函数,调用IoT物联网平台的 POP API :getDeviceShadow 来获取设备上报的实时数据。

官网文档:

https://help.aliyun.com/document_detail/69953.html

d6cf4bee72790341c3c13720e509b96d.png

获取设备影子核心代码,如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
// 1.构造获取设备影子 API参数 const action = 'GetDeviceShadow'; const params = { ProductKey: productKey, DeviceName: deviceName }; //2.发送请求 const response = yield aliyunPopAPIClient.request(action, params); const ShadowMessage = JSON.parse(response.ShadowMessage)

   3.Ruff 硬件开发   

Ruff 是一个支持 JavaScript 开发应用的物联网操作系统,为软件开发者提供开放、高效、敏捷的物联网应用开发平台,让 IoT 应用开发更简单。

整个 Ruff 开发体系包括 Ruff OS、Ruff SDK、Ruff 软件仓库、Ruff Kit 开发套件。只要您有JavaScript开发经验,就可以用 Ruff 开发硬件应用。

3.1.传感器驱动依赖:

058cbe6edd7c2b58f460bafe1b152a5f.png

3.2.读取温湿度和空气质量传感器数据:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 空气质量数据 $('#air').on('aqi', function(error, pm25, pm10) { if (error) return; reported.pm25 = pm25; reported.pm10 = pm10; }); // 温度数据 $('#dht').getTemperature(function(error, temperature) { if (!error) { reported.temperature = temperature; } }); // 湿度数据 $('#dht').getRelativeHumidity(function(error, humidity) { if (!error) { reported.humidity = humidity; } });

3.3.上报数据到 IoT 物联网平台云端:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 设备身份三元组信息 var options = { productKey: "替换productKey", deviceName: "替换deviceName", deviceSecret: "替换deviceSecret",     host: "MQTT接入点域名", }; var updateShadowTopic = "/shadow/update/" + options.productKey + "/" + options.deviceName; // 建立 MQTT 连接 var client = MQTT.createAliyunIotMqttClient(options); var data = { method: "update", state: { reported: reported }, version: Date.now() } // 上报数据 client.publish(updateShadowTopic, JSON.stringify(data));

   4.微信小程序   

4.1.在微信小程序后台配置合法域名:

28aeba5711bb0f1ce2814f399694659f.png

4.2.微信小程序交互界面开发:

3945cbf5decafda97f37befae38f168a.png

4.3.使用 wx.request 完成网络请求,获取云端设备影子数据:

7797a6129d9b7d09047f8840ef8d4a06.png

请求代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
wx.request({ url: '函数计算的 HTTP API url', method: 'POST', data: { "deviceName": "设备名", "productKey": "产品code" }, header: { 'content-type': 'application/json' }, success(res) { console.log(res.data) wx.hideLoading() // 更新到 UI 界面 updateUI(res.data) } })

4.4.使用ECharts中的仪表盘组件展示空气质量指数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import * as echarts from '../../ec-canvas/echarts'; function initChart(canvas, width, height) { const chart = echarts.init(canvas, null, { width: width, height: height });   canvas.setChart(chart); var option = { backgroundColor: "#f8f8f8", color: ["#37A2DA", "#32C5E9", "#67E0E3"], series: [{ name: '空气质量', min: 0, max: 500, splitNumber: 10, type: 'gauge', detail: { formatter: '{value}' }, axisLine: { show: true, lineStyle: { width: 10, shadowBlur: 0, color: [ [0.3, '#67e0e3'], [0.7, '#37a2da'], [1, '#fd666d'] ] } }, data: [{ value: 80, name:'空气质量' }], splitLine: { show: true, length: 13, lineStyle: { color: '#aaa', width: 2, type: 'solid' } }, title: { show: true, offsetCenter: [0, 70], textStyle: { color: '#333', fontSize: 15 } }, pointer: { length: '90%', width: 6, color: 'auto' } }] };   chart.setOption(option, true); return chart; } Page({ data: { ec: { onInit: initChart } } })

至此,我们基于JavaScript 语言完成了智能家居环境监测IoT应用场景的落地。

207e1511b1ee19b05910fdf3e56e2542.png


最后,赠送16元优惠券,加入国内最大IoT物联网开发者社区,获取1000+行业资料

5aa5a1edb30ac234adcc523933a93b08.png

往期推荐

☞ 中国云计算第一股关停 IoT云服务

☞ 2022年IoT平台趋势:私有化部署

☞ 国内MCU行业发展研究报告

☞ 2021年4G通信模组企业排行

☞ 国内4大 IoT物联网平台选型对比

☞ 云厂商的[IoT物联网平台]不香了吗?

4cc82bcbc7e5b804bedb764a097bdab5.gif

b145fe488dd093600dcc48f16d80ee75.gif

f9fc43f0c21572d73f27889125144f81.gif

cd29d55a062b8b23fb573794eb84e02f.gif

a87fa8b540e26c6dc81b1eda4aee518d.gif

最后

以上就是鲜艳草丛最近收集整理的关于【IoT 毕业设计】Ruff硬件+阿里云IoT+微信小程序构建环境监控系统的全部内容,更多相关【IoT内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部