BME280 on the LoPy



  • Hello,

    I'm currently trying to get the Adafruit BME280 sensor running with my LoPy.
    And I came across pretty much the same issue as michal in this post Wipy 2.0 and BME280 sensor.

    However my issue is looking a little different:

    Traceback (most recent call last):
      File "<stdin>", line 9, in <module>
      File "bme280.py", line 185, in __init__
      File "bme280.py", line 191, in _load_calibration
      File "bme280.py", line 151, in readU16LE
      File "bme280.py", line 134, in readU16
    OSError: I2C bus error
    

    I'm very new to the LoPy and as far as I read from the specs he has pretty much the same hardware as the WiPy. So I ask myself what could cause this issue. The original library: https://bitbucket.org/oscarBravo/wipy_bme280

    I initialized my BME like this:

    >>> from machine import I2C
    >>> import bme280
    >>> i2c = I2C(0, I2C.MASTER, baudrate=100000)
    >>> bme = bme280.BME280(i2c=i2c)
    

    Do you have any suggestions how to solve this?

    Thanks already in advance!



  • @Lucas_Vieira Yes, just change the last line of the method values() to:
    return t, p, h
    I never had sent values directly to cayenne, only through TTN. Which cayenne LPP module are you using?

    Edit: In my tests, the value for humidity was the one with the largest error, but still not 0. Are you sure that you have a BME280 device and not an BMP280? If you look at the package, there are two lines of letters
    BME280:
    xxx
    Ux
    BMP280:
    xxx
    Kx
    The difference is U vs. K



  • @robert-hh
    Hi Robert.

    1. The pressure value is strange because I had converted to ATM. But I returned to normal, getting the value in Pascal.
    2. can't your drive receive temperature, pressure and humidity values ​​separately? I need to send the data to my cayenne application.

    erro_atom_do_carai.png

    I am having these cayenne LPP errors. Can you help me? I need to open a new post or we can keep talking on this I2C topic, maybe it could be wrong?



  • @Lucas_Vieira The value for pressure is also strange. The class function values returns Pascal, so the expected numbers are in the range of 100000.
    You may also try my variations of the adafruit driver. Most of the BME280 drivers I have seen are based on that. You may have to change values(), to return t,p,h instead of formatted strings. I used that driver a lot with pycom devices and it worked well. The sensors I used were sometimes out of range.
    https://github.com/robert-hh/BME280
    There are two variants, which differn from the mode of internal calculations (float or int). The results are comparable.



  • @svonbentzel thank you very much for reply!

    1. i2c.scan() report: [118]
    2. What modes are they? I did not understand what you meant.
    3. Yes, the pins are correct!

    Note: Right now, it's running perfect. No more errors appear in the code, it is strange at least hahaha. However, since yesterday I have not received any sign of humidity, is there any clue where the error is? I can not find.
    (I uncommented "print (h)" in the last code).

    The image data is pressure (1.0), temperature (27ºC) and humidity (0.0) respectively.
    erro_pycom_forum.png



  • @Lucas_Vieira

    Do you have tried the other modes?
    The sensor is correct wired? Sda / p9 and scl p10 in example?



  • @Lucas_Vieira i2c.scan() reports the correct sensor? 108 (I mean)??



  • Hello guys. i'm having similar problems here. Would you help me?

    from machine import I2C
    import bme280 # from bme280
    
    i2c = I2C(0, I2C.MASTER)             # create and init as a master
    i2c.init(I2C.MASTER, baudrate=10000) # init as a master
    i2c.scan()
    print(i2c.scan())
    bmet = []   # Lista com as temperaturas lidas com o BME280
    bmeh = []   # Lista com as umiadades lidas com o BME280
    bmep = []   # Lista com as pressoes lidas com o BME280
    bme = bme280.BME280(i2c=i2c, pressure_mode=bme280.OSAMPLE_8, iir=bme280.FILTER_8)
    bme_s = True
    
    while True:
        for cycles in range(10):
            # Le valores BME280 com media para ter maior precisao :
            numreadings = 5
            samples_temperature = [0.0]*numreadings; mean_temperature = 0.0
            samples_pressure = [0.0]*numreadings; mean_pressure = 0.0
            samples_humidity = [0.0]*numreadings; mean_humidity = 0.0
            for i in range (numreadings):
                samples_temperature[i], samples_pressure[i], samples_humidity[i] = bme.values
            mean_temperature = sum(samples_temperature)/len(samples_temperature)
            mean_pressure = sum(samples_pressure)/len(samples_pressure)
            mean_humidity = sum(samples_humidity)/len(samples_humidity)
            t = mean_temperature
            p = mean_pressure/101325   # Pa -> hectoPa
            h = mean_humidity
            bmet.append(t)
            bmep.append(p)
            bmeh.append(h)
    
        print(t)
        print(p)
        #print(h)
        time.sleep(3)
    

    My problems:
    erro00001.png

    File lib bme280:
    bme280.py



  • @robd I think you didn't get me correctly.
    Anyways, try the code as below:

    from machine import I2C
    import BME280
    i2c = I2C(0,I2C.MASTER, baudrate=100000)
    bme = BME280.BME280(address=BME280_I2CADDR, i2c=i2c)
    print('Temp = ', bme.temperature())



  • @devang-sharma the code used was from https://github.com/LoRaWeather/BME280-LoPy-TTN/tree/master/Program/lib
    I copied everything over as it was and didn't modify anything.
    The code which I tried to write and didn't work was

    from machine import I2C
    import BME280
    i2c = I2C(0,I2C.MASTER, baudrate=100000)

    print('Temp = ', BME280.temperature())

    Yes I am new to python and right now it is kicking me, I didn't see any docs other than small comments in the code. So it was me trying to feel around and get this sensor working.
    I was able to get a SI7021(different code) to work but not the BME280.
    I will look at and try to understand your suggestions.
    Thank you for your help



  • @robd The first error you're getting is because of indentation of the return function. Write it within def humidity(self). I guess you're new to python, it'd be better if you read docs properly.

    The second error is due to the way you've used import BME280. The library has a class BME280. There are 2 ways to solve this:

    1. You can import it as from BME280 import * and make a constructor just after i2c something like this: bme = BME280(address=BME280_I2CADDR, i2c=i2c)

    OR
    2. keep it as import BME280 and constructor as:
    bme = BME280.BME280(address=BME280_I2CADDR, i2c=i2c)

    Make sure to use the same name as defined in the library.

    I hope this helps you.



  • @robert-hh I have tired several libraries, LoPy_BME280, oscarBravo-wipy_bme280, and mpy_bme280_esp8266.

    https://github.com/ButchAnton/LoPy_BME280
    https://github.com/catdog2/mpy_bme280_esp8266
    https://bitbucket.org/oscarBravo/wipy_bme280/src/7c03d16f48154a55a9c09af65049b0f03ef6c005?at=default

    and thank you to @devang-sharma
    https://github.com/LoRaWeather/BME280-LoPy-TTN/tree/master/Program/lib

    I must be doing something really wrong for nothing to work correctly.
    I switched over to a Si7021 and I was able to get that to work but I need to use the BME280.

    Thanks,



  • @devang-sharma thank you for the link. I really hate learning curves. I tried the TTN libraries and I still have issues. How are you calling the library?
    I am trying with (firmware version 1.10.2.b1)

    from machine import I2C
    import BME280
    i2c = I2C(0,I2C.MASTER, baudrate=100000)
    bme = bme280.BM280(i2c=i2c)

    print('Temp = ', bme280.temperate())

    the errors I am getting are
    0_1512438214685_1e156ce4-f907-42a4-b4fd-42e26d7d8984-image.png

    I then tried to see if my sensor was readable and I did

    from machine import I2C
    i2c = I2C(0, I2C.MASTER, baudrate=100000)
    i2c.scan()

    which returned 118 (0x76)
    then I did the following
    0_1512438859351_4daadb32-e396-4c98-bd3c-b1c5c68d854f-image.png

    Any pointers to my errors?

    Thank you



  • This post is deleted!


  • @robd I've faced a similar issue and added argument 'little' as @robert-hh suggested which worked for me. I don't know which library you're using. But I've been using this:

    https://github.com/LoRaWeather/BME280-LoPy-TTN/tree/master/Program/lib



  • @robd Actually, which bme280 code are you using at the moment? I'm a little bit lost too.



  • Hello,
    There are quite a few entries on the BME280.
    I have been reading them and I am still not able to fix my issues.
    I have tried several of the libraries and I am just at a stand still.
    When I do a scan of the I2C port I get 0x76 so I know that the sensor is there.
    What I get is as follows (firmware rev 1.10.2.b1)

    0_1512356556349_56247ec3-a520-4588-b803-29cfce44b408-image.png

    Thank you,



  • @devang-sharma No specific API doc needed, since it is standard Python behaviour now. It was incompatible before, but I'm not shure, for which MicroPython version. It may be that the Pycom branch always was compatible to the Standard.
    And yes, even in that case changes toward language compliance may break code, so it should be noted.



  • @robert-hh, that worked! Thanks, but the same error for write8:
    1_1507872596119_error2.png
    0_1507872596117_code2.png

    Is there any link to the changed API or library? That would be better than posting every single error! :'D

    EDIT: Solved the error. Replaced value.to_bytes(1) with "value"! No more Errors!



  • @devang-sharma Sorry, it should be a string:

            result = int.from_bytes(
                self._i2c.readfrom_mem(self._address, register, 2), 'little') & 0xFFFF
    

Log in to reply
 

Pycom on Twitter