Lucky Resistor
Menu
  • Home
  • Learn
    • Learn C++
    • Product Photography for Electronics
      • Required Equipment and Software
    • Soldering for Show
  • Projects
  • Libraries
  • Applications
  • Shop
  • About
    • About Me
    • Contact
    • Stay Informed
  •  
Menu

“Read SHT31” Command-Line Tool

Posted on 2020-12-132020-12-13 by Lucky Resistor

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:

Read SHT31 on 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:

  • Adafruit Breakout
  • Breakout from DFRobot

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

Logic Gates Puzzle 101

The message you got does not make any sense. Can you decypher it with the following logic puzzle? The circuit was not optimised for functionality but to create a challenging puzzle and gorgeous artwork. If ...
Read More
Candlelight Emulation – Complexity with Layering

Candlelight Emulation – Complexity with Layering

In this blog post I explain the microcontroller firmware that emulates candlelight in more detail. You can apply the explained techniques in similar situations to get organic effects on CPU and RAM-limited platforms. I will ...
Read More
New Version 2 of the Pattern Generator

New Version 2 of the Pattern Generator

The latest release of the Pattern Generator software, version 2.0.4, introduces a multitude of exciting improvements and enhancements. From a completely revamped backend to a range of additional features and optimizations, this update aims to ...
Read More
The Importance of Wall Profiles in 3D Printing

The Importance of Wall Profiles in 3D Printing

In this post, I write about wall profiles in 3D printing, and I will explain why they are essential to creating stable parts with minimal filament use. Also, I will show common mistakes that result ...
Read More
Better Bridging with Slicer Guides

Better Bridging with Slicer Guides

I got questions about a particular feature you find if some of my 3D models. In this short text, I will explain why I add it and why you should add features like this too ...
Read More
Large Update to the Circle Pattern Generator

Large Update to the Circle Pattern Generator

Today I published a significant update to the circle pattern generator. Version 1.4.1 of the application is available for macOS and Windows for download. This new version adds various shapes, rotations, colours and a generator ...
Read More

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Stay Updated / Donate

Join me on Mastodon!

☕️ Support my Work

Top Posts & Pages

  • Real Time Counter and Integer Overflow
  • Storage Boxes System for 3D Print
  • Simple Box with Lid for 3D-Print
  • Logic Gates Puzzles
  • Circle Pattern Generator
  • Welcome to My Shop
  • Use Enum with More Class!
  • Circle Pattern Generator
  • Units of Measurements for Safe C++ Variables
  • Build a 3D Printer Enclosure

Latest Posts

  • New Version 2 of the Pattern Generator2023-07-25
  • The Importance of Wall Profiles in 3D Printing2023-02-12
  • The Hinges and its Secrets for Perfect PETG Print2023-02-07
  • Better Bridging with Slicer Guides2023-02-04
  • Stronger 3D Printed Parts with Vertical Perimeter Linking2023-02-02
  • Logic Gates Puzzle 1012023-02-02
  • Candlelight Emulation – Complexity with Layering2023-02-01
  • Three Ways to Integrate LED Light Into the Modular Lantern2023-01-29

Categories

  • 3D Printing
  • Build
  • Common
  • Fail
  • Fun
  • Learn
  • Projects
  • Puzzle
  • Recommendations
  • Request for Comments
  • Review
  • Software
Copyright (c)2022 by Lucky Resistor. All rights reserved.