背景
IoTデバイスの接続環境構築のため、MQTT(mosquitto)の導入を行った。
記事の目的
MQTT(mosquitto)をUbuntuに導入する
mosquitto
ここではmosquittoについて記載する。
MQTT
MQTT(Message Queueing Telemetry Transport)とは、マシン間の通信に使用される標準ベースのメッセージングプロトコル、または一連のルールを指す。スマートセンサー、ウェアラブル、および他のモノのインターネット (IoT) デバイスは通常、帯域幅が制限されたリソースに制約のあるネットワークを介してデータを送受信する必要がある。これらの IoT デバイスは、実装が簡単で、IoT データを効率的に通信できるため、MQTT を使用してデータを送信する。MQTT は、デバイスからクラウドへ、およびクラウドからデバイスへのメッセージングをサポートしている。mosquitto
Eclipse Mosquittoは、MQTTプロトコルバージョン5.0、3.1.1、3.1を実装したオープンソース(EPL/EDLライセンス)メッセージブローカーである。Mosquittoは軽量で、低消費電力のシングルボードコンピュータからフルサーバーまで、あらゆるデバイスでの使用に適している。 MQTTプロトコルは、パブリッシュ/サブスクライブモデルを使用したメッセージングを実行する軽量な方法を提供している。このため、低消費電力のセンサーや電話などのモバイル機器、組み込みコンピュータ、マイクロコントローラなど、モノのインターネットを利用したメッセージングに適している。 Mosquittoプロジェクトは、MQTTクライアントを実装するためのCライブラリや、非常に人気のあるmosquitto_pubおよびmosquitto_subコマンドラインMQTTクライアントを提供している。Ubuntuへの導入法
Ubuntuへのmosquitto導入方法について記載する。- mosquittoのインストール aptを使用してインストールすることができる
- aptによるインストール
- apt install
- $ sudo add-apt-repository ppa:mosquitto-dev/mosquitto-ppa
- $ sudo apt install mosquitto mosquitto-clients
- 設定
- $ sudo vim /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.example
- pid_file /var/run/mosquitto.pid
- persistence true
- persistence_location /var/lib/mosquitto/
- # ログの出力先
- log_dest file /var/log/mosquitto/mosquitto.log
- # 別の設定(port以下を書いたconfファイル)を下記フォルダに追加すると反映される
- include_dir /etc/mosquitto/conf.d
- # mosquittoの接続先ポート
- port 8883
- # SSL通信を使用する際に必要
- # CA証明書
- cafile /etc/mosquitto/certs/ca.crt
- # 秘密鍵
- keyfile /etc/mosquitto/certs/server.key
- # サーバ証明書
- certfile /etc/mosquitto/certs/server.crt
- # 接続時にパスワードを設定する場合
- # ユーザー名を必須にする
- allow_anonymous false
- # パスワードの設定ファイル
- password_file /etc/mosquitto/password.txt
- サービスに登録 サービス起動
- $ sudo systemctl start mosquitto
- $ sudo systemctl status mosquitto
- $ sudo systemctl enable mosquitto
- mosquitto-clientsで起動状況の確認
- mosquitto_pubで送信する
- $ mosquitto_pub -d -t test/topic -m "Hello world!"
- mosquitto_subで受信する
Hello world!が受信できる- $ mosquitto_sub -d -t test/topic
SSLの証明書作成方法はMosquittoを用いてMQTT+SSL/TLS通信を試してみるを参照。
パスワードの設定方法は、Mosquitto で Username と Password を使うを参照。
起動状態確認 (起動失敗している場合は、confファイルを見直し)
自動起動に設定
トピック名の一番元に/(スラッシュ)は不要。
-d はデーモンで起動。
まとめ
- Ubuntuのmosquitto導入方法について調査、記載した
参考文献
変更履歴
- 2023/03/21: 新規作成