投稿

1月, 2026の投稿を表示しています

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 にハッシュ値?で保存される(違うユーザーで同じパスワードいれても違う値になるね)。...

MQTTでIoT通信(1)

  MQTTでIoT通信をしようと思う。まずはサーバーになるラズパイ(取りあえずはバーチャルマシンに構築したx64のRaspberry pi OSをインストールしたサーバー)にMQTT Broker(サーバーみたいなもの)をインストールソフトはmosquitto > sudo apt install mosquitto 次にパブリッシャーをインストール > sudo apt install mosquitto-clients ターミナルを2枚開いてAで受信状態にします。 mosquitto_sub -d -t /client/abc Bで送信コマンドを送る mosquitto_pub -d -t /client/abc -m "shee546978" A側で   Subscribed (mid: 1): 0 Client (null) received PUBLISH (d0, q0, r0, m0, '/client/abc', ... (10 bytes)) shee546978  と受信される。 取りあえず、これで動作確認ができる。 ただし現在はブローカーも、パブリッシャーもサブスクライパーも同じマシン上で動いていることに注意。 参考 https://qiita.com/sheep29/items/637f9f00e35cc707f681 https://elchika.com/article/ea67de45-ab62-4515-8a63-e9f83f056eed/ https://qiita.com/tsudaryo1715/items/da6092fecaa42f7d9b23