Skip to content

Commit f1d671e

Browse files
authored
Merge pull request #35 from imgrant/catch-init-ioerror
Catch IOError, raise as RuntimeError in init()
2 parents af5d35f + 069f12c commit f1d671e

File tree

6 files changed

+11
-8
lines changed

6 files changed

+11
-8
lines changed

examples/compensated-temperature.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
try:
1717
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
18-
except IOError:
18+
except (RuntimeError, IOError):
1919
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
2020

2121
# These oversampling settings can be tweaked to

examples/indoor-air-quality.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
try:
1717
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
18-
except IOError:
18+
except (RuntimeError, IOError):
1919
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
2020

2121
# These oversampling settings can be tweaked to

examples/read-all.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
try:
1313
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
14-
except IOError:
14+
except (RuntimeError, IOError):
1515
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
1616

1717
# These calibration data can safely be commented

examples/temperature-offset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
try:
1212
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
13-
except IOError:
13+
except (RuntimeError, IOError):
1414
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
1515

1616
# These oversampling settings can be tweaked to

examples/temperature-pressure-humidity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
try:
1515
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
16-
except IOError:
16+
except (RuntimeError, IOError):
1717
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
1818

1919
# These oversampling settings can be tweaked to

library/bme680/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ def __init__(self, i2c_addr=constants.I2C_ADDR_PRIMARY, i2c_device=None):
4242
import smbus
4343
self._i2c = smbus.SMBus(1)
4444

45-
self.chip_id = self._get_regs(constants.CHIP_ID_ADDR, 1)
46-
if self.chip_id != constants.CHIP_ID:
47-
raise RuntimeError('BME680 Not Found. Invalid CHIP ID: 0x{0:02x}'.format(self.chip_id))
45+
try:
46+
self.chip_id = self._get_regs(constants.CHIP_ID_ADDR, 1)
47+
if self.chip_id != constants.CHIP_ID:
48+
raise RuntimeError('BME680 Not Found. Invalid CHIP ID: 0x{0:02x}'.format(self.chip_id))
49+
except IOError:
50+
raise RuntimeError("Unable to identify BME680 at 0x{:02x} (IOError)".format(self.i2c_addr))
4851

4952
self.soft_reset()
5053
self.set_power_mode(constants.SLEEP_MODE)

0 commit comments

Comments
 (0)