Raspberry Pi 读取 BMP085 温度和压力
另一个有趣的小型 i2c 设备通过 eBay 廉价获得。它读取气压和温度。有一些相对简单的数学运算可以将输出转换为可读的内容。
ADAFruit 再次提供了一个很棒的指南和库,用于在其将 BMP085 与 Raspberry Pi 一起使用中使用该设备。和之前的帖子一样,我可以直接使用 ADAFruit 库。虽然我不觉得有必要重新发明轮子和从头开始重新编码,但我确实想了解它是如何工作的。
我从其他几个来源获取信息:
- Maarten Damen 的 Bus Pirate 读取 BMP085 温度(撰写本文时站点已关闭)
- John Burn 在使用 Raspberry Pi 从博世 BMP085 读取数据的 C 代码的 Python 端口。
下面是将设备连接到 Pi 的非常简单的示意图。
导入
smbus
导入
时间
class
BMP085():
OSS
=
3
def
__init__(
自我
, i2c, address):
自我
.i2c
=
i2c
自我
.address
=
地址
自我
.ac1
=
自我
.readSignedWord(
0xaa
)
自我
.ac2
=
自我
.readSignedWord(
0xac
)
自我
.ac3
=
自我
.readSignedWord(
0xae
)
自我
.ac4
=
自我
.readWord(
0xb0
)
自我
.ac5
=
自我
.readWord(
0xb2
)
自我
.ac6
=
自我
.readWord(
0xb4
)
自我
.b1
=
自我
.readSignedWord(
0xb6
)
自我
.b2
=
自我
.readSignedWord(
0xb8
)
自我
.mb
=
自我
.readSignedWord(
0xba
)
自我
.mc
=
自我
.readSignedWord(
0xbc
)
自我
.md
=
自我
.readSignedWord(
0xbe
)
def
readWord(
自我
, reg):
msb
=
自我
.i2c.read_byte_data(
自我
.address, reg)
lsb
=
自我
.i2c.read_byte_data(
自我
.address, reg
+
1
)
值
=
(msb <<
8
)
+
lsb
返回
值
def
readSignedWord(
自我
, reg):msb
=
自我
.i2c.read_byte_data(
自我
.address, reg) lsb
=
自我
.i2c.read_byte_data(
自我
.address, reg
+
1
)
if
(msb>
127
):
msb
=
msb
-
256
值
=
(msb <<
8
)
+
lsb
返回
值
def
readUT(
自我
):
自我
.i2c.write_byte_data(
自我
.address,
0xf4
,
0x2e
) time.sleep(
0.0045
) ut
=
自我
.readWord(
0xf6
)
返回
ut
def
readTemperature(
自我
):ut
=
自我
.readUT() x1
=
((ut
-
自我
.ac6)
*
自我
.ac5)>>
15
x2
=
(
自我
.mc <<
11
)
/
(x1
+
自我
.md)
自我
.b5
=
x1
+
x2
返回
((
自我
.b5
+
8
)>>
4
)
/
10.0
def
readUP(
自我
):
自我
.i2c.write_byte_data(
自我
.address,
0xf4
,
0x34
+
(
自我
.OSS <<
6
))
延迟
=
(
2
+
(
3
<<
自我
.OSS))
/
1000.0
time.sleep(delay)
msb
=
自我
.i2c.read_byte_data(
自我
.address,
0xf6
)
lsb
=
自我
.i2c.read_byte_data(
自我
.address,
0xf7
)
xlsb
=
自我
.i2c.read_byte_data(
自我
.address,
0xf8
)
向上
=
(msb <<
16
)
+
(lsb <<
8
)
+
xlsb
=
向上>> (
8
-
自我
.OSS)
返回
向上
def
readPressure(
自我
):
向上
=
自我
.readUP()
b6
=
自我
.b5
-
4000
x1
=
(
自我
.b2
*
(b6
*
b6)>>
12
)>>
11
x2
=
(
自我
.ac2
*
b6)>>
11
x3
=
x1
+
x2
b3
=
(((
自我
.ac1
*
4
+
x3)<>
2
x1
=
(
自我
.ac3
*
b6)>>
13
x2
=
(
自我
.b1
*
((b6
*
b6)>>
12
))>>
16
x3
=
((x1
+
x2)
+
2
)>>
2
b4
=
(
自我
.ac4
*
(x3
+
32768
))>>
15
b7
=
(向上
-
b3)
*
(
50000
>>
自我
.OSS)
if
(b7 <
0x80000000
):
p
=
(b7<<
1
)
/
b4
else
:
p
=
(b7
/
b4)<>
8
)
*
(p>
>
8
)
x1
=
(x1
*
3038
)>>
16
x2
=
(
-
7357
*
p)>>
16
p
+
=
(x1
+
x2
+
3791
)>>
4
返回
p
i2c
=
smbus.SMBus(
0
)
bmp085
=
BMP085(i2c,
0x77
)
t
=
bmp085.readTemperature()
p
=
bmp085.readPressure()
打印
"温度:%.2f C"
%
t
打印
"压力:%.2f hPa"
%
(p
/
100
)
有关更多详细信息:Raspberry Pi 读取 BMP085 温度和压力 制造工艺
- Raspberry Pi 温度记录器
- 使用 LabVIEW 的树莓派温度曲线
- 使用 1 线 DS18B20 传感器和 Raspberry Pi 测量温度
- Raspberry Pi 温湿度网络监视器
- 使用 DS18B20 读取温度 |树莓派 2
- Raspberry Pi 上的温度监控
- 使用 RASPBERRY PI 测量温度
- TMP006 温度传感器 Python 库,使用 Raspberry pi,
- Python 和 Raspberry Pi 温度传感器
- Raspberry Pi 上的简易温度和湿度
- 使用 Raspberry Pi 监测温度
- IoT 101 项目:来自 Raspberry Pi 的流温度