ラズパイでオムロンさんの環境センサのデータを取得してデータベースに収めるまでをまとめていきます。
はじめに
オムロンさんからは、いくつかの環境センサが発売されていますが、今回はラズパイに簡単に接続してという事で、USB(シリアル)経由でデータが取得できる2JCIE-BUを使ってみました。
搭載センサー |
---|
温度 |
湿度 |
照度 |
UV |
気圧 |
騒音 |
加速度 |
VOC |
![]() |
---|
アマゾンさんでは、だいたい12,000円ちょっとで販売されています。
2JCIE-BUを認識させる。
アットマークテクノさんのWebページを参考にして作業を進めていきました。
まず2JCIE-BUをラズパイに接続します。
![]() |
---|
2JCIE-BUのベンダーIDとプロダクトIDの確認
lsusbコマンドを使って一覧でUSB情報を表示しています。
バス、デバイス、ベンダーID、プロダクトID、ベンダー名の順で表示されます。
今回は、オムロンさんのセンサーなので、
ベンダーID | プロダクトID |
---|---|
0590 | 00d4 |
となります。
pi@raspberrypi:~ $ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 0590:00d4 Omron Corp.
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
FTDIのドライバと2JCIE-BUを関連付ける
pi@raspberrypi:~ $ sudo modprobe ftdi_sio
pi@raspberrypi:~ $ sudo sh -c "echo 0590 00d4 > /sys/bus/usb-serial/drivers/ftdi_sio/new_id"
確認
一番最後の行をみるとFTDI USB Serial Device converterがttyUSB0に割り当てられているのがわかります。
pi@raspberrypi:~ $ dmesg | grep USB
[ 1.357697] xhci_hcd 0000:01:00.0: new USB bus registered, assigned bus number 1
[ 1.362396] usb usb1: New USB device found, idVendor=1d6b, idProduct=0002, bcdDevice= 5.10
[ 1.362416] usb usb1: New USB device strings: Mfr=3, Product=2, SerialNumber=1
[ 1.363183] hub 1-0:1.0: USB hub found
[ 1.363978] xhci_hcd 0000:01:00.0: new USB bus registered, assigned bus number 2
[ 1.364003] xhci_hcd 0000:01:00.0: Host supports USB 3.0 SuperSpeed
[ 1.364605] usb usb2: New USB device found, idVendor=1d6b, idProduct=0003, bcdDevice= 5.10
[ 1.364624] usb usb2: New USB device strings: Mfr=3, Product=2, SerialNumber=1
[ 1.365395] hub 2-0:1.0: USB hub found
[ 1.379293] usbhid: USB HID core driver
[ 1.644465] usb 1-1: new high-speed USB device number 2 using xhci_hcd
[ 1.847238] usb 1-1: New USB device found, idVendor=2109, idProduct=3431, bcdDevice= 4.21
[ 1.847260] usb 1-1: New USB device strings: Mfr=0, Product=1, SerialNumber=0
[ 1.847277] usb 1-1: Product: USB2.0 Hub
[ 1.849627] hub 1-1:1.0: USB hub found
[ 321.036130] usb 1-1.4: new full-speed USB device number 3 using xhci_hcd
[ 321.182617] usb 1-1.4: New USB device found, idVendor=0590, idProduct=00d4, bcdDevice=10.00
[ 321.182639] usb 1-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 414.197645] usbserial: USB Serial support registered for generic
[ 414.207674] usbserial: USB Serial support registered for FTDI USB Serial Device
[ 439.635256] ftdi_sio 1-1.4:1.0: FTDI USB Serial Device converter detected
[ 439.639856] usb 1-1.4: FTDI USB Serial Device converter now attached to ttyUSB0
2JCIE-BUを自動認識するように設定
起動時や2JCIE-BUを挿したときに毎回コマンドを実行して認識させるのも面倒なので、/etc/udev/rules.d/に新しいルールファイルを作って自動認識させるようにします。
pi@raspberrypi:~ $ sudo vi /etc/udev/rules.d/80-2jcie-bu01-usb.rules
ACTION=="add", \
KERNEL=="ttyUSB0", ATTRS{idVendor}=="0590", ATTRS{idProduct}=="00d4", \
RUN+="/sbin/modprobe ftdi_sio", RUN+="/bin/sh -c 'echo 0590 00d4 > /sys/bus/usb-serial/drivers/ftdi_sio/new_id'"
キー | 意味 |
---|---|
ACTION | イベントの動作を指定("add" ならデバイスが接続された時) |
KERNEL | イベントデバイス名を指定 |
ATTRS{idVendor} | ベンダーIDを指定 |
ATTRS{idProduct} | プロダクトIDを指定 |
RUN | 実行するプログラムを指定 |
udevに新しいルールを適用します。
pi@raspberrypi:~ $ sudo udevadm control --reload
ルールの適用がうまく行くと、2JCIE-BUを挿し状態で起動したり、起動後に2JCIE-BUを挿すと自動的にドライバロードされて使える状態になります。
サンプルプログラムを動作させてみる。
ここにPython3で書かれたサンプルプログラムがあるので、実行してみましょう。
pi@raspberrypi:~ $ mkdir omron_sensor
pi@raspberrypi:~ $ cd omron_sensor/
pi@raspberrypi:~/omron_sensor $ git clone https://github.com/omron-devhub/2jciebu-usb-raspberrypi.git
Cloning into '2jciebu-usb-raspberrypi'...
remote: Enumerating objects: 24, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 24 (delta 0), reused 0 (delta 0), pack-reused 21
Unpacking objects: 100% (24/24), done.
pi@raspberrypi:~/omron_sensor $ cd 2jciebu-usb-raspberrypi/
pi@raspberrypi:~/omron_sensor/2jciebu-usb-raspberrypi $ ls
LICENSE.md README.md README_ja.md console_demo.png sample_2jciebu.py
pi@raspberrypi:~/omron_sensor/2jciebu-usb-raspberrypi $ sudo python3 sample_2jciebu.py
Time measured:2021/10/09 19:57:46
Temperature:24.72
Relative humidity:66.72
Ambient light:173
Barometric pressure:1022.985
Sound noise:79.24
eTVOC:30
eCO2:600
Discomfort index:73.11
Heat stroke:23.11
Vibration information:0
SI value:0.0
PGA:0.0
Seismic intensity:0.0
Temperature flag:0
Relative humidity flag:0
Ambient light flag:0
Barometric pressure flag:0
Sound noise flag:0
eTVOC flag:0
eCO2 flag:0
Discomfort index flag:0
Heat stroke flag:0
SI value flag:0
PGA flag:0
Seismic intensity flag:0
Time measured:2021/10/09 19:57:47
Temperature:24.73
Relative humidity:66.69
Ambient light:357
Barometric pressure:1022.989
Sound noise:61.55
eTVOC:32
eCO2:612
Discomfort index:73.12
Heat stroke:23.12
Vibration information:0
SI value:0.0
PGA:0.0
Seismic intensity:0.0
Temperature flag:0
Relative humidity flag:0
Ambient light flag:0
Barometric pressure flag:0
Sound noise flag:0
eTVOC flag:0
eCO2 flag:0
Discomfort index flag:0
Heat stroke flag:0
SI value flag:0
PGA flag:0
Seismic intensity flag:0
センサーにライトを当てて明るくしたり、手で覆って暗くしたりしたりすると“Ambient light”の値が変わりますし、近くで大きな音を立てると“Sound noise”の値が大きくなったりするので、センサーは正常に動作しているようです。