亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

10分鐘物聯網設備接入阿里云IoT平臺

1. 准备工作

1.1 注册阿里云账号

使用个人淘宝账号或手机号,开通阿里云账号,并通过实名认证(可以用支付宝认证)

1.2 免费开通IoT物联网套件

产品官网 https://www.aliyun.com/product/iot

Screen Shot 2018-06-01 at 13.53.55.png | center | 569x357

1.3 软件环境

Nodejs安装 https://nodejs.org/en/download/
编辑器 sublimeText/nodepad++/vscode

2. 开发步骤

2.1 云端开发

1) 创建高级版产品

image.png | left | 747x253

2) 功能定义,产品物模型添加属性

image.png | left | 747x186

3) 注册设备,获得身份三元组

image.png | left | 747x188

2.2 设备端开发

我们以nodejs程序来模拟设备,建立连接,上报数据。

1) 应用程序目录结构

image.png | left | 412x159

2) package.json添加阿里云IoT套件sdk依赖

{
  "name": "aliyun-iot",
  "dependencies": {
    "aliyun-iot-mqtt": "^0.0.4"
  },
  "author": "wongxming",
  "license": "MIT"
}

下载安装SDK

$npm install

3) 模拟设备thermometer.js代码

/**
"dependencies": { "aliyun-iot-mqtt": "^0.0.4" }
*/
const mqtt = require('aliyun-iot-mqtt');
//设备属性
const options = {
    productKey: "产品productKey",
    deviceName: "设备deviceName",
    deviceSecret: "设备的secret",
    regionId: "cn-shanghai"
};
//建立连接
const client = mqtt.getAliyunIotMqttClient(options);

//属性上报的Topic
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);
}
3. 启动运行

3.1 设备启动

$node thermometer.js

3.2 云端查看设备运行状态

image.png | left | 602x225

點擊查看更多內容
5人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
全棧工程師
手記
粉絲
5
獲贊與收藏
44

關注作者,訂閱最新文章

閱讀免費教程

感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消