LoginSignup
1
1

More than 5 years have passed since last update.

RaspberryPi3上のLXCコンテナからRaspberryPi3本体のI2Cにアクセスする

Last updated at Posted at 2018-06-12

やりたいこと

RaspberryPi3にLXC/LXDを使ってコンテナ環境を作り、その中のコンテナからRaspberryPi3本体にI2Cで接続された温度・湿度・気圧センサー(BME280)のデータを取得する。

環境

  • Raspberry Pi 3 MODEL B
  • Raspbian 9.4(stretch)
pi@raspberrypi:~ $ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 9.4 (stretch)
Release:        9.4
Codename:       stretch
pi@raspberrypi:~ $

やったこと

RaspberryPi3本体でI2Cにアクセス

RaspberryPi3本体にI2Cのデバイスファイルがあることを確認

pi@raspberrypi:~ $
pi@raspberrypi:~ $ ls -l /dev/i2c-1
crw-rw---- 1 root i2c 89, 1  5月 29 23:22 /dev/i2c-1
pi@raspberrypi:~ $

i2cdetectコマンドやi2cdumpコマンドで値が取れるか確認

pi@raspberrypi:~ $
pi@raspberrypi:~ $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- 76 --
pi@raspberrypi:~ $
pi@raspberrypi:~ $ i2cdump -y 1 0x76
No size specified (using byte-data access)
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
80: 85 50 89 32 82 33 6f 06 f2 6c 53 66 32 00 f1 92    ?P?2?3o??lSf2.??
90: ed d5 d0 0b 2c 23 69 ff f9 ff ac 26 0a d8 bd 10    ????,#i.?.?&????
a0: 00 4b ea 00 00 00 00 00 00 00 00 00 33 00 00 c0    .K?.........3..?
b0: 00 54 00 00 00 00 60 02 00 01 ff ff 1f 71 03 00    .T....`?.?..?q?.
c0: 00 00 27 ff 00 00 00 00 01 00 00 00 00 00 00 00    ..'.....?.......
d0: 60 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    `...............
e0: 00 6f 01 00 13 03 00 1e ad 41 ff ff ff ff ff ff    .o?.??.??A......
f0: ff 00 01 00 27 a0 00 4a fa 00 82 b5 00 75 40 80    ..?.'?.J?.??.u@?
pi@raspberrypi:~ $

RaspberryPi3のデバイスファイルをコンテナに登録する

pi@raspberrypi:~ $
pi@raspberrypi:~ $ lxc config device add container1 i2c-1 unix-char source=/dev/i2c-1 path=/dev/i2c-1
Device i2c-1 added to container1
pi@raspberrypi:~ $
pi@raspberrypi:~ $ lxc config device show hty-db01
i2c-1:
  path: /dev/i2c-1
  source: /dev/i2c-1
  type: unix-char
pi@raspberrypi:~ $

コンテナ側で確認

コンテナ側で /dev/i2c-1 が生成されているのを確認

pi@raspberrypi:~ $
pi@raspberrypi:~ $ lxc exec container1 /bin/bash
root@container1:~#
root@container1:~# ls -l /dev/i2c-1
crw-rw---- 1 root i2c 89, 1  6月 13 00:28 /dev/i2c-1
root@container1:~#

i2cdetectコマンドやi2cdumpコマンドで値が取れるか確認

root@container1:~#
root@container1:~# i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- 76 --
root@container1:~#
root@container1:~# i2cdump -y 1 0x76
No size specified (using byte-data access)
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
80: 85 50 89 32 82 33 6f 06 f2 6c 53 66 32 00 f1 92    ?P?2?3o??lSf2.??
90: ed d5 d0 0b 2c 23 69 ff f9 ff ac 26 0a d8 bd 10    ????,#i.?.?&????
a0: 00 4b ea 00 00 00 00 00 00 00 00 00 33 00 00 c0    .K?.........3..?
b0: 00 54 00 00 00 00 60 02 00 01 ff ff 1f 71 03 00    .T....`?.?..?q?.
c0: 00 00 27 ff 00 00 00 00 01 00 00 00 00 00 00 00    ..'.....?.......
d0: 60 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    `...............
e0: 00 6f 01 00 13 03 00 1e ad 41 ff ff ff ff ff ff    .o?.??.??A......
f0: ff 00 01 04 27 a0 00 4a f9 00 82 b1 00 75 39 80    ..??'?.J?.??.u9?
root@container1:~#
root@container1:~# exit
pi@raspberrypi:~ $

コンテナ側でもI2Cデバイスの値が取れることが確認できたので、あとは第39回「ラズベリーパイで温度・湿度・気圧をまとめて取得!AE-BME280でIC2通信」を参考にPythonスクリプトで値を取ることができる。

感想

LXCコンテナからRaspberryPi3本体のデバイスファイルにアクセスできるなんで知りませんでした・・

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1