為提供快速開發物聯網各種應用,解決移動型不同應用場域,邁特電子特別推出一款裝置,具有省電、長距離傳輸、低功耗通訊廣域網路,也支援開源開發環境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,如下:
- AT+QMTCFG:設定MQTT協議運作參數
- AT+QMTOPEN:開啟設定 MQTT裝置端(Client)連接MQTT服務器( Broker)
- AT+QMTCLOSE:關閉MQTT裝置端(Client) 連接MQTT服務器( Broker)
- AT+QMTCONN:將裝置端(Client)連接到 MQTT 服務器
- AT+QMTDISC:斷開裝置端(Client)連接與MQTT服務器的連接
- AT+QMTSUB:訂閱主題(Subscribe)
- AT+QMTUNS:取消訂閱主題(Unsubscribe)
- AT+QMTPUB:發佈訊息(Publish)
- AT+QMTPUBEX:發佈訊息(Publish)
- 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註冊即可享有一切福利!
會員福利
免費電子報
會員搶先看
主題訂閱
好文收藏