阿里云物联网IOT平台使用案例教程
我将介绍如何通过MQTT、阿里IOT SDK完成设备与云平台对接前言最近尝试了一下阿里云IoT物联网平台,还是蛮强大的。在此记录一下学习过程。本教程不需要任何外围硬件,一台电脑和一根能上网的网线即可。算是一篇Hello World了。先上效果图https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215517yvejr8d9z35j1jrm.jpg?x-oss-process=image/resize,w_1400/format,webp第一章 准备工作1.1 注册阿里云账号首先打开阿里云的官网并注册新用户,网址为:https://www.aliyun.com 【阿里云官网】https://www.aliyun.com/product/iot 【阿里云物联网平台】如果有不熟悉阿里云物联网平台怎么用的朋友,可以查阅官网教程文档 阿里云物联网帮助文档https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215131sup39ivsc9aoxz5u.jpg?x-oss-process=image/resize,w_1400/format,webp1.2 注册完成后自动跳转到实名认证界面,可以通过支付宝授权快速完成认证。https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215929kzrn2rmzr9zzsr0x.jpg?x-oss-process=image/resize,w_1400/format,webp1.3 开通物联网平台在左边菜单选择《产品与服务》中的物联网平台,并激活开通此功能。https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/220337b8kmi9i2i3vbekzo.jpg?x-oss-process=image/resize,w_1400/format,webp开通成功后,即可进入管理控制台https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215135wo6ygs6g0okvt66y.jpg?x-oss-process=image/resize,w_1400/format,webp1.4 软件环境搭建下载并安装编辑器VS Code(全称Visual Studio Code)https://code.visualstudio.com/https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215143evzu31m5hta3avvx.jpg?x-oss-process=image/resize,w_1400/format,webp1.5 下载并安装Nodejshttps://nodejs.org/en/download/https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215148th6e5fbn36s6643e.jpg?x-oss-process=image/resize,w_1400/format,webp第二章 IoT云端开发2.1 打开阿里云控制台https://iot.console.aliyun.comhttps://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215154ty9z22m22rux2yb2.jpg?x-oss-process=image/resize,w_1400/format,webp2.2 创建高级版产品在产品管理界面点击 创建产品,并选择高级版https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215212otth77zwt916un9n.jpg?x-oss-process=image/resize,w_1400/format,webp2.3 新增产品属性在产品详情的功能定义中点击 新增,创建属性https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215214r44zzis84e6wxet4.jpg?x-oss-process=image/resize,w_1400/format,webp新增 温度。此图是我更新,注意截图中的标识符是小写的temperature(原文是大写,与代码不一致)https://codeantenna.com/image/https://ucc.alicdn.com/images/user-upload-01/20190630220801983.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d3ZWlydQ==,size_16,color_FFFFFF,t_70&x-oss-process=image/resize,w_1400/format,webp新增 湿度https://codeantenna.com/image/https://ucc.alicdn.com/images/user-upload-01/2019063022101226.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d3ZWlydQ==,size_16,color_FFFFFF,t_70&x-oss-process=image/resize,w_1400/format,webp注意:标识符非常重要,是JSON里传递的属性!而且要区分大小写!属性界面一览https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215428tp55v01c55vlqvz5.jpg?x-oss-process=image/resize,w_1400/format,webp2.4 创建设备首先点击设备管理,并在下拉菜单中选择Demo产品https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215438pvtqqrrgz0fgmo1s.jpg?x-oss-process=image/resize,w_1400/format,webp点击 添加设备,并命名Unit01https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215442pd5mzqcwuzq0juwp.jpg?x-oss-process=image/resize,w_1400/format,webp创建完成后获得该设备的重要数据——三元组https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215445qkuuwd417ejw1wm5.jpg?x-oss-process=image/resize,w_1400/format,webp可以继续添加其他设备https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215502fara0cpn2x629pcc.jpg?x-oss-process=image/resize,w_1400/format,webp云端的任务告一段落。点击Unit01进入设备详情,可以看到运行状态中并无数据。https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215502eztpsefl9ph9fmmc.jpg?x-oss-process=image/resize,w_1400/format,webp第三章 设备端开发本教程用nodejs程序来模拟设备,建立连接,上报数据。无实际硬件。3.1 在电脑桌面创建设备端项目创建项目文件夹 aliyun-iot-demo,本Demo共需要2个文件和一个库创建文件package.json,代码如下{ "name": "aliyun-iot-demo", "dependencies": { "aliyun-iot-mqtt": "^0.0.4" }, "author": "wongxming", "license": "MIT" }注:代码中author是原作者 3.2 下载阿里云物联网套件硬件端SDK在VS Code中打开CMD界面 用CD指令找到项目所在路径 用NPM指令下载阿里IoT的MQTT库,笔者的下载进度卡住过,重试很多次才成功。文末有下载好的压缩包,复制到项目路径下即可。npm install aliyun-iot-mqtt -S注意:我在VS Code中和电脑CMD中都试过这段代码,一直报npm json错误。后来我去原文地址重新下载了源代码,并上传到了CSDN(CSDN设置积分为5分),资源名是“10分钟物联网设备接入阿里云IoT平台(源代码)”https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215513dqw1sjsh15zm3ww5.jpg?x-oss-process=image/resize,w_1400/format,webp3.3 创建Nodejs程序文件thermometer.js//1.引入阿里云IoT的sdkconst mqtt = require('aliyun-iot-mqtt'); //2.设备属性const options = { productKey: "产品productKey,这里是a1wgoXxYRbs", deviceName: "设备deviceName,这里是Unit01", deviceSecret: "设备的secret,AEU2vP略", regionId: "cn-shanghai"};//完整属性如下所示,需要根据您产品和设备的属性来修改//const options = {// productKey: "a1wgoXxYRbs", // deviceName: "Unit01",// deviceSecret: "AEU2vP",// regionId: "cn-shanghai"}; //3.建立连接const client = mqtt.getAliyunIotMqttClient(options); //4.属性数据上报const topic = `/sys/${options.productKey}/${options.deviceName}/thing/event/property/post`;setInterval(function() { //发布数据到topic client.publish(topic, getPostData()); }, 5 * 1000); function getPostData(){ const payloadJson = { id: Date.now(), params: { Temperature: Math.floor((Math.random() * 20) + 10), Humidity: Math.floor((Math.random() * 20) + 60) }, method: "thing.event.property.post" } console.log("===postData topic=" + topic) console.log(payloadJson) return JSON.stringify(payloadJson); }复制代码https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215514eblrrz6nzenr0enc.jpg?x-oss-process=image/resize,w_1400/format,webp3.4 运行程序在CMD窗口输入指令,运行代码,启动设备终端node thermometer.jshttps://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/224312qcn105sfpgpzp1cp.jpg?x-oss-process=image/resize,w_1400/format,webp程序会定期上传随机的温度和湿度数据https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215516kretodt0zryqvt5l.jpg?x-oss-process=image/resize,w_1400/format,webp第四章 最终效果回到控制台,在运行状态页面,激活 实时刷新,就可以看到温度和湿度值了https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/233539va2zat7gaj582fr8.jpg?x-oss-process=image/resize,w_1400/format,webp回到设备管理界面,可以看到当前只有Unit01设备在线https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/215521bz21p2ifdzrm41hr.jpg?x-oss-process=image/resize,w_1400/format,webp本例框图https://codeantenna.com/image/https://image.geek-workshop.com/forum/201809/10/233541t2ixbd2edy4xb6u6.jpg?x-oss-process=image/resize,w_1400/format,webp
页:
[1]