【啟動AI Maker世代 】2024 MAI 開發者社群大會(5/16-17)
|

【開箱評測】MTDuino IOT開發板: MQTT功能實作介紹

   
作者:柯大

為提供快速開發物聯網各種應用,解決移動型不同應用場域,邁特電子特別推出一款裝置,具有省電、長距離傳輸、低功耗通訊廣域網路,也支援開源開發環境Arduino IDE及Micro Python 的開發板 – MTDuino。

上一期介紹到 MTDuino基本功能,並實作了抓取GPS衛星定位位置,經實際測試不需特別的GPS天線,只用邁特電子出廠所附的小型天線,即可快速定位,冷暖開機定位速度均有不錯的效果,在道路上實際測試也能快速抓到定位經緯度。

本期將再介紹MTDuino的通訊功能,這塊板子採用Quectec BG77模組,除了內建GNSS GPS功能外,也支援LET Cat M1及LET Cat NB2二種低功耗物聯網通訊功能。本次實作將以Arduino IDE 撰寫3個實作,分別用MTDuino NBIOT MQTT通訊協定,實作訂閱(Subscribe)/發佈(Publish)/的機制傳訊。

實作前先介紹BG77模組MQTT協定相關AT command,如下:

  1. AT+QMTCFG:設定MQTT協議運作參數
  2. AT+QMTOPEN:開啟設定 MQTT裝置端(Client)連接MQTT服務器( Broker)
  3. AT+QMTCLOSE:關閉MQTT裝置端(Client) 連接MQTT服務器( Broker)
  4. AT+QMTCONN:將裝置端(Client)連接到 MQTT 服務器
  5. AT+QMTDISC:斷開裝置端(Client)連接與MQTT服務器的連接
  6. AT+QMTSUB:訂閱主題(Subscribe)
  7. AT+QMTUNS:取消訂閱主題(Unsubscribe)
  8. AT+QMTPUB:發佈訊息(Publish)
  9. AT+QMTPUBEX:發佈訊息(Publish)
  10. AT+QMTRECV:從緩衝區中讀取訊息

※以下AT command 說明文件資料來源:Quectec 原廠文件

AT command 範例:

AT+QMTOPEN=?

+QMTOPEN: (0-5),,(0-65535)

OK

AT+QMTOPEN=0,"iot-as-mqtt.cn-shanghai.aliyuncs.com",1883 //Open a network for MQTT client.

OK

+QMTOPEN: 0,0 //Opened the MQTT client network successfully.

AT+QMTOPEN?

+QMTOPEN: 0,"iot-as-mqtt.cn-shanghai.aliyuncs.com",1883

OK

AT+QMTCONN=?

+QMTCONN: (0-5),,,

OK

//Connect a client to MQTT server.

//If AliCloud is connected, AT+QMTCFG="aliauth" can be used to configure the device information in

advance, and there is no need to provide username/password here anymore.

AT+QMTCONN=0,"clientExample"

OK

+QMTCONN: 0,0,0 //Connected the client to MQTT server successfully.

AT+QMTSUB=?

+QMTSUB: (0-5),(1-65535),,(0-2)

OK

//Subscribe to topics.

AT+QMTSUB=0,1,"topic/example",2

OK

+QMTSUB: 0,1,0,2

AT+QMTSUB=0,1,"topic/pub",0

OK

+QMTSUB: 0,1,0,0

//If a client subscribes to a topic and other devices publish the same topic to the server, the module will

report the following information.

+QMTRECV: 0,0,"topic/example","This is the payload related to topic"

//Unsubscribe from topics.

AT+QMTUNS=0,1,"topic/example"

OK

+QMTUNS: 0,2,0

AT+QMTPUB=?

+QMTPUB : (0-5),(0-65535),(0-2),(0,1),,(1-4096)

OK

//Publish messages.

AT+QMTPUB=0,0,0,0,"topic/pub"

>This is test data, hello MQTT. //After receiving >, input data This is test data, hello MQTT. and

then send it. The maximum length of the data is 4096 bytes and the

data that beyond 4096 bytes will be omitted. After inputting data,

tap Ctrl+Z to send.

OK

+QMTPUB: 0,0,0

//If a client subscribes to a topic named "topic/pub" and other devices publish the same topic to the server,

the module will report the following information.

+QMTRECV: 0,0,"topic/pub","This is test data, hello MQTT."

AT+QMTDISC=0 //Disconnect a client from MQTT server.

OK

+QMTDISC: 0,0 //Connection closed successfully

//Start MQTT SSL connection

AT+QMTOPEN=0,"a1zgnxur10j8ux.iot.us-east-1.amazonaws.com",8883

OK

+QMTOPEN: 0,0

//Connect to MQTT server

AT+QMTCONN=0,"MQTT-1"

OK

+QMTCONN: 0,0,0

//Subscribe to topics.

AT+QMTSUB=0,1,"$aws/things/ MQTT-1/shadow/update/accepted",1

OK

+QMTSUB: 0,1,0,1

//Publish messages.

AT+QMTPUB=0,1,1,0,"$aws/things/MQTT-1/shadow/update/accepted"

>This is publish data from client

OK

+QMTPUB: 0,1,0

//If a client subscribes to a topic named “$aws/things/MQTT-1/shadow/update/accepted” and other

devices publish the same topic to the server, the module will report the following information.

+QMTRECV: 0,1,"$aws/things/MQTT-1/shadow/update/accepted","This is publish data from client"

//Disconnect a client from MQTT server.

AT+QMTDISC=0

OK

+QMTDISC: 0,0

AT+QMTCFG

該命令用於配置 MQTT 的可選參數。

本文為會員限定文章

立即加入會員! 全站文章無限看~

                               

已經是會員? 按此登入

只需不到短短一分鐘...

輸入您的信箱與ID註冊即可享有一切福利!

會員福利
1

免費電子報

2

會員搶先看

3

主題訂閱

4

好文收藏

ChingChang Ko

Author: ChingChang Ko

MakerPRO社群技術顧問 柯大創客屋社群創辦人,柯大有近30年資訊電子產業研發經驗,關心最新技術,並投入不綴並於社群分享技術經驗文。亦熱心指導新興團隊參與技術創意競賽,獲獎無數。專長包括物聯網軟硬體整合、整體服務平台規劃設計;熟悉各種物聯網開發板及藍芽、WiFi、NFC、Zigbee、LoRa等通訊技術並熟悉各種物聯網開發板:Arduino、Edison、Linkit one 、Ameba、Raspberry,精通物聯網低功耗通訊技術LPWAN:LoRa、Sigfox、NBIoT。

Share This Post On
468 ad

Submit a Comment

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *