将I2C TSL2561(照度传感器)和AE-BME280(温度/湿度/大气压表)与树莓派一起使用


尝试通过SPI获取传感器信息让我很沮丧,所以
听起来更轻松吗?我改用I2C传感器了。

渲染

环境

覆盆子pi零w(raspbian-jessie)
python3
mac

连接传感器,并使用i2cdetect检查地址。

1
2
3
4
5
6
7
8
9
10
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: -- -- -- -- -- -- -- -- -- 39 -- -- -- -- 3e --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- 76 --

I2C连接设备

39:TSL2561(照度传感器)
3e:LCD显示屏(AE-AQM0802)
76:AE-BME-280(温度/湿度/大气计)

可以进行I2C识别

TSL2561 I2C照度传感器

的介绍

我尝试使用AWS,Azure和Raspberry Pi进行IoT(1)

我被允许提及。

通过pip

安装tsl2561

1
2
3
4
5
pi@raspberrypi:~ $ pip install tsl2561
Collecting tsl2561
  Downloading tsl2561-3.3-py2.py3-none-any.whl
Installing collected packages: tsl2561
Successfully installed tsl2561-3.3

参考站点使用了以下内容,因此我将其合并到了程序(I2CSensor.py)中。

tsl2561.py

1
2
3
4
5
from tsl2561 import TSL2561

if __name__ == "__main__":
  tsl = TSL2561(debug=True)
  print(tsl.lux())

尝试运行它。

1
2
3
4
5
pi@raspberrypi:~ $ sudo python pyhome/I2CSensor.py
Traceback (most recent call last):
  File "pyhome/I2CSensor.py", line 4, in <module>
    from tsl2561 import TSL2561
ImportError: No module named tsl2561

有人告诉我没有

模块,因此请使用pip show查找tsl2561的位置。

1
2
3
4
5
6
7
8
9
10
pi@raspberrypi:~ $ pip show tsl2561
Name: tsl2561
Version: 3.3
Summary: Driver for the TSL2561 digital luminosity (light) sensors
Home-page: https://github.com/sim0nx/tsl2561
Author: Georges Toth
Author-email: [email protected]
License: BSD
Location: /home/pi/.pyenv/versions/3.5.1/lib/python3.5/site-packages
Requires:

现在我知道位置了,在程序开始处编写以下内容并加载

pyhome / I2CSensor.py

1
sys.path.append('/home/pi/.pyenv/versions/3.5.1/lib/python3.5/site-packages')

我能够显示它。 *左边是勒克斯数据,右边是检查是否有遗漏的计数器。

1
2
3
4
5
6
7
8
9
10
11
12
pi@raspberrypi:~ $ sudo python pyhome/I2CSensor.py
(422, 1)
(421, 2)
(421, 3)
(420, 4)
(420, 5)
(420, 6)
(420, 7)
(421, 8)
(423, 9)
(421, 10)
(420, 11)

AE-BME280(温度/湿度/大气计)

的介绍

似乎有

bme280.py,所以用wget来获取它。

1
2
3
4
5
6
7
8
9
10
11
pi@raspberrypi:~ $ wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/bme280.py
--2017-10-01 19:19:57--  https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/bme280.py
bitbucket.org (bitbucket.org) をDNSに問いあわせています... 2401:1d80:1010::150, 104.192.143.3, 104.192.143.2, ...
bitbucket.org (bitbucket.org)|2401:1d80:1010::150|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 5057 (4.9K) [text/plain]
`bme280.py' に保存中

bme280.py           100%[=====================>]   4.94K  --.-KB/s 時間 0s

2017-10-01 19:19:58 (11.6 MB/s) - `bme280.py` へ保存完了 [5057/5057]

当我运行它时,我可以看到它而无需进行任何更改!!!

1
2
3
4
5
6
pi@raspberrypi:~ $ sudo python bme280.py
Chip ID     : 96
Version     : 0
Temperature :  25.71 C
Pressure :  1018.94496737 hPa
Humidity :  63.7514647032 %

内部,通过readBME280All()函数的return返回温度,大气压力和湿度,因此似乎bme280可以与主程序位于同一位置,因此可以制成模块。

所以

1
2
3
import bme280

temperature, pressure, humidity = bme280.readBME280All()

如果添加

,则可以分别获取温度,压力和湿度。方便!
因此,基于此,我创建了以下内容。 (复制和粘贴的汇编)

I2CSensor.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/path/to/python
# -*- coding: utf-8 -*-

import sys
sys.path.append('/home/pi/.pyenv/versions/3.5.1/lib/python3.5/site-packages')

import BME280 #airpressure,temp,humidity sensor
import bme280
from tsl2561 import TSL2561 #light sensor
import smbus  # sudo apt-get install python-smbus
import time
CHANNEL = 0
GAIN = 1

bus = smbus.SMBus(1)  # Rev 2 Pi, Pi 2 & Pi 3 uses bus 1


class i2clcd:
    i2c = smbus.SMBus(1)
    addr = 0x3e
    contrast = 30  # 0~63

    def __init__(self):
        self.i2c.write_byte_data(self.addr, 0, 0x38)  # function set(IS=0)
        self.i2c.write_byte_data(self.addr, 0, 0x39)  # function set(IS=1)
        self.i2c.write_byte_data(self.addr, 0, 0x14)  # internal osc
        self.i2c.write_byte_data(self.addr, 0, (0x70 | (self.contrast & 0x0f)))  # contrast
        self.i2c.write_byte_data(self.addr, 0, (0x54 | ((self.contrast >> 4) & 0x03)))  # contrast/icon/power
        self.i2c.write_byte_data(self.addr, 0, 0x6c)  # follower control?
        time.sleep(0.2)

    def clear(self):
        self.i2c.write_byte_data(self.addr, 0, 0x38)  # function set(IS=0)
        self.i2c.write_byte_data(self.addr, 0, 0x0C)  # Display On
        self.i2c.write_byte_data(self.addr, 0, 0x01)  # Clear Display
        self.i2c.write_byte_data(self.addr, 0, 0x06)  # Entry Mode Set
        time.sleep(0.2)

    def puts(self, msg):
        self.i2c.write_byte_data(self.addr, 0, 0x38)  # function set(IS=0)
        [self.i2c.write_byte_data(self.addr, 0x40, ord(c)) for c in msg]

    def setaddress(self, line, col):
        self.i2c.write_byte_data(self.addr, 0, 0x38)  # function set(IS=0)
        self.i2c.write_byte_data(self.addr, 0, 0x80 | (0x40 if line > 0 else 0) | col)

    def setcg(self, no, cg):
        self.i2c.write_byte_data(self.addr, 0, 0x38)  # function set(IS=0)
        self.i2c.write_byte_data(self.addr, 0, 0x40 | (no << 3))
        [self.i2c.write_byte_data(self.addr, 0x40, c) for c in cg]

    def putcg(self, line, col, no):
        self.setaddress(line, col)
        self.i2c.write_byte_data(self.addr, 0x40, no)


if __name__ == "__main__":

    count=0
    errcount = 0

    while True:
        lcd = i2clcd()
        lcd.clear()
        temperature, pressure, humidity = bme280.readBME280All()
        tsl = TSL2561(debug=True)
        light = tsl.lux()
        count += 1

        pressure = int(pressure)
        temperature = round(temperature,1)
        humidity = round(humidity,1)

        try:
            lcd.setaddress(0, 0)
            lcd.puts(str(light).rjust(5)+"Lux") #rjust(5)で5桁の右揃え
            lcd.setaddress(1, 0)
            lcd.puts(str(pressure).rjust(5)+"hPa")

            # lcd.puts("c:"+str(count))

            time.sleep(2)
            lcd.clear()

            lcd.setaddress(0, 0)
            lcd.puts(str(temperature).rjust(5)+"'C")
            lcd.setaddress(1, 0)
            lcd.puts(str(humidity).rjust(5)+"%")

            print("OUT  :L"+str(light),"P"+str(pressure),"T"+str(temperature),"H"+str(humidity))

        except IOError:
            errcount +=1
            lcd.setaddress(1, 4)
            lcd.puts("e")
            lcd.setaddress(1, 5)
            lcd.puts(str(errcount))
            print(light, count,errcount)

        time.sleep(2)

我能够这样显示它。 (显示每2秒更改一次)

之后,使用csv,传感器和LCD程序导出
我已经将其制作成一个模块,但是就目前而言,感觉就像已经创建了基础。

注意(失败的版本)

首先,我参考了以下站点,并按顺序进行了跟踪,但停在了中间。我将其放在这里作为提醒。
在Raspberry pi 2

上使用TSL2561获得python的照度

下载
wget https://github.com/janheise/TSL2561/archive/master.zip

据说没有'quick2wire'。

1
2
3
4
5
pi@raspberrypi:~ $ python TSL2561-master/TSL2561.py
Traceback (most recent call last):
  File "TSL2561-master/TSL2561.py", line 3, in <module>
    import quick2wire.i2c as i2c
ImportError: No module named 'quick2wire'

安装'quick2wire'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pi@raspberrypi:~ $ cd TSL2561-master/
pi@raspberrypi:~/TSL2561-master $ cd ~
pi@raspberrypi:~ $ git clone https://github.com/quick2wire/quick2wire-python-api.git
Cloning into 'quick2wire-python-api'...
remote: Counting objects: 1674, done.
remote: Total 1674 (delta 0), reused 0 (delta 0), pack-reused 1674
Receiving objects: 100% (1674/1674), 3.10 MiB | 678.00 KiB/s, done.
Resolving deltas: 100% (916/916), done.
Checking connectivity... done.
pi@raspberrypi:~ $ cd quick2wire-python-api/
pi@raspberrypi:~/quick2wire-python-api $ 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
.
.
.
.

据说i2c-0不存在,因此使其成为符号链接(我不太了解)

1
2
3
4
5
6
7
pi@raspberrypi:~ $ sudo python3 TSL2561-master/TSL2561.py
Traceback (most recent call last):
  File "TSL2561-master/TSL2561.py", line 300, in <module>
    with i2c.I2CMaster(0) as bus:
  File "/usr/local/lib/python3.4/dist-packages/quick2wire_api-0.0.0.2-py3.4.egg/quick2wire/i2c.py", line 48, in __init__
FileNotFoundError: [Errno 2] No such file or directory: '/dev/i2c-0'
pi@raspberrypi:~ $ sudo ln -s /dev/i2c-1 /dev/i2c-0

↑在链接目标中,到目前为止,它仍然有效,但就我而言,出现以下错误。

OSError: [Errno 95] Operation not supported

1
2
3
4
5
6
pi@raspberrypi:~ $ sudo python3 TSL2561-master/TSL2561.py
Traceback (most recent call last):
  File "TSL2561-master/TSL2561.py", line 324, in <module>
    i2c.reading(address, 2)
  File "/usr/local/lib/python3.4/dist-packages/quick2wire_api-0.0.0.2-py3.4.egg/quick2wire/i2c.py", line 78, in transaction
OSError: [Errno 95] Operation not supported

我放弃了,因为我无法处理此处的错误。