DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
sht3x.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file sht3x.h
6 * @brief Sensirion SHT3x temperature / humidity sensor codec (DETWS_ENABLE_SHT3X).
7 *
8 * The SHT3x (SHT30 / SHT31 / SHT35) answers a single-shot measurement command with six bytes:
9 * a 16-bit temperature word + its CRC-8, then a 16-bit humidity word + its CRC-8. The CRC is
10 * the Sensirion CRC-8 (polynomial 0x31, init 0xFF, no reflection, no final XOR; the datasheet
11 * check value is 0xBEEF -> 0x92). Raw ticks convert linearly:
12 * T[C] = -45 + 175 * raw / 65535
13 * RH[%] = 100 * raw / 65535
14 *
15 * To stay heap- and float-printf-free, the results are returned as signed integer milli-units
16 * (milli-degrees C, milli-percent RH). The CRC check and the conversion are pure and
17 * host-tested; only the command write / data read touches I2C.
18 *
19 * A cheap solder-and-bench-test breakout (GY-SHT31 etc.): read it, bridge the reading onto the
20 * network as telemetry.
21 *
22 * @author Douglas Quigg (dstroy0)
23 * @date 2026
24 */
25
26#ifndef DETERMINISTICESPASYNCWEBSERVER_SHT3X_H
27#define DETERMINISTICESPASYNCWEBSERVER_SHT3X_H
28
29#include <stddef.h>
30#include <stdint.h>
31
32// Single-shot measurement commands (16-bit, sent most-significant byte first).
33#define SHT3X_CMD_SINGLE_HIGH 0x2400 ///< high repeatability, no clock stretching
34#define SHT3X_CMD_SINGLE_MED 0x240B ///< medium repeatability
35#define SHT3X_CMD_SINGLE_LOW 0x2416 ///< low repeatability
36#define SHT3X_CMD_SOFT_RESET 0x30A2 ///< soft reset
37#define SHT3X_CMD_READ_STATUS 0xF32D ///< read the status register
38#define SHT3X_CMD_HEATER_ON 0x306D ///< enable the on-chip heater
39#define SHT3X_CMD_HEATER_OFF 0x3066 ///< disable the on-chip heater
40
41/** @brief Sensirion CRC-8 (poly 0x31, init 0xFF) over @p len bytes. */
42uint8_t sht3x_crc8(const uint8_t *data, size_t len);
43
44/** @brief Convert a raw 16-bit temperature tick to milli-degrees Celsius. */
45int32_t sht3x_temp_mc(uint16_t raw);
46
47/** @brief Convert a raw 16-bit humidity tick to milli-percent relative humidity (0..100000). */
48int32_t sht3x_rh_mpct(uint16_t raw);
49
50/**
51 * @brief Decode a six-byte single-shot response (T msb/lsb/crc, RH msb/lsb/crc). Verifies both
52 * CRC-8 words, then fills @p temp_mc and @p rh_mpct (either may be null).
53 * @return false if a CRC does not match (a corrupt read).
54 */
55bool sht3x_parse(const uint8_t resp[6], int32_t *temp_mc, int32_t *rh_mpct);
56
57// --- ESP32 binding (I2C via Wire; no-ops on a host build) ------------------------------------
58
59/** @brief Soft-reset the SHT3x at @p addr over I2C. @return true if it acknowledged. */
60bool sht3x_begin(uint8_t addr);
61
62/**
63 * @brief Trigger a single-shot high-repeatability measurement, read + verify the six bytes, and
64 * return the temperature (milli-C) and humidity (milli-%RH). @return false on I2C or CRC error.
65 */
66bool sht3x_read(int32_t *temp_mc, int32_t *rh_mpct);
67
68#endif // DETERMINISTICESPASYNCWEBSERVER_SHT3X_H
int32_t sht3x_rh_mpct(uint16_t raw)
Convert a raw 16-bit humidity tick to milli-percent relative humidity (0..100000).
bool sht3x_parse(const uint8_t resp[6], int32_t *temp_mc, int32_t *rh_mpct)
Decode a six-byte single-shot response (T msb/lsb/crc, RH msb/lsb/crc). Verifies both CRC-8 words,...
int32_t sht3x_temp_mc(uint16_t raw)
Convert a raw 16-bit temperature tick to milli-degrees Celsius.
bool sht3x_begin(uint8_t addr)
Soft-reset the SHT3x at addr over I2C.
bool sht3x_read(int32_t *temp_mc, int32_t *rh_mpct)
Trigger a single-shot high-repeatability measurement, read + verify the six bytes,...
uint8_t sht3x_crc8(const uint8_t *data, size_t len)
Sensirion CRC-8 (poly 0x31, init 0xFF) over len bytes.