阿里云数据分析最佳实践:二维数据可视化 + 设备数据下发

简介: 物联网数据分析,又称Link Analytics,是阿里云为物联网开发者提供的设备智能分析服务,全链路覆盖了设备数据生成、管理(存储)、清洗、分析及可视化等环节。有效降低数据分析门槛,助力物联网开发工作。这里分别演示通过二维数据可视化功能展示设备位置 + 通过数据分析实现定时下发数据到设备。

概述

物联网数据分析,又称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连接阿里云IoT

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 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"}

参考链接

快速接入设备位置到地图
数据开发之分析决策直达设备

相关实践学习
钉钉群中如何接收IoT温控器数据告警通知
本实验主要介绍如何将温控器设备以MQTT协议接入IoT物联网平台,通过云产品流转到函数计算FC,调用钉钉群机器人API,实时推送温湿度消息到钉钉群。
阿里云AIoT物联网开发实战
本课程将由物联网专家带你熟悉阿里云AIoT物联网领域全套云产品,7天轻松搭建基于Arduino的端到端物联网场景应用。 开始学习前,请先开通下方两个云产品,让学习更流畅: IoT物联网平台:https://iot.console.aliyun.com/ LinkWAN物联网络管理平台:https://linkwan.console.aliyun.com/service-open
相关文章
|
2月前
|
数据采集 数据可视化 数据挖掘
数据分析案例-汽车客户信息数据可视化分析
数据分析案例-汽车客户信息数据可视化分析
213 0
|
25天前
|
数据采集 存储 自然语言处理
Python爬虫与数据可视化:构建完整的数据采集与分析流程
Python爬虫与数据可视化:构建完整的数据采集与分析流程
|
数据可视化 数据挖掘 API
漏刻有时可视化数据分析专向大屏训练通升级说明
漏刻有时可视化数据分析专向大屏训练通升级说明
125 0
|
2月前
|
搜索推荐 数据可视化 算法
数据可视化系列-03AIPL消费者行为全链路可视化模型实践
数据可视化系列-03AIPL消费者行为全链路可视化模型实践
数据可视化系列-03AIPL消费者行为全链路可视化模型实践
|
11月前
|
SQL 数据采集 搜索推荐
开源大数据分析实验(4)——简单用户画像分析之数据可视化展现
本场景主要介绍基于海量日志数据进行简单用户画像分析为背景,如何通过使用DataWorks完成数据采集 、加工数据、配置数据质量监控和数据可视化展现等任务。
|
11月前
|
机器学习/深度学习 存储 数据可视化
数据分析与可视化概述
数据分析与可视化概述
170 1
|
数据采集 自然语言处理 监控
智慧数据驱动:基于smardaten构建多维数据可视化大屏
前段时间有位粉丝找到我问数据可视化大屏怎么做,我说我之前的博文里介绍过基于Flask和Echarts搭建的大屏,你可以去看看。结果他来了句“没学过任何编程代码怎么办?”,好家伙,一下给我整懵了,于是为了解决这个问题,照顾零基础的粉丝,我花了几天时间找了个不需要代码就可以做可视化大屏的方法,这篇文章就讲手把手教会你使用smardaten搭建数据可视化大屏(无需代码)。
161 0
|
移动开发 前端开发 数据可视化
数据透视表上线!如何在纯前端实现这个强大的数据分析功能?(1)
数据透视表上线!如何在纯前端实现这个强大的数据分析功能?
225 0
数据透视表上线!如何在纯前端实现这个强大的数据分析功能?(1)
|
移动开发 JSON 前端开发
数据透视表上线!如何在纯前端实现这个强大的数据分析功能?(2)
数据透视表上线!如何在纯前端实现这个强大的数据分析功能?
573 0
数据透视表上线!如何在纯前端实现这个强大的数据分析功能?(2)
|
SQL JSON 数据可视化
阿里云数据分析最佳实践:二维数据可视化 + 设备数据下发
这里分别演示通过二维数据可视化功能展示设备位置 + 通过数据分析实现定时下发数据到设备。
阿里云数据分析最佳实践:二维数据可视化 + 设备数据下发