logo elektroda
logo elektroda
X
logo elektroda

Integrating BME280 Sensor with OpenBK on WB2S Modules for Home Automation

bilbs84 4506 75
ADVERTISEMENT
  • #1 20860257
    bilbs84
    Level 2  

    Hi.

    I'm fairly new to the open Beken project, and since discovering it, I have been finding ways to use all my old WB2S modules that I'd replaced in a heap of my devices.

    I was wondering if it's possible to use one of my BME280 sensors with Beken to create a few temperature sensors that I can incorporate into my automations.

    I've had a look and can't seem to find any examples of it, and I know there are other sensors already supported, but I have a few of these BME280's and would like to use them if possible.

    I'm aware that there is software-defined I2C in Beken, but wouldn't have the slightest clue where to start with that.

    Any help would be greatly appreciated. Thanks.
  • ADVERTISEMENT
  • #2 20860932
    p.kaczmarek2
    Moderator Smart Home
    That's a good idea. I have BMP280 sensor somewhere, I used it for this project:
    https://www.elektroda.pl/rtvforum/topic3961713.html#20467262
    I will see if I can port an existing driver to OBK for you. Do you know C?
    Helpful post? Buy me a coffee.
  • #3 20861133
    bilbs84
    Level 2  

    I know a little, enough to scrape by with a little searching anyway.
  • ADVERTISEMENT
  • #4 20861222
    p.kaczmarek2
    Moderator Smart Home
    I've got it almost ready for you, here are first signs of life:
    Screenshot of system log showing I2C address: 0x76.
    And here is temperature read:
    Screenshot of temperature readings from the BMP280 sensor.
    I will try to commit it soon and let you test. Are your BMP280 (or E, they are similiar, right?) sensors ready?
    Helpful post? Buy me a coffee.
  • #5 20861554
    bilbs84
    Level 2  

    Yeah, it's all wired, and sitting ready. That's awesome that you have it nearly operational so quickly. I'd imagine that with the temp working, it shouldn't be any problem getting the humidity and pressure readings as well.
  • #6 20861794
    p.kaczmarek2
    Moderator Smart Home
    Do you need to support multiple sensors on the single bus? I am asking because while the library port is already working, it's currently only linked in a static manner, so my test code handles one sensor per device.
    Helpful post? Buy me a coffee.
  • #7 20861877
    bilbs84
    Level 2  

    I only intend on using the one BME on each chip. I was having a look through your code earlier. Is it hard coded to specific pins on the device, or will it pick up whichever pins I have defined as Software I2C?

    I was in the process of building the latest commit with the driver included, but the clock ran out, and I had to leave for work.
  • #8 20861893
    p.kaczmarek2
    Moderator Smart Home
    I will make a build for you and attach it here, no problem. I still need to add the two or so lines of code allowing you to choose the GPIOs. Currently they are hardcoded.

    The same most likely will also apply to the I2C address of the sensor, as it may vary, right?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #9 20861920
    bilbs84
    Level 2  

    This is a link to a website with the sensor I have (or close enough to mine for all other purposes):

    https://core-electronics.com.au/piicodev-atmospheric-sensor-bme280.html

    As you can see, the address can be selected between 0x77 or 0x76 (depending on a dip switch on the device I have, but with the ADDR line on other dev boards).

    I've used it for over a year with an ESP32, and it's actually quite a decent and reliable sensor.
  • #10 20862018
    p.kaczmarek2
    Moderator Smart Home
    Ok, how would you like to process the measurements? Do you need to send them to Home Assistant via MQTT, or maybe would like to send them somewhere else, for instance via HTTP GET?
    Helpful post? Buy me a coffee.
  • #11 20862051
    bilbs84
    Level 2  

    For the time being, I'll be sending them to Home Assistant with MQTT. If I decide to implement the POST and GET routines that I used to use, then I can probably have HA handle that kind of thing.
  • #12 20862067
    p.kaczmarek2
    Moderator Smart Home
    I have added ability to choose pins for single BMP280 driver. I do not use IO roles enum because it's getting very long these days, so the setup now looks like this:
    
    // startDriver BMP280 8 14 1 2
    // startDriver BMP280 [CLK] [DATA] [ChannelForTemp] [ChannelForPressure]
    

    We will also need to check which adjustments we need for BME280 (if any). Futhermore, I will need to add a channel type, because currently our channels are integers (like in TuyaMCU) and we have channel types to specify the divisor to get fractional values. Still, OBK can prescale values before sending them to HA.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #13 20862072
    bilbs84
    Level 2  

    p.kaczmarek2 wrote:

    We will also need to check which adjustments we need for BME280 (if any).


    From my research, the BME280 is backwards compatible, but obviously, for the humidity reading, there will need to be some addition to the driver to handle that.

    I'd also assume that it's not too difficult to be able to set the address in the driver start command (Although not too critical, I don't think too many people would have more than one I2C device running at a time)

    Added after 26 [minutes]:

    Info:SENSOR:BMP280_REG_CHIPID OK!
    Info:SENSOR:BMP280 ready!
    Info:CMD:[WebApp Cmd 'startDriver BMP280 24 11 1 2' Result] OK
    Info:SENSOR:T 2391, P 100185!
    Info:SENSOR:T 2400, P 100178!
    Info:SENSOR:T 2380, P 100140!


    So I have the temp and pressure readings working on my chip (Finally!!!)

    I was pulling my hair for a while, it kept giving an error about incorrect chip ID, eventually, I realized what was going on. The BME280 chip ID is 0x60, instead of the 0x58 of the BMP280.

    What library did you use to port the driver to that state that it's in? Is it an Arduino based one? That would make it fairly simple to analyze in order to add the humidity measurement in as well.

    For the moment, I'm just over the moon that even Temp and pressure work, and so quickly from my initial inquiry. So far, I'm very impressed with the OpenBK project, both in terms of support, development, and capability.
  • Helpful post
    #14 20862222
    p.kaczmarek2
    Moderator Smart Home
    Thank you for your kind words, the library I used is the same which I used in this topic: https://www.elektroda.pl/rtvforum/topic3961713.html#20467262 , it was originally for PIC but I ported it, it was created by https://simple-circuit.com/

    I am adding ability to choose address now, I also added ID detection:
    Code snippet for initializing the BMP280 sensor with ID detection.
    I will next ook into the humidity measurement.

    I will also need to rewrite that library anyway, because at the moment it's a bit too static for multiple sensors usage, but I can do that once we have BME fully working
    Helpful post? Buy me a coffee.
  • #15 20862268
    bilbs84
    Level 2  

    Thanks again for your help.

    I messed around with trying to implement the humidity measurement, however I wasn't able to fully understand how the driver reads from the correct registers. I'm also thinking that I had something messed up in the part that applies the transformation of the read value from the calibration data.

    I think the BMx280 is a great sensor, because of the inbuilt calibration data in each chip. It takes a lot of the guesswork out of things.

    One thing that I also don't quite understand is how to get it to publish the (currently) Temperature and Pressure readings to Home Assistant.

    Added after 22 [minutes]:

    bilbs84 wrote:
    One thing that I also don't quite understand is how to get it to publish the (currently) Temperature and Pressure readings to Home Assistant
    bilbs84 wrote:
    One thing that I also don't quite understand is how to get it to publish the (currently) Temperature and Pressure readings to Home Assistant.


    OK, so it has to do with the channel types. I can set the Temp to Temperature (Although, I think for this we either need a Temp_div100 or to alter the published value in the BMP280.h file so that it works with the current channel types. And is it easier to add Pressure as a new channel type, or can a custom channel type be defined manually?
  • #16 20862313
    p.kaczmarek2
    Moderator Smart Home
    Well, there many ways it can be done. You can wait for me to integrate BMP280 into HASS discovery. Or you can just set channel type (like Temp_div100) and use channel-type-based Discovery:



    If you are a more advanced user, or you just don't want to wait, you can script the publish yourself:
    
    publishFloat myTemp $CH1*0.01
    publishFloat myPress $CH2*0.1
    

    The above method will work without setting any channel types. Of course, you have to script it to, for example, run every 5 seconds or so, or on every change.
    
    addEventHandler OnChannelChange 1 publishFloat myTemp $CH1*0.01
    

    See more at:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands.md
    but in case of manual publish, you would have to write YAML for Home Assistant to receive and process it

    We may also have floating point channels support (or flexible dividers) in the future, but currently we're following the TuyaMCU approach, that's why it may seem strange at first, but that's how TuyaMCU handles variables. In TuyaMCU thermometers, temperature is also stored as integer.
    Helpful post? Buy me a coffee.
  • #17 20862559
    Helmi_Beh
    Level 2  

    Folks, hope you've read the datasheet of the BME280: the temperature value is only for correcting the humidity and pressure values. This is only the chip temperature, which is 2 to 3 degrees higher than the surrounding temperature. So don't be astonished if the room temperature is much lower than the measured one!
  • #18 20862569
    p.kaczmarek2
    Moderator Smart Home
    That's good point, I can add a simple calibration mechanism (just an offset) for people who insist on using that value
    Helpful post? Buy me a coffee.
  • #19 20863167
    bilbs84
    Level 2  

    Helmi_B wrote:
    Folks, hope you've read the datasheet of the BME280: the temperature value is only for correcting the humidity and pressure values.


    Wow, I'd never noticed that. I'd never even bothered to question the reading straight off the chip, as of all my IoT things, it's the one that reads closest to my home's thermostat. If I place the BME on top of it, I get a reading, usually within 1 degree of it. The BME280 that I have is actually sold as a temperature sensor, and the Datasheet also mentions that the temperature reading can also be used for estimating ambient temperature. So I figure that for my purposes, it's accurate enough.

    p.kaczmarek2 wrote:
    That's a good point, I can add a simple calibration mechanism (just an offset) for people who insist on using that value


    I think that's a good solution, kind of like the CHT8305 sensor that's in one of my devices.

    p.kaczmarek2 wrote:
    Well, there are many ways it can be done. You can wait for me to integrate BMP280 into HASS discovery. Or you can just set channel type (like Temp_div100) and use channel-type-based Discovery:


    My temporary workaround is to set the pressure channel type to Humidity so that both values are published to HA, then I apply the correct divider and measurement unit with a template sensor for dashboard display. Not the best way of doing it, I know, but at 3am, it was about the best that I could manage.

    With the rate of development on the OpenBK project in general, I suddenly find myself no longer looking past any devices with the WB2S modules like I used to.
  • #20 20864322
    p.kaczmarek2
    Moderator Smart Home
    Ok I have added pressure, but I only tested in simulator:
    Screenshot of a control panel with device information and logbook.
    Screenshot of the OpenBeken simulator showing the connection schematic of LEDs and controller.
    Screenshot of device pin settings and channel types with Pressure_div100 entry highlighted.
    I will commit changes now, let me know if it works for you
    Helpful post? Buy me a coffee.
  • #21 20864417
    bilbs84
    Level 2  

    I see the channel type. If I set it (Ch2 on my configuration), then the HA autodiscovery works and it adds the pressure channel. However, in the device web page, the value is blank where the channel types are usually displayed.

    User interface displaying sensor information: pressure, RSSI, and temperature. OpenBK7231T device status page showing temperature, pressure, and MQTT connection info.
  • #22 20864475
    p.kaczmarek2
    Moderator Smart Home
    Well, on my own screenshot, it doesn't seem blank, so maybe you need to refresh your page? As far as I remember it fetches the channel type names from OBK itself.
    Helpful post? Buy me a coffee.
  • #23 20864517
    bilbs84
    Level 2  

    Sorry, I may not have been clear.

    The function in HA is perfect, the missing part is in the OBK device webpage. At the top, where it displays the channel types with scaling. In my screenshot, you see the temperature, but not pressure.
  • #24 20865409
    Osrx
    Level 5  

    Hello! Thank you for your work.

    I have BME280 sensor and it's generally work.
    Firmware: Build: Build on Dec 17 2023 11:15:27 version 1.17.360
    Autoexec: startDriver BMP280 14 16 2 3
    Screenshot showing a dropdown list with various sensor settings.

    But in Index page Pressure not calculated properly. Pressure_div100 actually divided by 10 not 100.
    Screenshot of a BME280 sensor reading panel showing data on temperature, pressure, and energy.

    Humidity not work for now.
  • #25 20865733
    bilbs84
    Level 2  

    Hi

    Glad to see another with the BME280, it will be good to have another that can test.

    The unit reading in the index page, is in deka pascals, not hecto pascals. I think that in the long run, it would be best to apply the correct scaling to hPa here too. If you're familiar with C, and building your own version of OBK, it's actually fairly trivial to change how it is displayed here.

    in drv_bmp280.c Change the line,
    hprintf255(request, "<h2>BMP280 Temperature=%f, Pressure=%f</h2>", g_temperature*0.01f, g_pressure*0.1f);
    to
    hprintf255(request, "<h2>BMP280 Temperature=%f, Pressure=%f</h2>", g_temperature*0.01f, g_pressure*0.01f);
    and then the index page will be in hecto pascals pascals instead.

    I've just not bothered changing it during any of the development, as right now, I'm just glad that it IS being actively developed, and I don't want to waste the devs time with such a trivial change. The scaling in Home assistant is correct however, and that is where I'm viewing all my sensors anyway. I guess this is all stuff that we can wait until humidity is working to iron out.
  • #26 20865737
    p.kaczmarek2
    Moderator Smart Home
    I will push a fix and required channel types in few moments. You were correct @bilbs84 that something was missing.

    My testing setup:
    A breadboard with connected electronic components, such as sensors and modules.

    Added after 7 [minutes]:

    Adding that dot position fix:
    https://github.com/openshwprojects/OpenBK7231...mmit/53591c3a9511e5ae6d362ea631220a3bbe3e78ee
    Helpful post? Buy me a coffee.
  • #27 20865805
    bilbs84
    Level 2  

    p.kaczmarek2 wrote:
    My testing setup:
    Breadboard with ESP32, BME280 module, and other electronic components.


    Certainly a lot more practical than mine. I just have wires soldered to a WB2S module, powered by an ESP32, and dupont pins on the BME salvaged from my old weather station, all precariously balanced on a shelf lmao
  • #28 20865860
    p.kaczmarek2
    Moderator Smart Home
    This is my development board:



    I will add changes to repository soon:
    User interface for an IoT device displaying sensor data. User interface of the BK7231T_FourRelays development board with temperature and pressure readings and control toggles.
    Helpful post? Buy me a coffee.
  • #29 20865929
    bilbs84
    Level 2  

    Yeah, I think your setup is a little nicer than mine, I might look into one of those boards.

    Have you had a chance to look at the humidity on the BME chip?

    I had a little play with trying to add it by taking some code from the Adafruit library for the BME, and adding it to the code that you used, however, I suspect I couldn't get it working because I need to enable the humidity register in the device. I don't quite understand the code well enough to go any further, however. I get lost when you start dealing with pointers, registers, and references.
  • #30 20866002
    p.kaczmarek2
    Moderator Smart Home
    I must admit it's a bit hard for me to do this because I don't have BME at hand so I am not able to test what I implement. I will look into it more soon, maybe we could also try to use the alternate approach, maybe I could just try to get another library ported instead of extending that one? I'll have to think about it.
    Helpful post? Buy me a coffee.

Topic summary

The discussion revolves around integrating the BME280 sensor with OpenBK firmware on WB2S modules for home automation. A user seeks guidance on utilizing their BME280 sensors with OpenBeken, expressing a lack of examples and knowledge about software-defined I2C. Contributors share their progress in porting drivers, troubleshooting issues related to sensor IDs, and implementing temperature, pressure, and humidity readings. They discuss the need for proper scaling in Home Assistant (HA) and the possibility of adjusting measurement intervals. The conversation also touches on the compatibility of BME680 sensors and the challenges faced in enabling drivers for different platforms, including BL602. Overall, the community collaborates to enhance sensor functionality and integration with home automation systems.
Summary generated by the language model.
ADVERTISEMENT