以前のリビジョンの文書です
このサンプルは、下記により構成されています。
監視したい DI のリストを定義した設定ファイルを作成します。
[MQTT] HOST = localhost PORT = 51883 TOPIC = DI [DI] DI_0 = Raw DI_1 = Raw DI_2 = Raw DI_3 = Filtered DI_4 = Raw DI_5 = Raw DI_6 = Raw DI_7 = Raw DI_8 = Raw DI_9 = Raw DI_10 = Raw DI_11 = Raw
上記のような設定ファイルを指定して、コマンドを実行します。
root@metis:/tmp# ./di_mqtt -c di_mqtt.conf
DI を監視するコマンドと同じ設定ファイルを指定して、コマンドを実行します。
root@metis:/tmp# ./mqtt_soracom -c di_mqtt.conf
MQTT で publish されるデータを確認することもできます。
user1@metis:~$ nmqtt_sub -p 51883 -t DI
2つのコマンドを実行している状態で DI の状態を変化させると、MQTT Broker に DI の状態が publish され、それを MQTT Broker 経由で受け取ったプログラムが SORACOM Harvest Data にアップロードします。
MQTT 確認用で subscribe しているプログラムの出力
user1@metis:~$ nmqtt_sub -p 51883 -t DI DI: {"timestamp":1687424585957,"state":{"DI_10":0,"DI_3":0,"DI_8":0,"DI_0":0,"DI_5":0,"DI_4":0,"DI_6":0,"DI_9":0,"DI_11":0,"DI_7":0,"DI_1":0,"DI_2":0}}
syslog に SORACOM Harvest Data へアップロードした、というログも記録されます。
Jun 22 18:03:06 metis mqtt_soracom[8021]: upload: timestamp: 1687424585957 Jun 22 18:03:06 metis mqtt_soracom[8021]: upload: payload: {"DI_10":0,"DI_3":0,"DI_8":0,"DI_0":0,"DI_5":0,"DI_4":0,"DI_6":0,"DI_9":0,"DI_11":0,"DI_7":0,"DI_1":0,"DI_2":0} Jun 22 18:03:06 metis mqtt_soracom[8021]: upload: response code: 201 Created
※ “timestamp” は、SORACOM Harvest Data の timestamp に設定するため、データから抜き出して HTTP Header “X-Soracom-timestamp” にセットするようになっています。
サンプルアプリのコードです。
File | Stat | SHA1SUM | Info |
---|---|---|---|
di_mqtt.tar.xz | 2023/06/22 19:23 8.5 KB | f6d915c19bfcac942885c36ebe635e8cb455ae04 | DI to MQTT/MQTT to SORACOM Harvest |
nim_gpiod.tar.xz | 2023/06/22 19:23 3.1 KB | 97eb2b65b748f4bd049a9a7e60ff157a88aa5127 | libgpiod wrapper library |
アプリケーションのビルド方法です。
ファームウェアの作成 でも必要なのでインストールしておきます。
Nim 言語 で記述しているので、Nim Compiler をインストール します。
user1@max3xx-devel:~$ curl https://nim-lang.org/choosenim/init.sh -sSf | sh choosenim-init: Downloading choosenim-0.8.4_linux_amd64 Prompt: Can choosenim record and send anonymised telemetry data? [y/n] ... Anonymous aggregate user analytics allow us to prioritise ... fixes and features based on how, where and when people use Nim. ... For more details see: https://goo.gl/NzUEPf. Answer: n Downloading Nim 1.6.12 from nim-lang.org [##################################################] 100.0% 0kb/s Extracting nim-1.6.12-linux_x64.tar.xz Extracting nim-1.6.12-linux_x64.tar Building Nim 1.6.12 Compiler: Already built Installed component 'nim' Installed component 'nimble' Installed component 'nimgrep' Installed component 'nimpretty' Installed component 'nimsuggest' Installed component 'testament' Installed component 'nim-gdb' Switched to Nim 1.6.12 choosenim-init: ChooseNim installed in /home/user1/.nimble/bin choosenim-init: You must now ensure that the Nimble bin dir is in your PATH. choosenim-init: Place the following line in the ~/.profile or ~/.bashrc file. choosenim-init: export PATH=/home/user1/.nimble/bin:$PATH user1@max3xx-devel:~$
最後の部分で指示されているように、PATH を設定しておきます。
user1@max3xx-devel:~$ nano -w .bashrc
※ 最後の行に追加しておきます。
実行できるように .bashrc を読み直し、実行の確認をしてみます。
user1@max3xx-devel:~$ source .bashrc user1@max3xx-devel:~$ nim -v Nim Compiler Version 1.6.12 [Linux: amd64] Compiled at 2023-03-10 Copyright (c) 2006-2023 by Andreas Rumpf git hash: 1aa9273640c0c51486cf3a7b67282fe58f360e91 active boot switches: -d:release user1@max3xx-devel:~$
きちんとインストールされて実行できることが確認できました。
Nim のパッケージリストを更新しておきます。
user1@max3xx-devel:~$ nimble refresh Downloading Official package list Success Package list downloaded. user1@max3xx-devel:~$
apt で追加しておきます。
user1@max3xx-devel:~$ sudo apt install libgpiod-dev [sudo] password for user1: Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: libgpiod2 Suggested packages: libgpiod-doc The following NEW packages will be installed: libgpiod-dev libgpiod2 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded. Need to get 103 kB of archives. After this operation, 492 kB of additional disk space will be used. Do you want to continue? [Y/n] Get:1 http://archive.ubuntu.com/ubuntu jammy/universe amd64 libgpiod2 amd64 1.6.3-1build1 [42.2 kB] Get:2 http://archive.ubuntu.com/ubuntu jammy/universe amd64 libgpiod-dev amd64 1.6.3-1build1 [61.1 kB] Fetched 103 kB in 2s (52.0 kB/s) Selecting previously unselected package libgpiod2:amd64. (Reading database ... 33935 files and directories currently installed.) Preparing to unpack .../libgpiod2_1.6.3-1build1_amd64.deb ... Unpacking libgpiod2:amd64 (1.6.3-1build1) ... Selecting previously unselected package libgpiod-dev:amd64. Preparing to unpack .../libgpiod-dev_1.6.3-1build1_amd64.deb ... Unpacking libgpiod-dev:amd64 (1.6.3-1build1) ... Setting up libgpiod2:amd64 (1.6.3-1build1) ... Setting up libgpiod-dev:amd64 (1.6.3-1build1) ... Processing triggers for libc-bin (2.35-0ubuntu3.1) ... user1@max3xx-devel:~$
適当なディレクトリ以下に、2 つのソースのアーカイブを展開します。
user1@max3xx-devel:~$ cd src/ user1@max3xx-devel:~/src$
user1@max3xx-devel:~/src$ tar axf ../nim_gpiod.tar.xz user1@max3xx-devel:~/src$ tar axf ../di_mqtt.tar.xz user1@max3xx-devel:~/src$ ls -l total 12 drwxrwxr-x 3 user1 user1 4096 Jun 22 19:22 di_mqtt <---- drwxrwxr-x 27 user1 user1 4096 Apr 13 13:50 linux-kernel drwxrwxr-x 3 user1 user1 4096 Jun 22 19:22 nim_gpiod <---- user1@max3xx-devel:~/src$