MQTTでIoT通信(2)mosquittoの設定

 mosquittoの設定する。

/etc/mosquitto/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
#pid_file /run/mosquitto/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d

詳細は /etc/mosquitto/conf.d 以下からインクルードするようになっている。特にファイルは置かれていない(READMEだけ)。取りあえず最低限の記述をするために /etc/mosquitto/conf.d/01mosquitto.conf を作成

listener 1883 0.0.0.0
allow_anonymous true

全てのIPからポート1883で受信する

パスワード認証とかは無し という設定です。  

sudo systemctl restart mosquitto

で再起動。

mosquitto_sub -d -t /client/abc

を実行。別クライアントから

mosquitto_pub -h [IP address] -p 1883  -t /client/abc -m "shee546978"

で通信できる。


ユーザー認証

以下でユーザー作成1回目だけ-c をつける(ファイル pwfile が作成される)
sudo mosquitto_passwd -c /etc/mosquitto/pwfile testuser
パスワード入力を2回求められるので適当にいれると ファイル pwfile にハッシュ値?で保存される(違うユーザーで同じパスワードいれても違う値になるね)。さらに、ファイルのパーミッションを変更

sudo chown mosquitto:mosquitto /etc/mosquitto/pwfile
sudo chmod 600 /etc/mosquitto/pwfile

/etc/mosquitto/conf.d/01mosquitto.conf の 以下の部分を変更
allow_anonymous false
で再起動

sudo systemctl restart mosquitto

ブローカーで

mosquitto_sub -d -t /client/abc -u testuser -P test

を実行

 Client null sending CONNECT
Client null received CONNACK (5)
Connection error: Connection Refused: not authorised.
Client null sending DISCONNECT

でOKになる。


パブリッシャーから 

mosquitto_pub -h [IP address] -p 1883 -t /client/abc -u testuser1 -P test -m "shee546978"

で送信できる。ただし、この場合は通信経路の暗号化をしていないはずなので、セキュリティー的には×である。 ただ、取りあえずのローカルの実験ではここまでか。証明書作成するのは後日に

 

 

コメント

このブログの人気の投稿

ImDisk Toolkitが開発終了していた。AIM Toolkitに乗り換え

Windows 10 デフォルトゲートウェイに0.0.0.0が追加される

iOS VLC でSMB共有できなかった点について