embedded_dht_rs/
dht20.rs

1use embedded_hal::{delay::DelayNs, i2c::I2c};
2
3use crate::{SensorError, SensorReading};
4
5pub struct Dht20<I: I2c, D: DelayNs> {
6    pub i2c: I,
7    pub delay: D,
8}
9
10impl<I: I2c, D: DelayNs> Dht20<I, D> {
11    const SENSOR_ADDRESS: u8 = 0x38;
12
13    pub fn new(i2c: I, delay: D) -> Self {
14        Self { i2c, delay }
15    }
16
17    pub fn read(&mut self) -> Result<SensorReading<f32>, SensorError> {
18        // Check status
19        let mut status_response: [u8; 1] = [0; 1];
20        let _ = self
21            .i2c
22            .write_read(Self::SENSOR_ADDRESS, &[0x71], &mut status_response);
23
24        // Callibration if needed
25        if status_response[0] & 0x18 != 0x18 {
26            let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0x1B, 0, 0]);
27            let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0x1C, 0, 0]);
28            let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0x1E, 0, 0]);
29        }
30
31        // Trigger the measurement
32        self.delay.delay_ms(10);
33        let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0xAC, 0x33, 0x00]);
34
35        // Read the measurement status
36        self.delay.delay_ms(80);
37        loop {
38            let mut measurement_status_response: [u8; 1] = [0; 1];
39            let _ = self
40                .i2c
41                .read(Self::SENSOR_ADDRESS, &mut measurement_status_response);
42            let status_word = measurement_status_response[0];
43            if status_word & 0b1000_0000 == 0 {
44                break;
45            }
46            self.delay.delay_ms(1);
47        }
48
49        // Read the measurement (1 status + 5 data + 1 crc)
50        let mut measurement_response: [u8; 7] = [0; 7];
51        let _ = self
52            .i2c
53            .read(Self::SENSOR_ADDRESS, &mut measurement_response);
54
55        // Humidity 20 bits (8 + 8 + 4)
56        let mut raw_humidity = measurement_response[1] as u32;
57        raw_humidity = (raw_humidity << 8) + measurement_response[2] as u32;
58        raw_humidity = (raw_humidity << 4) + (measurement_response[3] >> 4) as u32;
59        let humidity_percentage = (raw_humidity as f32 / ((1 << 20) as f32)) * 100.0;
60
61        // Temperature 20 bits
62        let mut raw_temperature = (measurement_response[3] & 0b1111) as u32;
63        raw_temperature = (raw_temperature << 8) + measurement_response[4] as u32;
64        raw_temperature = (raw_temperature << 8) + measurement_response[5] as u32;
65        let temperature_percentage = (raw_temperature as f32 / ((1 << 20) as f32)) * 200.0 - 50.0;
66
67        // Compare the calculated CRC with the received CRC
68        let data = &measurement_response[..6];
69        let received_crc = measurement_response[6];
70        let calculcated_crc = Self::calculate_crc(data);
71        if received_crc != calculcated_crc {
72            return Err(SensorError::ChecksumMismatch);
73        }
74
75        Ok(SensorReading {
76            humidity: humidity_percentage,
77            temperature: temperature_percentage,
78        })
79    }
80
81    fn calculate_crc(data: &[u8]) -> u8 {
82        let polynomial = 0x31u8; // x^8 + x^5 + x^4 + 1
83        let mut crc = 0xFFu8;
84
85        for &byte in data {
86            crc ^= byte;
87            // CRC8 - process every bit
88            for _ in 0..8 {
89                if crc & 0x80 != 0 {
90                    crc = (crc << 1) ^ polynomial;
91                } else {
92                    crc <<= 1;
93                }
94            }
95        }
96
97        crc
98    }
99}