Skip to main content

A library to handle i2c sensors with the Raspberry Pi

Project description

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

This library implements the following i2c sensors: - Bosch BME280 Environmental sensor (temperature, humidity and pressure) - HTU21D temperature and humidity sensor - BH1750FVI light level sensor

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.

  • v0.0.3: Minor fixes for BH1750, fix README.rst.

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

i2csense-0.0.4.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

i2csense-0.0.4-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file i2csense-0.0.4.tar.gz.

File metadata

  • Download URL: i2csense-0.0.4.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for i2csense-0.0.4.tar.gz
Algorithm Hash digest
SHA256 6f9c0a37d971e5b8a60c54982bd580cff84bf94fedc08c097e603a8e5609c33f
MD5 9ea49035340fa163741b43f14bfd5eb9
BLAKE2b-256 1aa33d5c3466836eb6833b79316654731191b029958fb40ab97e7370a2e9ebdf

See more details on using hashes here.

File details

Details for the file i2csense-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for i2csense-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 19b92e6334ffaf91a86e9ae75ca193a6617da07adb9aaf2f32ccf68b01330980
MD5 2618546b3244cf1f949fb320ce5b1487
BLAKE2b-256 f608c7a81dc1c773a5b7ef8ddaa5585f8166ed373c6336199ea1e6a1547d1ff0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page