pi3g / bme68x-python-library

Python 3 Library for BME688 and BME680 (Bosch Sensortec sensors), supports Bosch BSEC
MIT License
44 stars 17 forks source link

SystemError: <method 'get_bsec_data' of 'bme68x.BME68X' objects> returned NULL without setting an error #8

Open Loketony opened 2 years ago

Loketony commented 2 years ago

Hey!

I'm struggling to get sane BSEC data with Raspberry Pi Zero and the BME688 sensor.

Before version 1.1.0 I was able to get data – but the IAQ was stuck at 25, and the IAQ accuracy never raised above 1.

Now at version 1.1.0 I can't read any data at all (with BSEC); Instead I get the error: SystemError: <method 'get_bsec_data' of 'bme68x.BME68X' objects> returned NULL without setting an error

To reproduce:

python3
>>> from bme68x import BME68X
>>> import bme68xConstants as cnst
>>> import bsecConstants as bsec
>>> bme = BME68X(cnst.BME68X_I2C_ADDR_HIGH, bsec.BSEC_ENABLE)

INITIALIZED BME68X
VARIANT BME688
INITIALIZED BSEC
BSEC VERSION: 2.0.6.1
SET BME68X CONFIG
SET HEATER CONFIG (FORCED MODE)

>>> bme.get_bsec_data()
BSEC SENSOR CONTROL 0
TRIGGER MEASUREMENT 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: <method 'get_bsec_data' of 'bme68x.BME68X' objects> returned NULL without setting an error

Without BSEC it works:

>>> bme.get_data()
{'sample_nr': 2, 'timestamp': 3275449, 'raw_temperature': 22.32749366760254, 'raw_pressure': 1008.672607421875, 'raw_humidity': 46.31848907470703, 'raw_gas': 12.735706329345703, 'status': 176}

Another thing, enabling or disabling the new debug mode is confusing:

>>> bme.enable_debug_mode()
'Disabled debug mode'

>>> bme.disable_debug_mode()
'Enabled debug mode'
NathanBusler commented 2 years ago

Hi Loketony The get_bsec_data() function is supposed to be called in a loop since it returns None until the timing is correct. Try the following:

`def get_data(sensor): try: data = sensor.get_bsec_data() except: data = None return data

data = get_data(bme) while not data: data = get_data(bme) print(data)`

The debug mode enables additional console outputs to show what is going on in the background. The returned strings are mixed up and are going to be corrected, thank you :)

Loketony commented 2 years ago

Ok! I see. Thanks for that.

I added handling of exception in my code as you suggested:

while(True):
    try:
        data = bme.get_bsec_data()
    except SystemError:
        continue

Now I get:

INITIALIZED BME68X
VARIANT BME688
INITIALIZED BSEC
BSEC VERSION: 2.0.6.1
SET BME68X CONFIG
SET HEATER CONFIG (FORCED MODE)
BSEC SENSOR CONTROL 0
TRIGGER MEASUREMENT 0
BSEC SENSOR CONTROL 100
TRIGGER MEASUREMENT 0
BSEC SENSOR CONTROL 100
TRIGGER MEASUREMENT 0
BSEC SENSOR CONTROL 100
TRIGGER MEASUREMENT 0
BSEC SENSOR CONTROL 100
TRIGGER MEASUREMENT 0
BSEC SENSOR CONTROL 100
TRIGGER MEASUREMENT 0
BSEC SENSOR CONTROL 100
TRIGGER MEASUREMENT 0
BSEC SENSOR CONTROL 100
TRIGGER MEASUREMENT 0
BSEC SENSOR CONTROL 100
...
### JUST CONTINUES …
### PRESSING CTRL-C
^CTraceback (most recent call last):
  File "bme688_bsec_data.py", line 38, in <module>
    data = bme.get_bsec_data()
SystemError: <method 'get_bsec_data' of 'bme68x.BME68X' objects> returned NULL without setting an error

A step forward indeed – but still no data :-(

Loketony commented 2 years ago

I saw that there was a big chunk of code that was commented out (line 220 … 263). I enabled it and ran the sudo python3 setup.py install thing again.

Now I got: open BSEC config: No such file or directory

Aha! I have seen that error before! So I changed the path at line 24 to an absolute path:

const char *bsec_conf_path = "/home/pi/bme68x-python-library/BSEC_2.0.6.1_Generic_Release_04302021/config/bsec_sel_iaq_33v_4d/2021_04_29_02_51_bsec_h2s_nonh2s_2_0_6_1 .config";

And – finally I got data again! :-)

{
    "sample_nr": 138,
    "timestamp": 51637289294629,
    "iaq": 25,
    "iaq_accuracy": 1,
    "temperature": 15.262669563293457,
    "raw_temperature": 21.58180809020996,
    "raw_pressure": 1014.8922729492188,
    "humidity": 43.61470031738281,
    "raw_humidity": 43.41573715209961,
    "raw_gas": 46.011985778808594,
    "static_iaq": 25,
    "static_iaq_accuracy": 1,
    "co2_equivalent": 500,
    "co2_accuracy": 1,
    "breath_voc_equivalent": 0.4999999403953552,
    "breath_voc_accuracy": 1,
    "comp_gas_value": 3.1604151725769043,
    "comp_gas_accuracy": 0,
    "gas_percentage": 0,
    "gas_percentage_accuracy": 1,
    "gas_estimate_1": 0,
    "gas_estimate_1_accuracy": 0,
    "gas_estimate_2": 0,
    "gas_estimate_2_accuracy": 0,
    "gas_estimate_3": 0,
    "gas_estimate_3_accuracy": 0,
    "gas_estimate_4": 0,
    "gas_estimate_4_accuracy": 0
}

Will let this run for a while and see if the IAQ will start to work! :-D

PS. I think it's fishy that the "temperature" and "raw_temperature" differs so much though. I guess that needs to be adressed as well...

Loketony commented 2 years ago

Unfortunately, after running for over 46 hours, the IAQ is still at 25 and the accuracy is stuck at 1. :-(

Something is missing.

{
    "sample_nr": 55418,
    "timestamp": 219464577836236,
    "iaq": 25,
    "iaq_accuracy": 1,
    "temperature": 16.581077575683594,
    "raw_temperature": 22.931629180908203,
    "raw_pressure": 996.307373046875,
    "humidity": 42.60945129394531,
    "raw_humidity": 42.41388702392578,
    "raw_gas": 58.473201751708984,
    "static_iaq": 25,
    "static_iaq_accuracy": 1,
    "co2_equivalent": 500,
    "co2_accuracy": 1,
    "breath_voc_equivalent": 0.4999999403953552,
    "breath_voc_accuracy": 1,
    "comp_gas_value": 3.2675533294677734,
    "comp_gas_accuracy": 0,
    "gas_percentage": 0,
    "gas_percentage_accuracy": 1,
    "gas_estimate_1": 0,
    "gas_estimate_1_accuracy": 0,
    "gas_estimate_2": 0,
    "gas_estimate_2_accuracy": 0,
    "gas_estimate_3": 0,
    "gas_estimate_3_accuracy": 0,
    "gas_estimate_4": 0,
    "gas_estimate_4_accuracy": 0
}

And about the difference between raw_temperature and temperature; I can answer myself. I read somewhere that the sensor by default is adding 5° C to compensate for the heat generated by the electronic components. So somehow you need to deal with that. I just subtract 5° in my python code that reads the sensor data.

nukleuz80 commented 2 years ago

@Loketony - could you pls offer your python script where you get the bsec data?

thanks

Loketony commented 2 years ago

Hey @nukleuz80

Sure can do!

#!/usr/bin/env python3
# Read data from bme688
from bme68x import BME68X
import time
import bme68xConstants as cnst
import bsecConstants as bsec

bme = BME68X(cnst.BME68X_I2C_ADDR_HIGH, bsec.BSEC_ENABLE)

# bme.enable_debug_mode()

while(True):
    try:
        data = bme.get_bsec_data()
    except SystemError:
        print("No data available yet.")
        time.sleep(3)
        continue

    # Ignore first reading
    if data["sample_nr"] == 1:
        continue

    print(data)

The temperature needs to be adjusted with +5° and the IAQ is still not working; after 5 minutes the accuracy goes up to 1 and the iaq to 25; but that's it; no more adjustments will happen.

nukleuz80 commented 2 years ago

Thanks - do you have a apecific BSEC conf?

This is my output:

INITIALIZED BME68X VARIANT = BME688 INITIALIZED BSEC BSEC VERSION: 2.0.6.1 open BSEC config: No such file or directory USING DEFAULT BSEC CONFIG bsec_rslt = 10 bsec_set_sample_rate: No such file or directory {'sample_nr': 2, 'timestamp': 198002329399627, 'iaq': 25.0, 'iaq_accuracy': 0, 'temperature': 15.132987976074219, 'raw_temperature': 21.601415634155273, 'raw_pressure': 985.68359375, 'humidity': 44.930076599121094, 'raw_humidity': 44.72218704223633, 'raw_gas': 0.7308517098426819, 'static_iaq': 25.0, 'static_iaq_accuracy': 0, 'co2_equivalent': 500.0, 'co2_accuracy': 0, 'breath_voc_equivalent': 0.4999999403953552, 'breath_voc_accuracy': 0, 'comp_gas_value': 1.3530540466308594, 'comp_gas_accuracy': 0, 'gas_percentage': 0.0, 'gas_percentage_accuracy': 0} The IAQ doesn`t change, too...

Loketony commented 2 years ago

No I use the one set in bme68xmodule.c @ line 22; but I changed the path to an absolute one. And you also need to rebuild after the edit with sudo python3 setup.py install.

nukleuz80 commented 2 years ago

@Loketony

When i run your script i get this error:

pi@rpi3-test:~/bme68x-python-library/examples $ python3 test.py INITIALIZED BME68X VARIANT BME688 INITIALIZED BSEC BSEC VERSION: 2.0.6.1 SET BME68X CONFIG SET HEATER CONFIG (FORCED MODE) Traceback (most recent call last): File "test.py", line 21, in <module> if data["sample_nr"] == 1: TypeError: 'NoneType' object is not subscriptable

Do you have any suggestions?

Loketony commented 2 years ago

Did you change the path in bme68xmodule.c and rebuild with sudo python3 setup.py install?

nukleuz80 commented 2 years ago

No, a few days ago Nathan updated the repo - now the IAQ is working 😎 But you script don‘t work… Sorry i m ill… I had to told you before ☺️

nukleuz80 commented 2 years ago

The example script airquality.py is working well but i need yours 😇

nukleuz80 commented 2 years ago

Do you have the same issue? i´m not the real coder... :)

Loketony commented 2 years ago

Ok! Lot's of code changes in this new release.

Try this Python code instead:

#!/usr/bin/env python3
# Read data from bme688
from bme68x import BME68X
import time
import bme68xConstants as cnst
import bsecConstants as bsec

bme = BME68X(cnst.BME68X_I2C_ADDR_HIGH, 0)
bme.set_sample_rate(bsec.BSEC_SAMPLE_RATE_LP)

def get_data(sensor):
    data = {}
    try:
        data = sensor.get_bsec_data()
    except Exception as e:
        print(e)
        return None
    if data is None or data == {}:
        time.sleep(0.1)
        return None
    else:
        time.sleep(3)
        return data

while(True):
    data = get_data(bme)
    print(data)
nukleuz80 commented 2 years ago

Thx a lot, but i get this error:

`File "test.py", line 31

print(data)

    ^

SyntaxError: invalid syntax`

Loketony commented 2 years ago

Yeah look at my edited comment, you have to remove the try:-part i the while: loop.

nukleuz80 commented 2 years ago

Pretty nice - thanks!!!