參考:https://ithelp.ithome.com.tw/articles/10226629
1.安裝MQTT server
sudo apt install mosquitto2.安裝MQTT clientsudo apt install mosquitto-clients mosquitto_sub : 訂閱
mosquitto_pub : 發布
-d : debug 模式 => debug
-t : 訂閱的主題 => topic
-h : Broker 的 IP => host
-m : 發送的內容 => message
-v : 顯示主題名稱 => verbosely 終端機-1 (Subscribe)訂閱
訂閱mosquitto_sub -d -t Try/MQTT 或mosquitto_sub -d -h 54.xx.xx.xx -t Try/MQTT終端機-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"使用帳密
使用帳密mosquitto_passwd -c <passwordfile> <username>指定密碼檔案 sudo mosquitto_passwd -c /etc/mosquitto/passwd try 這樣會覆蓋掉原來的passwd檔。先編一個帳密碼vim usr.pwd內容如下user:123456student:123使用指令轉成加密檔 mosquitto_passwd -U usr.pwd sudo mv usr.pwd /etc/mosquitto/ 設定ACL(Access Control List)
sudo vi /etc/mosquitto/acl 內容如下
# 用戶 try 能夠讀寫 Try/MQTT這個主題
#user try
#topic readwrite Try/MQTT(設定後失敗,先註解掉) 在mosquitto.conf 參數檔案,開啟模式
打開 mosquitto.conf 參數檔案
設定檔位置在 /etc/mosquitto/mosquitto.conf
增加
listener 1883
protocol mqtt
listener 8081 127.0.0.1
protocol websocketsallow_anonymous true這樣js 才能用ws://127.0.0.1:8081連線 password_file /etc/mosquitto/passwdper_listener_setting true
acl_file /etc/mosquitto/acl per_listener_setting參數設成true,代表個別設置每個偵聽器物件的安全性: allow_anonymous 允許匿名存取,設成false 代表需使用者名稱。
password_file 帳號密碼的檔案
acl_file 對於使用者權限的設置檔案 重啟服務sudo service mosquitto stop
sudo service mosquitto start 訂閱 mosquitto_sub -v -d -t Try/MQTT -u "try" -P "xxxx" mosquitto_sub -d -t Try/MQTT mosquitto_sub -d -h 54.xx.xx.xx -t Try/MQTT發佈 mosquitto_pub -d -t Try/MQTT -m "Try Message" mosquitto_pub -d -h 54.xx.xx.xx -t Try/MQTT -m "Try Message"
測試了一下一個網站還不錯:
http://mqttgo.io/
使用osep離線版線線網址為:
ws://MQTTGO.io:8000/mqtt
沒有留言:
張貼留言