AT+MQTT指令接入示例
背景信息
隨著MQTT協(xié)議在物聯(lián)網(wǎng)的流行,越來(lái)越多的模組內(nèi)置MQTT協(xié)議棧,并提供MQTT的指令集供MCU側(cè)使用。相對(duì)于使用TCP/SSL指令的接入方案,使用MQTT指令接入可以進(jìn)一步降低MCU側(cè)的資源消耗。
AT+MQTT接入是設(shè)備上云的一種方案,這種方案使用MCU+模組作為硬件,MCU與模組之間的通信采用AT+MQTT的指令集且MQTT協(xié)議棧運(yùn)行在模組側(cè)。
環(huán)境準(zhǔn)備
環(huán)境配置 | 說(shuō)明 |
MCU開(kāi)發(fā)板 |
|
模組通信板 | |
SIM卡 | 物聯(lián)網(wǎng)卡,不限運(yùn)營(yíng)商,插入模組開(kāi)發(fā)板 |
接口連接 |
|
開(kāi)發(fā)環(huán)境 | |
示例工程 | |
設(shè)備認(rèn)證信息 |
使用步驟
打開(kāi)示例。
使用MDK-Arm打開(kāi)
./LinkSDK/portfiles/aiot_port/project/stm32l476-at/MDK-ARM/L476.uvprojx
。MDK-Arm的使用說(shuō)明,請(qǐng)參見(jiàn)MDK-Arm。替換設(shè)備身份認(rèn)證信息。
打開(kāi)./LinkSDK/portfiles/aiot-port/project/stm32l476-at/Core/mqtt_at_basic_demo.c,配置設(shè)備認(rèn)證信息。
參數(shù)
說(shuō)明
PRODUCT_KEY
設(shè)備認(rèn)證信息。登錄物聯(lián)網(wǎng)平臺(tái)控制臺(tái),單擊目標(biāo)物聯(lián)網(wǎng)平臺(tái)實(shí)例卡片,在左側(cè)導(dǎo)航欄選擇設(shè)備管理>設(shè)備,單擊目標(biāo)設(shè)備的DeviceName/備注名稱(chēng),在詳情頁(yè)查看設(shè)備的認(rèn)證信息。
DEVICE_NAME
DEVICE_SECRET
INSTANCE_ID
物聯(lián)網(wǎng)平臺(tái)企業(yè)實(shí)例ID。
可選:MCU串口適配。
如果您的MCU選型或串口實(shí)現(xiàn)與本示例不同,可以通過(guò)修改硬件適配。在路徑
./LinkSDK/portfiles/aiot-port/project/stm32l476-at/Core/hal_adapter.c
中實(shí)現(xiàn)了UART的發(fā)送和接收功能。說(shuō)明本示例為Debug工程,包含AT指令的發(fā)送和接收功能,并且在這些功能中輸出日志信息。建議在發(fā)布版本的工程中關(guān)閉這些日志輸出,以提高程序的執(zhí)行效率,具體功能是在
at_uart_send
和at_uart_recv
函數(shù)實(shí)現(xiàn)。可選:模組適配。
若您的模組選型與示例不同,可以修改AT驅(qū)動(dòng)以適配您的模組。在路徑
./LinkSDK/portfiles/aiot-port/at_mqtt/aiot_at_mqtt_api.c
中實(shí)現(xiàn)AT+MQTT驅(qū)動(dòng)。
運(yùn)行結(jié)果
模組初始化
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
發(fā)布消息
[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
收到發(fā)布的消息
[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