Skip to content

Add I2c BME280 temperature, humidity and pressure sensor for Raspberry Pi #7989

Merged
pvizeli merged 10 commits into
home-assistant:devfrom
azogue:i2c-sensor-bme280
Jun 21, 2017
Merged

Add I2c BME280 temperature, humidity and pressure sensor for Raspberry Pi #7989
pvizeli merged 10 commits into
home-assistant:devfrom
azogue:i2c-sensor-bme280

Conversation

@azogue
Copy link
Copy Markdown
Member

@azogue azogue commented Jun 10, 2017

Introduction:

Many of us use a Raspberry Pi to run Home Assistant, and we have different sensors connected to it, but for many of them there is no direct support, and it is necessary to use template sensors or command line sensors to make them work within Home Assistant.

Problem is using shell commands to retrieve the values of some sensors limits the working modes of these sensors, and, IMHO, it could be an entry barrier for some beginners.

I think that, as it exists for sensors like the DHT11/22 or the onewire ones, specific platforms for good and cheap digital sensors, with a simple configuration in Home Assistant, are very convenient.

This is the first of a series of PR that I intend to propose for supporting I2c digital sensors compatible with Raspberry Pi. A light level sensor (BH1750) and another temperature and humidity sensor (HTU21D) will be as follows.

Description:

Bosch BME280 Environment sensor support as a sensor platform, with multiple working modes as described in its data sheet.
Senses temperature, humidity and pressure.

The docs include a section to easy install the i2c and smbus support for the home_assistant user, to get things done quickly.

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#2797

Example entry for configuration.yaml (if applicable):

sensor:

  - platform: bme280

    name: Ambient


A more complex, and customised configuration could be:

# Example of customized configuration.yaml entry
sensor:
  - platform: bme280
    name: Ambient
    i2c_address: 0x77
    operation_mode: 2  # forced mode
    time_standby: 5
    oversampling_temperature: 4
    oversampling_pressure: 4
    oversampling_humidity: 4
    delta_temperature: -0.5
    monitored_conditions:
      - temperature
      - humidity
      - pressure
    scan_interval: 40

Checklist:

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

@mention-bot
Copy link
Copy Markdown

@azogue, thanks for your PR! By analyzing the history of the files in this pull request, we identified @balloob, @fabaff and @robbiet480 to be potential reviewers.

@fabaff
Copy link
Copy Markdown
Member

fabaff commented Jun 12, 2017

I think that i2c should be a component and the sensors (BMP280, BH1750, ADS1115, and alike) platforms. This way the platforms can share the logic for the bus and, just looking in the future, it could become possible to discover the available sensors on the bus and set them up automatically.

@azogue
Copy link
Copy Markdown
Member Author

azogue commented Jun 12, 2017

@fabaff, I thought about that structure, but I dismissed it by verifying that the common I2c bus logic is minimal compared to the specific logic required for each sensor.

Although the idea of running and parsing the output of i2cdetect in discovery mode to automatically configure detected sensors is attractive. The problem is what to do with the 'hats' that usually carry several sensors and are being treated as a single set...

So what do you think I should do now? I have the other 2 PRs ready, I was planning to propose them today. We can try switching to component i2c + platforms after integrating these, or do it sooner.

@azogue
Copy link
Copy Markdown
Member Author

azogue commented Jun 12, 2017

For the three sensors I'm working with (BME280, BH1750 and HTU21D), after getting the i2c bus:

import smbus
bus = smbus.SMBus(config.get(CONF_I2C_BUS))

The only common code relative to the i2c bus are the calls to:

bus.write_byte
bus.write_byte_data
bus.read_byte_data
bus.read_i2c_block_data
bus.read_word_data

Another thing I thought of is to share the bus handler in hass.data (also changing the module sensor.envirophat, to make the calls to the bus:

with hass.data['SMBUS'] as bus:
    bus.read_byte_data ...
    ...

I do not know if it's a good idea...

Copy link
Copy Markdown
Member

@pvizeli pvizeli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work but it is not allow to have proto specific code inside HomeAssistant. Please export this device specific code into a new mini library and use this for this sensor.

@azogue
Copy link
Copy Markdown
Member Author

azogue commented Jun 16, 2017

@pvizeli, thanks for your review.

There are already a lot of libraries to handle sensors on the I2c bus, some better and some worse, some more feature-complete and some simpler. For that reason, there are already multiple ways to integrate one of these sensors into Home Assistant, usually via command line sensors.

The reason for these PRs was to implement a simple, but feature-complete, and explicit way of including these sensors in HA, I was not aware of the prohibition of including 'proto specific code' (which I understand you mean the byte operations to talk to sensors, extracted from its spec sheets, right?)

I guess I can upload the nth I2c sensor library to pypi, and place the classes there to handle the sensors. Any suggestions for the name?

@azogue
Copy link
Copy Markdown
Member Author

azogue commented Jun 16, 2017

@pvizeli , I've done that. I uploaded a new library i2csense to pypi with the sensors code, and now I'm importing it from here. I've updated the requirements_all.txt with the new lib, but I'm not sure if the lib has to be commented there:

# homeassistant.components.sensor.bme280
i2csense==0.0.2

or

# homeassistant.components.sensor.bme280
# i2csense==0.0.2

like the smbus lib:

# homeassistant.components.sensor.bme280
# homeassistant.components.sensor.envirophat
# smbus-cffi==0.5.1

If you were so kind to review these changes, please...

I'm going to push the changes for the other PRs

@pvizeli
Copy link
Copy Markdown
Member

pvizeli commented Jun 19, 2017

Good work 👍

You can add this here: https://github.com/home-assistant/home-assistant/blob/dev/script/gen_requirements_all.py#L9

Please remove noinspection PyUnusedLocal and use pylint: disable=import-error on place where is needed.

@azogue
Copy link
Copy Markdown
Member Author

azogue commented Jun 19, 2017

Thanks for reviewing @pvizeli,

I will do the changes, but first I want to polish a little more the new library, so I will increase the version needed here and in the others PRs (the BH1750 light level sensor is giving me consistent but very low lux values, so I'm not sure about the implementation, I need to play with sensitivity values and low/high res modes to be sure). I hope this week will be done.

@azogue
Copy link
Copy Markdown
Member Author

azogue commented Jun 21, 2017

Hi @pvizeli, changes are done, and I've increased the version of the external lib to use. I think is ready to go.

The light sensors were working fine, in fact, this implementation is much more complete than the ones I found on the network for arduinos or in python. It was a problem of sensor location. I've added an optional light level multiplier parameter (from 0.1 to 10) for that sensor, so it's easy to correct the value for dark corners or bad angles.

I've pushed the changes to the others PR's: #8049 and #8050. Changes in script/gen_requirements_all.py and requirements_all.txt overlap, so, when the first PR merge, I'll rebase the others.

@pvizeli
Copy link
Copy Markdown
Member

pvizeli commented Jun 21, 2017

Amazing. I'll merge it after tests pass

@home-assistant home-assistant deleted a comment from houndci-bot Jun 21, 2017
@home-assistant home-assistant deleted a comment from houndci-bot Jun 21, 2017
@home-assistant home-assistant deleted a comment from houndci-bot Jun 21, 2017
@home-assistant home-assistant deleted a comment from houndci-bot Jun 21, 2017
@pvizeli pvizeli merged commit bb05600 into home-assistant:dev Jun 21, 2017
@pvizeli
Copy link
Copy Markdown
Member

pvizeli commented Jun 21, 2017

Please rebase others and add my last changes to that

@azogue azogue deleted the i2c-sensor-bme280 branch June 22, 2017 09:41
@balloob balloob mentioned this pull request Jul 1, 2017
dethpickle pushed a commit to dethpickle/home-assistant that referenced this pull request Aug 18, 2017
…y Pi (home-assistant#7989)

* Add new BME280 temperature, humidity and pressure sensor

* Add BME280 sensor to optional requirements and .coveragerc

* move validation to sensor handler, async fix in setup

* fix Invalid attribute name

* review changes: move sensor code to external module

* async fix

* add i2csense to COMMENT_REQUIREMENTS, require i2csense 0.0.3, round prec to 1 dec

* change style for hass

* fix lint

* fix lint part 2
@home-assistant home-assistant locked and limited conversation to collaborators Oct 20, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants