i2csense

A library to handle i2c sensors with the Raspberry Pi


Keywords
raspberry, i2c, sensors, python3, bme280, bh1750, htu21d, home-automation, i2c-bus, i2c-sensors, raspberry-pi, smbus
License
MIT
Install
pip install i2csense==0.0.4

Documentation

i2csense

Another library to handle sensors connected via I2c bus (SDA, SCL pins) to your Raspberry Pi.

This library implements the following i2c sensors:

Installation

This library needs the i2c bus to be enabled, and uses the smbus-cffi module to communicate with it, so, before installing with pip, make sure the bus is enabled, and the necessary dependencies are available:

Directions for installing smbus support on Raspberry Pi:

Enable I2c interface with the Raspberry Pi config utility:

# Enable i2c interface
sudo raspi-config

Select Interfacing options->I2C choose <Yes> and hit Enter, then go to Finish and you'll be prompted to reboot.

Install dependencies for use the smbus-cffi module and reboot:

sudo apt-get install build-essential libi2c-dev i2c-tools python-dev libffi-dev
sudo reboot
Check the i2c address of the sensors

After installing i2c-tools, a new utility is available to scan the addresses of the connected sensors:

/usr/sbin/i2cdetect -y 1

It will output a table like this:

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- 23 -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- 77

So you can see three sensors are present at 0x23 (BH1750), 0x40 (HTU21D) and 0x77 (BME280) addresses.

Install with pip

Finally, in your python environment,

pip install i2csense

Usage

This library is intended to be used by other applications, where sensors are instantiated with their configuration parameters, to be read at the desired intervals. However, if you want to use as a simple logger via command line, a simple CLI is also available to test the sensors.

import smbus
from i2csense.bme280 import BME280

bus = smbus.Bus(1)
sensor = BME280(bus)
delta_secs = 5

while True:
    sensor.update()
    if not sensor.sample_ok:
        print("An error has occured")
        break
    print(sensor.current_state_str)
    sleep(delta_secs)

CLI usage

Find sensors:

i2csense
# or specify the i2c bus:
i2csense -b 0

Test sensors:

# Test BME280 sensor with default params:
i2csense -s bme280

# Test BME280 sensor with custom params every 10 secs:
i2csense -d 10 --bus 0 --address 0x77 --sensor bme280 --params osrs_t=4 osrs_p=4 osrs_h=4 mode=2 filter_mode=1

Changelog

  • v0.0.1: First release with 3 sensors: BME280, BH1750, HTU21D.
  • v0.0.2: Minor fixes.

TODO:

  • Append more sensors.
  • finish CLI interface with better help and more configuration options.

Although the library only covers three sensors, it would be ideal to continue completing it with more sensors or actuators running in the i2c bus, so I encourage you to contribute with more sensors, or to copy, change, edit, or suggest any changes.