BME680 Examples

Hi,
struggling to run thew examples.
Followed the instructions here -

but i can’t figure out the correct way to insert the I2C address in the code. It doesn’t like my attempts. Always fails on line 13

Raspberry Pi? Python?
Try these examples.
bme680-python/examples at master · pimoroni/bme680-python (github.com)

#!/usr/bin/env python

import bme680
import time

print("""read-all.py - Displays temperature, pressure, humidity, and gas.

Press Ctrl+C to exit!

""")

try:
    sensor = bme680.BME680(bme680.I2C 0x76)
except (RuntimeError, IOError):
    sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)

# These calibration data can safely be commented
# out, if desired.

print('Calibration data:')
for name in dir(sensor.calibration_data):

    if not name.startswith('_'):
        value = getattr(sensor.calibration_data, name)

        if isinstance(value, int):
            print('{}: {}'.format(name, value))

# These oversampling settings can be tweaked to
# change the balance between accuracy and noise in
# the data.

sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_4X)
sensor.set_temperature_oversample(bme680.OS_8X)
sensor.set_filter(bme680.FILTER_SIZE_3)
sensor.set_gas_status(bme680.ENABLE_GAS_MEAS)

print('\n\nInitial reading:')
for name in dir(sensor.data):
    value = getattr(sensor.data, name)

    if not name.startswith('_'):
        print('{}: {}'.format(name, value))

sensor.set_gas_heater_temperature(320)
sensor.set_gas_heater_duration(150)
sensor.select_gas_heater_profile(0)

# Up to 10 heater profiles can be configured, each
# with their own temperature and duration.
# sensor.set_gas_heater_profile(200, 150, nb_profile=1)
# sensor.select_gas_heater_profile(1)

print('\n\nPolling:')
try:
    while True:
        if sensor.get_sensor_data():
            output = '{0:.2f} C,{1:.2f} hPa,{2:.2f} %RH'.format(
                sensor.data.temperature,
                sensor.data.pressure,
                sensor.data.humidity)

            if sensor.data.heat_stable:
                print('{0},{1} Ohms'.format(
                    output,
                    sensor.data.gas_resistance))

            else:
                print(output)

        time.sleep(1)

except KeyboardInterrupt:
    pass
1 Like

~/bme680/examples $ python read-all.py
File “/home/bme680/examples/read-all.py”, line 13
sensor = bme680.BME680(bme680.I2C 0x76)
^
SyntaxError: invalid syntax

the ‘^’ is under my entering of the I2C address

Hmm, I am no Pythonista, but this looks problematic. The BME680() method is given two parameters, but it is not clear if they should be one parameter, or separated by a comma (which is how parameters are separated). What documentation are you looking at? Could you link to it?

Update

In this file it looks like it should take one parameter, thus:

sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)

Thanks for the feedback.
Original as you write didn’t work either.

I thought i was to substitute the _ADDR_PRIMARY with my I2C address, the breakout board is default 76 or 0x76 according to the guide

If its using the default address (AFAIK) you don’t need the 0x76.
Try sensor = bme680.BME680(bme680.I2C)

Assuming the all-caps variables are just integer constants, I should think that hardwiring would work too:

sensor = bme680.BME680(0x76)

If it doubt about a class variable, one can try printing it to the console:

print(bme680.I2C_ADDR_PRIMARY) # Substitute the one you want to try here

This will print it in decimal though, not in hex - so have your programmers’ calculator to hand!

Should work, you’d do that if you were using two bme sensors.
or ‘sensor = bme680.BME680(i2c,0x76)’ if the above doesn’t work.
It’s easy in Micropython, haven’t done it in Python in while.

>>> %Run read-all.py
read-all.py - Displays temperature, pressure, humidity, and gas.

Press Ctrl+C to exit!


Traceback (most recent call last):
  File "/home/bme680/examples/read-all.py", line 14, in <module>
    sensor = bme680.BME680(bme680.I2C)
AttributeError: module 'bme680' has no attribute 'I2C'
>>> 

Alas, no joy. I feel i must be doing something really obvious and stupid. Pimoroni must have sold hundreds of these things

i see I2c and also I2C mentioned above, i’ve tried both

Try this, if you haven’t already
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)

1 Like

There’s a bit too much guessing going on in this thread - I2C isn’t in the library code, so I imagine it won’t do anything.

These are the constants that should work, assuming you are using the Pimoroni library: bme680-python/constants.py at master · pimoroni/bme680-python · GitHub

Here is the class definition: bme680-python/__init__.py at master · pimoroni/bme680-python · GitHub

I don’t really “do” Python - @alphanumeric could you explain how this line works?

def __init__(self, i2c_addr=constants.I2C_ADDR_PRIMARY, i2c_device=None):

If this is an object constructor, what should be passed in self? It looks like the address needs to be the second instantiation param, not the first.

However I think @gbar is following the example correctly - it looks like the address should be passed as the first param. See this example: bme680-python/read-all.py at master · pimoroni/bme680-python · GitHub

If it’s wired up correctly, and the install script has been run?
The examples should just work?
I don’t have a BME680 handy that I can plug into my Pi based Breakout Garden. I think they are all wired up to Pi PICO’s? I will have a good look for one though.

That self is just the weird way (Micro)Python passes in self-references; you can ignore that - it’s showing two arguments (i2c_addr and i2c_device) along with their defaults.

As such, sensor = bms.BME680() should work, and do the same as BME680(bme680.I2C_ADDR_PRIMARY).

If you’re using a different I2C address, you should be able to add it as @alphanumeric says.

1 Like

Spotted a few bits of out of date information in the BME680 Learn article, so I’ve done a few ninja updates 🐱‍👤

1 Like

Hi,

Thanks to everyone who has chimed in but…i see a DS18B20 in my future.
Before starting down this path i had a fresh install. Pi4 in use.
Followed the revised ‘Getting Started’

~/Pimoroni/bme680/examples $ python read-all.py
read-all.py - Displays temperature, pressure, humidity, and gas.

Press Ctrl+C to exit!


Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/bme680-1.1.1-py3.9.egg/bme680/__init__.py", line 46, in __init__
  File "/usr/local/lib/python3.9/dist-packages/bme680-1.1.1-py3.9.egg/bme680/__init__.py", line 352, in _get_regs
OSError: [Errno 121] Remote I/O error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/graeme/Pimoroni/bme680/examples/read-all.py", line 13, in <module>
    sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
  File "/usr/local/lib/python3.9/dist-packages/bme680-1.1.1-py3.9.egg/bme680/__init__.py", line 50, in __init__
RuntimeError: Unable to identify BME680 at 0x76 (IOError)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/bme680-1.1.1-py3.9.egg/bme680/__init__.py", line 46, in __init__
  File "/usr/local/lib/python3.9/dist-packages/bme680-1.1.1-py3.9.egg/bme680/__init__.py", line 352, in _get_regs
OSError: [Errno 121] Remote I/O error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/graeme/Pimoroni/bme680/examples/read-all.py", line 15, in <module>
    sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
  File "/usr/local/lib/python3.9/dist-packages/bme680-1.1.1-py3.9.egg/bme680/__init__.py", line 50, in __init__
RuntimeError: Unable to identify BME680 at 0x77 (IOError)

That last line "…at 0x77 (IOError)
according to docs should be 0x76?

Have you used the i2cdetect tools to see what your Pi can detect on your system?

I used i2cdetect -l when playing around with a USB-to-I2C board recently. It should give you what I2C system it is running on (i2cX, where X is an integer).

embarassing…
hardware issue.
sorry for wasting everyones time.
working now but i think maybe only since the guide was updated

2 Likes