• 中移4G模块-ML302-OpenCpu开发-服务器搭建


    B站:https://space.bilibili.com/309103931

    中移4G模块-ML302专栏:https://blog.csdn.net/qq_33259323/category_10453372.html

    中移4G模块-ML302文集:https://www.bilibili.com/read/readlist/rl328642

    中移4G模块-ML302-OpenCpu开发-服务器搭建

    https://blog.csdn.net/qq_33259323/article/details/108960799

    https://www.bilibili.com/read/cv7880356

    模块:ML302-OpenCpu

    MQTT平台:阿里云

    后端:Java,SpringBoot等

    使用开发工具:IDEA

    1. pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.mm.iot</groupId>
        <artifactId>iotdemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>iotdemo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <!-- 阿里云IOT SDK aliyun-java-sdk-iot -->
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-iot</artifactId>
                <version>7.13.0</version>
            </dependency>
    
            <!-- 阿里云公共包 -->
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-core</artifactId>
                <version>4.5.6</version>
            </dependency>
    
            <!-- 小辣椒 -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.18</version>
            </dependency>
    
            <!-- web -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!-- json -->
            <dependency>
                <groupId>net.sf.json-lib</groupId>
                <artifactId>json-lib</artifactId>
                <version>2.4</version>
                <classifier>jdk15</classifier>
            </dependency>
    
            <!-- 测试 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!-- 热部署 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>RELEASE</version>
                <scope>compile</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    2.项目目录

     3.项目yaml配置

    server:
      port: 8085
    spring:
      resources:
        static-locations: classpath:/iot/dist

    4.阿里云IOT客户端配置bean

    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    
    @Configuration
    public class AliIOTConfig {
    
        private static final String ACCESSKEY = "xxx";
        private static final String ACCESSSECRET = "yyy";
    
        /**
         * 默认AliIOTConfig
         * @return
         */
        @Primary
        @Bean
        public DefaultAcsClient restTemplate() throws ClientException {
            DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Iot", "iot.cn-shanghai.aliyuncs.com");
            IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", ACCESSKEY, ACCESSSECRET);
            DefaultAcsClient client = new DefaultAcsClient(profile); //初始化SDK客户端。
    
            return client;
        }
    
    }

    5.Test模拟发送数据给设备

    import com.aliyuncs.CommonRequest;
    import com.aliyuncs.CommonResponse;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.http.MethodType;
    import com.aliyuncs.iot.model.v20180120.PubRequest;
    import com.aliyuncs.iot.model.v20180120.PubResponse;
    import net.sf.json.JSONObject;
    import org.apache.commons.codec.DecoderException;
    import org.apache.commons.codec.binary.Base64;
    import org.apache.commons.codec.binary.Hex;
    import org.junit.jupiter.api.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import java.io.IOException;
        
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class IotdemoApplicationTests {
    
        @Autowired
        private DefaultAcsClient defaultAcsClient;
    
        @Test
        void send() throws DecoderException {
    
            JSONObject data = new JSONObject();
    
            data.put("gpio_out_a",255);
            data.put("gpio_out_b",255);
    
            PubRequest request = new PubRequest();
            request.setProductKey("XXXXXXX");
            request.setMessageContent(Base64.encodeBase64String((data.toString()).getBytes()));
            request.setTopicFullName("/a1fkKLo7NeH/YYYYYYYYYYYY/user/uart");
            request.setQos(0); //目前支持QoS0和QoS1。
            try {
                PubResponse response = defaultAcsClient.getAcsResponse(request);
                System.out.println(response);
                //System.out.println(response.getSuccess());
                //System.out.println(response.getErrorMessage());
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
    
        }
    }
  • 相关阅读:
    PHP学习当中遗漏的知识点
    sql必知必会(第四版) 学习笔记
    servlet 笔记
    sql server 快捷键
    233
    第 四 课 数据类型
    第三课 go语言基础语法
    第二课 go语言的结构
    第 1 课 Go 简介
    linux 学习 查看文件占用空间大小
  • 原文地址:https://www.cnblogs.com/kawayidamiao/p/13843726.html
Copyright © 2020-2023  润新知