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

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
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
Three Ways to Integrate LED Light Into the Modular Lantern

Three Ways to Integrate LED Light Into the Modular Lantern

After creating the modular lantern system, I experimented with different cheap ways to integrate LED lights into it and turn it into a decorative lamp. In this post, I describe the three ways I found ...
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
Build a Sustainable Refillable Active Coal Filter

Build a Sustainable Refillable Active Coal Filter

If you ever used air filters to keep smells, fumes and dust at a low level in your workshop, you for sure came in contact with these expensive throwaway active coal filters. While throwing-away the ...
Read More
The 3D Printed Modular Lantern

The 3D Printed Modular Lantern

I designed a very modular 19th-century-style lantern. You can print it in its simplest form as a simple candlelight to put on a table or a shelf. By printing additional elements, you create a wonderful ...
Read More

Leave a Reply Cancel reply

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

Stay Updated

Join me on Mastodon!

Top Posts & Pages

  • Storage Boxes System for 3D Print
  • Use Enum with More Class!
  • Simple Box with Lid for 3D-Print
  • Fan Controller
  • Shop
  • Real Time Counter and Integer Overflow
  • How and Why to use Namespaces
  • The Hinges and its Secrets for Perfect PETG Print
  • Extreme Integers – Doom from Below
  • Build a 3D Printer Enclosure

Latest Posts

  • 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
  • The 3D Printed Modular Lantern2023-01-17

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.