i2c-bme280 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/i2c/bme280.rb +173 -0
- data/lib/i2c/bme280/version.rb +5 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d3fb7e76d1c6b27b73396ecafeda512b56569dae
|
4
|
+
data.tar.gz: e2ddc8957153a4f4418efc6b17ca11486534db0e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e8aaccf55f6cfadccc7ca0b232c1211381a1176c43cb7ab3f8b8a33ea85d984852823a35a381abf45553246b3bf9a1ed9982d950caf3d7b67f8412a66f872ab8
|
7
|
+
data.tar.gz: e8408ef0905e4b2acd0c80b478dd9df0359d476f4b28b29e0a4720fb683f0c46388b65703be6686eb4a55999533ec3379ab5cf05d1b8a72fdbd7d9efc5a920cf
|
data/lib/i2c/bme280.rb
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
require 'i2c'
|
2
|
+
|
3
|
+
module I2C
|
4
|
+
module Driver
|
5
|
+
class BME280
|
6
|
+
I2C_ADDRESS = 0x76 # I2C Bus address
|
7
|
+
|
8
|
+
# @param [Integer|String|I2C::Dev] device The I2C device id of i2c-dev, a string that points to the i2c-dev device file or an already initialized I2C::Dev instance
|
9
|
+
# @param [Fixnum] i2c_address The i2c address of the BME280. Factory default is 0x76.
|
10
|
+
def initialize(device:, i2c_address: I2C_ADDRESS)
|
11
|
+
device = "/dev/i2c-#{device}" if device.is_a?(Integer)
|
12
|
+
|
13
|
+
if device.is_a?(String)
|
14
|
+
raise ArgumentError, "I2C device #{device} not found. Is the I2C kernel module enabled?" unless File.exists?(device)
|
15
|
+
device = I2C.create(device)
|
16
|
+
end
|
17
|
+
|
18
|
+
raise ArgumentError unless device.is_a?(I2C::Dev)
|
19
|
+
|
20
|
+
@device = device
|
21
|
+
@i2c_address = i2c_address
|
22
|
+
end
|
23
|
+
|
24
|
+
# returns all sensor values in a hash
|
25
|
+
# t: temperature
|
26
|
+
# p: pressure
|
27
|
+
# h: humidity
|
28
|
+
# @return [Hash] All sensor values
|
29
|
+
def all
|
30
|
+
data
|
31
|
+
end
|
32
|
+
|
33
|
+
# @return [Float] The temperature in Celsius
|
34
|
+
def temperature
|
35
|
+
data[:t]
|
36
|
+
end
|
37
|
+
|
38
|
+
# @return [Float] The pressure in hectoPascal
|
39
|
+
def pressure
|
40
|
+
data[:p]
|
41
|
+
end
|
42
|
+
|
43
|
+
# @return [Float] The humidity in percent (0.0-100.0)
|
44
|
+
def humidity
|
45
|
+
data[:h]
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
# tells the chip to update its data registers
|
51
|
+
def update(
|
52
|
+
s_t: 1, # Temperature oversampling = x1
|
53
|
+
s_h: 1, # Humidity oversampling = x1
|
54
|
+
s_p: 1, # Pressure oversampling = x1
|
55
|
+
mode: 1, # Normal mode
|
56
|
+
t_s: 5, # Standby time = 1000ms
|
57
|
+
filter: 0, # Disable filter
|
58
|
+
spi: 0 # Disable SPI
|
59
|
+
)
|
60
|
+
|
61
|
+
tp_reg = 0xF4 # Register address for temperature/pressure settings
|
62
|
+
h_reg = 0xF2 # Register address for humidity settings
|
63
|
+
config_reg = 0xF5 # Register address for config settings
|
64
|
+
|
65
|
+
tp_val = (s_t << 5) | (s_p << 2) | mode
|
66
|
+
h_val = s_h
|
67
|
+
config_val = (t_s << 5) | (filter << 2) | spi
|
68
|
+
|
69
|
+
write(tp_reg, tp_val)
|
70
|
+
write(h_reg, h_val)
|
71
|
+
write(config_reg, config_val)
|
72
|
+
end
|
73
|
+
|
74
|
+
# Read calibration parameters of the BME280
|
75
|
+
# https://cdn-shop.adafruit.com/datasheets/BST-BME280_DS001-10.pdf Page 22
|
76
|
+
def calib_params
|
77
|
+
# data addresses
|
78
|
+
dig_t_reg = 0x88
|
79
|
+
dig_p_reg = 0x8E
|
80
|
+
dig_h_reg1 = 0xA1
|
81
|
+
dig_h_reg2 = 0xE1
|
82
|
+
|
83
|
+
# read calibration bytes
|
84
|
+
dig_t = read(dig_t_reg, 6).unpack("S1s2")
|
85
|
+
dig_p = read(dig_p_reg, 18).unpack("S1s8")
|
86
|
+
dig_h = (read(dig_h_reg1, 1) + read(dig_h_reg2, 7)).unpack("C1s1C1C3c1")
|
87
|
+
|
88
|
+
# H4,H5 are saved in an inconvenient format
|
89
|
+
dig_h4 = (dig_h[3] << 4) + (dig_h[4] & 0x0f)
|
90
|
+
dig_h5 = (dig_h[4] >> 4) + (dig_h[5] << 4)
|
91
|
+
|
92
|
+
# reassemble dig_h
|
93
|
+
dig_h = dig_h[0..2] + [ dig_h4, dig_h5 ] + dig_h[6..6]
|
94
|
+
|
95
|
+
{
|
96
|
+
t: dig_t,
|
97
|
+
p: dig_p,
|
98
|
+
h: dig_h
|
99
|
+
}
|
100
|
+
end
|
101
|
+
|
102
|
+
# return all compensated values
|
103
|
+
# ported from bme280_sample.py
|
104
|
+
def data
|
105
|
+
# tell the chip to update its data
|
106
|
+
update
|
107
|
+
|
108
|
+
# read calibration data
|
109
|
+
calib = calib_params
|
110
|
+
|
111
|
+
# read raw data
|
112
|
+
data_reg = 0xF7
|
113
|
+
data = read(data_reg, 8).unpack("C8")
|
114
|
+
raw_t = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4)
|
115
|
+
raw_p = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4)
|
116
|
+
raw_h = (data[6] << 8) | data[7]
|
117
|
+
|
118
|
+
# commonly used for compensation
|
119
|
+
v1 = (raw_t / 16384.0 - calib[:t][0] / 1024.0) * calib[:t][1]
|
120
|
+
v2 = (raw_t / 131072.0 - calib[:t][0] / 8192.0) * (raw_t / 131072.0 - calib[:t][0] / 8192.0) * calib[:t][2]
|
121
|
+
t_fine = v1 + v2
|
122
|
+
|
123
|
+
# compensate temperature
|
124
|
+
t = t_fine / 5120.0
|
125
|
+
|
126
|
+
# compensate pressure
|
127
|
+
v1 = (t_fine / 2.0) - 64000.0
|
128
|
+
v2 = (((v1 / 4.0) * (v1 / 4.0)) / 2048) * calib[:p][5]
|
129
|
+
v2 = v2 + ((v1 * calib[:p][4]) * 2.0)
|
130
|
+
v2 = (v2 / 4.0) + (calib[:p][3] * 65536.0)
|
131
|
+
v1 = (((calib[:p][2] * (((v1 / 4.0) * (v1 / 4.0)) / 8192)) / 8) + ((calib[:p][1] * v1) / 2.0)) / 262144
|
132
|
+
v1 = ((32768 + v1) * calib[:p][0]) / 32768
|
133
|
+
|
134
|
+
if v1 == 0
|
135
|
+
p = 0.0
|
136
|
+
else
|
137
|
+
p = ((1048576 - raw_p) - (v2 / 4096)) * 3125
|
138
|
+
p = p < 0x80000000 ? (p * 2.0) / v1 : (p / v1) * 2.0
|
139
|
+
v1 = (calib[:p][8] * (((p / 8.0) * (p / 8.0)) / 8192.0)) / 4096
|
140
|
+
v2 = ((p / 4.0) * calib[:p][7]) / 8192.0
|
141
|
+
p = p + ((v1 + v2 + calib[:p][6]) / 16.0)
|
142
|
+
p = p / 100.0
|
143
|
+
end
|
144
|
+
|
145
|
+
# compensate humidity
|
146
|
+
h = t_fine - 76800.0
|
147
|
+
if h != 0
|
148
|
+
h = (raw_h - (calib[:h][3] * 64.0 + calib[:h][4]/16384.0 * h)) * (calib[:h][1] / 65536.0 * (1.0 + calib[:h][5] / 67108864.0 * h * (1.0 + calib[:h][2] / 67108864.0 * h)))
|
149
|
+
h = h * (1.0 - calib[:h][0] * h / 524288.0)
|
150
|
+
h = 100.0 if h > 100.0
|
151
|
+
h = 0.0 if h < 0.0
|
152
|
+
end
|
153
|
+
|
154
|
+
# return as hash
|
155
|
+
{
|
156
|
+
t: t,
|
157
|
+
p: p,
|
158
|
+
h: h
|
159
|
+
}
|
160
|
+
end
|
161
|
+
|
162
|
+
# write to device
|
163
|
+
def write(reg_address, data)
|
164
|
+
@device.write(@i2c_address, reg_address, data)
|
165
|
+
end
|
166
|
+
|
167
|
+
# read from device
|
168
|
+
def read(reg_address, size = 1)
|
169
|
+
@device.read(@i2c_address, size, reg_address)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: i2c-bme280
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lukas Prasuhn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-07-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: i2c
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.4'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- lukas@cvguy.de
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- lib/i2c/bme280.rb
|
35
|
+
- lib/i2c/bme280/version.rb
|
36
|
+
homepage: https://github.com/lukasjapan/i2c-bme280
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 2.6.11
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Ruby driver for the Bosch BME280 sensor.
|
60
|
+
test_files: []
|