User avatar
przemof
Posts: 127
Joined: Mon Dec 30, 2013 6:43 pm

BMP280, testers needed

Sun Oct 21, 2018 11:52 am

I'm looking for testers of Adafruit BMP280 SPI/I2C Barometric Pressure & Altitude Sensor python driver [1].
Currently only SPI is supported, but all other configuration & filtering options are available.
Hardware required: a raspberry pi and this sensor [2].

[1] https://github.com/PrzemoF/bmp280
[2] https://www.adafruit.com/product/2651

User avatar
DougieLawson
Posts: 34096
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: BMP280, testers needed

Sun Oct 21, 2018 2:19 pm

Why not use the kernel driver?

Add

Code: Select all

dtoverlay=i2c-sensor,bmp280
to /boot/config.txt

Your BMP280 will then appear as /sys/bus/iio/devices/iio:device0 with various "virtual" files in there to give you the readings from the sensor.

I don't have a BMP280.

My BMP180 appears as:
[email protected]:/sys/bus/iio/devices/iio:device0 # ls
dev in_temp_oversampling_ratio_available
in_pressure_input name
in_pressure_oversampling_ratio of_node
in_pressure_oversampling_ratio_available power
in_temp_input subsystem
in_temp_oversampling_ratio uevent
[email protected]:/sys/bus/iio/devices/iio:device0 # cat in_pressure_input
101.399000000
[email protected]:/sys/bus/iio/devices/iio:device0 # cat in_pressure_oversampling_ratio
8
[email protected]:/sys/bus/iio/devices/iio:device0 # cat in_pressure_oversampling_ratio_available
1 2 4 8
[email protected]:/sys/bus/iio/devices/iio:device0 # cat in_temp_input
19400
[email protected]:/sys/bus/iio/devices/iio:device0 # cat in_temp_oversampling_ratio
1
[email protected]:/sys/bus/iio/devices/iio:device0 # cat in_temp_oversampling_ratio_available
1
[email protected]:/sys/bus/iio/devices/iio:device0 #

You have to multiply in_pressure_input by 10 to get hPa. You have to divide in_temp_input by 1000 to get °C.

That's an order of magnitude easier to use in python or C/C++ than any of the Adafruit stuff.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
przemof
Posts: 127
Joined: Mon Dec 30, 2013 6:43 pm

Re: BMP280, testers needed

Sun Oct 21, 2018 3:19 pm

Thanks for the detailed info! I'll take a closer look. I didn't know that there is a kernel level driver, so i wrote my own :lol:

User avatar
przemof
Posts: 127
Joined: Mon Dec 30, 2013 6:43 pm

Re: BMP280, testers needed

Sun Oct 21, 2018 3:49 pm

OK, I got it connected, but i don't see the data:

Code: Select all

[email protected]:~ $ cat /boot/config.txt |grep dt | grep -v "^#"
dtparam=i2c_arm=on
dtparam=audio=on
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
dtoverlay=i2c-sensor,bmp280
dtoverlay=pitft28-capacitive,rotate=0,speed=32000000,fps=20
[email protected]:~ $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         
[email protected]:~ $ ls /sys/bus/i2c/devices/1-0076/
modalias  name  of_node  power  subsystem  uevent
[email protected]:~ $ cat /sys/bus/i2c/devices/1-0076/name
bmp280
[email protected]:~ $ cat /sys/bus/i2c/devices/1-0038/name
ft6236
ft6236 is PiTFT touchscreen, but I'm not sure why I can't see bmp280 in the resulst of i2cdetect, but i see it in the /sys/bus/i2c tree. Any ideas?

Edit: OK, what I see is the software side, not the hardware.

User avatar
przemof
Posts: 127
Joined: Mon Dec 30, 2013 6:43 pm

Re: BMP280, testers needed

Sun Oct 21, 2018 4:06 pm

There is nothing in /sys/bus/iio/devices, I guess I have communication problem.
Edit:
dmesg show this:

Code: Select all

[   10.122270] 1-0076 supply vddd not found, using dummy regulator
[   10.122375] 1-0076 supply vdda not found, using dummy regulator
[   10.191302] bmp280: probe of 1-0076 failed with error -121

User avatar
buja
Posts: 474
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: BMP280, testers needed

Sun Oct 21, 2018 4:16 pm

Useful info from Dougie, but I tried the Python program anyway.

First issue: get rid of the degree sign, my Pi did not like it.

Second issue: it's not going well, see below.

Code: Select all

python measure.py
BMP280 returned 0 instead of 88. Expect problems.
/home/wwx/bmp280-master/bmp280.py:290: RuntimeWarning: overflow encountered in long_scalars
  v2 = (v1 * v1 * self.P6) / 32768
/home/wwx/bmp280-master/bmp280.py:296: RuntimeWarning: divide by zero encountered in double_scalars
  p = ((p - v2 / 4096.0) * 6250.0) / v1
/home/wwx/bmp280-master/bmp280.py:297: RuntimeWarning: invalid value encountered in multiply
  v1 = self.P9 * p * p / 2147483648
/home/wwx/bmp280-master/bmp280.py:298: RuntimeWarning: invalid value encountered in double_scalars
  v2 = p * self.P8 / 32768
Temperature: 0.00000 [C]
Pressure: nan [hPa]
Temperature: 0.00000 [C]
Pressure: nan [hPa]
Temperature: 0.00000 [C]
Pressure: nan [hPa]
Temperature: 0.00000 [C]
Pressure: nan [hPa]
Temperature: 0.00000 [C]
Pressure: nan [hPa]
Temperature: 0.00000 [C]
Pressure: nan [hPa]
Temperature: 0.00000 [C]
Pressure: nan [hPa]
Temperature: 0.00000 [C]
Pressure: nan [hPa]
Temperature: 0.00000 [C]
Pressure: nan [hPa]
Temperature: 0.00000 [C]
Pressure: nan [hPa]

User avatar
przemof
Posts: 127
Joined: Mon Dec 30, 2013 6:43 pm

Re: BMP280, testers needed

Sun Oct 21, 2018 4:18 pm

Thanks! That "BMP280 returned 0 instead of 88. Expect problems." tells me that my driver don't see bmp280. What gpio did you use? It has to be somewhere "free", so GPIO can simulate SPI. I2C doesn't work yet.

The degree sign issue is strange. What error did you get?

User avatar
buja
Posts: 474
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: BMP280, testers needed

Sun Oct 21, 2018 4:20 pm

Could it be that Dougie's way does not work because in the Python setup the sensor is connected via SPI, and not I2C?

User avatar
przemof
Posts: 127
Joined: Mon Dec 30, 2013 6:43 pm

Re: BMP280, testers needed

Sun Oct 21, 2018 4:27 pm

buja wrote:
Sun Oct 21, 2018 4:20 pm
Could it be that Dougie's way does not work because in the Python setup the sensor is connected via SPI, and not I2C?
I changed the connection to I2C. I see it now, but the address is 0x77, not 0x76. I also disconnected PiTFT for now.

Code: Select all

[email protected]:~ $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- 77   
After reboot:

Code: Select all

[email protected]:~ $ grep bmp280 /boot/config.txt 
dtoverlay=i2c-sensor,addr=0x77,bmp280
[email protected]:~ $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- UU   
[email protected]:~ $ ls /sys/bus/iio/devices/iio\:device0
dev                in_pressure_oversampling_ratio            in_temp_input               in_temp_oversampling_ratio_available  of_node  subsystem
in_pressure_input  in_pressure_oversampling_ratio_available  in_temp_oversampling_ratio  name                                  power    uevent
So, it's up&running, but the dmesg error is still there. Thanks for the help lads!!

User avatar
przemof
Posts: 127
Joined: Mon Dec 30, 2013 6:43 pm

Re: BMP280, testers needed

Sun Oct 21, 2018 4:30 pm

I don't see any IIR filter settings in the tree. Do you know if it's implemented? I'll need it - the sensor will be part of a cycling computer and filtering will be crucial.

User avatar
buja
Posts: 474
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: BMP280, testers needed

Sun Oct 21, 2018 4:33 pm

przemof wrote:
Sun Oct 21, 2018 4:18 pm
Thanks! That "BMP280 returned 0 instead of 88. Expect problems." tells me that my driver don't see bmp280. What gpio did you use? It has to be somewhere "free", so GPIO can simulate SPI. I2C doesn't work yet.

The degree sign issue is strange. What error did you get?

Code: Select all

python measure.py~
  File "measure.py~", line 17
SyntaxError: Non-ASCII character '\xc2' in file measure.py~ on line 17, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
Deleting the degree sign solved this, but it would be nice to print it of course. Maybe it's just a setup issue.

User avatar
przemof
Posts: 127
Joined: Mon Dec 30, 2013 6:43 pm

Re: BMP280, testers needed

Sun Oct 21, 2018 4:35 pm

It's python3 only, that's why it doesn't like the degree sign.

User avatar
DougieLawson
Posts: 34096
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: BMP280, testers needed

Sun Oct 21, 2018 4:50 pm

Try this python Class

Code: Select all

class BMP280():
    def __init__(self):
        self._pressure = None
        self._temperature = None

    def pressure(self):
        '''Returns a pressure value.  Returns None if no valid value is set
        yet.

        '''
        with open('/sys/bus/iio/devices/iio:device0/in_pressure_input', 'r') as press:
                value = float(press.read())
                self._pressure = value * 10.0
                return self._pressure

    def temperature(self):
        '''Returns a temperature value.  Returns None if no valid value is
        set yet.

        '''
        with open('/sys/bus/iio/devices/iio:device0/in_temp_input', 'r') as temp:
                value = float(temp.read())
                self._temperature = value / 1000.0
                return self._temperature

    def pressure_and_temperature(self):
        '''Returns pressure and temperature values as a tuple.  This call can
        save 1 transaction than getting a pressure and temperature
        values separetely.  Returns (None, None) if no valid values
        are set yet.

        '''
        self.pressure()
        self.temperature()
        return (self._pressure, self._temperature)


Call that with

Code: Select all

#!/usr/bin/python3

import bmp280
sensor = bmp280.BMP280()
print (sensor.pressure_and_temperature())
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
przemof
Posts: 127
Joined: Mon Dec 30, 2013 6:43 pm

Re: BMP280, testers needed

Sun Oct 21, 2018 6:14 pm

Thanks, tested, works! :D Looks like the IIR filter is defined in the kernel driver [1], but I'm not sure yet if it's fixed at X4 or there is a way of changing it from the user space.

[1] https://git.kernel.org/pub/scm/linux/ke ... ed3ab#n549

User avatar
DougieLawson
Posts: 34096
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: BMP280, testers needed

Sun Oct 21, 2018 6:58 pm

What "virtual" files do you find in /sys/bus/iio/devices/iio:device0/* on your system?

You may be able to "echo" some values into the controls for your sensor using those.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
przemof
Posts: 127
Joined: Mon Dec 30, 2013 6:43 pm

Re: BMP280, testers needed

Sun Oct 21, 2018 7:04 pm

Code: Select all

[email protected]:~/Open-Cycling-Computer/code $ ls -a1 /sys/bus/iio/devices/iio\:device0
.
..
dev
in_pressure_input
in_pressure_oversampling_ratio
in_pressure_oversampling_ratio_available
in_temp_input
in_temp_oversampling_ratio
in_temp_oversampling_ratio_available
name
of_node
power
subsystem
uevent
i did a quick test and looks like I might be OK with IIR filter set to X4. Especially that I also use Kalman filter

User avatar
DougieLawson
Posts: 34096
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: BMP280, testers needed

Sun Oct 21, 2018 7:21 pm

Try reading (with cat) / changing (with echo) those oversampling ratio values.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
przemof
Posts: 127
Joined: Mon Dec 30, 2013 6:43 pm

Re: BMP280, testers needed

Sun Oct 21, 2018 7:53 pm

Oversampling settings are in the tree and looks like I could change them, but they are set by default to what I need. There are also 2 more parameters that BMP280 allows to set (t_standby - time between measurements and IIR filter) that are not exposed to the user space by the kernel driver. Anyway I just went for a 4k walk/run and the data looks very good, so hopefully I won't need it. Thanks again for your help! :D

User avatar
przemof
Posts: 127
Joined: Mon Dec 30, 2013 6:43 pm

Re: BMP280, testers needed

Mon Oct 22, 2018 11:15 am

przemof wrote:
Sun Oct 21, 2018 4:06 pm
There is nothing in /sys/bus/iio/devices, I guess I have communication problem.
Edit:
dmesg show this:

Code: Select all

[   10.122270] 1-0076 supply vddd not found, using dummy regulator
[   10.122375] 1-0076 supply vdda not found, using dummy regulator
[   10.191302] bmp280: probe of 1-0076 failed with error -121
Just to have a complete set of info in one place: the vddd and vdda errors come from the kernel driver [1], but they don't seem to indicate any problems with bmp280.

[1] https://git.kernel.org/pub/scm/linux/ke ... ed3ab#n970

User avatar
buja
Posts: 474
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: BMP280, testers needed

Wed Oct 24, 2018 8:49 am

DougieLawson wrote:
Sun Oct 21, 2018 2:19 pm
Why not use the kernel driver?

Add

Code: Select all

dtoverlay=i2c-sensor,bmp280
to /boot/config.txt

Your BMP280 will then appear as /sys/bus/iio/devices/iio:device0 with various "virtual" files in there to give you the readings from the sensor.
I have my BMP280 sensor working, I checked with a Python script for the BME280 sensor (after changing the I2C address of course).

But the kernel approach does not work. I added the line above to /boot/config.txt and rebooted, but nothing shows up under the devices subdirectory. Is there anything else I should do?

User avatar
przemof
Posts: 127
Joined: Mon Dec 30, 2013 6:43 pm

Re: BMP280, testers needed

Wed Oct 24, 2018 8:58 am

Use dmesg. I had address problem that was fixed with:

Code: Select all

dtoverlay=i2c-sensor,addr=0x77,bmp280
Use i2cdetect -y 1 to check it the sensor is there.

User avatar
buja
Posts: 474
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: BMP280, testers needed

Wed Oct 24, 2018 9:11 am

That's it, thanks!

User avatar
przemof
Posts: 127
Joined: Mon Dec 30, 2013 6:43 pm

Re: BMP280, testers needed

Fri Nov 02, 2018 1:29 pm

Thanks again for testing. I use the kernel approach (thanks, DougieLawson !) and with catching those 2 errors seems to be bulletproof:

Code: Select all

        try:
            with open('/sys/bus/iio/devices/iio:device0/in_pressure_input', 'r') as press:
                self.pressure = float(press.read()) * 1000.0
            with open('/sys/bus/iio/devices/iio:device0/in_temp_input', 'r') as temp:
                self.temperature = float(temp.read()) / 1000.0
        except (FileNotFoundError, OSError) as e:
            # FileNotFoundError: [Errno 2] No such file or directory: '/sys/bus/iio/devices/iio:device0/in_pressure_input'
            # OSError: [Errno 121] Remote I/O error
            self.log.critical("Reading from bmp280 caused exception: {}".format(e), extra=self.extra)
I recommend a simple Kalman filter [1][2] for the bmp280 (nof - not-filtered, I use Q=0.02, R=1.0, but that depends on application):
kalman_filter_on_bmp280.png
kalman_filter_on_bmp280.png (76.18 KiB) Viewed 212 times
[1] https://github.com/PrzemoF/Open-Cycling ... /kalman.py
[2] https://en.wikipedia.org/wiki/Kalman_filter

User avatar
DougieLawson
Posts: 34096
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: BMP280, testers needed

Fri Nov 02, 2018 5:11 pm

Thank you. I think I've just learned how to smooth out the bumps in a graph. Although by only taking a reading every three minutes I get a fairly smooth curve anyway.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
przemof
Posts: 127
Joined: Mon Dec 30, 2013 6:43 pm

Re: BMP280, testers needed

Fri Nov 02, 2018 5:26 pm

Tweak Q and R. It's all about finding balance between smooth results and speed of the reaction. I need bmp280 to cycling, so I need quick reaction to calculate altitude change, but for weather monitoring I'd go for very smooth results. If you use my kalman filter code you can setup 3 filters for the same unfiltered measurement and compare the results. I never posted an example how to use the filter:
Setup:

Code: Select all

import kalman
k = kalman.kalman(Q=0.02, R=1.0)
k.set_initial_value(pressure_unfiltered)
In the measurement loop:

Code: Select all

k.update_unfiltered_value(pressure_unfiltered)
k.update()
pressure = k.value_estimate
Edit: Q and R are both important, not only Q/R ratio. I need to update comment in the code

Return to “Python”