概述
物聯網資料分析,又稱Link Analytics,是阿裡雲為物聯網開發者提供的裝置智能分析服務,全鍊路覆寫了裝置資料生成、管理(存儲)、清洗、分析及可視化等環節。有效降低資料分析門檻,助力物聯網開發工作。這裡分别示範通過二維資料可視化功能展示裝置位置 + 通過資料分析實作定時下發資料到裝置。
Step By Step
1、建立産品,導入物模型,
參考連結物模型json内容
{
"schema": "https://iotx-tsl.oss-ap-southeast-1.aliyuncs.com/schema.json",
"profile": {
"productKey": "a1kVHWEOsM2"
},
"properties": [
{
"identifier": "GeoLocation",
"name": "地理位置",
"accessMode": "rw",
"required": true,
"dataType": {
"type": "struct",
"specs": [
{
"identifier": "Longitude",
"name": "經度",
"dataType": {
"type": "double",
"specs": {
"min": "-180",
"max": "180",
"unit": "°",
"unitName": "度",
"step": "0.01"
}
}
},
{
"identifier": "Latitude",
"name": "緯度",
"dataType": {
"type": "double",
"specs": {
"min": "-90",
"max": "90",
"unit": "°",
"unitName": "度",
"step": "0.01"
}
}
},
{
"identifier": "Altitude",
"name": "海拔",
"dataType": {
"type": "double",
"specs": {
"min": "0",
"max": "9999",
"unit": "m",
"unitName": "米",
"step": "0.01"
}
}
},
{
"identifier": "CoordinateSystem",
"name": "坐标系統",
"dataType": {
"type": "enum",
"specs": {
"1": "WGS_84",
"2": "GCJ_02"
}
}
}
]
}
},
{
"identifier": "CurrentHumidity",
"name": "濕度",
"accessMode": "rw",
"required": false,
"dataType": {
"type": "double",
"specs": {
"min": "0",
"max": "100",
"unit": "%",
"unitName": "百分比",
"step": "0.01"
}
}
},
{
"identifier": "CurrentTemperature",
"name": "溫度",
"accessMode": "rw",
"required": false,
"dataType": {
"type": "double",
"specs": {
"min": "-40",
"max": "120",
"unit": "℃",
"unitName": "攝氏度",
"step": "0.01"
}
}
}
],
"events": [
{
"identifier": "post",
"name": "post",
"type": "info",
"required": true,
"desc": "屬性上報",
"method": "thing.event.property.post",
"outputData": [
{
"identifier": "GeoLocation",
"name": "地理位置",
"dataType": {
"type": "struct",
"specs": [
{
"identifier": "Longitude",
"name": "經度",
"dataType": {
"type": "double",
"specs": {
"min": "-180",
"max": "180",
"unit": "°",
"unitName": "度",
"step": "0.01"
}
}
},
{
"identifier": "Latitude",
"name": "緯度",
"dataType": {
"type": "double",
"specs": {
"min": "-90",
"max": "90",
"unit": "°",
"unitName": "度",
"step": "0.01"
}
}
},
{
"identifier": "Altitude",
"name": "海拔",
"dataType": {
"type": "double",
"specs": {
"min": "0",
"max": "9999",
"unit": "m",
"unitName": "米",
"step": "0.01"
}
}
},
{
"identifier": "CoordinateSystem",
"name": "坐标系統",
"dataType": {
"type": "enum",
"specs": {
"1": "WGS_84",
"2": "GCJ_02"
}
}
}
]
}
},
{
"identifier": "CurrentHumidity",
"name": "濕度",
"dataType": {
"type": "double",
"specs": {
"min": "0",
"max": "100",
"unit": "%",
"unitName": "百分比",
"step": "0.01"
}
}
},
{
"identifier": "CurrentTemperature",
"name": "溫度",
"dataType": {
"type": "double",
"specs": {
"min": "-40",
"max": "120",
"unit": "℃",
"unitName": "攝氏度",
"step": "0.01"
}
}
}
]
}
],
"services": [
{
"identifier": "set",
"name": "set",
"required": true,
"callType": "async",
"desc": "屬性設定",
"method": "thing.service.property.set",
"inputData": [
{
"identifier": "GeoLocation",
"name": "地理位置",
"dataType": {
"type": "struct",
"specs": [
{
"identifier": "Longitude",
"name": "經度",
"dataType": {
"type": "double",
"specs": {
"min": "-180",
"max": "180",
"unit": "°",
"unitName": "度",
"step": "0.01"
}
}
},
{
"identifier": "Latitude",
"name": "緯度",
"dataType": {
"type": "double",
"specs": {
"min": "-90",
"max": "90",
"unit": "°",
"unitName": "度",
"step": "0.01"
}
}
},
{
"identifier": "Altitude",
"name": "海拔",
"dataType": {
"type": "double",
"specs": {
"min": "0",
"max": "9999",
"unit": "m",
"unitName": "米",
"step": "0.01"
}
}
},
{
"identifier": "CoordinateSystem",
"name": "坐标系統",
"dataType": {
"type": "enum",
"specs": {
"1": "WGS_84",
"2": "GCJ_02"
}
}
}
]
}
},
{
"identifier": "CurrentHumidity",
"name": "濕度",
"dataType": {
"type": "double",
"specs": {
"min": "0",
"max": "100",
"unit": "%",
"unitName": "百分比",
"step": "0.01"
}
}
},
{
"identifier": "CurrentTemperature",
"name": "溫度",
"dataType": {
"type": "double",
"specs": {
"min": "-40",
"max": "120",
"unit": "℃",
"unitName": "攝氏度",
"step": "0.01"
}
}
}
],
"outputData": []
},
{
"identifier": "get",
"name": "get",
"required": true,
"callType": "async",
"desc": "屬性擷取",
"method": "thing.service.property.get",
"inputData": [
"GeoLocation",
"CurrentHumidity",
"CurrentTemperature"
],
"outputData": [
{
"identifier": "GeoLocation",
"name": "地理位置",
"dataType": {
"type": "struct",
"specs": [
{
"identifier": "Longitude",
"name": "經度",
"dataType": {
"type": "double",
"specs": {
"min": "-180",
"max": "180",
"unit": "°",
"unitName": "度",
"step": "0.01"
}
}
},
{
"identifier": "Latitude",
"name": "緯度",
"dataType": {
"type": "double",
"specs": {
"min": "-90",
"max": "90",
"unit": "°",
"unitName": "度",
"step": "0.01"
}
}
},
{
"identifier": "Altitude",
"name": "海拔",
"dataType": {
"type": "double",
"specs": {
"min": "0",
"max": "9999",
"unit": "m",
"unitName": "米",
"step": "0.01"
}
}
},
{
"identifier": "CoordinateSystem",
"name": "坐标系統",
"dataType": {
"type": "enum",
"specs": {
"1": "WGS_84",
"2": "GCJ_02"
}
}
}
]
}
},
{
"identifier": "CurrentHumidity",
"name": "濕度",
"dataType": {
"type": "double",
"specs": {
"min": "0",
"max": "100",
"unit": "%",
"unitName": "百分比",
"step": "0.01"
}
}
},
{
"identifier": "CurrentTemperature",
"name": "溫度",
"dataType": {
"type": "double",
"specs": {
"min": "-40",
"max": "120",
"unit": "℃",
"unitName": "攝氏度",
"step": "0.01"
}
}
}
]
}
]
}

2、裝置端通過開源MQTT SDK上傳資料,
基于開源JAVA MQTT Client連接配接阿裡雲IoTimport 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 IoTDemoPubSubDemo {
// 裝置三元組資訊
public static String productKey = "a1kVH******";
public static String deviceName = "device1";
public static String deviceSecret = "XADek3EYXzzTtxJ6a****************";
public static String regionId = "cn-shanghai";
// 物模型-屬性上報topic
private static String pubTopic = "/sys/" + productKey + "/" + deviceName + "/thing/event/property/post";
// 物模型-屬性訂閱topic
private static String subTopic = "/sys/" + productKey + "/" + deviceName + "/thing/service/property/set";
private static MqttClient mqttClient;
public static void main(String [] args){
initAliyunIoTClient();
// 彙報屬性
postDeviceProperties();
try {
mqttClient.subscribe(subTopic); // 訂閱Topic
} catch (MqttException e) {
System.out.println("error:" + e.getMessage());
e.printStackTrace();
}
// 設定訂閱監聽
mqttClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable throwable) {
System.out.println("connection Lost");
}
@Override
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
System.out.println("Sub message");
System.out.println("Topic : " + s);
System.out.println(new String(mqttMessage.getPayload())); //列印輸出消息payLoad
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
}
/**
* 初始化 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 {
System.out.println("上報屬性值");
String payloadJson = "{\"params\":{\"CurrentHumidity\":12.3,\"CurrentTemperature\":12.3,\"GeoLocation\":{\"CoordinateSystem\":1,\"Latitude\":29.93089,\"Longitude\":121.59923,\"Altitude\":10}}}";
// https://yq.aliyun.com/articles/706989
MqttMessage message = new MqttMessage(payloadJson.getBytes("utf-8"));
message.setQos(1);
mqttClient.publish(pubTopic, message);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
payLoad裝置參考 連結 。
3、屬性上報情況檢視
4、通過物聯網資料分析中的二維資料可視化功能,接入裝置位置到地圖
5、物聯網資料分析通過SQL将資料下發至裝置
- 5.1 查詢資料
阿裡雲資料分析最佳實踐:二維資料可視化 + 裝置資料下發
- 5.2 使用SQL下發資料
insert into ${pk.a1kVH******.device1} select 53.3 as CurrentHumidity; -- 導入資料到表,下發資料到裝置
- 5.3 裝置端訂閱情況
上報屬性值
Sub message
Topic : /sys/a1kVH******/device1/thing/service/property/set
{"method":"thing.service.property.set","id":"419651605","params":{"CurrentHumidity":53.3},"version":"1.0.0"}