chakokuのブログ(rev4)

テック・コミック・DTM・・・ごくまれにチャリ

Ubuntu(WSL)にMQTT Brokerを入れる、動作テストまで

背景:RPi Picoのセンサデータを手軽に集計してグラフ化したい
取り組み:RPiに液晶モニタをつけてもいいけど、集計の試行錯誤はできたらPC上で行いたいので、収集データをPCに取り込みたい。PC上でMQTT Brokerを走らせて、RPiからはMQTT でpublishする。PC上のMQTT ClientでSubscribeしてデータを受信してグラフ化する。そして集計する。
手順:
MQTT BrokerアプリをUbuntuにインストール手順としては、普通のaptで入るようであった。手順に沿って入れてみる。

# apt-get install mosquitto
# ps aux | grep mos
mosquit+    9941  0.0  0.0  15732  7984 ?        Ss   16:16   0:00 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
# netstat -na | grep LISTEN
tcp        0      0 10.255.255.254:53       0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:1883          0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN
tcp6       0      0 ::1:1883                :::*                    LISTEN

MQTTのTCP接続ポートは1883らしいので、上記でListenがなされていると理解

MQTTで得られた値をグラフ化する方法としていろいろあるのだろうけど、自分はNode-RED + Dashboardぐらいか思い浮かばないので、Ubuntuにnode-red を入れる

sudo npm install -g --unsafe-perm node-red

実行すると、npmも上げろとメッセージが出たので、npmも上げる

npm notice New minor version of npm available! 10.4.0 -> 10.8.3
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.8.3
npm notice Run npm install -g npm@10.8.3 to update!

ユーザ権限でnode-redを起動する

$ node-red
29 Sep 17:11:21 - [info]

Welcome to Node-RED
===================

29 Sep 17:11:21 - [info] Node-RED version: v4.0.3
29 Sep 17:11:21 - [info] Node.js  version: v18.17.1
29 Sep 17:11:21 - [info] Linux 5.15.153.1-microsoft-standard-WSL2 x64 LE
29 Sep 17:11:21 - [info] Loading palette nodes
29 Sep 17:11:22 - [info] Settings file  : /home/sumi/.node-red/settings.js
29 Sep 17:11:22 - [info] Context store  : 'default' [module=memory]
29 Sep 17:11:22 - [info] User directory : /home/sumi/.node-red
29 Sep 17:11:22 - [warn] Projects disabled : editorTheme.projects.enabled=false
29 Sep 17:11:22 - [info] Flows file     : /home/sumi/.node-red/flows.json
29 Sep 17:11:22 - [warn]

---------------------------------------------------------------------
Your flow credentials file is encrypted using a system-generated key.

If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.

You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.
---------------------------------------------------------------------

29 Sep 17:11:22 - [info] Server now running at http://127.0.0.1:1880/
29 Sep 17:11:22 - [info] Starting flows
29 Sep 17:11:22 - [info] Started flows

Node-REDを使ってMQTTをしゃべらせてみる
ローカルPC上でMQTT Brokerが走っているのでlocalhost:1883と指定してみる

Node-REDでPublish/Subscribeさせて、折り返しできるかをテスト、HelloWorldとPublishして、Subscribeできるのを確認

MQTT Brokerのログも一応確認、以下のようにnoderedから接続されました・・とログが出ている

1727597734: New connection from 127.0.0.1:43078 on port 1883.
1727597734: New client connected from 127.0.0.1:43078 as nodered_b4f00373eb656159 (p2, c1, k60).
1727597807: Saving in-memory database to /var/lib/mosquitto//mosquitto.db.

次は、RP2040のMicroPython側からセンサ取得情報をMQTTでPublishさせるとPCまでは届くと・・ 届いたデータをNode-REDのDashboardでグラフ化したら波形が見えるはず。
■追記
当初PC上のUbuntu(WSL)にMQTT Brokerを入れたのだけど、WindowsFireWall等に遮断されて疎通できない状況であった。FireWallの設定を変更して1883ポートを通過させてもいいのだが、いろいろ面倒そうなので、RaspberryPy(多分Raspbian)にmosquittoを入れた。
自ホスト内の接続であればそのままの設定で通信が確立できるが、自ホスト以外の端末から接続を許すには追加設定が必要
例: file: /etc/mosquitto/conf.d/custom.conf

listener 1883
allow_anonymous true

■追記
WSLとWindowsとの接続はPC内でVirtualNetworkで接続されているらしい。だからPC外のClientからWSL上で稼働するサーバや各種Webサービスに接続することはできない。だったらどうするか、、Windows環境でポートフォワーダを走らせて、Windows環境に接続した接続をWSLのネットワークに転送させることで接続可能になるそうだ。
【WSL2】外部からWSL2のサービスに接続する
例えば、WiFi搭載のマイコンでデータ収集した時、MQTTを使ってWSL上のMQTTブローカにPublishできれば、Windows環境でログ収集することが可能になる。前回テストした結果では結構高速にデータ収集が可能であり、マイコンのデータをリアルタイムにPCに取り込むことが可能になると期待できる。
■参考URL
Install Mosquitto MQTT Broker on Ubuntu | Litmus Automation Documentation - Litmus Technical Documentation