How to connect a BME680?

When I paste benissimo code I se this in terminal:

BME680 Starting
Estimate indoor air quality

    Runs the sensor for a burn-in period, then uses a
    combination of relative humidity and gas resistance
    to estimate indoor air quality as a percentage.

    Press Ctrl+C to exit

    
Collecting gas resistance burn-in data for 5 mins

Gas: 11477 Ohms
Gas: 12720 Ohms
Gas: 14080 Ohms
Gas: 15983 Ohms
Gas: 17957 Ohms
Gas: 20113 Ohms
Gas: 22274 Ohms
Gas: 24657 Ohms
Gas: 26956 Ohms
Gas: 29436 Ohms
Gas: 31683 Ohms
Gas: 34190 Ohms
Gas: 36641 Ohms
Gas: 38776 Ohms
Gas: 41297 Ohms
Gas: 43892 Ohms
Gas: 46368 Ohms
Gas: 48042 Ohms
Gas: 50657 Ohms
Gas: 52897 Ohms
Gas: 55307 Ohms
Gas: 57589 Ohms
Gas: 59981 Ohms
Gas: 62024 Ohms
Gas: 64508 Ohms
Gas: 66612 Ohms
Gas: 69199 Ohms
^CTraceback (most recent call last):
  File "bme680-client.py", line 93, in <module>
    gas_baseline, hum_baseline, hum_weighting = bme_start()
  File "bme680-client.py", line 83, in bme_start
    return gas_baseline, hum_baseline, hum_weighting
UnboundLocalError: local variable 'gas_baseline' referenced before assignment

Do I have to add Value display in my android app in that sensor dashboard?

try this code:

#!/usr/bin/env python
import cayenne.client
import time
import logging

import bme680
import time

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = "MQTT_USERNAME"
MQTT_PASSWORD  = "MQTT_PASSWORD"
MQTT_CLIENT_ID = "MQTT_CLIENT_ID"

print("""read-all.py - Displays temperature, pressure, humidity, and gas.
Press Ctrl+C to exit!
""")
client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO)
# For a secure connection use port 8883 when calling client.begin:
# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883, loglevel=logging.INFO)

i=0
timestamp = 0

try:
    sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
except 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:
        client.loop()
        if (time.time() > timestamp + 10):
            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)
                client.celsiusWrite(1, bme_sensor.data.temperature)
                client.hectoPascalWrite(2, bme_sensor.data.pressure)
                client.virtualWrite(3, bme_sensor.data.humidity,"rel_hum","p")    

                if sensor.data.heat_stable:
                    print('{0},{1} Ohms'.format(
                        output,
                        sensor.data.gas_resistance))
                    client.virtualWrite(4, sensor.data.gas_resistance, "co","ohms")
                else:
                    print(output)


except KeyboardInterrupt:
    pass

I try Your code but I have error:
$python bme680.py

read-all.py - Displays temperature, pressure, humidity, and gas.
Press Ctrl+C to exit!

Traceback (most recent call last):
  File "bme680.py", line 6, in <module>
    import bme680
  File "/home/pi/Cayenne-MQTT-Python/examples/bme680.py", line 18, in <module>
    client.on_message = on_message
NameError: name 'on_message' is not defined

I don’t know what to type there: on_message

Now I try Example-01-SendData.py and also I don’t see anything in Dashboard (I add new device>Bring Your own thing) in web browser [waiting for live data…] or android app [No active widgets or sensors].

Edit. I add port=8883 and I see temperature etc in example device dashboard.

lets start fresh, i guess you have installed the cayenne python library. if not install it using:

git clone https://github.com/myDevicesIoT/Cayenne-MQTT-Python
cd Cayenne-MQTT-Python
python3 setup.py install

Next, on your cayenne dashboard, add a new device by navigating to add new ---> device/widgets --> arduino You will get the MQTT credentials.
On your pi, navigate to cd Cayenne-MQTT-Python/examples and open sudo nano Example-03-CayenneClient.py . add the MQTT credentials into this code, save and exit. Now, run using sudo python3 Example-03-CayenneClient.py. you should see the device shown online on the cayenne dashboard.
Next, open the code again and just add the BME680 code from the code i gave above.

The code should similar to this.

Hi, as I post above Example-01-SendData.py works fine and show data on dashboard.
Where I have to paste that code You gave me? Do I have to delete some code from example.py file?

have a look at my code and compare it with the Example-01-SendData.py. it is the same, the only difference is the BME680 code.

By BME680 code You mean this:

import bme680

try:
    sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
except IOError:
    sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)

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)

?

that is just a part of the code, here is entire code for bme680 https://github.com/pimoroni/bme680-python/blob/master/examples/read-all.py

So can You tell me how and where exactly to paste that code?

it is very simple and I have repeated it a couple of times now. Copy code from https://github.com/pimoroni/bme680-python/blob/master/examples/read-all.py into https://github.com/myDevicesIoT/Cayenne-MQTT-Python/blob/master/examples/Example-01-SendData.py to look similar to the code I gave above.

I try to combine those files and always have some errors I post You above. I’m not a programmer and I don’t know where to paste that code. Can You help me and show me how it should look?

import cayenne.client
import time
import logging

import bme680
import time

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = "MQTT_USERNAME"
MQTT_PASSWORD  = "MQTT_PASSWORD"
MQTT_CLIENT_ID = "MQTT_CLIENT_ID"

print("""read-all.py - Displays temperature, pressure, humidity, and gas.
Press Ctrl+C to exit!
""")
client = cayenne.client.CayenneMQTTClient()
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO)
# For a secure connection use port 8883 when calling client.begin:
# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883, loglevel=logging.INFO)

i=0
timestamp = 0

try:
    sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
except 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:
        client.loop()
        if (time.time() > timestamp + 10):
            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)
                client.celsiusWrite(1, bme_sensor.data.temperature)
                client.hectoPascalWrite(2, bme_sensor.data.pressure)
                client.virtualWrite(3, bme_sensor.data.humidity,"rel_hum","p")    

                if sensor.data.heat_stable:
                    print('{0},{1} Ohms'.format(
                        output,
                        sensor.data.gas_resistance))
                    client.virtualWrite(4, sensor.data.gas_resistance, "co","ohms")
                else:
                    print(output)


except KeyboardInterrupt:
    pass

Thank You. I will check that when I back home because I’m in work now.

Hi, I paste Your code to empty file bme680.py and start it:

pi@raspberrypi:~/Cayenne-MQTT-Python/examples $ python bme680.py
read-all.py - Displays temperature, pressure, humidity, and gas.
Press Ctrl+C to exit!

Connecting to mqtt.mydevices.com:1883
Traceback (most recent call last):
  File "bme680.py", line 5, in <module>
    import bme680
  File "/home/pi/Cayenne-MQTT-Python/examples/bme680.py", line 25, in <module>
    sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
AttributeError: 'module' object has no attribute 'BME680'

you have two files with same name bme680.py now when the python code looks for the main library to import bme680 it lands to your new file where there is nothing.
Now rename the file using mv bme680.py test_bme,py

I did it and now:

pi@raspberrypi:~/Cayenne-MQTT-Python/examples $ ls
bensimmo.py       bme680.pyc              Example-02-ReceiveData.py    Example-04-SendDataOnTrigger.py  read-cayenne.py.save
bme680-client.py  Example-01-SendData.py  Example-03-CayenneClient.py  read-cayenne.py                  test_bme,py
pi@raspberrypi:~/Cayenne-MQTT-Python/examples $ nano test_bme,py 
pi@raspberrypi:~/Cayenne-MQTT-Python/examples $ python test_bme.py
python: can't open file 'test_bme.py': [Errno 2] No such file or directory
pi@raspberrypi:~/Cayenne-MQTT-Python/examples $ 

Edit. there was , instead .py and now I have one file:

pi@raspberrypi:~/Cayenne-MQTT-Python/examples $ ls
bensimmo.py       bme680.pyc              Example-02-ReceiveData.py    Example-04-SendDataOnTrigger.py  read-cayenne.py.save
bme680-client.py  Example-01-SendData.py  Example-03-CayenneClient.py  read-cayenne.py                  test_bme680.py

and still error:

 python test_bme680.py
read-all.py - Displays temperature, pressure, humidity, and gas.
Press Ctrl+C to exit!

Connecting to mqtt.mydevices.com:8883
Traceback (most recent call last):
  File "test_bme680.py", line 7, in <module>
    import bme680
  File "/home/pi/Cayenne-MQTT-Python/examples/bme680.py", line 25, in <module>
AttributeError: 'module' object has no attribute 'BME680'

ohh so sorry for a typo , there is , inplace of .
try this mv test_bme,py test_bme.py

i hope this works

there was , instead .py and now I have one file:

pi@raspberrypi:~/Cayenne-MQTT-Python/examples $ ls
bensimmo.py       bme680.pyc              Example-02-ReceiveData.py    Example-04-SendDataOnTrigger.py  read-cayenne.py.save
bme680-client.py  Example-01-SendData.py  Example-03-CayenneClient.py  read-cayenne.py                  test_bme680.py

and still error:

 python test_bme680.py
read-all.py - Displays temperature, pressure, humidity, and gas.
Press Ctrl+C to exit!

Connecting to mqtt.mydevices.com:8883
Traceback (most recent call last):
  File "test_bme680.py", line 7, in <module>
    import bme680
  File "/home/pi/Cayenne-MQTT-Python/examples/bme680.py", line 25, in <module>
AttributeError: 'module' object has no attribute 'BME680'

there is still a bme680.py file in /home/pi/Cayenne-MQTT-Python/examples/bme680.py. can you check using ls and then rename it.