
技术教室中的搬运工〜2020-04-0319页面浏览量
简介: 本文从为物联网平台创建产品和设备开始,并逐步介绍了整个链接的完整实现.
作者: 乔巴
您可以使用规则引擎将数据从IoT平台转发到消息队列(RocketMQ)进行存储. 为了实现消息从设备,物联网平台,RocketMQ到设备服务器的高可靠性传输能力. 本文从为物联网平台创建产品和设备开始,然后逐步介绍整个链接的完整实现.

参考阿里云IoT平台Qucik开始快速创建产品和设备.

a. 配置概述


b. 处理数据

c您当前的网络存在链路层劫持,转发数据


d. 配置完成后您当前的网络存在链路层劫持,打开规则引擎

相关参考:
SQL语句参考

[数据转发到消息队列RocketMQ
]()
设备报告属性消息
import com.alibaba.taro.AliyunIoTSignUtil;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.util.HashMap;
import java.util.Map;
public class IoTDemoPubSubDemo1 {
public static String productKey = "*********";
public static String deviceName = "*********";
public static String deviceSecret = "*********";
public static String regionId = "cn-shanghai";
// 物模型-属性上报topic
private static String pubTopic = "/sys/" + productKey + "/" + deviceName + "/thing/event/property/post";
private static MqttClient mqttClient;
public static void main(String [] args){
// 初始化Mqtt Client对象
initAliyunIoTClient();
// 汇报属性
postDeviceProperties();
}
/**
* 初始化 Client 对象
*/
private static void initAliyunIoTClient() {
try {
// 构造连接需要的参数
String clientId = "java" + System.currentTimeMillis();
Map<String, String> params = new HashMap<>(16);
params.put("productKey", productKey);
params.put("deviceName", deviceName);
params.put("clientId", clientId);
String timestamp = String.valueOf(System.currentTimeMillis());
params.put("timestamp", timestamp);
// cn-shanghai
String targetServer = "tcp://" + productKey + ".iot-as-mqtt."+regionId+".aliyuncs.com:1883";
String mqttclientId = clientId + "|securemode=3,signmethod=hmacsha1,timestamp=" + timestamp + "|";
String mqttUsername = deviceName + "&" + productKey;
String mqttPassword = AliyunIoTSignUtil.sign(params, deviceSecret, "hmacsha1");
connectMqtt(targetServer, mqttclientId, mqttUsername, mqttPassword);
} catch (Exception e) {
System.out.println("initAliyunIoTClient error " + e.getMessage());
}
}
public static void connectMqtt(String url, String clientId, String mqttUsername, String mqttPassword) throws Exception {
MemoryPersistence persistence = new MemoryPersistence();
mqttClient = new MqttClient(url, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
// MQTT 3.1.1
connOpts.setMqttVersion(4);
connOpts.setAutomaticReconnect(false);
// connOpts.setCleanSession(true);
connOpts.setCleanSession(false);
connOpts.setUserName(mqttUsername);
connOpts.setPassword(mqttPassword.toCharArray());
connOpts.setKeepAliveInterval(60);
mqttClient.connect(connOpts);
}
/**
* 汇报属性
*/
private static void postDeviceProperties() {
try {
//上报数据
//高级版 物模型-属性上报payload
System.out.println("上报属性值");
String payloadJson = "{\"params\":{\"Status\":1,\"Data\":\"33\"}}";
MqttMessage message = new MqttMessage(payloadJson.getBytes("utf-8"));
message.setQos(1);
mqttClient.publish(pubTopic, message);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
运行状态显示

参考链接: 基于开源JAVA MQTT客户端连接到阿里云IoT
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-168211-1.html
很棒棒哒
但综合性能比我们的052C和052D都要先进
加油啊看好你哟~