AT+MQTT指令接入示例

背景信息

随着MQTT协议在物联网的流行,越来越多的模组内置MQTT协议栈,并提供MQTT的指令集供MCU侧使用。相对于使用TCP/SSL指令的接入方案,使用MQTT指令接入可以进一步降低MCU侧的资源消耗。

AT+MQTT接入是设备上云的一种方案,这种方案使用MCU+模组作为硬件,MCU与模组之间的通信采用AT+MQTT的指令集且MQTT协议栈运行在模组侧。

image

环境准备

环境配置

说明

MCU开发板

  • 名称:STM32 Nucleo板

  • 型号:STM32L476RG

  • 系统:FreeRTOS

  • 参考文档:NUCLEO-L476RG

模组通信板

EC200S

SIM卡

物联网卡,不限运营商,插入模组开发板

接口连接

  • STM32串口1和EC200之间进行AT指令通信,使用的波特率是115200

  • STM32串口3与PC之间进行STM32日志输出,使用的波特率是115200

开发环境

MDK-Arm

示例工程

下载链接

设备认证信息

获取设备认证信息

使用步骤

  1. 打开示例。

    使用MDK-Arm打开./LinkSDK/portfiles/aiot_port/project/stm32l476-at/MDK-ARM/L476.uvprojx。MDK-Arm的使用说明,请参见MDK-Arm

  2. 替换设备身份认证信息。

    打开./LinkSDK/portfiles/aiot-port/project/stm32l476-at/Core/mqtt_at_basic_demo.c,配置设备认证信息。

    参数

    说明

    PRODUCT_KEY

    设备认证信息。登录物联网平台控制台,单击目标物联网平台实例卡片,在左侧导航栏选择设备管理>设备,单击目标设备的DeviceName/备注名称,在详情页查看设备的认证信息。

    DEVICE_NAME

    DEVICE_SECRET

    INSTANCE_ID

    物联网平台企业实例ID。

  3. 可选:MCU串口适配。

    如果您的MCU选型或串口实现与本示例不同,可以通过修改硬件适配。在路径./LinkSDK/portfiles/aiot-port/project/stm32l476-at/Core/hal_adapter.c中实现了UART的发送和接收功能。

    说明

    本示例为Debug工程,包含AT指令的发送和接收功能,并且在这些功能中输出日志信息。建议在发布版本的工程中关闭这些日志输出,以提高程序的执行效率,具体功能是在at_uart_sendat_uart_recv函数实现。

  4. 可选:模组适配。

    若您的模组选型与示例不同,可以修改AT驱动以适配您的模组。在路径./LinkSDK/portfiles/aiot-port/at_mqtt/aiot_at_mqtt_api.c中实现AT+MQTT驱动。

运行结果

  • 模组初始化

    at_hal_init 
    [uart]>>> AT
    [uart]<<< 
    OK
    [uart]>>> ATE0
    [uart]<<< 
    OK
    [uart]>>> ATI
    [uart]<<< 
    Quectel
    EC200S
    Revision: EC200SCNAAR01A08M16
    OK
    [uart]>>> AT+CGSN
    [uart]<<< 
    866222052347609
    OK
    [uart]>>> AT+CGMR
    [uart]<<< 
    EC200SCNAAR01A08M16
    OK
    [uart]>>> AT+CPIN?
    [uart]<<< 
    +CPIN: READY
    OK
    [uart]>>> AT+CIMI
    [uart]<<< 
    460115060724225
    OK
    [uart]>>> AT+CSQ
    [uart]<<< 
    +CSQ: 31,99
    OK
    [uart]>>> AT+QICSGP=1,1,"UNINET","","",1
    [uart]<<< 
    OK
    [uart]>>> AT+QIDEACT=1
    [uart]<<< 
    OK
    [uart]<<< 
    +QMTSTAT: 0,1
    [uart]>>> AT+QIACT=1
    [uart]<<< 
    OK
    [uart]>>> AT+QIACT?
    [uart]<<< 
    +QIACT: 1,1,1,"10.40.31.***"
    OK
  • MQTT建连

    [372][LK-0313] MQTT user calls aiot_mqtt_connect api, connect
    [404][LK-032A] mqtt host: iot-******.mqtt.iothub.aliyuncs.com
    [418][LK-0317] user name: mqtt_********
    [uart]>>> AT+QMTCFG="recv/mode",0,0,1
    [uart]<<< 
    OK
    [uart]>>> AT+QMTCFG="dataformat",0,0,0
    [uart]<<< 
    OK
    [uart]>>> AT+QSSLCFG="sslversion",1,1
    [uart]<<< 
    OK
    [uart]>>> AT+QSSLCFG="ciphersuite",1,0X0035
    [uart]<<< 
    OK
    [uart]>>> AT+QSSLCFG="cacert",1,"UFS:cacert.pem"
    [uart]<<< 
    OK
    [uart]>>> AT+QSSLCFG="seclevel",1,1
    [uart]<<< 
    OK
    [uart]>>> AT+QFOPEN="cacert.pem",1,1
    [uart]<<< 
    +QFOPEN: 1
    OK
    [uart]>>> AT+QFWRITE=1,1280
    [uart]<<< 
    CONNECT
    [uart]>>> -----BEGIN CERTIFICATE-----
    
    MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG
    
    A1**********RjbgbME
    
    HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==
    
    -----END CERTIFICATE-----
    
    [uart]<<< +QFWRITE: 1280,1280
    OK
    [uart]>>> AT+QFCLOSE=1
    [uart]<<< 
    OK
    [uart]>>> AT+QMTCFG="SSL",0,1,1
    [uart]<<< 
    OK
    [uart]>>> AT+QMTOPEN=0,"iot-******.mqtt.iothub.aliyuncs.com",443
    [uart]<<< 
    OK
    [uart]<<< 
    +QMTOPEN: 0,0
    [uart]>>> AT+QMTCONN=0,"gb8*****.mqtt_basic_demo|timestamp=2524608000000,_ss=1,_v=sdk-c-4.1.0,securemode=2,signmethod=hmacsha256,ext=3,mid=ec200s_at_mqtt,os=freertos,cpu=stm32l476rg,_conn=tls_0|","mqtt_basic_demo&gb80sFmX7yX","D35E447B87879FFB1C80E014E5997B475262CBC1ACD6426EB38B******"
    [uart]<<< 
    OK
    [uart]<<< 
    +QMTCONN: 0,0,0
    [1.688][LK-0313] MQTT connect success in 1317 ms
    AIOT_MQTTEVT_CONNECT
  • 订阅Topic

    [1.755][LK-0309] sub: /gb8*****/mqtt_basic_demo/user/get
    [uart]>>> AT+QMTSUB=0,1,"/gb8*****/mqtt_basic_demo/user/get",1
    [uart]<<< 
    OK
    [1.788][LK-0000] mqtt sub result: 0
    [uart]<<< 
    +QMTSUB: 0,1,0,1
    suback, res: -0x0000, packet id: 1, max qos: 0
  • 取消订阅Topic

    [1.711][LK-0309] unsub: /gb8*****/mqtt_basic_demo/user/get
    [uart]>>> AT+QMTUNS=0,100,"/gb8****/mqtt_basic_demo/user/get
    [uart]<<< 
    OK
    [uart]<<< 
    +QMTUNS: 0,100,0
    unsuback, packet id: 100
  • 发布消息

    [3.800][LK-0309] pub: /sys/gb8******/mqtt_basic_demo/thing/event/property/post
    [LK-030A] > 7B 22 69 64 22 3A 22 31  22 2C 22 76 65 72 73 69 | {"id":"1","versi
    [LK-030A] > 6F 6E 22 3A 22 31 2E 30  22 2C 22 70 61 72 61 6D | on":"1.0","param
    [LK-030A] > 73 22 3A 7B 22 4C 69 67  68 74 53 77 69 74 63 68 | s":{"LightSwitch
    [LK-030A] > 22 3A 30 7D 7D                                   | ":0}}           
    [uart]>>> AT+QMTPUBEX=0,0,0,0,"/sys/gb8******/mqtt_basic_demo/thing/event/property/post",53
    [uart]<<< 
    > 
    [uart]>>> {"id":"1","version":"1.0","params":{"LightSwitch":0}}
    [uart]<<< 
    OK
    [uart]<<< 
    +QMTPUBEX: 0,0,0
  • 收到发布的消息

    [uart]<<< 
    +QMTRECV: 0
    [uart]<<< ,0,"/sys/gb8*****/mqtt_basic_demo/thing/event/property/post_reply",104,"{"code":200,"data":{},"id":"1","message":"success","method":"thing.event.property.post","version":"1.0"}"
    [4.099][LK-0309] pub: /sys/gb8*****/mqtt_basic_demo/thing/event/property/post_reply
    [LK-030A] < 7B 22 63 6F 64 65 22 3A  32 30 30 2C 22 64 61 74 | {"code":200,"dat
    [LK-030A] < 61 22 3A 7B 7D 2C 22 69  64 22 3A 22 31 22 2C 22 | a":{},"id":"1","
    [LK-030A] < 6D 65 73 73 61 67 65 22  3A 22 73 75 63 63 65 73 | message":"succes
    [LK-030A] < 73 22 2C 22 6D 65 74 68  6F 64 22 3A 22 74 68 69 | s","method":"thi
    [LK-030A] < 6E 67 2E 65 76 65 6E 74  2E 70 72 6F 70 65 72 74 | ng.event.propert
    [LK-030A] < 79 2E 70 6F 73 74 22 2C  22 76 65 72 73 69 6F 6E | y.post","version
    [LK-030A] < 22 3A 22 31 2E 30 22 7D                          | ":"1.0"}        
    
    pub, qos: 0, topic: /sys/gb8*****/mqtt_basic_demo/thing/event/property/post_reply