iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 25
0
自我挑戰組

從python入門到物聯網系列 第 25

Day25 - AWS EC2 安裝 Mosquitto

Mosquitto MQTT Broker

當使用MQTT協議進行數據傳輸時,需要一個 Broker 來轉發資料,在這裡選擇 Mosquitto 當 Broker。Mosquitto 是一個開放原始碼 MQTT broker,特點是輕巧,在低功耗的裝置或者完整服務器的所有設備上都適合使用。

它的功能包括存儲數據以及處理數據,如果收到任何訊息,會根據不同Topic分類,存儲在該Topic下的database中,以及將訊息傳給有訂閱的client。

使用 mosquitto pub和mosquitto sub來發布和訂閱訊息。

安裝 Mosquitto MQTT Broker

Step1 連線:

連線EC2

ssh -i ~/.ssh/key名稱.pem ubuntu@IP_Address

Step2 安裝:

  1. 先來更新伺服器的套件管理
sudo apt update
  • sudo 提高權限,讓自己變成管理員
  • apt 套件管理程式
  1. 安裝 Mosquitto Broker
sudo apt install mosquitto

  1. 安裝 Mosquitto Client
sudo apt install mosquitto-clients

Step3 測試:

Publish、Subscribe測試
開啟兩個終端機
一台為 Subscribe
一台為 Publish

  • 功能:
    mosquitto_sub : 訂閱
    mosquitto_pub : 發布
    -d : debug 模式 => debug
    -t : 訂閱的主題 => topic
    -h : Broker 的 IP => host
    -m : 發送的內容 => message
    -v : 顯示主題名稱 => verbosely

更多指令內容可以查看:
mosquitto_sub --help
mosquitto_pub --help

終端機-1 (Subscribe)

訂閱Try/MQTT的主題 (主題名稱不包含空白)

mosquitto_sub -d -t Try/MQTT

或者

mosquitto_sub -d -h 54.xx.xx.xx -t Try/MQTT

-h : mqtt host to connect to. Defaults to localhost.

接著就會看到
Topic、Qos等...資訊

Client mosqsub|3323-ip-172-31- sending PINGREQ
Client mosqsub|3323-ip-172-31- received PINGRESP

持續保持連線

終端機-2 (Publish)

mosquitto_pub -d -t Try/MQTT -m "Try Message"

或者

mosquitto_pub -d -h 54.xx.xx.xx -t Try/MQTT -m "Try Message"

接著就會看到

發布後,終端機1 (Subscribe)得到以下資訊

使用帳號&密碼

上述是在沒有任何帳號密碼的保護下,大家都能看到喔!
接下來就在 mosquitto 使用帳號和密碼吧!

新增帳號密碼

利用 mosquitto_passwd 設立密碼,還有密碼檔案。
指令:

mosquitto_passwd -c <passwordfile> <username>

-c 創建 passwordfile 檔案,再次新增使用者就需要加-c

密碼檔案路徑 /etc/mosquitto/passwd
建立帳號名稱 try,接著輸入密碼後即可建立成功

sudo mosquitto_passwd -c /etc/mosquitto/passwd try
Password: xxxx
Reenter password: xxxx

設定ACL(Access Control List)

sudo vi /etc/mosquitto/acl

創建一個acl檔案

  1. 使用 vi 進入一般模式

新建的檔案

  1. 按下 i 進入編輯模式,開始編輯文字
# 用戶 try 能夠讀寫 Try/MQTT這個主題
user try 
topic readwrite Try/MQTT

  1. 按下 [ESC] 按鈕回到一般模式

  2. 在一般模式中按下 :wq 儲存後離開

有關文件編輯的相關指令可以參考 vi 文書處理軟體

mosquitto.conf 參數檔案,開啟模式

打開 mosquitto.conf 參數檔案
設定檔位置在 /etc/mosquitto/mosquitto.conf

輸入指令 - 打開並編輯mosquitto.conf 參數檔案:

sudo vi /etc/mosquitto/mosquitto.conf

會看到預設 mosquitto.conf的檔案長這樣:

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d
  1. 使用 vi 進入一般模式

  1. 按下 i 進入編輯模式,在檔案新增以下資訊
allow_anonymous false 
password_file /etc/mosquitto/passwd
acl_file /etc/mosquitto/acl

allow_anonymous 允許匿名存取,設成false 代表需使用者名稱。
password_file 帳號密碼的檔案
acl_file 對於使用者權限的設置檔案

  1. 按下 [ESC] 按鈕回到一般模式

  2. 在一般模式中按下 :wq 儲存後離開

測試

因為剛剛修改了mosquitto.conf設定檔案
所以要重新開啟 mosquitto service

sudo service mosquitto stop
sudo service mosquitto start

或者

sudo /etc/init.d/mosquitto stop
sudo /etc/init.d/mosquitto start

可以用 status 查看 mosquitto 狀態

service mosquitto status

Subscribe

訂閱Try/MQTT的主題

來試試如果不加帳號密碼會如何吧!

Connection Refused: not authorised.
就會像這樣被拒絕連接 因為沒有授權。

格式:

mosquitto_sub -v -d -t <topic> -u <user> -P <Possword>

範例:

mosquitto_sub -v -d -t Try/MQTT -u "try" -P "xxxx"

Publish

格式:

mosquitto_pub -d -t <Topic> -m <Message> -u <User> -P <Possword>

範例:

mosquitto_pub -d -t Try/MQTT -m "Try Message" -u "try" -P "xxxx"

發布後 Subscribe 得到以下資訊:

今天都是自己連線到同一台(EC2虛擬機),明天來使用 MQTTlens 用其他地方來連進EC2虛擬機的Mosquitto Broker! /images/emoticon/emoticon08.gif

參考資料

https://mosquitto.org/

https://blog.gtwang.org/iot/raspberry-pi/raspberry-pi-mosquitto-mqtt-broker-iot-integration/

http://linux.vbird.org/linux_basic/0310vi/0310vi.php


上一篇
Day24 - 建立 Amazon EC2 虛擬主機
下一篇
Day26 - MQTTlens
系列文
從python入門到物聯網30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言