使用阿里云物联网平台流程

freps / 2024-10-13 / 原文

  模拟设备连接阿里云物联网平台

编辑

编辑

1.找到物联网平台 

阿里云物联网平台是一个集成了设备接入、设备管理、数据安全通信、消息订阅、消息转发和数据服务(存储、分析、过滤、解析、集成等)等能力的一体化平台。向下支持连接海量设备,采集设备数据上云;向上提供云端API,服务端可通过云端SDK调用云端API将指令下发至设备端,实现远程控制。

编辑

2.我已经试用过了直接点管理控制台

编辑

3.实例详情 

编辑

产品就是你要做的产品类型,如智能手表是一个类型,智能水表是一个类型

设备就是你产品下的设备,比如手表001,手表002 

4.物模型

物模型包括,产品 and 产品属性和事件

编辑

编辑

 编辑

5.给产品添加设备

 编辑

6.下载MQTTX 

MQTTX 是一个跨平台的 MQTT 客户端工具,旨在帮助用户快速、简单地与 MQTT 代理进行交互。它提供了一个图形用户界面,使得用户可以方便地测试和调试 MQTT 应用。

桌面端下载 https://mqttx.app/zh

web浏览器网页地址 https://mqttx.app/web-client

7.使用MQTTX模拟设备连接阿里云物联网平台

编辑

 编辑

参数和MQTTX对应 

编辑 填好连接信息

编辑

 连接成功

编辑

 查看阿里云物联网平台会显示在线

编辑

8.连接成功设置设备属性值

编辑

复制到 

编辑

 把${}中的值换成你自己的设备名

这里设置湿度为例子

编辑

 编辑

查看物联网平台设置成功

编辑

JAVA连接阿里云物联网平台

1.在maven中导入jar包

<!-- https://mvnrepository.com/artifact/com.aliyun/iot20180120 -->
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>iot20180120</artifactId>
    <version>3.0.8</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>tea-openapi</artifactId>
    <version>0.2.2</version>
</dependency>

 2.在yml文件中添加

这个accessKeyId之前oss连接的时候有讲,不会创建回去看

zzyl:
  aliyun:
    accessKeyId: LTAI5tMA5wSWRmeuyYPei9Vk
    accessKeySecret: GSNxohmhEcFa4g7p61RRTzRdABMzcH
    consumerGroupId: DEFAULT_GROUP
    regionId: cn-shanghai
    iotInstanceId: iot-06z00cxspzjxue9
    host: iot-06z00cxspzjxue9.amqp.iothub.aliyuncs.com

编辑 3.给yml中的数据书写配置类

package com.zzyl.properties;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Setter
@Getter
@NoArgsConstructor
@ToString
@Configuration
@ConfigurationProperties(prefix = "zzyl.aliyun")
public class AliIoTConfigProperties {

    /**
     * 访问Key
     */
    private String accessKeyId;
    /**
     * 访问秘钥
     */
    private String accessKeySecret;
    /**
     * 区域id
     */
    private String regionId;
    /**
     * 实例id
     */
    private String iotInstanceId;
    /**
     * 域名
     */
    private String host;

    /**
     * 消费组
     */
    private String consumerGroupId;

}

 4.添加连接BeanConfig

package com.zzyl.config;

import com.aliyun.iot20180120.Client;
import com.aliyun.teaopenapi.models.Config;
import com.zzyl.properties.AliIoTConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class IotClientConfig {

    @Autowired
    private AliIoTConfigProperties aliIoTConfigProperties;

    /**
     * LTAI5tDQKg9F61aJhbmhqVRK
     * LYUKZH7HQGBoD025pmSq0fQsREaOYD
     * @return
     * @throws Exception
     */
    @Bean(name = "iotClient")
    public Client instance() throws Exception {
        Config config = new Config();
        config.accessKeyId = aliIoTConfigProperties.getAccessKeyId();
        config.accessKeySecret = aliIoTConfigProperties.getAccessKeySecret();
        // 您的可用区ID 默认上海
        config.regionId = aliIoTConfigProperties.getRegionId();
        return new Client(config);
    }
}

 5.使用案例

 @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteDevice(QueryDeviceDetailDTO dto) {
        DeleteDeviceRequest request = new DeleteDeviceRequest();
        request.setIotInstanceId(aliIoTConfigProperties.getIotInstanceId());
        request.setIotId(dto.getIotId());
        request.setIotInstanceId(aliIoTConfigProperties.getIotInstanceId());
        request.setProductKey(dto.getProductKey());
        try {
            DeleteDeviceResponse response = iotClient.deleteDevice(request);
        } catch (Exception e) {
            throw new RuntimeException("删除设备失败");
        }
        deviceMapper.deleteDevice(dto.getIotId());
        return ResponseResult.success("删除成功");
    }