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

简介: 物联网数据分析,又称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
相关文章
|
11月前
|
数据可视化 数据挖掘 API
漏刻有时可视化数据分析专向大屏训练通升级说明
漏刻有时可视化数据分析专向大屏训练通升级说明
117 0
|
1月前
|
搜索推荐 数据可视化 算法
数据可视化系列-03AIPL消费者行为全链路可视化模型实践
数据可视化系列-03AIPL消费者行为全链路可视化模型实践
数据可视化系列-03AIPL消费者行为全链路可视化模型实践
|
8月前
|
消息中间件 数据挖掘 Kafka
实时数据分析演示
实时数据分析演示
|
10月前
|
SQL 数据采集 搜索推荐
开源大数据分析实验(4)——简单用户画像分析之数据可视化展现
本场景主要介绍基于海量日志数据进行简单用户画像分析为背景,如何通过使用DataWorks完成数据采集 、加工数据、配置数据质量监控和数据可视化展现等任务。
|
10月前
|
SQL 分布式计算 调度
开源大数据分析实验(1)——简单用户画像分析之采集数据
本场景主要介绍基于海量日志数据进行简单用户画像分析为背景,如何通过使用DataWorks完成数据采集 、加工数据、配置数据质量监控和数据可视化展现等任务。
|
10月前
|
机器学习/深度学习 存储 数据可视化
数据分析与可视化概述
数据分析与可视化概述
162 1
|
人工智能 数据可视化 安全
数据分析选它就对了,只需三步轻松搞定数据可视化分析
随时时代的不断发展,云计算,大数据,人工智能,物联网,5G这些新兴行业不断演进,快速的决策与行动能力已经成为每一家企业的必备能力,但是往往数据分析相关的采购成本高,链条长,技术复杂度高,让企业遥不可及,针对这些问题,今天就给大家分享一款简单易用的大数据可视化分析工具,让每家企业都能拥有数据洞察能力。 NBI一站式数据可视化分析平台 NBI一站式大数据分析平台作为新一代自助式、探索式分析工具,在产品设计理念上始终从用户的角度出发,一直围绕简单、易用,强调交互分析为目的的新型产品。我们将数据分析的各环节(数据准备、自服务数据建模、探索式分析、权限管控)融入到系统当中,让企业有序的、安全的管理数
数据分析选它就对了,只需三步轻松搞定数据可视化分析
|
SQL JSON 数据可视化
阿里云数据分析最佳实践:二维数据可视化 + 设备数据下发
这里分别演示通过二维数据可视化功能展示设备位置 + 通过数据分析实现定时下发数据到设备。
阿里云数据分析最佳实践:二维数据可视化 + 设备数据下发
|
机器学习/深度学习 数据采集 SQL
|
监控 数据可视化 BI
实时数据的可视化
通常来说,可视化的报表会以更高效率的方式将数据背后隐藏的信息传递给我们。通过一个简单的BarChart,我们就很容易对比某商品在第二季度中的销量差异;而通过一条简单的LineChart,则很容易看出员工平均工作时间在某个月份的分布。
2416 0