back to index

SHT3x-read


Why
How
      chip
      code
Usage
      options
      output
            examples
Compiling
Files
TODO

Why

The SHT30 is a fairly inexpensive I2C temperature and relative humidity sensor.

Python library is available.

For reading from shell scripts, especially on underpowered boards like Raspberry Pi 1, python is less suitable due to the overheads at process startup. Insane amount of files has to be read and interpreted. A frequently executed task will bring a significant load on the machine.


How

A simple C code was derived from the sw_chip_PCF8591 one.

As the sensor doesn't do conventional registers, and only bare reads and writes are used (so eg. i2cdump will show just all zeroes and confuse the register settings), the barest of interfacing will do. File and IOCTL access to /dev/i2c-something was therefore chosen.

chip

The chip operates in a simple way. Send a two-byte command. Optionally wait. Read raw bytes from the chip, calculate output values.

code

The code first opens the I2C device (or fails when device doesn't exist or is inaccessible). IOCTL I2C_SLAVE is executed on the resulting file handle, with the chip's address as parameter.

The command word is sent (code fails here if the chip is not on the given address).

Then several bytes are read. For the temp/humi values, it is six bytes, with two bytes for temperature (MSB+LSB) and third for CRC, then two bytes for humidity (MSB+LSB) and third for CRC. The CRC is at this moment ignored.


Usage

options

If executed without options, it queries first SHT30 sensor on the /dev/i2c-1 bus. This is a default configuration on raspberry pi boards.

SHT3x sensor read
Usage: sht30 [-h<0|1>] [-i] [-r<h|t|ht>] [-j] [-s] [-sc] [-a0] [-a1] [-d <dev>] [-v] 
measure:
  -i        integer mode (no float)
  -rh       output single-line humidity
  -rt       output single-line temperature
  -rht      output single-line humidity and temperature
  -j        JSON format
  -nr       do not read humidity/temp
chip setting:
  -h1       on-chip heater enable
  -h0       on-chip heater disable
  -s        read status word
  -sc       clear status word
  -R        reset chip
I2C:
  -a0       address ADDR=L (0x44, default)
  -a1       address ADDR=H (0x45)
  -d <dev>  specify I2C device, default /dev/i2c-1
general:
  -v        verbose mode
  -h,--help this help

output

If all is okay, result code is 0. Otherwise, if something crashes, result is 1.

If not verbose or help, two lines with human-readable data go to stdout; or only one or both values.

examples

./sht30
Humidity : 39.40 %
Temperature: 23.90 'C

 

./sht30 -rt -i
24
 
./sht30 -j
{"humi":39.43,"temp":23.90}
 
./sht30 -nr -s
Status: 0000
pending alert : false
heater enabled : false
humi alert : false
temp alert : false
read periodic : false
reset detect : false
command fail : false
checksum fail : false


Compiling

The code doesn't need any special compilation. A simple call will do:

or, for statically linked version,


Files


TODO


If you have any comments or questions about the topic, please let me know here:
Your name:
Your email:
Spambait
Leave this empty!
Only spambots enter stuff here.
Feedback: