0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ラズパイにMQTT Broker、Windows11にMQTTXをインストールしてみる

Posted at

お仕事でMQTTの話が出てくることが多いので、どんなものか理解するために家のラズパイにMQTTの環境を作ってみました。

Raspberry Pi 5にMQTT Brokerをインストールする

今回はMQTTのパッケージとしてmosquittoを使っていきます。

mosquittoパッケージのインストール

sudo apt install mosquitto mosquitto-clients

mosquitto(MQTT Broker)の有効化

sudo systemctl enable mosquitto

実行結果

Synchronizing state of mosquitto.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable mosquitto

mosquittoが有効になったか確認

sudo systemctl status mosquitto

実行結果

● mosquitto.service - Mosquitto MQTT Broker
     Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; preset: en>
     Active: active (running) since Sun 2025-01-12 19:52:40 JST; 3min 20s ago
       Docs: man:mosquitto.conf(5)
             man:mosquitto(8)
   Main PID: 1927 (mosquitto)
      Tasks: 1 (limit: 9564)
        CPU: 18ms
     CGroup: /system.slice/mosquitto.service
             mq1927 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Jan 12 19:52:40 raspberrypi5 systemd[1]: Starting mosquitto.service - Mosquitto>
Jan 12 19:52:40 raspberrypi5 systemd[1]: Started mosquitto.service - Mosquitto >

ローカルでMQTTのテスト

ラズパイにssh接続

サブスクライバーを起動

mosquitto_sub -v -t test/message

実行画面
image.png

パブリッシャーを起動

Hello Worldを送信してみます。

mosquitto_pub -t test/message -m 'Hello World!'

実行画面
image.png

サブスクライバーの方で受け取れました。

実行画面(サブスクライバー側)
image.png

リモート接続の準備

WindowsにMQTTクライアントのMQTTXをインストールして、ラズパイ上で動作しているMQTT Broker経由で送受信してみます。

ユーザ認証の準備

sudo mosquitto_passwd -c /etc/mosquitto/passwd [ユーザ名]

実行結果

nomura@raspberrypi5:~ $ sudo mosquitto_passwd -c /etc/mosquitto/passwd nomura
Password:
Reenter password:

/etc/mosquittoにpasswdといファイルができます。

nomura@raspberrypi5:~ $ ls /etc/mosquitto/passwd
/etc/mosquitto/passwd

設定ファイルを変更します。

sudo vi /etc/mosquitto/mosquitto.conf

以下の設定を追加します。

mosquitto.conf
listener 1883
allow_anonymous false
password_file /etc/mosquitto/passwd

mosquittoを再起動します。

sudo systemctl restart mosquitto

再起動して状態確認

nomura@raspberrypi5:~ $ sudo systemctl restart mosquitto
nomura@raspberrypi5:~ $ sudo systemctl status mosquitto
● mosquitto.service - Mosquitto MQTT Broker
     Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; preset: enabled)
     Active: active (running) since Mon 2025-01-13 10:22:46 JST; 26s ago
       Docs: man:mosquitto.conf(5)
             man:mosquitto(8)
    Process: 1938 ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto (code=exited, status=0/SUCCESS)
    Process: 1939 ExecStartPre=/bin/chown mosquitto /var/log/mosquitto (code=exited, status=0/SUCCESS)
    Process: 1940 ExecStartPre=/bin/mkdir -m 740 -p /run/mosquitto (code=exited, status=0/SUCCESS)
    Process: 1941 ExecStartPre=/bin/chown mosquitto /run/mosquitto (code=exited, status=0/SUCCESS)
   Main PID: 1942 (mosquitto)
      Tasks: 1 (limit: 9564)
        CPU: 13ms
     CGroup: /system.slice/mosquitto.service
             mq1942 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Jan 13 10:22:46 raspberrypi5 systemd[1]: Starting mosquitto.service - Mosquitto MQTT Broker...
Jan 13 10:22:46 raspberrypi5 systemd[1]: Started mosquitto.service - Mosquitto MQTT Broker.

Windows PCにMQTTXをインストール

設定したユーザ認証が有効になっているか動作確認するためにMQTTXを使ってみます。

今回はWindows PCにインストールしてテストしました。

ダウンロード(Windows版)

ダウンロードページのWindows x86-64用をダウンロードしてインストールしました。
image.png

インストールすると以下のような画面が立ち上がります。
image.png

MQTTXの設定

New Connectionボタンを押下して、新しいコネクションを作成していきます。
image.png

Brokerの名前、Brokerの動作しているラズパイのIPアドレス、上記で設定したユーザ名、パスワードを記載してConnetを押下します。
※Brokerの名前はあとからわかりやすものをつけておくと良いです。
image.png

ラズパイ上のBroker(mosquitto)に接続されると以下のような画面になります。
image.png

データの送信

トピックを設定して送信ボタンを押します。
image.png

データの受信準備

サブスクライバーの設定をします。
送信のところで設定したトピックと同じものを設定します。
image.png

登録されました。
image.png

再送信してみます。

image.png

Receivedタブにすると、受信したことが確認できます。
image.png

ALLタブにすると両方の状態が見られます。
ほぼチャットツールの画面だな。w
image.png

Raspberry PiにmosquittoをインストールしてMQTT Brokerを動作させるところまでは確認できたので、次はM5Stack Core2やM5StickC Plus2などを使ってIoTっぽいことをしていこうかと考えております。

今回参考にしたサイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?