blog.lexruee.ch
Yet another blog on the Web.

A couple of months ago I started to write a small and straightforward sensor library for the I2C sensors that I already bought and used in my sensor-based projects at home.

The library ships two CLI scripts for retrieving and exposing sensor data. The first one allows one to retrieve sensor data via the terminal. The other one gives one the possibilty to launch a sensor node that exposes the sensor via an HTTP and a WebSocket interface.

As of today, the library provides support for the following popular I2C sensors:

  • am2315/am2321
  • bh1750
  • bmp280/bmp180/bmp85
  • bme280
  • hdc1008
  • lm75
  • mcp9808
  • mpl115a2
  • mpl311a2
  • sht31
  • si7021

The sourcecode of the project can be found on github.

Installation

Installing it is just a matter of executing the following commands:

git clone https://github.com/lexruee/senlib
cd senlib
./setup.py install

Usage

Assumed you have a BME280 sensor and all what you need is a simple sensor node whose sensor data can be accessed over an HTTP or a WebSocket interface, then you can just use the CLI program sennnode. The listing below shows how a sensor node can be launched. The option -a takes the I2C address of the sensor, while the option -b takes the I2C bus number on which the sensor is connected to.

sennode bme280 -a 0x77 -b 1

Once started, the sensor data is exposed under the following URLs:

  • http://0.0.0.0:8080
  • ws://0.0.0.0:8080

But there is more. If you like to use the MQTT protocol, then you can simply configure the sensor node to publish periodically sensor data to a MQTT broker.

As the listing below illustrates, you can provide the options --mqtt-address and --mqtt-port to indicate the URL of the MQTT broker and MQTT port, respectively. Last but not least, the option -i is used for specifying the periodic interval in seconds to publish the data and the option -t is used for indicating the MQTT topic under which the data is published.

sennode bme280 -a 0x77 -b 1 --mqtt-address ws://poseidon.local --mqtt-port 9001 -i 5 -t 'home/og1/bme280'

Next, we have the CLI program called senlib. The latter is just a basic script that allows one to retreive in a simple way sensor data.

For example, to poll the sensor periodically every second, we can take advantage of the command below. Just as before, we have the options -a and -b. Additonally, we have the option -i for specyfing the polling interval in seconds and the option -p to launch the program in polling mode.

senlib bme280 -a 0x77 -b 1 -p -i 1

That's it. So far this small library has been helpful for me in collecting temperature measurments and plotting them using matplotlib.

Plot

© Alexander Rüedlinger, 2017-2018
Proudly powered by Pelican, which takes great advantage of Python.