[−][src]Crate shtcx
Introduction
This is a platform agnostic Rust driver for the Sensirion SHTCx temperature /
humidity sensor series, based on the
embedded-hal
traits.
Supported Devices
Tested with the following sensors:
The following sensors were not tested, but should work out-of-the-box:
Blocking / Non-Blocking Modes
This driver currently uses only blocking calls. Non-blocking measurements may be added in the future. Clock stretching is not implemented and probably won't be.
Examples
There are a few examples in the examples
directory: The linux-<target>
example queries the sensor a few times using linux-embedded-hal
, while
the monitor-<target>
example implements a terminal based real-time
graphical temperature/humidity monitoring tool.
Usage
Setup
Instantiate a new driver instance using a blocking I²C HAL
implementation
and a blocking Delay
instance.
For example, using linux-embedded-hal
and an SHTC3 sensor:
use linux_embedded_hal::{Delay, I2cdev}; use shtcx; let dev = I2cdev::new("/dev/i2c-1").unwrap(); let mut sht = shtcx::shtc3(dev, Delay);
Device Info
Then, you can query information about the sensor:
let device_id = sht.device_identifier().unwrap(); let raw_id = sht.raw_id_register().unwrap();
Measurements
For measuring your environment, you can either measure just temperature, just humidity, or both:
use shtcx::PowerMode; let temperature = sht.measure_temperature(PowerMode::NormalMode).unwrap(); let humidity = sht.measure_humidity(PowerMode::NormalMode).unwrap(); let combined = sht.measure(PowerMode::NormalMode).unwrap(); println!("Temperature: {} °C", temperature.as_degrees_celsius()); println!("Humidity: {} %RH", humidity.as_percent()); println!("Combined: {} °C / {} %RH", combined.temperature.as_degrees_celsius(), combined.humidity.as_percent());
You can also use the low power mode for less power consumption, at the cost of reduced repeatability and accuracy of the sensor signals. For more information, see the "Low Power Measurement Mode" application note.
let measurement = sht.measure(PowerMode::LowPower).unwrap();
Low Power Mode
Some of the sensors (e.g. the SHTC3, but not the SHTC1) support a low power mode, where the sensor can be set to sleep mode when in idle state.
For this, the LowPower
trait needs to be imported:
use shtcx::LowPower;
Then you can send the sensor to sleep and wake it up again before triggering a new measurement:
sht.sleep().unwrap(); // ... sht.wakeup().unwrap();
Invoking any command other than
wakeup
while the sensor is in
sleep mode will result in an error.
Soft Reset
The SHTCx provides a soft reset mechanism that forces the system into a well-defined state without removing the power supply. If the system is in its idle state (i.e. if no measurement is in progress) the soft reset command can be sent. This triggers the sensor to reset all internal state machines and reload calibration data from the memory.
sht.reset().unwrap();
Generic Driver
The shtcx
driver supports use cases where the exact model of the sensor
is not known in advance. In that case, use the generic
factory function to create an instance of the driver that supports all
features available in all supported sensor types.
Note however that sending commands to sensors that don't implement them
(e.g. sending a sleep
-command to an
SHTC1 sensor) will result in a runtime error. Furthermore, maximal timing
tolerances will be ensured, so using the generic driver with the SHTC3 will
result in slightly slower measurements (and slightly higher power
consumption) than when using the SHTC3 specific driver.
Modules
sensor_class | Type parameters for the different sensor classes. |
Structs
Humidity | A humidity measurement. |
Measurement | A combined temperature / humidity measurement. |
ShtCx | Driver for the SHTCx sensor. |
Temperature | A temperature measurement. |
Enums
Error | All possible errors in this crate |
PowerMode | Measurement power mode: Normal mode or low power mode. |
Traits
LowPower | Low power functionality (sleep and wakeup). |
ShtSensor | Marker trait implemented for all supported sensor classes. |
Functions
generic | Create a new generic instance of the driver. |
shtc1 | Create a new instance of the driver for the SHTC1. |
shtc3 | Create a new instance of the driver for the SHTC3. |
shtw2 | Create a new instance of the driver for the SHTW2. |