|
| 1 | +# Author: Paul Cunnane 2016 |
| 2 | +# |
| 3 | +# This module borrows heavily from the Adafruit BME280 Python library |
| 4 | +# and the Adafruit GPIO/I2C library. Original copyright notices are reproduced |
| 5 | +# below. |
| 6 | +# |
| 7 | +# Those libraries were written for the Raspberry Pi. This modification is |
| 8 | +# intended for the MicroPython and WiPy boards. |
| 9 | +# |
| 10 | +# |
| 11 | +# Copyright (c) 2014 Adafruit Industries |
| 12 | +# Author: Tony DiCola |
| 13 | +# |
| 14 | +# Based on the BMP280 driver with BME280 changes provided by |
| 15 | +# David J Taylor, Edinburgh (www.satsignal.eu) |
| 16 | +# |
| 17 | +# Based on Adafruit_I2C.py created by Kevin Townsend. |
| 18 | +# |
| 19 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 20 | +# of this software and associated documentation files (the "Software"), to deal |
| 21 | +# in the Software without restriction, including without limitation the rights |
| 22 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 23 | +# copies of the Software, and to permit persons to whom the Software is |
| 24 | +# furnished to do so, subject to the following conditions: |
| 25 | +# |
| 26 | +# The above copyright notice and this permission notice shall be included in |
| 27 | +# all copies or substantial portions of the Software. |
| 28 | +# |
| 29 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 30 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 31 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 32 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 33 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 34 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 35 | +# THE SOFTWARE. |
| 36 | + |
| 37 | +import time |
| 38 | +import math |
| 39 | + |
| 40 | + |
| 41 | +# BME280 default address. |
| 42 | +BME280_I2CADDR = 0x76 |
| 43 | + |
| 44 | +# Operating Modes |
| 45 | +BME280_OSAMPLE_1 = 1 |
| 46 | +BME280_OSAMPLE_2 = 2 |
| 47 | +BME280_OSAMPLE_4 = 3 |
| 48 | +BME280_OSAMPLE_8 = 4 |
| 49 | +BME280_OSAMPLE_16 = 5 |
| 50 | + |
| 51 | +# BME280 Registers |
| 52 | + |
| 53 | +BME280_REGISTER_DIG_T1 = 0x88 # Trimming parameter registers |
| 54 | +BME280_REGISTER_DIG_T2 = 0x8A |
| 55 | +BME280_REGISTER_DIG_T3 = 0x8C |
| 56 | + |
| 57 | +BME280_REGISTER_DIG_P1 = 0x8E |
| 58 | +BME280_REGISTER_DIG_P2 = 0x90 |
| 59 | +BME280_REGISTER_DIG_P3 = 0x92 |
| 60 | +BME280_REGISTER_DIG_P4 = 0x94 |
| 61 | +BME280_REGISTER_DIG_P5 = 0x96 |
| 62 | +BME280_REGISTER_DIG_P6 = 0x98 |
| 63 | +BME280_REGISTER_DIG_P7 = 0x9A |
| 64 | +BME280_REGISTER_DIG_P8 = 0x9C |
| 65 | +BME280_REGISTER_DIG_P9 = 0x9E |
| 66 | + |
| 67 | +BME280_REGISTER_DIG_H1 = 0xA1 |
| 68 | +BME280_REGISTER_DIG_H2 = 0xE1 |
| 69 | +BME280_REGISTER_DIG_H3 = 0xE3 |
| 70 | +BME280_REGISTER_DIG_H4 = 0xE4 |
| 71 | +BME280_REGISTER_DIG_H5 = 0xE5 |
| 72 | +BME280_REGISTER_DIG_H6 = 0xE6 |
| 73 | +BME280_REGISTER_DIG_H7 = 0xE7 |
| 74 | + |
| 75 | +BME280_REGISTER_CHIPID = 0xD0 |
| 76 | +BME280_REGISTER_VERSION = 0xD1 |
| 77 | +BME280_REGISTER_SOFTRESET = 0xE0 |
| 78 | + |
| 79 | +BME280_REGISTER_CONTROL_HUM = 0xF2 |
| 80 | +BME280_REGISTER_CONTROL = 0xF4 |
| 81 | +BME280_REGISTER_CONFIG = 0xF5 |
| 82 | +BME280_REGISTER_PRESSURE_DATA = 0xF7 |
| 83 | +BME280_REGISTER_TEMP_DATA = 0xFA |
| 84 | +BME280_REGISTER_HUMIDITY_DATA = 0xFD |
| 85 | + |
| 86 | + |
| 87 | +class Device: |
| 88 | + """Class for communicating with an I2C device. |
| 89 | +
|
| 90 | + Allows reading and writing 8-bit, 16-bit, and byte array values to |
| 91 | + registers on the device.""" |
| 92 | + |
| 93 | + def __init__(self, address, i2c): |
| 94 | + """Create an instance of the I2C device at the specified address using |
| 95 | + the specified I2C interface object.""" |
| 96 | + self._address = address |
| 97 | + self._i2c = i2c |
| 98 | + |
| 99 | + def writeRaw8(self, value): |
| 100 | + """Write an 8-bit value on the bus (without register).""" |
| 101 | + value = value & 0xFF |
| 102 | + self._i2c.writeto(self._address, value.to_bytes(1, "little")) |
| 103 | + |
| 104 | + def write8(self, register, value): |
| 105 | + """Write an 8-bit value to the specified register.""" |
| 106 | + value = value & 0xFF |
| 107 | + self._i2c.writeto_mem(self._address, register, value.to_bytes(1, "little")) |
| 108 | + |
| 109 | + def write16(self, register, value): |
| 110 | + """Write a 16-bit value to the specified register.""" |
| 111 | + value = value & 0xFFFF |
| 112 | + self.i2c.writeto_mem(self._address, register, value.to_bytes(1, "little")) |
| 113 | + |
| 114 | + def readRaw8(self): |
| 115 | + """Read an 8-bit value on the bus (without register).""" |
| 116 | + return int.from_bytes(self._i2c.readfrom(self._address, 1), "little") & 0xFF |
| 117 | + |
| 118 | + def readU8(self, register): |
| 119 | + """Read an unsigned byte from the specified register.""" |
| 120 | + return int.from_bytes( |
| 121 | + self._i2c.readfrom_mem(self._address, register, 1), "little") & 0xFF |
| 122 | + |
| 123 | + def readS8(self, register): |
| 124 | + """Read a signed byte from the specified register.""" |
| 125 | + result = self.readU8(register) |
| 126 | + if result > 127: |
| 127 | + result -= 256 |
| 128 | + return result |
| 129 | + |
| 130 | + def readU16(self, register, little_endian=True): |
| 131 | + """Read an unsigned 16-bit value from the specified register, with the |
| 132 | + specified endianness (default little endian, or least significant byte |
| 133 | + first).""" |
| 134 | + result = int.from_bytes( |
| 135 | + self._i2c.readfrom_mem(self._address, register, 2), "little") & 0xFFFF |
| 136 | + if not little_endian: |
| 137 | + result = ((result << 8) & 0xFF00) + (result >> 8) |
| 138 | + return result |
| 139 | + |
| 140 | + def readS16(self, register, little_endian=True): |
| 141 | + """Read a signed 16-bit value from the specified register, with the |
| 142 | + specified endianness (default little endian, or least significant byte |
| 143 | + first).""" |
| 144 | + result = self.readU16(register, little_endian) |
| 145 | + if result > 32767: |
| 146 | + result -= 65536 |
| 147 | + return result |
| 148 | + |
| 149 | + def readU16LE(self, register): |
| 150 | + """Read an unsigned 16-bit value from the specified register, in little |
| 151 | + endian byte order.""" |
| 152 | + return self.readU16(register, little_endian=True) |
| 153 | + |
| 154 | + def readU16BE(self, register): |
| 155 | + """Read an unsigned 16-bit value from the specified register, in big |
| 156 | + endian byte order.""" |
| 157 | + return self.readU16(register, little_endian=False) |
| 158 | + |
| 159 | + def readS16LE(self, register): |
| 160 | + """Read a signed 16-bit value from the specified register, in little |
| 161 | + endian byte order.""" |
| 162 | + return self.readS16(register, little_endian=True) |
| 163 | + |
| 164 | + def readS16BE(self, register): |
| 165 | + """Read a signed 16-bit value from the specified register, in big |
| 166 | + endian byte order.""" |
| 167 | + return self.readS16(register, little_endian=False) |
| 168 | + |
| 169 | + |
| 170 | +class BME280: |
| 171 | + def __init__(self, mode=BME280_OSAMPLE_16, address=BME280_I2CADDR, i2c=None, |
| 172 | + **kwargs): |
| 173 | + # Check that mode is valid. |
| 174 | + if mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4, |
| 175 | + BME280_OSAMPLE_8, BME280_OSAMPLE_16]: |
| 176 | + raise ValueError( |
| 177 | + 'Unexpected mode value {0}. Set mode to one of ' |
| 178 | + 'BME280_ULTRALOWPOWER, BME280_STANDARD, BME280_HIGHRES, or ' |
| 179 | + 'BME280_ULTRAHIGHRES'.format(mode)) |
| 180 | + self._mode = mode |
| 181 | + # Create I2C device. |
| 182 | + if i2c is None: |
| 183 | + raise ValueError('An I2C object is required.') |
| 184 | + self._device = Device(address, i2c) |
| 185 | + # Load calibration values. |
| 186 | + self._load_calibration() |
| 187 | + self._device.write8(BME280_REGISTER_CONTROL, 0x3F) |
| 188 | + self.t_fine = 0 |
| 189 | + |
| 190 | + def _load_calibration(self): |
| 191 | + |
| 192 | + self.dig_T1 = self._device.readU16LE(BME280_REGISTER_DIG_T1) |
| 193 | + self.dig_T2 = self._device.readS16LE(BME280_REGISTER_DIG_T2) |
| 194 | + self.dig_T3 = self._device.readS16LE(BME280_REGISTER_DIG_T3) |
| 195 | + |
| 196 | + self.dig_P1 = self._device.readU16LE(BME280_REGISTER_DIG_P1) |
| 197 | + self.dig_P2 = self._device.readS16LE(BME280_REGISTER_DIG_P2) |
| 198 | + self.dig_P3 = self._device.readS16LE(BME280_REGISTER_DIG_P3) |
| 199 | + self.dig_P4 = self._device.readS16LE(BME280_REGISTER_DIG_P4) |
| 200 | + self.dig_P5 = self._device.readS16LE(BME280_REGISTER_DIG_P5) |
| 201 | + self.dig_P6 = self._device.readS16LE(BME280_REGISTER_DIG_P6) |
| 202 | + self.dig_P7 = self._device.readS16LE(BME280_REGISTER_DIG_P7) |
| 203 | + self.dig_P8 = self._device.readS16LE(BME280_REGISTER_DIG_P8) |
| 204 | + self.dig_P9 = self._device.readS16LE(BME280_REGISTER_DIG_P9) |
| 205 | + |
| 206 | + self.dig_H1 = self._device.readU8(BME280_REGISTER_DIG_H1) |
| 207 | + self.dig_H2 = self._device.readS16LE(BME280_REGISTER_DIG_H2) |
| 208 | + self.dig_H3 = self._device.readU8(BME280_REGISTER_DIG_H3) |
| 209 | + self.dig_H6 = self._device.readS8(BME280_REGISTER_DIG_H7) |
| 210 | + |
| 211 | + h4 = self._device.readS8(BME280_REGISTER_DIG_H4) |
| 212 | + h4 = (h4 << 24) >> 20 |
| 213 | + self.dig_H4 = h4 | (self._device.readU8(BME280_REGISTER_DIG_H5) & 0x0F) |
| 214 | + |
| 215 | + h5 = self._device.readS8(BME280_REGISTER_DIG_H6) |
| 216 | + h5 = (h5 << 24) >> 20 |
| 217 | + self.dig_H5 = h5 | ( |
| 218 | + self._device.readU8(BME280_REGISTER_DIG_H5) >> 4 & 0x0F) |
| 219 | + |
| 220 | + def read_raw_temp(self): |
| 221 | + """Reads the raw (uncompensated) temperature from the sensor.""" |
| 222 | + meas = self._mode |
| 223 | + self._device.write8(BME280_REGISTER_CONTROL_HUM, meas) |
| 224 | + meas = self._mode << 5 | self._mode << 2 | 1 |
| 225 | + self._device.write8(BME280_REGISTER_CONTROL, meas) |
| 226 | + sleep_time = 1250 + 2300 * (1 << self._mode) |
| 227 | + sleep_time = sleep_time + 2300 * (1 << self._mode) + 575 |
| 228 | + sleep_time = sleep_time + 2300 * (1 << self._mode) + 575 |
| 229 | + time.sleep_us(sleep_time) # Wait the required time |
| 230 | + msb = self._device.readU8(BME280_REGISTER_TEMP_DATA) |
| 231 | + lsb = self._device.readU8(BME280_REGISTER_TEMP_DATA + 1) |
| 232 | + xlsb = self._device.readU8(BME280_REGISTER_TEMP_DATA + 2) |
| 233 | + raw = ((msb << 16) | (lsb << 8) | xlsb) >> 4 |
| 234 | + return raw |
| 235 | + |
| 236 | + def read_raw_pressure(self): |
| 237 | + """Reads the raw (uncompensated) pressure level from the sensor.""" |
| 238 | + """Assumes that the temperature has already been read """ |
| 239 | + """i.e. that enough delay has been provided""" |
| 240 | + msb = self._device.readU8(BME280_REGISTER_PRESSURE_DATA) |
| 241 | + lsb = self._device.readU8(BME280_REGISTER_PRESSURE_DATA + 1) |
| 242 | + xlsb = self._device.readU8(BME280_REGISTER_PRESSURE_DATA + 2) |
| 243 | + raw = ((msb << 16) | (lsb << 8) | xlsb) >> 4 |
| 244 | + return raw |
| 245 | + |
| 246 | + def read_raw_humidity(self): |
| 247 | + """Assumes that the temperature has already been read """ |
| 248 | + """i.e. that enough delay has been provided""" |
| 249 | + msb = self._device.readU8(BME280_REGISTER_HUMIDITY_DATA) |
| 250 | + lsb = self._device.readU8(BME280_REGISTER_HUMIDITY_DATA + 1) |
| 251 | + raw = (msb << 8) | lsb |
| 252 | + return raw |
| 253 | + |
| 254 | + def read_temperature(self): |
| 255 | + """Get the compensated temperature in 0.01 of a degree celsius.""" |
| 256 | + adc = self.read_raw_temp() |
| 257 | + var1 = ((adc >> 3) - (self.dig_T1 << 1)) * (self.dig_T2 >> 11) |
| 258 | + var2 = (( |
| 259 | + (((adc >> 4) - self.dig_T1) * ((adc >> 4) - self.dig_T1)) >> 12) * |
| 260 | + self.dig_T3) >> 14 |
| 261 | + self.t_fine = var1 + var2 |
| 262 | + return (self.t_fine * 5 + 128) >> 8 |
| 263 | + |
| 264 | + def read_pressure(self): |
| 265 | + """Gets the compensated pressure in Pascals.""" |
| 266 | + adc = self.read_raw_pressure() |
| 267 | + var1 = self.t_fine - 128000 |
| 268 | + var2 = var1 * var1 * self.dig_P6 |
| 269 | + var2 = var2 + ((var1 * self.dig_P5) << 17) |
| 270 | + var2 = var2 + (self.dig_P4 << 35) |
| 271 | + var1 = (((var1 * var1 * self.dig_P3) >> 8) + |
| 272 | + ((var1 * self.dig_P2) >> 12)) |
| 273 | + var1 = (((1 << 47) + var1) * self.dig_P1) >> 33 |
| 274 | + if var1 == 0: |
| 275 | + return 0 |
| 276 | + p = 1048576 - adc |
| 277 | + p = (((p << 31) - var2) * 3125) // var1 |
| 278 | + var1 = (self.dig_P9 * (p >> 13) * (p >> 13)) >> 25 |
| 279 | + var2 = (self.dig_P8 * p) >> 19 |
| 280 | + return ((p + var1 + var2) >> 8) + (self.dig_P7 << 4) |
| 281 | + |
| 282 | + def read_humidity(self): |
| 283 | + adc = self.read_raw_humidity() |
| 284 | + # print 'Raw humidity = {0:d}'.format (adc) |
| 285 | + h = self.t_fine - 76800 |
| 286 | + h = (((((adc << 14) - (self.dig_H4 << 20) - (self.dig_H5 * h)) + |
| 287 | + 16384) >> 15) * (((((((h * self.dig_H6) >> 10) * (((h * |
| 288 | + self.dig_H3) >> 11) + 32768)) >> 10) + 2097152) * |
| 289 | + self.dig_H2 + 8192) >> 14)) |
| 290 | + h = h - (((((h >> 15) * (h >> 15)) >> 7) * self.dig_H1) >> 4) |
| 291 | + h = 0 if h < 0 else h |
| 292 | + h = 419430400 if h > 419430400 else h |
| 293 | + return h >> 12 |
| 294 | + |
| 295 | + def temperature(self): |
| 296 | + "Return the temperature in degrees." |
| 297 | + return self.read_temperature() / 100 |
| 298 | + |
| 299 | + |
| 300 | + def pressure(self): |
| 301 | + "Return the pressure in hPa." |
| 302 | + return self.read_pressure() / 25600 |
| 303 | + |
| 304 | + def humidity(self): |
| 305 | + "Return the humidity in percent." |
| 306 | + return self.read_humidity() / 1024 |
0 commit comments