※こちらは旧サイトです(新サイトはこちら

RaspberryPI2で照度センサー(TSL2561)をつけて照度を測るまでのメモ

2016-11-01 00:12:30

最近家族のトイレが長くて使えない時が多いのでトイレの明かりが付いているかリモートで確認しようと思って作った時のメモ

用意したもの

はんだ付け一式なかったのでAmazonでぽちった

あと使ってるラズパイは Raspberry PI 2 Model B です

繋ぐ

Fritzingのサイトに繋がらなかったのでそのうちちゃんと書く予定

i2cを有効にする

$ sudo raspi-config

以上が終わったらリブート

$ sudo reboot

その後、lsmodして、以下の2つが見えていれば良いらしいです

$ lsmod | grep i2c
i2c_dev                 5859  0
i2c_bcm2708             4834  0

接続がうまくいってるか見てみる

$ sudo apt-get install -y i2c-tools

接続がうまくいっていれば、i2cdetectで以下のように見えました

$ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- 39 -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Pythonで照度をとる

i2cで必要らしい

$ sudo apt-get install -y python-smbus

以下からコードを拝借

    $ git clone https://github.com/janheise/TSL2561
    $ git clone https://github.com/quick2wire/quick2wire-python-api.git
    $ cd quick2wire-python-api/
    $ sudo python3 setup.py install

実行

$ ./TSL2561/TSL2561.example.py
0a
d7 00
15 08
Full:     0815
Infrared: 00d7
Visible:  073e
0a
Found sensor...
Full luminosity value: 1900817
Full luminosity value: 0x1d0111
IR: 1c
Full: 105
Visible: 0xe6
Visible, calculated: 0xe9
Lux: 3359

と出れば成功 (最終行のLuxが明るさ)

watchコマンド叩いて手をかざしたりしてみたらLuxの値が小さくなるのが確認できました

参考にさせて頂いたサイト