Skip to main content

A pure Python library for some I2C sensors.

Project description

Pure Python-based I2C sensor library for some I2C sensors. Work in progress. Nothing serious, just a hobby.

Currently, the library supports the following sensors:

  • am2315/am2321

  • bh1750

  • bmp280/bmp180/bmp85

  • bme280

  • hdc1008

  • lm75

  • mcp9808

  • mpl115a2

  • mpl311a2

  • sht31

  • si7021

Installation

Before you can install senlib, you should install first pipenv using the package manager of your operating system.

On Arch Linux run the following command to install pipenv:

sudo pacman -S python-pipenv

Next, clone this repo and cd into the senlib directory:

git clone https://github.com/lexruee/senlib
cd senlib

Next, create a Python 3 environment as shown below:

pipenv --three

Lastly, install the package:

pipenv install -e .

Testing/Development

For installation see section Installation.

To run the tests use:

pipenv run pytest tests

or cd into the directory and run:

pipenv shell && pytest tests

CLI

sennode

sennode --help
Usage: sennode [OPTIONS] SENSOR

Options:
  --address TEXT              I2C address of the sensor.
  --node TEXT                 Node name.
  --http                      Start Web API.
  --http-host TEXT            HTTP host to use.
  --http-port INTEGER         HTTP port to use.
  -t, --mqtt-topic TEXT       MQTT topic to use.
  -L, --mqtt-broker-url TEXT  MQTT broker to publish data.
  -i, --interval FLOAT        Sampling interval.
  --mock, --fake, --simulate  Mock sensor.
  -d, --debug                 Debug mode.
  -V, --version               Show version.
  --help                      Show this message and exit.

Example Usage

sennode bme280 --address 'i2c://1/0x77' --mqtt-broker-url mqtts://user:password@mqtt.xyz.ch:8883

Once started, the sensor data is exposed over an HTTP interface and a WebSocket interface:

Besides that, the sensor data is perodically pushlished to the specified MQTT broker url.

senlib

senlib --help
Usage: senlib [OPTIONS] SENSOR

Options:
  --address TEXT              I2C address of the sensor.
  -p, --poll INTEGER          Polls the sensor.
  -i, --interval FLOAT        Sampling interval.
  --output FILENAME           Save output to a file.
  -f, --format TEXT           Output format to use.
  --mock, --fake, --simulate  Mock sensor.
  -d, --debug                 Debug mode.
  --pretty-print, --pp        Pretty printing.
  -V, --version               Show version.
  --help                      Show this message and exit.

Example Usage

senlib bme280 --address 'i2c://1/0x77'

Code Examples

For details and examples have a look in the examples or tests directory.

Basic code example:

#!/usr/bin/env python3
from senlib.core.i2c import SMBus
from senlib.i2c.sensors.bmex import BME280
import time

if __name__ == '__main__':
    bus = SMBus(bus=1)
    sensor = BME280(bus=bus, addr=0x77)
    for _ in range(0, 5):
       time.sleep(0.2)
       data = sensor.measure()
       for key, value in data.items():
           print("{}:{:0.4f}".format(key, value))

    sensor.close()

The shorter example uses the with syntax to automatically close the sensor object:

#!/usr/bin/env python3
from senlib.core.i2c import SMBus
from senlib.i2c.sensors.bmex import BME280
import time

if __name__ == '__main__':
    bus = SMBus(bus=1)
    with BME280(bus=bus, addr=0x77) as sensor:
       for _ in range(0, 5):
           time.sleep(0.2)
           data = sensor.measure()
           for key, value in data.items():
               print("{}:{:0.4f}".format(key, value))

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

senlib-0.1.0.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

senlib-0.1.0-py2.py3-none-any.whl (36.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file senlib-0.1.0.tar.gz.

File metadata

  • Download URL: senlib-0.1.0.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for senlib-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7c0febb6ee1851f2e15fd376f5fa349a1632afc7fd5b22516bac70fd25fe3f37
MD5 6e43ff351b0b8f27623defd9f695f917
BLAKE2b-256 05d180ea88ab14f6224e50f0a84206935e83e96f4a5dfe8ff954537854a0a3c6

See more details on using hashes here.

File details

Details for the file senlib-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: senlib-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 36.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for senlib-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 50e18bf89c9eb6fa2f61b691df75a8e08b7867daefb9f8ef14d8c0ae27e4e1f7
MD5 1c25fbf9d662d74cf8d4fb9c64e94c19
BLAKE2b-256 ddc89f59ef38c42c7a2b429a78cf9f88aacb67672a09851126a4fb1d827da2b2

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