0.准备工作
0.1 注册阿里云账号
使用个人淘宝账号或手机号,开通阿里云账号,并通过实名认证(可以用支付宝认证)
0.2 免费开通IoT物联网套件
IoT套件产品官网
https://www.aliyun.com/product/iot
消息服务官网
https://mns.console.aliyun.com/#/list/cn-shanghai
0.3 软件环境
Nodejs安装
https://nodejs.org/en/download/
Java8 安装
开发工具:Sublime Text3/ IntelliJ IDEA
https://www.sublimetext.com/3
1.阿里云IoT控制台配置服务端订阅
阿里云IoT物联网套件开通
https://www.aliyun.com/product/iot
1.1 创建产品(基础版)
我们在阿里云IoT控制台,创建产品:空气检测,选择基础版。
1.2 在产品下添加设备
我们在阿里云IoT控制台,设备管理里
空气检测产品下添加一个具体设备。
1.2 在产品下配置服务端订阅
我们在阿里云IoT控制台,为
空气检测产品开通服务端订阅,勾选设备上报消息和设备状态变化通知,开通后会有MNS的区域:cn-shanghai和队列信息:aliyun-iot-a1jnUEKYhw4,如下。
通过阅读阿里云IoT文档,我们了解到队列中消息结构体如下:
{
"payload": "Base64 Encode的数据",
"messagetype": "status",
"messageid": 996000000000000001,
"topic": "具体的设备Topic",
"timestamp": 1526450324
}
{
"name": "aliyun-iot",
"dependencies": {
"aliyun-iot-mqtt": "^0.0.4"
},
"author": "wongxming",
"license": "MIT"
}
下载安装SDK
$npm install
/**
* package.json 添加依赖:"aliyun-iot-mqtt": "0.0.4"
node iot-mns.js
*/
const mqtt = require('aliyun-iot-mqtt');
//设备三元组
const options = {
productKey: "RY8ExdyS6lU",
deviceName: "officeThermometer",
deviceSecret: "oauYYavdIV9QOt7d9WcrnIjXSNc2i26A",
regionId: "cn-shanghai"
};
//设备与云 建立连接,设备上线
const client = mqtt.getAliyunIotMqttClient(options);
//主题topic
const topic = `${options.productKey}/${options.deviceName}/update`;
var data = {
temperature: Math.floor((Math.random()*20)+10),
humidity: Math.floor((Math.random()*80)+20),
};
//指定topic发布数据到云端
client.publish(topic, JSON.stringify(data));
console.log("===postData topic=" + topic)
console.log(data)
//设备下线
//client.end()
$node iot-mns.js
<dependencies>
<dependency>
<groupId>com.aliyun.mns</groupId>
<artifactId>aliyun-sdk-mns</artifactId>
<version>1.1.8</version>
<classifier>jar-with-dependencies</classifier>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.42</version>
</dependency>
</dependencies>
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.mns.client.CloudAccount;
import com.aliyun.mns.client.CloudQueue;
import com.aliyun.mns.client.MNSClient;
import com.aliyun.mns.model.Message;
import org.apache.commons.codec.binary.Base64;
public class ComsumerDemo {
public static String accessKeyId = "AK";
public static String accessKeySecret = "AK秘钥";
public static String endpoint = "mns公网Endpoint";
public static void main(String[] args) {
CloudAccount account = new CloudAccount(accessKeyId,accessKeySecret,endpoint);
MNSClient client = account.getMNSClient();
//获取消息队列
CloudQueue queue = client.getQueueRef("aliyun-iot-a1jnUEKYhw4");
while (true) {
Message popMsg = queue.popMessage(10);
if (popMsg != null) {
System.out.println("message id: " + popMsg.getMessageId());
System.out.println("message body: \n" + decodeBase64(popMsg.getMessageBodyAsString()));
//删除消息
queue.deleteMessage(popMsg.getReceiptHandle());
}
}
}
public static String decodeBase64(String jsonString) {
try {
JSONObject json = JSON.parseObject(jsonString);
String payload = new String(Base64.decodeBase64(json.getString("payload")));
json.put("payload", JSON.parseObject(payload));
return json.toJSONString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
message id: E2CF179AD5686386-2-16367878417-200000009
message body:
{
"payload": {
"lastTime": "2018-05-16 13:58:44.413",
"clientIp": "42.120.74.246",
"time": "2018-05-16 13:58:44.427",
"productKey": "a1jnUEKYhw4",
"deviceName": "suw8umOqgJ72yCADerZp",
"status": "online"
},
"messagetype": "status",
"messageid": 996631012001751041,
"timestamp": 1526450324
}
2) timestamp=1526450334时,设备上报了数据,通过payload可以看到完整数据
message id: "656A4F66B391367-1-1636787AAC0-20000000C"
message body:
{
"payload": {
"temperature": 14,
"humidity": 116
},
"messagetype": "upload",
"topic": "/a1jnUEKYhw4/suw8umOqgJ72yCADerZp/update",
"messageid": 996631053735047169,
"timestamp": 1526450334
}
message id: E2CF179AD5686386-2-1636787F5F1-20000000A
message body:
{
"payload": {
"lastTime": "2018-05-16 13:59:04.381",
"time": "2018-05-16 13:59:13.571",
"productKey": "a1jnUEKYhw4",
"deviceName": "suw8umOqgJ72yCADerZp",
"status": "offline"
},
"messagetype": "status",
"messageid": 996631134240534528,
"timestamp": 1526450353
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。