Try   HackMD

新人資訊

技術-開發板 #3-在 Arduino 實作 MQTT Client

在上一回合中已經搞定網路連線部分,接下來準備往雲端丟資料了,怎們丟呢?大體上有兩種,頻繁而少量的適合用 MQ 這種較輕量化的通信協定,資料大一點且搭配即時運算邏輯的則採用 REST Api,但它的 Server 端占用資源較多而不適合很頻繁的呼叫。從此篇文章 (https://funprojects.blog/2018/12/07/rabbitmq-for-iot/) 我才了解,MQ 又分最輕量化的 MQTT 和稍微複雜的 AMQP,而這三種 Rabbit MQ 全部支援,因此我申請了雲端的 Rabbit MQ (https://www.cloudamqp.com/) 來實驗,先把雲端環境準備好,如下 Little Lemur 免費方案額度超大,實驗用綽綽有餘。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

隨意建一個 instance,命名為 newmanMessage

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

點選 newmanMessage 顯示詳細資料中,有一個 MQTT 屬性區塊,記好這四個欄位,client 連線需要指定的。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

進到 newmanMessage 的管理介面,建立一個 Queue 命名為 newcore-iot

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

在 Exchange 頁籤中的 amq.topic 建立一個新的 Routing key newcore-iot 導到剛剛建立的 queue

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

按下 Bind 之後該區塊出現 Binding 成功的圖樣,newcore-iot 也必須記住,client 需要用到。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

現在開始寫 Arduino 端的程式,之前為了 wifi client 而寫的程式必須保留,因為它是連線的基礎。另外 include 必要的函示庫 (這是內建,不用安裝),設好幾的常數存放 MQTT 所需要的參數值,並做 Global 變數型別宣告。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

主程式流程先執行連網動作,再執行 mq 連線,連練都完成後執行一次發布訊息和訂閱一個 topic newcore-to-device,預期功用是擔任 server 傳送指令給裝置端的重要樞紐。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

從 COM Port 監視視窗中已經確認 WiFi 連線成功後,MQTT 也成功了,下一步就是到雲端平台去看看吧。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

雲端平台果然顯示有訊息傳入,執行 Get Message 確認內容「Hello from Arduino」無誤,Arduino 已經與雲端的 Rabbit MQ 溝通成功了。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

以上發送訊息部分很單純如預期,但「訂閱」訊息的行為非常怪異,程式連線成功後會自動建立一個 queue,也非我指定的 newcore-to-device 反倒出現以下一個怪怪的 queue,預設板子離線後就會消失,後來我把 clean session 參數指定為 false 後,則此 queue 可持續存留,在此發送訊息會順利觸發 callback(),勉強算有測通,但掌握度還差。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

邁向 IoT 系統又前進一小步,接下來還要測通 REST Api,然後還需要回頭把 MQ 機制搞的更清楚,此通訊模式在物連網系統中的角色算是相當吃重。

By Newman Chen 2022/4/11

Updeated 「Subscription part」at 2022/5/4

參考資料:

https://funprojects.blog/2018/12/07/rabbitmq-for-iot/

https://www.cloudamqp.com/