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.
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.
The chip operates in a simple way. Send a two-byte command. Optionally wait. Read raw bytes from the chip, calculate output values.
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.
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
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.
The code doesn't need any special compilation. A simple call will do: