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

简介: 这里分别演示通过二维数据可视化功能展示设备位置 + 通过数据分析实现定时下发数据到设备。

作者:俏巴

概述

物联网数据分析,又称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"
            }
          }
        }
      ]
    }
  ]
}

image.png

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、属性上报情况查看

image.png

4、通过物联网数据分析中的二维数据可视化功能,接入设备位置到地图

image.png

image.png

image.png

5、物联网数据分析通过SQL将数据下发至设备


5.1 查询数据
image.png
image.png

image.png

5.2 使用SQL下发数据

insert into ${pk.a1kVH******.device1} select 53.3 as CurrentHumidity;  -- 导入数据到表,下发数据到设备

image.png

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
相关文章
|
3月前
|
数据可视化 数据挖掘 Linux
震撼发布!Python数据分析师必学,Matplotlib与Seaborn数据可视化实战全攻略!
在数据科学领域,数据可视化是连接数据与洞察的桥梁,能让复杂的关系变得直观。本文通过实战案例,介绍Python数据分析师必备的Matplotlib与Seaborn两大可视化工具。首先,通过Matplotlib绘制基本折线图;接着,使用Seaborn绘制统计分布图;最后,结合两者在同一图表中展示数据分布与趋势,帮助你提升数据可视化技能,更好地讲述数据故事。
53 1
|
3月前
|
数据挖掘 PyTorch TensorFlow
|
2月前
|
机器学习/深度学习 人工智能 搜索推荐
某A保险公司的 数据图表和数据分析
某A保险公司的 数据图表和数据分析
61 0
某A保险公司的 数据图表和数据分析
|
4月前
|
数据采集 DataWorks 数据挖掘
提升数据分析效率:DataWorks在企业级数据治理中的应用
【8月更文第25天】本文将探讨阿里巴巴云的DataWorks平台如何通过建立统一的数据标准、规范以及实现数据质量监控和元数据管理来提高企业的数据分析效率。我们将通过具体的案例研究和技术实践来展示DataWorks如何简化数据处理流程,减少成本,并加速业务决策。
464 54
|
2月前
|
SQL 分布式计算 数据挖掘
加速数据分析:阿里云Hologres在实时数仓中的应用实践
【10月更文挑战第9天】随着大数据技术的发展,企业对于数据处理和分析的需求日益增长。特别是在面对海量数据时,如何快速、准确地进行数据查询和分析成为了关键问题。阿里云Hologres作为一个高性能的实时交互式分析服务,为解决这些问题提供了强大的支持。本文将深入探讨Hologres的特点及其在实时数仓中的应用,并通过具体的代码示例来展示其实际应用。
186 0
|
3月前
|
机器学习/深度学习 数据可视化 数据挖掘
数据可视化大不同!Python数据分析与机器学习中的Matplotlib、Seaborn应用新视角!
在数据科学与机器学习领域,数据可视化是理解数据和优化模型的关键。Python凭借其强大的可视化库Matplotlib和Seaborn成为首选语言。本文通过分析一份包含房屋面积、卧室数量等特征及售价的数据集,展示了如何使用Matplotlib绘制散点图,揭示房屋面积与售价的正相关关系;并利用Seaborn的pairplot探索多变量间的关系。在机器学习建模阶段,通过随机森林模型展示特征重要性的可视化,帮助优化模型。这两个库在数据分析与建模中展现出广泛的应用价值。
50 2
|
3月前
|
数据可视化 数据挖掘 Python
惊呆了!Python数据分析师如何用Matplotlib、Seaborn秒变数据可视化大师?
在数据驱动时代,分析师们像侦探一样在数字海洋中寻找线索,揭示隐藏的故事。数据可视化则是他们的“魔法棒”,将复杂数据转化为直观图形。本文将带你探索Python数据分析师如何利用Matplotlib与Seaborn这两大神器,成为数据可视化大师。Matplotlib提供基础绘图功能,而Seaborn在此基础上增强了统计图表的绘制能力,两者结合使数据呈现更高效、美观。无论是折线图还是箱形图,这两个库都能助你一臂之力。
45 4
|
3月前
|
数据可视化 数据挖掘 Python
告别枯燥数字,拥抱视觉盛宴!Python 数据分析中的数据可视化艺术,你 get 了吗?
在数据驱动时代,数据分析至关重要,但单纯依赖数据表格难以揭示其背后的洞见。这时,数据可视化便彰显出其重要性,尤其借助 Python 的强大工具如 Matplotlib、Seaborn 和 Plotly 等,可将数据转化为直观的图形。Matplotlib 提供高度定制的图表,Seaborn 则简化了图表美化过程。通过折线图、散点图、箱线图、小提琴图及热力图等多种图表形式,我们可以更深入地理解数据分布与关系,有效传达信息并支持决策制定。数据可视化不仅是一门技术,更是讲述数据故事的艺术。
69 3
|
2月前
|
机器学习/深度学习 数据采集 数据可视化
如何理解数据分析及数据的预处理,分析建模,可视化
如何理解数据分析及数据的预处理,分析建模,可视化
53 0
|
3月前
|
机器学习/深度学习 数据挖掘 TensorFlow
🔍揭秘Python数据分析奥秘,TensorFlow助力解锁数据背后的亿万商机
【9月更文挑战第11天】在信息爆炸的时代,数据如沉睡的宝藏,等待发掘。Python以简洁的语法和丰富的库生态成为数据分析的首选,而TensorFlow则为深度学习赋能,助你洞察数据核心,解锁商机。通过Pandas库,我们可以轻松处理结构化数据,进行统计分析和可视化;TensorFlow则能构建复杂的神经网络模型,捕捉非线性关系,提升预测准确性。两者的结合,让你在商业竞争中脱颖而出,把握市场脉搏,释放数据的无限价值。以下是使用Pandas进行简单数据分析的示例:
46 5