Deprecated: Return type of Requests_Cookie_Jar::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Cookie/Jar.php on line 63

Deprecated: Return type of Requests_Cookie_Jar::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Cookie/Jar.php on line 73

Deprecated: Return type of Requests_Cookie_Jar::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Cookie/Jar.php on line 89

Deprecated: Return type of Requests_Cookie_Jar::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Cookie/Jar.php on line 102

Deprecated: Return type of Requests_Cookie_Jar::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Cookie/Jar.php on line 111

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 40

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 51

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 68

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 82

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 91

Deprecated: Return type of AIOSEO\Plugin\Common\Models\Model::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Models/Model.php on line 409

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Utils/Database.php on line 535

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Utils/Database.php on line 535

Deprecated: json_decode(): Passing null to parameter #1 ($json) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Models/Model.php on line 174

Deprecated: json_decode(): Passing null to parameter #1 ($json) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Models/Model.php on line 174

Deprecated: json_decode(): Passing null to parameter #1 ($json) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Models/Model.php on line 174
CentOS7上でUSB温度計で温度を記録してgrafanaでグラフ化する | reyfox blog

reyfox blog

reyfox tech info

CentOS grafana influxdb Linux

CentOS7上でUSB温度計で温度を記録してgrafanaでグラフ化する

投稿日:2019年1月27日 更新日:

AmazonでUSBで温度(室温)が図れるデバイスが売っていたのでこれをCentOS7で動いているNUCにつなげて温度をグラフ化してみたいと思います。

やりたいこと

USBで温度を計測、その値をinfluxDBに保存しその内容をgrafanaでグラフ化します。
USBをそのままNUCに差し込むとNUCの温度を拾う可能性があるので、USB延長ケーブルによってちょっと離したところに設置してます。

事前準備

grafana と influxDB を使用可能にしておく必要があります。
設定方法は当サイトの記事: grafana+prometheusでサーバ監視をしグラフィカルなダッシュボードを作る を参考にしてください。

デバイスID、プロダクトIDの確認

USBに差し込むと dmesgもしくは lsusb コマンドでデバイスIDとプロダクトIDを確認することができます。

lsusb が入ってない場合は、yum install usbutils によりインストールが可能です。

lsusb
Bus 002 Device 002: ID 04bb:014a I-O Data Device, Inc.
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 8087:0aaa Intel Corp.
Bus 001 Device 003: ID 413d:2107
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

413d:2107が該当のIDとなります。

昔のモデルは別なIDが振られていましたが今Amazonで買えるものは、[413d:2107]のようです。
昔のものは temper というユーティリティで簡単に取れましたが今回はちょっと難しかったのでGoogleで解決方法を検索しました。

hidapiのインストール

USB経由でアクセスするためhidapiというものを使用します。
依存関係でlibudevなどが必要になるので下記コマンドでインストールしておきます。


yum install libudev-devel libusbx-devel cmake

hidapiのクローン


mkdir ~/src
cd ~/src
git clone https://github.com/signal11/hidapi
cd hidapi/linux
make -f Makefile-manual

makeに失敗する場合は、developer tools が入っていないので yum でインストールしてください。

当サイトの参考記事: Intel NUC BOXNUC7PJYHのUnix Bench

必要なファイル(shared object)を/usr/local/lib へコピーしシンボリックリンクを作成します。


cp libhidapi-hidraw.so /usr/local/lib/
cd /usr/local/lib
ln -s libhidapi-hidraw.so libhidapi-hidraw.so.0

TEMPered のインストール

temper では正しく温度が取れなかったので、TEMPered というものを使います。
git で管理されているソースですが途中から Mac 用に変わっているらしくLinuxで扱えるところまで巻き戻して使用します。


cd ~/src/
git clone https://github.com/hughesr/TEMPered
cd TEMPered
git checkout hack-413d-2107
git reset --hard 75aa1e2
cmake .
make
make install

次に、TEMPered で使用するshared object を /usr/local/lib に設置してシンボリックリンクを作成します。


cd ~/src/TEMPered/libtempered
cp libtempered.so /usr/local/lib/
cd ..
cd libtempered-util/
cp libtempered-util.so /usr/local/lib/
cd /usr/local/lib
ln -s libtempered.so libtempered.so.0
ln -s libtempered-util.so libtempered-util.so.0

ldconfig の調整

/usr/local/lib に必要な shared object をコピーしましたが、ldconfig を実行した際に読み込みに行ってくれないので /etc/ld.so.conf を以下のように書き換えます。


include ld.so.conf.d/*.conf
/usr/local/lib

このあと ldconfig を実行し shared object を読み込み直します。


ldconfig

TEMPered の実行


tempered
/dev/hidraw1 0: temperature 17.18 °C
/dev/hidraw1 1: Failed to get the temperature: Not enough data was read from the sensor.
/dev/hidraw1 1: no sensor data available

/usr/local/bin/tempered が make install によってインストールされていますので、実行します。
17.18℃という結果が取れました。
hidraw1 1: の方は別なセンサーが搭載されているモデルで使うようなので今回はスルーします。

TEMPered 実行時のエラー


tempered: error while loading shared libraries: libtempered.so.0: cannot open shared object file: No such file or directory

上記エラーが出た場合、shared object が正しく設置されていない、ldconfig が行われていない、シンボリックリンクが正しくはられていないの原因が考えられます。


Failed to enumerate devices: No HID devices were found.

上記エラーが出た場合、USBの温度計が正しく認識されていない場合があります。管理人もこのエラーに遭遇しましたが、一度USB温度計を抜いて、さし直ししたところ正しく動作するようになりました。

温度をinfluxDBへ記録する

正しく温度が取れる状態になればあとはデータを加工して蓄積するだけです。

下記のシェルを作成します。(例: grafana_get_temp.sh)


#!/bin/sh
TEMPER=`/usr/local/bin/tempered | head -n 1 | awk '{ print $4 }'`

# curl で influxDB に送る
curl -i -XPOST 'http://localhost:8086/write?db=grafana&precision=s' --data-binary "temp,host=temp.reyfox.local,region=jp value=$TEMPER"

tempered で得られた結果の最初の1行を取得し、awk コマンドで温度だけ抜き出してます。

host=の部分は環境によって置き換えてください。

cronに登録する

crontab -e でcronを以下のように設定します。(今回は root 権限で実行)


* * * * * sh /root/grafana_get_temp.sh >/dev/null 2>&1

毎分温度を取得し、influxDBへ書き込みます。

grafana でグラフ化する

grafanaにログインし、Add panel から Graph を選びます。

Data Source から influxDB を選択します。

Aのクエリを

default temp where host = temp.reyfox.local

とします。(host = は先ほど設定したシェルの内容を調整してください)

Group by の time() の部分に1分を表す 1m を設定します。

以上の手順で以下のようなグラフが作成できます。
温度のデータは毎分取るのでしばらく経ってからではないとグラフは出ません。

Add panel の Singlestat を使うと上記画像の右側のようなゲージのグラフも作成できます。

-CentOS, grafana, influxdb, Linux

執筆者:

関連記事

CentOS7にMySQLサーバ(mariaDB)をインストールする

CentOS7からMySQLにかわりmariaDBが採用されていますが、使い方はほぼ MySQLと同じでいけます。 インストール いつもの通り、yumで。 yum install mariadb-se …

CentOS7で内向き専用DNSサーバを立てる

内向きDNSとは、LAN内でだけドメイン名を解決できるDNSサーバを言います。今回は、LAN内だけでドメイン名を解決するDNSサーバを立て、デフォルトのDNSとして動作するように設定します。なお、内向 …

CentOS7にApacheをインストール

CentOS7はnetworkインストールで最小構成だと httpd(Apache)が入りません。しかし、yumコマンドで簡単にインストールすることが出来ます。 インストール yum install …

CentOS7の初期設定

CentOS7 を Intel NUC にインストールしましたが今の所家庭内ネットワークからのみの想定で、外部からのアクセスはすべてルータでフィルタリングしているため、利便性のため下記の設定を行います …

CentOS7にDHCPサーバをインストールしルータのDHCPサーバを停止

現在販売されている市販のルータにはDHCPサーバという家庭内LANのIPを自動で払い出してくれる機能がだいたい標準で装備されています。ただ、製品によってはDHCPで払い出しているマシンに固定IPを割り …


Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Utils/Database.php on line 535

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Utils/Database.php on line 535

Deprecated: json_decode(): Passing null to parameter #1 ($json) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Models/Model.php on line 174

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Utils/Database.php on line 535

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Utils/Database.php on line 535

Deprecated: json_decode(): Passing null to parameter #1 ($json) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Models/Model.php on line 174