2017年10月6日金曜日

Amazonで売ってる最安値のUSB温度計「TEMPer」をRaspberry Piで試してみた

AmazonでUSB温度計を検索すると、最安値で出てくる以下のような商品がある。



いろんな商品名で販売されているが、中身はTEMPerという製品のようだ。今回、このTEMPerを試すことができる機会があったので、実際に温度計測を実施してみることにした。


Amazonで売っているこれらの製品には、Windows用の温度計測ツールは用意されているようだが、Linux用は用意されていない。しかし、GitHubには様々な計測用のスクリプトが存在しているようなので、そちらを利用することにする。

当初はESXiのUSBポートに刺してパススルーして、Linuxから使うことを想定していたが、このUSB温度計はパススルーできない仕様のようで、うまく仮想マシンに認識させることができなかった。そこで、Raspberry PiのUSBポートにTEMPerを接続して、温度計測を実施してみることにした。

TEMPerを接続する

とりあえず、TEMPerをRaspberry PiのUSBポートに刺してみる。

lsusbで確認すると、下線部のデバイスが増えていることが確認できる。これがTEMPerとなる。

$ lsusb
------------------------------
Bus 001 Device 004: ID 0c45:7401 Microdia
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
------------------------------

スクリプトをインストールする

今回利用するスクリプトはこちら。

・GitHub - padelt/temper-python
https://github.com/padelt/temper-python

まずは、git cloneでスクリプトを入手する。

$ git clone https://github.com/padelt/temper-python.git
------------------------------
Cloning into 'temper-python'...
remote: Counting objects: 603, done.
remote: Total 603 (delta 0), reused 0 (delta 0), pack-reused 603
Receiving objects: 100% (603/603), 138.40 KiB | 263.00 KiB/s, done.
Resolving deltas: 100% (364/364), done.
Checking connectivity... done.
------------------------------

入手したファイルは以下の通り。setup.pyがインストールするためのPythonスクリプトとなる。

$ cd temper-python/
$ ls -l
------------------------------
-rw-r--r-- 1 pi pi  1232  9月 23 15:44 CHANGELOG.md
-rw-r--r-- 1 pi pi  5443  9月 23 15:44 DEVELOPMENT.md
-rw-r--r-- 1 pi pi 32612  9月 23 15:44 LICENSE.md
-rw-r--r-- 1 pi pi    18  9月 23 15:44 MANIFEST.in
-rw-r--r-- 1 pi pi 14903  9月 23 15:44 README.md
drwxr-xr-x 2 pi pi  4096  9月 23 15:44 etc
-rw-r--r-- 1 pi pi   886  9月 23 15:44 setup.py
drwxr-xr-x 2 pi pi  4096  9月 23 15:44 temperusb
------------------------------

前提となるライブラリをインストールする。なお、snmpdは外部からSNMPを使って温度取得するために必要となるもので、スクリプトのみで温度を取得するだけであればインストール不要となる。

$ sudo apt-get install python-usb python-setuptools snmpd
------------------------------
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
python-setuptools はすでに最新版です。
python-setuptools は手動でインストールしたと設定されました。
以下の追加パッケージがインストールされます:
  libperl5.20 libsensors4 libsnmp-base libsnmp30 perl perl-base perl-modules
提案パッケージ:
  lm-sensors snmp-mibs-downloader perl-doc libterm-readline-gnu-perl
  libterm-readline-perl-perl libb-lint-perl libcpanplus-dist-build-perl
  libcpanplus-perl libfile-checktree-perl libobject-accessor-perl snmptrapd
以下のパッケージが新たにインストールされます:
  libperl5.20 libsensors4 libsnmp-base libsnmp30 python-usb snmpd
以下のパッケージはアップグレードされます:
  perl perl-base perl-modules
アップグレード: 3 個、新規インストール: 6 個、削除: 0 個、保留: 7 個。
9,416 kB 中 5,709 kB のアーカイブを取得する必要があります。
この操作後に追加で 6,785 kB のディスク容量が消費されます。
続行しますか? [Y/n] y

~(以下略)~
------------------------------

次に、setup.pyを実行してスクリプトをインストールするのだが、以下の通りエラーで失敗してしまった。

$ sudo python setup.py install
------------------------------
Traceback (most recent call last):
  File "setup.py", line 10, in <module>
    long_description=open('README.md', encoding='utf-8').read(),
TypeError: 'encoding' is an invalid keyword argument for this function
pi@t3015rasp:~/temper-python $ file -i README.md
README.md: text/plain; charset=utf-8
------------------------------

どうやら、pythonのバージョンの問題のように思われる。Raspbianでは、PythonコマンドはPython2.7のシンボリックリンクになっているので、Python3コマンド(Python3.4コマンドのシンボリックリンク)で試してみると、問題なくインストールに成功した。

$ sudo python3 setup.py install
------------------------------
running install
Checking .pth file support in /usr/local/lib/python3.4/dist-packages/
/usr/bin/python3 -E -c pass
TEST PASSED: /usr/local/lib/python3.4/dist-packages/ appears to support .pth files
running bdist_egg
running egg_info
creating temperusb.egg-info
writing requirements to temperusb.egg-info/requires.txt
writing temperusb.egg-info/PKG-INFO
writing dependency_links to temperusb.egg-info/dependency_links.txt
writing entry points to temperusb.egg-info/entry_points.txt
writing top-level names to temperusb.egg-info/top_level.txt
writing manifest file 'temperusb.egg-info/SOURCES.txt'
reading manifest file 'temperusb.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'temperusb.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-armv7l/egg
running install_lib
running build_py
creating build
creating build/lib
creating build/lib/temperusb
copying temperusb/cli.py -> build/lib/temperusb
copying temperusb/snmp.py -> build/lib/temperusb
copying temperusb/__init__.py -> build/lib/temperusb
copying temperusb/temper.py -> build/lib/temperusb
creating build/bdist.linux-armv7l
creating build/bdist.linux-armv7l/egg
creating build/bdist.linux-armv7l/egg/temperusb
copying build/lib/temperusb/cli.py -> build/bdist.linux-armv7l/egg/temperusb
copying build/lib/temperusb/snmp.py -> build/bdist.linux-armv7l/egg/temperusb
copying build/lib/temperusb/__init__.py -> build/bdist.linux-armv7l/egg/temperusb
copying build/lib/temperusb/temper.py -> build/bdist.linux-armv7l/egg/temperusb
byte-compiling build/bdist.linux-armv7l/egg/temperusb/cli.py to cli.cpython-34.pyc
byte-compiling build/bdist.linux-armv7l/egg/temperusb/snmp.py to snmp.cpython-34.pyc
byte-compiling build/bdist.linux-armv7l/egg/temperusb/__init__.py to __init__.cpython-34.pyc
byte-compiling build/bdist.linux-armv7l/egg/temperusb/temper.py to temper.cpython-34.pyc
creating build/bdist.linux-armv7l/egg/EGG-INFO
copying temperusb.egg-info/PKG-INFO -> build/bdist.linux-armv7l/egg/EGG-INFO
copying temperusb.egg-info/SOURCES.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
copying temperusb.egg-info/dependency_links.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
copying temperusb.egg-info/entry_points.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
copying temperusb.egg-info/requires.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
copying temperusb.egg-info/top_level.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/temperusb-1.5.3-py3.4.egg' and adding 'build/bdist.linux-armv7l/egg' to it
removing 'build/bdist.linux-armv7l/egg' (and everything under it)
Processing temperusb-1.5.3-py3.4.egg
Copying temperusb-1.5.3-py3.4.egg to /usr/local/lib/python3.4/dist-packages
Adding temperusb 1.5.3 to easy-install.pth file
Installing temper-poll script to /usr/local/bin
Installing temper-snmp script to /usr/local/bin

Installed /usr/local/lib/python3.4/dist-packages/temperusb-1.5.3-py3.4.egg
Processing dependencies for temperusb==1.5.3
Searching for pyusb>=1.0.0rc1
Reading https://pypi.python.org/simple/pyusb/
Best match: PyUSB 1.0.0
Downloading https://pypi.python.org/packages/8a/19/66fb48a4905e472f5dfeda3a1bafac369fbf6d6fc5cf55b780864962652d/PyUSB-1.0.0.tar.gz#md5=c8a571bfdba778555156af3facaea6fc
Processing PyUSB-1.0.0.tar.gz
Writing /tmp/easy_install-f2wopxqc/pyusb-1.0.0/setup.cfg
Running pyusb-1.0.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-f2wopxqc/pyusb-1.0.0/egg-dist-tmp-3sda3exd
zip_safe flag not set; analyzing archive contents...
Adding pyusb 1.0.0 to easy-install.pth file

Installed /usr/local/lib/python3.4/dist-packages/pyusb-1.0.0-py3.4.egg
Finished processing dependencies for temperusb==1.5.3
------------------------------

温度を計測してみる

それでは、温度取得コマンドを叩いてみる。が、失敗。

$ temper-poll
------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/temperusb-1.5.3-py3.4.egg/temperusb/temper.py", line 95, in __init__
  File "/usr/local/lib/python3.4/dist-packages/temperusb-1.5.3-py3.4.egg/temperusb/temper.py", line 164, in lookup_sensor_count
  File "/usr/local/lib/python3.4/dist-packages/pyusb-1.0.0-py3.4.egg/usb/core.py", line 841, in product
  File "/usr/local/lib/python3.4/dist-packages/pyusb-1.0.0-py3.4.egg/usb/util.py", line 314, in get_string
ValueError: The device has no langid

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/temper-poll", line 9, in <module>
    load_entry_point('temperusb==1.5.3', 'console_scripts', 'temper-poll')()
  File "/usr/local/lib/python3.4/dist-packages/temperusb-1.5.3-py3.4.egg/temperusb/cli.py", line 38, in main
  File "/usr/local/lib/python3.4/dist-packages/temperusb-1.5.3-py3.4.egg/temperusb/temper.py", line 420, in __init__
  File "/usr/local/lib/python3.4/dist-packages/temperusb-1.5.3-py3.4.egg/temperusb/temper.py", line 419, in <listcomp>
  File "/usr/local/lib/python3.4/dist-packages/temperusb-1.5.3-py3.4.egg/temperusb/temper.py", line 97, in __init__
AttributeError: 'ValueError' object has no attribute 'message'
------------------------------

原因はsudoを忘れたことによるものだった。sudoを付けて実行すると、0.5秒ほど間があって温度取得に成功した。

$ sudo temper-poll
------------------------------
Found 1 devices
Device #0: 38.4°C 101.2°F
------------------------------

温度計測の精度について

自宅のRaspberry Piでは、DHT11というセンサーを利用した温度計測も実施している。
※詳細は以下記事を参照

・Raspberry Pi + DHT11 + Zabbixによる温度・湿度監視
https://tech-mmmm.blogspot.jp/2017/08/raspberry-pi-dht11-zabbix.html

TEMPerの計測温度が38.4℃の時のDHT11の温度は32℃となっており、TEMPerの方が計測温度がかなり高い。

これは、Raspberry Pi本体の温度がTEMPer本体に伝わり熱くなってしまうことが原因で、温度が高く計測されてしまっているようだ。したがって、Raspberry Pi本体の熱を伝わらないようにするために、USB延長ケーブルなどで接続する必要がありそうだ。

実際にUSB延長ケーブルでTEMPerを接続すると、以下の通りほぼ同じ温度での計測ができることが確認できた。

・DHT11:33℃
・TEMPer:32.6℃

0 件のコメント:

コメントを投稿

人気の投稿