For various sensors, attached to Raspberry-Pi computers, I wrote small command-line tools to read the sensor values over the I2C connection. While you can do this with a Python script directly, I found using a compiled C or C++ program accessing the low-level device more reliable.
The program I publish today, reads temperature and humidity data from a SHT31 sensor. It outputs a simple JSON document you can parse in your script.
Usage
There are a number of arguments you can use:
Usage: read_sht31 [-h][-v] -h|--help Display this help. -s Get the sensor status. -t1 -t0 Enable/disable heater. -a0 -a1 Select the chip address. 0 is the default. -b0 -b1 Select the bus. 1 is the default. -d Show debugging messages.
If you call the command, you will get JSON output:
$ read_sht32 { "temperature_celsius": 24.8, "relative_humidity": 28.4 }
The idea is to call this command from your script and parse the returned JSON output.
Compile and Install
In order to compile and install the tool on your Raspberry-Pi, you need to install the compiler, CMake and the I2C-Tools first:
sudo apt install gcc cmake i2c-tools
Download the files of this git repository to a subdirectory in your home directory. In this example, I downloaded the files to the directory /home/pi/read_sht31
.
Now create a separate build directory. In this example, I create the directory /home/pi/build_read_sht31
.
Next switch to the build directory, execute cmake
to create the build script. Build the executable with make
and install the executable in your system with sudo make install
.
cd build_read_sht31 cmake ../read_sht31 make sudo make install
Download
Download or clone the files from GitHub:
License
The code is licensed under the GPL license version 3.
Available Sensors
The SHT31 sensor is available from various sources as breakout board. Here two examples:
Conclusion
Hopefully this small tool will help with your next Raspberry-Pi project. 😄
If you have any questions, missed information, or simply want to provide feedback, feel free to comment below or contact me on Twitter. 😄
More Posts

Logic Gates Puzzle 101

Candlelight Emulation – Complexity with Layering

New Version 2 of the Pattern Generator

The Importance of Wall Profiles in 3D Printing

Better Bridging with Slicer Guides
