[MQTT]Mosquitto的內網連接(intranet)和使用者/密碼權限設置

9 篇文章 0 订阅
7 篇文章 0 订阅

[MQTT | Raspberry Pi]Publish and Subscribe with RSSI Data of Esp32 on Intranet

延續[MQTT]Mosquitto的簡介、安裝與連接測試文章,接著將繼續測試在內網的兩台機器是否也可以完成發佈和訂閱作業。

同一網段的兩台電腦測試:

假設兩台電腦的配置如下:

A電腦為發佈端兼broker角色,IP:192.168.0.101
B電腦為訂閱端,IP:192.168.0.102

a.請在A電腦的mosquitto.conf內,新增下列內容
#For remote access
listener 1883
allow_anonymous true

完成修改後,重新啟動mosquitto broker。

b.A電腦作為發佈端來發佈主題,並同時為中介端,接收來自發佈端和訂閱端的消息。
mosquitto_pub -h 192.168.0.101 -t sensor_1 -m “25.3C”
c.B電腦訂閱來自A電腦發佈的主題
mosquitto_sub -h 192.168.0.101 -t sensor_1

另外,mosquitto官網也有提供一個broker供使用者測試訂閱和發佈功能是否正常。

使用者/密碼權限設定

在mosquitto中,可利用建置一個user/password文件來做發佈者和訂閱者的管理,步驟如下:

a.在A電腦建置一個檔名為password_file的文件,並將username和password資訊放入該檔案中。
sudo touch password_file
sudo mosquitto_passwd -b password_file username password
b.在A電腦的mosquitto.conf文件中,請一併新增下列配置內容
#For authentication premission
password_file /etc/mosquitto/password_file

完成設置的password_file內容如下圖一,並重新啟動 mosquitto broker。

c.A電腦為發佈端的指令如下
mosquitto_pub -h 192.168.0.101 -t sensor_1 -m “25.3C” -u <username> -P <password>
d.B電腦為訂閱端的指令如下
mosquitto_sub -h 192.168.0.101 -t sensor_1 -u <username> -P <password>

關於權限設定,mosquitto提供Password, Acess Control List(ACL)和Pre-Shared Key(PSK)三種方式,對應的範例文件分別為pwfile.example, aclfile.example和pskfile.example,可在/usr/share/doc/mosquitto/examples中開啟檢閱。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值