├── .github └── workflows │ └── rust.yml ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md ├── docs ├── dht11_22.md ├── dht11_22_step2.png ├── dht11_22_step3.png ├── dht11_22_step4.png ├── dht11_22_steps.png ├── dht20.md ├── dht20_step2.png ├── dht20_step4.png ├── dht20_steps.png ├── example_esp32_dht_running.png ├── example_esp32_schematic.png └── example_esp32_wired.jpg ├── examples ├── .cargo │ └── config.toml ├── .gitignore ├── .vscode │ └── settings.json ├── Cargo.lock ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── build.rs ├── rust-toolchain.toml └── src │ └── main.rs └── src ├── dht.rs ├── dht11.rs ├── dht20.rs ├── dht22.rs └── lib.rs /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | name: Rust 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | env: 10 | CARGO_TERM_COLOR: always 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v4 19 | 20 | - name: Build 21 | run: cargo build --verbose 22 | 23 | - name: Run tests 24 | run: cargo test --verbose 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "autocfg" 7 | version = "1.4.0" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" 10 | 11 | [[package]] 12 | name = "embedded-dht-rs" 13 | version = "0.4.0" 14 | dependencies = [ 15 | "embedded-hal 1.0.0", 16 | "embedded-hal-mock", 17 | ] 18 | 19 | [[package]] 20 | name = "embedded-hal" 21 | version = "0.2.7" 22 | source = "registry+https://github.com/rust-lang/crates.io-index" 23 | checksum = "35949884794ad573cf46071e41c9b60efb0cb311e3ca01f7af807af1debc66ff" 24 | dependencies = [ 25 | "nb 0.1.3", 26 | "void", 27 | ] 28 | 29 | [[package]] 30 | name = "embedded-hal" 31 | version = "1.0.0" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | checksum = "361a90feb7004eca4019fb28352a9465666b24f840f5c3cddf0ff13920590b89" 34 | 35 | [[package]] 36 | name = "embedded-hal-mock" 37 | version = "0.11.1" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | checksum = "f9a0f04f8886106faf281c47b6a0e4054a369baedaf63591fdb8da9761f3f379" 40 | dependencies = [ 41 | "embedded-hal 0.2.7", 42 | "embedded-hal 1.0.0", 43 | "embedded-hal-nb", 44 | "embedded-time", 45 | "nb 1.1.0", 46 | "void", 47 | ] 48 | 49 | [[package]] 50 | name = "embedded-hal-nb" 51 | version = "1.0.0" 52 | source = "registry+https://github.com/rust-lang/crates.io-index" 53 | checksum = "fba4268c14288c828995299e59b12babdbe170f6c6d73731af1b4648142e8605" 54 | dependencies = [ 55 | "embedded-hal 1.0.0", 56 | "nb 1.1.0", 57 | ] 58 | 59 | [[package]] 60 | name = "embedded-time" 61 | version = "0.12.1" 62 | source = "registry+https://github.com/rust-lang/crates.io-index" 63 | checksum = "d7a4b4d10ac48d08bfe3db7688c402baadb244721f30a77ce360bd24c3dffe58" 64 | dependencies = [ 65 | "num", 66 | ] 67 | 68 | [[package]] 69 | name = "nb" 70 | version = "0.1.3" 71 | source = "registry+https://github.com/rust-lang/crates.io-index" 72 | checksum = "801d31da0513b6ec5214e9bf433a77966320625a37860f910be265be6e18d06f" 73 | dependencies = [ 74 | "nb 1.1.0", 75 | ] 76 | 77 | [[package]] 78 | name = "nb" 79 | version = "1.1.0" 80 | source = "registry+https://github.com/rust-lang/crates.io-index" 81 | checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d" 82 | 83 | [[package]] 84 | name = "num" 85 | version = "0.3.1" 86 | source = "registry+https://github.com/rust-lang/crates.io-index" 87 | checksum = "8b7a8e9be5e039e2ff869df49155f1c06bd01ade2117ec783e56ab0932b67a8f" 88 | dependencies = [ 89 | "num-complex", 90 | "num-integer", 91 | "num-iter", 92 | "num-rational", 93 | "num-traits", 94 | ] 95 | 96 | [[package]] 97 | name = "num-complex" 98 | version = "0.3.1" 99 | source = "registry+https://github.com/rust-lang/crates.io-index" 100 | checksum = "747d632c0c558b87dbabbe6a82f3b4ae03720d0646ac5b7b4dae89394be5f2c5" 101 | dependencies = [ 102 | "num-traits", 103 | ] 104 | 105 | [[package]] 106 | name = "num-integer" 107 | version = "0.1.46" 108 | source = "registry+https://github.com/rust-lang/crates.io-index" 109 | checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" 110 | dependencies = [ 111 | "num-traits", 112 | ] 113 | 114 | [[package]] 115 | name = "num-iter" 116 | version = "0.1.45" 117 | source = "registry+https://github.com/rust-lang/crates.io-index" 118 | checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" 119 | dependencies = [ 120 | "autocfg", 121 | "num-integer", 122 | "num-traits", 123 | ] 124 | 125 | [[package]] 126 | name = "num-rational" 127 | version = "0.3.2" 128 | source = "registry+https://github.com/rust-lang/crates.io-index" 129 | checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07" 130 | dependencies = [ 131 | "autocfg", 132 | "num-integer", 133 | "num-traits", 134 | ] 135 | 136 | [[package]] 137 | name = "num-traits" 138 | version = "0.2.19" 139 | source = "registry+https://github.com/rust-lang/crates.io-index" 140 | checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" 141 | dependencies = [ 142 | "autocfg", 143 | ] 144 | 145 | [[package]] 146 | name = "void" 147 | version = "1.0.2" 148 | source = "registry+https://github.com/rust-lang/crates.io-index" 149 | checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" 150 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "embedded-dht-rs" 3 | version = "0.5.0" 4 | edition = "2021" 5 | license = "Apache-2.0" 6 | description = "A driver for interfacing with DHT11, DHT20 (AHT20), DHT22 (AM2302) temperature and humidity sensors, designed for embedded systems." 7 | homepage = "https://github.com/rust-dd/embedded-dht-rs" 8 | documentation = "https://docs.rs/embedded-dht-rs" 9 | repository = "https://github.com/rust-dd/embedded-dht-rs" 10 | readme = "README.md" 11 | keywords = ["embedded", "no-std", "dht11", "dht20", "dht22"] 12 | autoexamples = false 13 | 14 | [dependencies] 15 | embedded-hal = "1.0.0" 16 | 17 | [dev-dependencies] 18 | embedded-hal-mock = "0.11.1" 19 | 20 | [features] 21 | dht11 = [] 22 | dht20 = [] 23 | dht22 = [] 24 | default = [] 25 | 26 | [lib] 27 | doctest = false 28 | 29 | [profile.release] 30 | codegen-units = 1 31 | lto = true 32 | 33 | [package.metadata.docs.rs] 34 | # build docs for all features 35 | all-features = true 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![build workflow](https://github.com/rust-dd/embedded-dht-rs/actions/workflows/rust.yml/badge.svg) 2 | [![Crates.io](https://img.shields.io/crates/v/embedded-dht-rs?style=flat-square)](https://crates.io/crates/embedded-dht-rs) 3 | ![Crates.io](https://img.shields.io/crates/l/embedded-dht-rs?style=flat-square) 4 | [![API](https://docs.rs/embedded-dht-rs/badge.svg)](https://docs.rs/embedded-dht-rs) 5 | 6 | # embedded-dht-rs 7 | 8 | `embedded-dht-rs` is a Rust library designed to simplify interfacing with DHT sensors in embedded systems. 9 | 10 | This library is `#![no_std]` and depends only on `embedded_hal`, making it versatile and compatible with virtually any microcontroller. 11 | 12 | **Support for DHT11, DHT20, and DHT22 Sensors**: All three sensors are fully implemented and ready for use. 13 | 14 | The library has been tested with the ESP32-WROOM, and a detailed example is provided below to help you get started. 15 | 16 | ## Getting Started 17 | 18 | ### Tutorials 19 | 20 | Here are some general tutorials that provide brief introductions to embedded programming: 21 | 22 | - **Part 1 (Introduction)** - [Introduction to Embedded Systems with Rust: A Beginner's Guide Using ESP32](https://rust-dd.com/post/introduction-to-embedded-systems-with-rust-a-beginner-s-guide-using-esp32) 23 | - **Part 2 (LED + Button)** - [Building a Simple LED and Button Interface with Rust on ESP32](https://rust-dd.com/post/building-a-simple-led-and-button-interface-with-rust-on-esp32) 24 | - **Part 3 (DHT11 Library)** - [Building a Rust library for DHT11 sensor](https://rust-dd.com/post/building-a-rust-library-for-dht11-sensor-a-step-by-step-guide) 25 | 26 | 27 | ### Install 28 | 29 | 30 | To include the `dht11` feature: 31 | 32 | ```rust 33 | cargo add embedded-dht-rs --features "dht11" 34 | ``` 35 | 36 | To include all features (`dht11`, `dht20`, and `dht22`): 37 | 38 | ```rust 39 | cargo add embedded-dht-rs --features "dht11,dht20,dht22" 40 | ``` 41 | 42 | ### Example - ESP32 43 | 44 | You can find the full example in the [examples](./examples) folder. 45 | 46 | ```rust 47 | #![no_std] 48 | #![no_main] 49 | 50 | use embedded_dht_rs::{dht11::Dht11, dht20::Dht20, dht22::Dht22}; 51 | use esp_backtrace as _; 52 | use esp_hal::{ 53 | clock::ClockControl, delay::Delay, gpio::{Io, Level, OutputOpenDrain, Pull}, i2c::I2C, peripherals::Peripherals, prelude::*, system::SystemControl 54 | }; 55 | use fugit::HertzU32; 56 | 57 | #[entry] 58 | fn main() -> ! { 59 | let peripherals = Peripherals::take(); 60 | let system = SystemControl::new(peripherals.SYSTEM); 61 | let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); 62 | let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); 63 | 64 | esp_println::logger::init_logger_from_env(); 65 | 66 | let delay = Delay::new(&clocks); 67 | 68 | let od_for_dht11 = OutputOpenDrain::new(io.pins.gpio4, Level::High, Pull::None); 69 | let od_for_dht22 = OutputOpenDrain::new(io.pins.gpio5, Level::High, Pull::None); 70 | let i2c_for_dht20 = I2C::new( 71 | peripherals.I2C0, 72 | io.pins.gpio21, 73 | io.pins.gpio22, 74 | HertzU32::kHz(400), 75 | &clocks 76 | ); 77 | 78 | let mut dht11 = Dht11::new(od_for_dht11, delay); 79 | let mut dht22 = Dht22::new(od_for_dht22, delay); 80 | let mut dht20 = Dht20::new(i2c_for_dht20, delay); 81 | 82 | loop { 83 | delay.delay(5000.millis()); 84 | 85 | match dht11.read() { 86 | Ok(sensor_reading) => log::info!( 87 | "DHT 11 Sensor - Temperature: {} °C, humidity: {} %", 88 | sensor_reading.temperature, 89 | sensor_reading.humidity 90 | ), 91 | Err(error) => log::error!("An error occurred while trying to read sensor: {:?}", error), 92 | } 93 | 94 | match dht22.read() { 95 | Ok(sensor_reading) => log::info!( 96 | "DHT 22 Sensor - Temperature: {} °C, humidity: {} %", 97 | sensor_reading.temperature, 98 | sensor_reading.humidity 99 | ), 100 | Err(error) => log::error!("An error occurred while trying to read sensor: {:?}", error), 101 | } 102 | 103 | match dht20.read() { 104 | Ok(sensor_reading) => log::info!( 105 | "DHT 20 Sensor - Temperature: {} °C, humidity: {} %", 106 | sensor_reading.temperature, 107 | sensor_reading.humidity 108 | ), 109 | Err(error) => log::error!("An error occurred while trying to read sensor: {:?}", error), 110 | } 111 | 112 | log::info!("-----"); 113 | } 114 | } 115 | ``` 116 | 117 | ![running](https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/refs/heads/main/docs/example_esp32_dht_running.png) 118 | 119 | 120 | ## Implementation Specification 121 | 122 | We have gathered all the information you need to understand in order to implement a library like this. Additionally, we’ve included a few comments in the code for those curious about the details, based on the following specification. 123 | 124 | The DHT20 differs from the DHT11 and DHT22 because it uses the I2C communication protocol, while both the DHT11 and DHT22 rely on a single-wire signal for data transmission. 125 | 126 | 127 | - [DHT11 and DHT22 Documentation](https://github.com/rust-dd/embedded-dht-rs/blob/main/docs/dht11_22.md) 128 | - [DHT20 Documentation](https://github.com/rust-dd/embedded-dht-rs/blob/main/docs/dht20.md) 129 | 130 | 131 | 132 | ## Comparison of DHT11, DHT20, and DHT22 40-Bit Data Formats 133 | 134 | | Feature | DHT11 | DHT20 | DHT22 | 135 | |-----------------------|----------------------------------------------------|--------------------------------------------------------|---------------------------------------------------------| 136 | | **Data Structure** | - Byte 1: Humidity Int
- Byte 2: Humidity Dec (0)
- Byte 3: Temp Int
- Byte 4: Temp Dec (0)
- Byte 5: Checksum | - Byte 1: Humidity High
- Byte 2: Humidity Low
- Byte 3: Temp High
- Byte 4: Temp Low
- Byte 5: CRC | - Byte 1: Humidity High
- Byte 2: Humidity Low
- Byte 3: Temp High
- Byte 4: Temp Low
- Byte 5: Checksum | 137 | | **Precision** | Integer only | Higher precision with decimals | Higher precision with decimals | 138 | | **Example Temp** | 25°C | 25.6°C | 25.6°C | 139 | | **Example Humidity** | 60% | 60.5% | 60.5% | 140 | | **Example Data Bytes** | `60, 0, 25, 0, 85` | `2, 93, 1, 0, CRC` | `2, 93, 1, 0, 96` | 141 | | **Range** | Temp: 0–50°C
Hum: 20–90% | Temp: -40–80°C
Hum: 10–90% | Temp: -40–80°C
Hum: 0–100% | 142 | 143 | ## Example Schematic 144 | 145 | | ![Schematic](https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/refs/heads/main/docs/example_esp32_schematic.png) [Click to zoom](https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/refs/heads/main/docs/example_esp32_schematic.png) | ![Running](https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/refs/heads/main/docs/example_esp32_wired.jpg) [Click to zoom](https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/refs/heads/main/docs/example_esp32_wired.jpg) | 146 | |:--:|:--:| 147 | -------------------------------------------------------------------------------- /docs/dht11_22.md: -------------------------------------------------------------------------------- 1 | # DHT11/DHT22 2 | 3 | ![steps](https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/refs/heads/main/docs/dht11_22_steps.png) 4 | 5 | ## Step 1 6 | 7 | After powering on the DHT11/DHT22 (once powered, allow 1 second to pass during which the sensor stabilizes; during this time, no commands should be sent), it measures the temperature and humidity of the surrounding environment and stores the data. Meanwhile, the DATA line of the DHT11/DHT22 is kept high by a pull-up resistor. The DATA pin of the DHT11/DHT22 is in input mode, ready to detect any external signals. 8 | 9 | ## Step 2 10 | 11 | The microprocessor's I/O pin is set to output mode and pulled low, holding this state for at least 18 milliseconds. Then, the microprocessor's I/O is switched to input mode. Due to the pull-up resistor, the microprocessor’s I/O line and the DHT11/DHT22 DATA line will remain high, waiting for the DHT11/DHT22 to respond with a signal, as illustrated below: 12 | 13 | ![step2](https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/refs/heads/main/docs/dht11_22_step2.png) 14 | 15 | 16 | ## Step 3 17 | 18 | The DHT11/DHT22’s DATA pin detects an external signal and goes low, indicating that it is waiting for the external signal to complete. Once the signal ends, the DHT11/DHT22’s DATA pin switches to output mode, producing a low signal for 80 microseconds as a response. This is followed by an 80-microsecond high signal, notifying the microprocessor that the sensor is ready to transmit data. At this point, the microprocessor's I/O pin, still in input mode, detects the low signal from the DHT11/DHT22 (indicating the response) and then waits for the 80-microsecond high signal to start receiving data. The sequence of signal transmission is illustrated below: 19 | 20 | ![step3](https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/refs/heads/main/docs/dht11_22_step3.png) 21 | 22 | ## Step 4 23 | 24 | The DHT11/DHT22 outputs 40 bits of data through the DATA pin, and the microprocessor receives these 40 data bits. The format for a data bit "0" consists of a low level lasting 50 microseconds, followed by a high level lasting 26-28 microseconds, depending on changes in the I/O level. For a data bit "1," the format includes a low level of 50 microseconds followed by a high level lasting up to 70 microseconds. The signal formats for data bits "0" and "1" are shown below. 25 | 26 | ![step4](https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/refs/heads/main/docs/dht11_22_step4.png) 27 | 28 | ## End signal 29 | 30 | After outputting a low signal for 50 microseconds, the DHT11/DHT22 completes sending the 40 bits of data and switches the DATA pin back to input mode, which, along with the pull-up resistor, returns to a high state. Meanwhile, the DHT11/DHT22 internally re-measures the environmental temperature and humidity, records the new data, and waits for the next external signal. 31 | -------------------------------------------------------------------------------- /docs/dht11_22_step2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/593b78757a90543d53ea99ca30ebbe00b640fc9a/docs/dht11_22_step2.png -------------------------------------------------------------------------------- /docs/dht11_22_step3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/593b78757a90543d53ea99ca30ebbe00b640fc9a/docs/dht11_22_step3.png -------------------------------------------------------------------------------- /docs/dht11_22_step4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/593b78757a90543d53ea99ca30ebbe00b640fc9a/docs/dht11_22_step4.png -------------------------------------------------------------------------------- /docs/dht11_22_steps.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/593b78757a90543d53ea99ca30ebbe00b640fc9a/docs/dht11_22_steps.png -------------------------------------------------------------------------------- /docs/dht20.md: -------------------------------------------------------------------------------- 1 | # DHT20 2 | 3 | ![steps](https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/refs/heads/main/docs/dht20_steps.png) 4 | 5 | - SDA = Serial Data Line 6 | - SCL = Serial Clock Line 7 | 8 | ## Start the sensor 9 | 10 | The initial step is to supply power to the sensor using the chosen VDD voltage, which can range from 2.2V to 5.5V. Once powered on, the sensor requires less than 100ms to stabilize (with SCL held high during this period) before entering the idle state, after which it is ready to accept commands from the host (MCU). 11 | 12 | ## Step 1 13 | 14 | After powering on, wait at least 100ms. Before reading the temperature and humidity values, retrieve a status byte by sending 0x71. If the result of the status byte and 0x18 is not equal to 0x18, initialize the 0x1B, 0x1C, and 0x1E registers. 15 | 16 | ## Step 2 17 | Wait for 10ms before sending the 0xAC command to trigger the measurement. The command consists of two bytes: the first byte is 0x33 and the second byte is 0x00. 18 | 19 | ![step4](https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/refs/heads/main/docs/dht20_step2.png) 20 | 21 | ## Step 3 22 | Wait for 80ms for the measurement to complete. If Bit [7] of the status word is 0, the measurement is done, and you can proceed to read six bytes continuously; if not, continue waiting. 23 | 24 | ## Step 4 25 | After receiving the six bytes, the following byte is the CRC check data, which can be read if needed. If the receiver requires CRC validation, an ACK is sent after the sixth byte is received; otherwise, send a NACK to terminate. The initial CRC value is 0xFF, and the CRC8 check uses the polynomial: CRC [7:0] = 1 + X^4 + X^5 + X^8. 26 | 27 | ![step4](https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/refs/heads/main/docs/dht20_step4.png) 28 | 29 | ## Step 5 30 | Compute the temperature and humidity values. -------------------------------------------------------------------------------- /docs/dht20_step2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/593b78757a90543d53ea99ca30ebbe00b640fc9a/docs/dht20_step2.png -------------------------------------------------------------------------------- /docs/dht20_step4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/593b78757a90543d53ea99ca30ebbe00b640fc9a/docs/dht20_step4.png -------------------------------------------------------------------------------- /docs/dht20_steps.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/593b78757a90543d53ea99ca30ebbe00b640fc9a/docs/dht20_steps.png -------------------------------------------------------------------------------- /docs/example_esp32_dht_running.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/593b78757a90543d53ea99ca30ebbe00b640fc9a/docs/example_esp32_dht_running.png -------------------------------------------------------------------------------- /docs/example_esp32_schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/593b78757a90543d53ea99ca30ebbe00b640fc9a/docs/example_esp32_schematic.png -------------------------------------------------------------------------------- /docs/example_esp32_wired.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rust-dd/embedded-dht-rs/593b78757a90543d53ea99ca30ebbe00b640fc9a/docs/example_esp32_wired.jpg -------------------------------------------------------------------------------- /examples/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.xtensa-esp32-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | 5 | [env] 6 | ESP_LOGLEVEL="INFO" 7 | 8 | [build] 9 | rustflags = [ 10 | "-C", "link-arg=-nostartfiles", 11 | ] 12 | 13 | target = "xtensa-esp32-none-elf" 14 | 15 | [unstable] 16 | build-std = ["core"] 17 | -------------------------------------------------------------------------------- /examples/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /examples/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "rust-analyzer.check.allTargets": false, 3 | } 4 | -------------------------------------------------------------------------------- /examples/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "anstream" 7 | version = "0.6.15" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" 10 | dependencies = [ 11 | "anstyle", 12 | "anstyle-parse", 13 | "anstyle-query", 14 | "anstyle-wincon", 15 | "colorchoice", 16 | "is_terminal_polyfill", 17 | "utf8parse", 18 | ] 19 | 20 | [[package]] 21 | name = "anstyle" 22 | version = "1.0.8" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" 25 | 26 | [[package]] 27 | name = "anstyle-parse" 28 | version = "0.2.5" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" 31 | dependencies = [ 32 | "utf8parse", 33 | ] 34 | 35 | [[package]] 36 | name = "anstyle-query" 37 | version = "1.1.1" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" 40 | dependencies = [ 41 | "windows-sys 0.52.0", 42 | ] 43 | 44 | [[package]] 45 | name = "anstyle-wincon" 46 | version = "3.0.4" 47 | source = "registry+https://github.com/rust-lang/crates.io-index" 48 | checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" 49 | dependencies = [ 50 | "anstyle", 51 | "windows-sys 0.52.0", 52 | ] 53 | 54 | [[package]] 55 | name = "anyhow" 56 | version = "1.0.89" 57 | source = "registry+https://github.com/rust-lang/crates.io-index" 58 | checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" 59 | 60 | [[package]] 61 | name = "autocfg" 62 | version = "1.4.0" 63 | source = "registry+https://github.com/rust-lang/crates.io-index" 64 | checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" 65 | 66 | [[package]] 67 | name = "bare-metal" 68 | version = "1.0.0" 69 | source = "registry+https://github.com/rust-lang/crates.io-index" 70 | checksum = "f8fe8f5a8a398345e52358e18ff07cc17a568fbca5c6f73873d3a62056309603" 71 | 72 | [[package]] 73 | name = "basic-toml" 74 | version = "0.1.9" 75 | source = "registry+https://github.com/rust-lang/crates.io-index" 76 | checksum = "823388e228f614e9558c6804262db37960ec8821856535f5c3f59913140558f8" 77 | dependencies = [ 78 | "serde", 79 | ] 80 | 81 | [[package]] 82 | name = "bitfield" 83 | version = "0.16.1" 84 | source = "registry+https://github.com/rust-lang/crates.io-index" 85 | checksum = "d5acf59e2452f0c4b968b15ce4b9468f57b45f7733b919d68b19fcc39264bfb8" 86 | 87 | [[package]] 88 | name = "bitflags" 89 | version = "2.6.0" 90 | source = "registry+https://github.com/rust-lang/crates.io-index" 91 | checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" 92 | 93 | [[package]] 94 | name = "bytemuck" 95 | version = "1.18.0" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | checksum = "94bbb0ad554ad961ddc5da507a12a29b14e4ae5bda06b19f575a3e6079d2e2ae" 98 | 99 | [[package]] 100 | name = "cfg-if" 101 | version = "1.0.0" 102 | source = "registry+https://github.com/rust-lang/crates.io-index" 103 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 104 | 105 | [[package]] 106 | name = "clap" 107 | version = "4.5.20" 108 | source = "registry+https://github.com/rust-lang/crates.io-index" 109 | checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8" 110 | dependencies = [ 111 | "clap_builder", 112 | "clap_derive", 113 | ] 114 | 115 | [[package]] 116 | name = "clap_builder" 117 | version = "4.5.20" 118 | source = "registry+https://github.com/rust-lang/crates.io-index" 119 | checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54" 120 | dependencies = [ 121 | "anstream", 122 | "anstyle", 123 | "clap_lex", 124 | "strsim", 125 | ] 126 | 127 | [[package]] 128 | name = "clap_derive" 129 | version = "4.5.18" 130 | source = "registry+https://github.com/rust-lang/crates.io-index" 131 | checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" 132 | dependencies = [ 133 | "heck", 134 | "proc-macro2", 135 | "quote", 136 | "syn 2.0.79", 137 | ] 138 | 139 | [[package]] 140 | name = "clap_lex" 141 | version = "0.7.2" 142 | source = "registry+https://github.com/rust-lang/crates.io-index" 143 | checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" 144 | 145 | [[package]] 146 | name = "colorchoice" 147 | version = "1.0.2" 148 | source = "registry+https://github.com/rust-lang/crates.io-index" 149 | checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" 150 | 151 | [[package]] 152 | name = "critical-section" 153 | version = "1.1.3" 154 | source = "registry+https://github.com/rust-lang/crates.io-index" 155 | checksum = "f64009896348fc5af4222e9cf7d7d82a95a256c634ebcf61c53e4ea461422242" 156 | 157 | [[package]] 158 | name = "darling" 159 | version = "0.20.10" 160 | source = "registry+https://github.com/rust-lang/crates.io-index" 161 | checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989" 162 | dependencies = [ 163 | "darling_core", 164 | "darling_macro", 165 | ] 166 | 167 | [[package]] 168 | name = "darling_core" 169 | version = "0.20.10" 170 | source = "registry+https://github.com/rust-lang/crates.io-index" 171 | checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5" 172 | dependencies = [ 173 | "fnv", 174 | "ident_case", 175 | "proc-macro2", 176 | "quote", 177 | "strsim", 178 | "syn 2.0.79", 179 | ] 180 | 181 | [[package]] 182 | name = "darling_macro" 183 | version = "0.20.10" 184 | source = "registry+https://github.com/rust-lang/crates.io-index" 185 | checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" 186 | dependencies = [ 187 | "darling_core", 188 | "quote", 189 | "syn 2.0.79", 190 | ] 191 | 192 | [[package]] 193 | name = "delegate" 194 | version = "0.12.0" 195 | source = "registry+https://github.com/rust-lang/crates.io-index" 196 | checksum = "4e018fccbeeb50ff26562ece792ed06659b9c2dae79ece77c4456bb10d9bf79b" 197 | dependencies = [ 198 | "proc-macro2", 199 | "quote", 200 | "syn 2.0.79", 201 | ] 202 | 203 | [[package]] 204 | name = "document-features" 205 | version = "0.2.10" 206 | source = "registry+https://github.com/rust-lang/crates.io-index" 207 | checksum = "cb6969eaabd2421f8a2775cfd2471a2b634372b4a25d41e3bd647b79912850a0" 208 | dependencies = [ 209 | "litrs", 210 | ] 211 | 212 | [[package]] 213 | name = "embedded-can" 214 | version = "0.4.1" 215 | source = "registry+https://github.com/rust-lang/crates.io-index" 216 | checksum = "e9d2e857f87ac832df68fa498d18ddc679175cf3d2e4aa893988e5601baf9438" 217 | dependencies = [ 218 | "nb", 219 | ] 220 | 221 | [[package]] 222 | name = "embedded-dht-rs" 223 | version = "0.3.2" 224 | source = "registry+https://github.com/rust-lang/crates.io-index" 225 | checksum = "fbfc523590c7a10e16f279a30c77137a6688010062b7a04aede6a584ddb55dd2" 226 | dependencies = [ 227 | "embedded-hal", 228 | ] 229 | 230 | [[package]] 231 | name = "embedded-dht-rs-on-esp32" 232 | version = "0.1.0" 233 | dependencies = [ 234 | "embedded-dht-rs", 235 | "esp-backtrace", 236 | "esp-hal", 237 | "esp-println", 238 | "fugit", 239 | "log", 240 | ] 241 | 242 | [[package]] 243 | name = "embedded-hal" 244 | version = "1.0.0" 245 | source = "registry+https://github.com/rust-lang/crates.io-index" 246 | checksum = "361a90feb7004eca4019fb28352a9465666b24f840f5c3cddf0ff13920590b89" 247 | 248 | [[package]] 249 | name = "embedded-hal-nb" 250 | version = "1.0.0" 251 | source = "registry+https://github.com/rust-lang/crates.io-index" 252 | checksum = "fba4268c14288c828995299e59b12babdbe170f6c6d73731af1b4648142e8605" 253 | dependencies = [ 254 | "embedded-hal", 255 | "nb", 256 | ] 257 | 258 | [[package]] 259 | name = "enum-as-inner" 260 | version = "0.6.1" 261 | source = "registry+https://github.com/rust-lang/crates.io-index" 262 | checksum = "a1e6a265c649f3f5979b601d26f1d05ada116434c87741c9493cb56218f76cbc" 263 | dependencies = [ 264 | "heck", 265 | "proc-macro2", 266 | "quote", 267 | "syn 2.0.79", 268 | ] 269 | 270 | [[package]] 271 | name = "enumset" 272 | version = "1.1.5" 273 | source = "registry+https://github.com/rust-lang/crates.io-index" 274 | checksum = "d07a4b049558765cef5f0c1a273c3fc57084d768b44d2f98127aef4cceb17293" 275 | dependencies = [ 276 | "enumset_derive", 277 | ] 278 | 279 | [[package]] 280 | name = "enumset_derive" 281 | version = "0.10.0" 282 | source = "registry+https://github.com/rust-lang/crates.io-index" 283 | checksum = "59c3b24c345d8c314966bdc1832f6c2635bfcce8e7cf363bd115987bba2ee242" 284 | dependencies = [ 285 | "darling", 286 | "proc-macro2", 287 | "quote", 288 | "syn 2.0.79", 289 | ] 290 | 291 | [[package]] 292 | name = "equivalent" 293 | version = "1.0.1" 294 | source = "registry+https://github.com/rust-lang/crates.io-index" 295 | checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" 296 | 297 | [[package]] 298 | name = "esp-backtrace" 299 | version = "0.14.1" 300 | source = "registry+https://github.com/rust-lang/crates.io-index" 301 | checksum = "9c2ff4bce686f28fe48a5d16aaa48c30b627a423bb689be57949bb210b8551d0" 302 | dependencies = [ 303 | "esp-build", 304 | "esp-println", 305 | "semihosting", 306 | ] 307 | 308 | [[package]] 309 | name = "esp-build" 310 | version = "0.1.0" 311 | source = "registry+https://github.com/rust-lang/crates.io-index" 312 | checksum = "b94a4b8d74e7cc7baabcca5b2277b41877e039ad9cd49959d48ef94dac7eab4b" 313 | dependencies = [ 314 | "quote", 315 | "syn 2.0.79", 316 | "termcolor", 317 | ] 318 | 319 | [[package]] 320 | name = "esp-hal" 321 | version = "0.20.1" 322 | source = "registry+https://github.com/rust-lang/crates.io-index" 323 | checksum = "64f5393b8f7e7f055455d9f86706ddb675f943c12f12a7b80b8a79c3a94233ff" 324 | dependencies = [ 325 | "basic-toml", 326 | "bitfield", 327 | "bitflags", 328 | "bytemuck", 329 | "cfg-if", 330 | "critical-section", 331 | "delegate", 332 | "document-features", 333 | "embedded-can", 334 | "embedded-hal", 335 | "embedded-hal-nb", 336 | "enumset", 337 | "esp-build", 338 | "esp-hal-procmacros", 339 | "esp-metadata", 340 | "esp-riscv-rt", 341 | "esp32", 342 | "fugit", 343 | "nb", 344 | "paste", 345 | "portable-atomic", 346 | "rand_core", 347 | "serde", 348 | "strum", 349 | "void", 350 | "xtensa-lx", 351 | "xtensa-lx-rt", 352 | ] 353 | 354 | [[package]] 355 | name = "esp-hal-procmacros" 356 | version = "0.13.0" 357 | source = "registry+https://github.com/rust-lang/crates.io-index" 358 | checksum = "6eac531546027909a355fc9c2449f22c839955fa4b7f1976b64ddd04b2f22f83" 359 | dependencies = [ 360 | "darling", 361 | "document-features", 362 | "litrs", 363 | "proc-macro-crate", 364 | "proc-macro-error", 365 | "proc-macro2", 366 | "quote", 367 | "syn 2.0.79", 368 | ] 369 | 370 | [[package]] 371 | name = "esp-metadata" 372 | version = "0.3.0" 373 | source = "registry+https://github.com/rust-lang/crates.io-index" 374 | checksum = "b471bc61fa817ca4ae41a31d5d453258328b31e5ad82db72b473621d36cc4cb6" 375 | dependencies = [ 376 | "anyhow", 377 | "basic-toml", 378 | "clap", 379 | "lazy_static", 380 | "serde", 381 | "strum", 382 | ] 383 | 384 | [[package]] 385 | name = "esp-println" 386 | version = "0.11.0" 387 | source = "registry+https://github.com/rust-lang/crates.io-index" 388 | checksum = "0d9dd4fc40306450e432cdf104ab00c8f6bd5c4f6c77b76c5fc3024c0e2a535d" 389 | dependencies = [ 390 | "critical-section", 391 | "esp-build", 392 | "log", 393 | "portable-atomic", 394 | ] 395 | 396 | [[package]] 397 | name = "esp-riscv-rt" 398 | version = "0.9.0" 399 | source = "registry+https://github.com/rust-lang/crates.io-index" 400 | checksum = "bfc32298ed7c263b06c8b031704d8517cc62c819f2a9d5c261d0cb119634d6e9" 401 | dependencies = [ 402 | "document-features", 403 | "riscv", 404 | "riscv-rt-macros", 405 | ] 406 | 407 | [[package]] 408 | name = "esp32" 409 | version = "0.33.0" 410 | source = "registry+https://github.com/rust-lang/crates.io-index" 411 | checksum = "85287b57fae3e318b62fd860787b1ac85a5e7bf91ad43eb66837c5e567218009" 412 | dependencies = [ 413 | "critical-section", 414 | "vcell", 415 | "xtensa-lx", 416 | ] 417 | 418 | [[package]] 419 | name = "fnv" 420 | version = "1.0.7" 421 | source = "registry+https://github.com/rust-lang/crates.io-index" 422 | checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" 423 | 424 | [[package]] 425 | name = "fugit" 426 | version = "0.3.7" 427 | source = "registry+https://github.com/rust-lang/crates.io-index" 428 | checksum = "17186ad64927d5ac8f02c1e77ccefa08ccd9eaa314d5a4772278aa204a22f7e7" 429 | dependencies = [ 430 | "gcd", 431 | ] 432 | 433 | [[package]] 434 | name = "gcd" 435 | version = "2.3.0" 436 | source = "registry+https://github.com/rust-lang/crates.io-index" 437 | checksum = "1d758ba1b47b00caf47f24925c0074ecb20d6dfcffe7f6d53395c0465674841a" 438 | 439 | [[package]] 440 | name = "hashbrown" 441 | version = "0.15.0" 442 | source = "registry+https://github.com/rust-lang/crates.io-index" 443 | checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" 444 | 445 | [[package]] 446 | name = "heck" 447 | version = "0.5.0" 448 | source = "registry+https://github.com/rust-lang/crates.io-index" 449 | checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" 450 | 451 | [[package]] 452 | name = "ident_case" 453 | version = "1.0.1" 454 | source = "registry+https://github.com/rust-lang/crates.io-index" 455 | checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" 456 | 457 | [[package]] 458 | name = "indexmap" 459 | version = "2.6.0" 460 | source = "registry+https://github.com/rust-lang/crates.io-index" 461 | checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" 462 | dependencies = [ 463 | "equivalent", 464 | "hashbrown", 465 | ] 466 | 467 | [[package]] 468 | name = "is_terminal_polyfill" 469 | version = "1.70.1" 470 | source = "registry+https://github.com/rust-lang/crates.io-index" 471 | checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" 472 | 473 | [[package]] 474 | name = "lazy_static" 475 | version = "1.5.0" 476 | source = "registry+https://github.com/rust-lang/crates.io-index" 477 | checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" 478 | 479 | [[package]] 480 | name = "litrs" 481 | version = "0.4.1" 482 | source = "registry+https://github.com/rust-lang/crates.io-index" 483 | checksum = "b4ce301924b7887e9d637144fdade93f9dfff9b60981d4ac161db09720d39aa5" 484 | dependencies = [ 485 | "proc-macro2", 486 | ] 487 | 488 | [[package]] 489 | name = "lock_api" 490 | version = "0.4.12" 491 | source = "registry+https://github.com/rust-lang/crates.io-index" 492 | checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" 493 | dependencies = [ 494 | "autocfg", 495 | "scopeguard", 496 | ] 497 | 498 | [[package]] 499 | name = "log" 500 | version = "0.4.22" 501 | source = "registry+https://github.com/rust-lang/crates.io-index" 502 | checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" 503 | 504 | [[package]] 505 | name = "memchr" 506 | version = "2.7.4" 507 | source = "registry+https://github.com/rust-lang/crates.io-index" 508 | checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" 509 | 510 | [[package]] 511 | name = "minijinja" 512 | version = "2.3.1" 513 | source = "registry+https://github.com/rust-lang/crates.io-index" 514 | checksum = "1028b628753a7e1a88fc59c9ba4b02ecc3bc0bd3c7af23df667bc28df9b3310e" 515 | dependencies = [ 516 | "serde", 517 | ] 518 | 519 | [[package]] 520 | name = "mutex-trait" 521 | version = "0.2.0" 522 | source = "registry+https://github.com/rust-lang/crates.io-index" 523 | checksum = "b4bb1638d419e12f8b1c43d9e639abd0d1424285bdea2f76aa231e233c63cd3a" 524 | 525 | [[package]] 526 | name = "nb" 527 | version = "1.1.0" 528 | source = "registry+https://github.com/rust-lang/crates.io-index" 529 | checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d" 530 | 531 | [[package]] 532 | name = "paste" 533 | version = "1.0.15" 534 | source = "registry+https://github.com/rust-lang/crates.io-index" 535 | checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" 536 | 537 | [[package]] 538 | name = "portable-atomic" 539 | version = "1.9.0" 540 | source = "registry+https://github.com/rust-lang/crates.io-index" 541 | checksum = "cc9c68a3f6da06753e9335d63e27f6b9754dd1920d941135b7ea8224f141adb2" 542 | 543 | [[package]] 544 | name = "proc-macro-crate" 545 | version = "3.2.0" 546 | source = "registry+https://github.com/rust-lang/crates.io-index" 547 | checksum = "8ecf48c7ca261d60b74ab1a7b20da18bede46776b2e55535cb958eb595c5fa7b" 548 | dependencies = [ 549 | "toml_edit", 550 | ] 551 | 552 | [[package]] 553 | name = "proc-macro-error" 554 | version = "1.0.4" 555 | source = "registry+https://github.com/rust-lang/crates.io-index" 556 | checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" 557 | dependencies = [ 558 | "proc-macro-error-attr", 559 | "proc-macro2", 560 | "quote", 561 | "syn 1.0.109", 562 | "version_check", 563 | ] 564 | 565 | [[package]] 566 | name = "proc-macro-error-attr" 567 | version = "1.0.4" 568 | source = "registry+https://github.com/rust-lang/crates.io-index" 569 | checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" 570 | dependencies = [ 571 | "proc-macro2", 572 | "quote", 573 | "version_check", 574 | ] 575 | 576 | [[package]] 577 | name = "proc-macro2" 578 | version = "1.0.87" 579 | source = "registry+https://github.com/rust-lang/crates.io-index" 580 | checksum = "b3e4daa0dcf6feba26f985457cdf104d4b4256fc5a09547140f3631bb076b19a" 581 | dependencies = [ 582 | "unicode-ident", 583 | ] 584 | 585 | [[package]] 586 | name = "quote" 587 | version = "1.0.37" 588 | source = "registry+https://github.com/rust-lang/crates.io-index" 589 | checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" 590 | dependencies = [ 591 | "proc-macro2", 592 | ] 593 | 594 | [[package]] 595 | name = "r0" 596 | version = "1.0.0" 597 | source = "registry+https://github.com/rust-lang/crates.io-index" 598 | checksum = "bd7a31eed1591dcbc95d92ad7161908e72f4677f8fabf2a32ca49b4237cbf211" 599 | 600 | [[package]] 601 | name = "rand_core" 602 | version = "0.6.4" 603 | source = "registry+https://github.com/rust-lang/crates.io-index" 604 | checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" 605 | 606 | [[package]] 607 | name = "riscv" 608 | version = "0.11.1" 609 | source = "registry+https://github.com/rust-lang/crates.io-index" 610 | checksum = "2f5c1b8bf41ea746266cdee443d1d1e9125c86ce1447e1a2615abd34330d33a9" 611 | dependencies = [ 612 | "critical-section", 613 | "embedded-hal", 614 | ] 615 | 616 | [[package]] 617 | name = "riscv-rt-macros" 618 | version = "0.2.1" 619 | source = "registry+https://github.com/rust-lang/crates.io-index" 620 | checksum = "a8d100d466dbb76681ef6a9386f3da9abc570d57394e86da0ba5af8c4408486d" 621 | dependencies = [ 622 | "proc-macro2", 623 | "quote", 624 | "syn 1.0.109", 625 | ] 626 | 627 | [[package]] 628 | name = "rustversion" 629 | version = "1.0.17" 630 | source = "registry+https://github.com/rust-lang/crates.io-index" 631 | checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" 632 | 633 | [[package]] 634 | name = "scopeguard" 635 | version = "1.2.0" 636 | source = "registry+https://github.com/rust-lang/crates.io-index" 637 | checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" 638 | 639 | [[package]] 640 | name = "semihosting" 641 | version = "0.1.15" 642 | source = "registry+https://github.com/rust-lang/crates.io-index" 643 | checksum = "3f69d4d7b8d5f6595ac8901b8c4ede3339b1b4c8565f9d3180d20fc046cca177" 644 | 645 | [[package]] 646 | name = "serde" 647 | version = "1.0.210" 648 | source = "registry+https://github.com/rust-lang/crates.io-index" 649 | checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" 650 | dependencies = [ 651 | "serde_derive", 652 | ] 653 | 654 | [[package]] 655 | name = "serde_derive" 656 | version = "1.0.210" 657 | source = "registry+https://github.com/rust-lang/crates.io-index" 658 | checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" 659 | dependencies = [ 660 | "proc-macro2", 661 | "quote", 662 | "syn 2.0.79", 663 | ] 664 | 665 | [[package]] 666 | name = "serde_spanned" 667 | version = "0.6.8" 668 | source = "registry+https://github.com/rust-lang/crates.io-index" 669 | checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" 670 | dependencies = [ 671 | "serde", 672 | ] 673 | 674 | [[package]] 675 | name = "spin" 676 | version = "0.9.8" 677 | source = "registry+https://github.com/rust-lang/crates.io-index" 678 | checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" 679 | dependencies = [ 680 | "lock_api", 681 | ] 682 | 683 | [[package]] 684 | name = "strsim" 685 | version = "0.11.1" 686 | source = "registry+https://github.com/rust-lang/crates.io-index" 687 | checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" 688 | 689 | [[package]] 690 | name = "strum" 691 | version = "0.26.3" 692 | source = "registry+https://github.com/rust-lang/crates.io-index" 693 | checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" 694 | dependencies = [ 695 | "strum_macros", 696 | ] 697 | 698 | [[package]] 699 | name = "strum_macros" 700 | version = "0.26.4" 701 | source = "registry+https://github.com/rust-lang/crates.io-index" 702 | checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" 703 | dependencies = [ 704 | "heck", 705 | "proc-macro2", 706 | "quote", 707 | "rustversion", 708 | "syn 2.0.79", 709 | ] 710 | 711 | [[package]] 712 | name = "syn" 713 | version = "1.0.109" 714 | source = "registry+https://github.com/rust-lang/crates.io-index" 715 | checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" 716 | dependencies = [ 717 | "proc-macro2", 718 | "quote", 719 | "unicode-ident", 720 | ] 721 | 722 | [[package]] 723 | name = "syn" 724 | version = "2.0.79" 725 | source = "registry+https://github.com/rust-lang/crates.io-index" 726 | checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" 727 | dependencies = [ 728 | "proc-macro2", 729 | "quote", 730 | "unicode-ident", 731 | ] 732 | 733 | [[package]] 734 | name = "termcolor" 735 | version = "1.4.1" 736 | source = "registry+https://github.com/rust-lang/crates.io-index" 737 | checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" 738 | dependencies = [ 739 | "winapi-util", 740 | ] 741 | 742 | [[package]] 743 | name = "toml" 744 | version = "0.8.19" 745 | source = "registry+https://github.com/rust-lang/crates.io-index" 746 | checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e" 747 | dependencies = [ 748 | "serde", 749 | "serde_spanned", 750 | "toml_datetime", 751 | "toml_edit", 752 | ] 753 | 754 | [[package]] 755 | name = "toml_datetime" 756 | version = "0.6.8" 757 | source = "registry+https://github.com/rust-lang/crates.io-index" 758 | checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" 759 | dependencies = [ 760 | "serde", 761 | ] 762 | 763 | [[package]] 764 | name = "toml_edit" 765 | version = "0.22.22" 766 | source = "registry+https://github.com/rust-lang/crates.io-index" 767 | checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" 768 | dependencies = [ 769 | "indexmap", 770 | "serde", 771 | "serde_spanned", 772 | "toml_datetime", 773 | "winnow", 774 | ] 775 | 776 | [[package]] 777 | name = "unicode-ident" 778 | version = "1.0.13" 779 | source = "registry+https://github.com/rust-lang/crates.io-index" 780 | checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" 781 | 782 | [[package]] 783 | name = "utf8parse" 784 | version = "0.2.2" 785 | source = "registry+https://github.com/rust-lang/crates.io-index" 786 | checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" 787 | 788 | [[package]] 789 | name = "vcell" 790 | version = "0.1.3" 791 | source = "registry+https://github.com/rust-lang/crates.io-index" 792 | checksum = "77439c1b53d2303b20d9459b1ade71a83c716e3f9c34f3228c00e6f185d6c002" 793 | 794 | [[package]] 795 | name = "version_check" 796 | version = "0.9.5" 797 | source = "registry+https://github.com/rust-lang/crates.io-index" 798 | checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" 799 | 800 | [[package]] 801 | name = "void" 802 | version = "1.0.2" 803 | source = "registry+https://github.com/rust-lang/crates.io-index" 804 | checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" 805 | 806 | [[package]] 807 | name = "winapi-util" 808 | version = "0.1.9" 809 | source = "registry+https://github.com/rust-lang/crates.io-index" 810 | checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" 811 | dependencies = [ 812 | "windows-sys 0.59.0", 813 | ] 814 | 815 | [[package]] 816 | name = "windows-sys" 817 | version = "0.52.0" 818 | source = "registry+https://github.com/rust-lang/crates.io-index" 819 | checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" 820 | dependencies = [ 821 | "windows-targets", 822 | ] 823 | 824 | [[package]] 825 | name = "windows-sys" 826 | version = "0.59.0" 827 | source = "registry+https://github.com/rust-lang/crates.io-index" 828 | checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" 829 | dependencies = [ 830 | "windows-targets", 831 | ] 832 | 833 | [[package]] 834 | name = "windows-targets" 835 | version = "0.52.6" 836 | source = "registry+https://github.com/rust-lang/crates.io-index" 837 | checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" 838 | dependencies = [ 839 | "windows_aarch64_gnullvm", 840 | "windows_aarch64_msvc", 841 | "windows_i686_gnu", 842 | "windows_i686_gnullvm", 843 | "windows_i686_msvc", 844 | "windows_x86_64_gnu", 845 | "windows_x86_64_gnullvm", 846 | "windows_x86_64_msvc", 847 | ] 848 | 849 | [[package]] 850 | name = "windows_aarch64_gnullvm" 851 | version = "0.52.6" 852 | source = "registry+https://github.com/rust-lang/crates.io-index" 853 | checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" 854 | 855 | [[package]] 856 | name = "windows_aarch64_msvc" 857 | version = "0.52.6" 858 | source = "registry+https://github.com/rust-lang/crates.io-index" 859 | checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" 860 | 861 | [[package]] 862 | name = "windows_i686_gnu" 863 | version = "0.52.6" 864 | source = "registry+https://github.com/rust-lang/crates.io-index" 865 | checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" 866 | 867 | [[package]] 868 | name = "windows_i686_gnullvm" 869 | version = "0.52.6" 870 | source = "registry+https://github.com/rust-lang/crates.io-index" 871 | checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" 872 | 873 | [[package]] 874 | name = "windows_i686_msvc" 875 | version = "0.52.6" 876 | source = "registry+https://github.com/rust-lang/crates.io-index" 877 | checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" 878 | 879 | [[package]] 880 | name = "windows_x86_64_gnu" 881 | version = "0.52.6" 882 | source = "registry+https://github.com/rust-lang/crates.io-index" 883 | checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" 884 | 885 | [[package]] 886 | name = "windows_x86_64_gnullvm" 887 | version = "0.52.6" 888 | source = "registry+https://github.com/rust-lang/crates.io-index" 889 | checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" 890 | 891 | [[package]] 892 | name = "windows_x86_64_msvc" 893 | version = "0.52.6" 894 | source = "registry+https://github.com/rust-lang/crates.io-index" 895 | checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" 896 | 897 | [[package]] 898 | name = "winnow" 899 | version = "0.6.20" 900 | source = "registry+https://github.com/rust-lang/crates.io-index" 901 | checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b" 902 | dependencies = [ 903 | "memchr", 904 | ] 905 | 906 | [[package]] 907 | name = "xtensa-lx" 908 | version = "0.9.0" 909 | source = "registry+https://github.com/rust-lang/crates.io-index" 910 | checksum = "e758f94e1a1f71758f94052a2766dcb12604998eb372b8b2e30576e3ab1ba1e6" 911 | dependencies = [ 912 | "bare-metal", 913 | "mutex-trait", 914 | "spin", 915 | ] 916 | 917 | [[package]] 918 | name = "xtensa-lx-rt" 919 | version = "0.17.1" 920 | source = "registry+https://github.com/rust-lang/crates.io-index" 921 | checksum = "2ceb69c1487b78d83531c5d94fb81d0dceef1ccb0affba29f29420b1f72d3ddb" 922 | dependencies = [ 923 | "anyhow", 924 | "bare-metal", 925 | "document-features", 926 | "enum-as-inner", 927 | "minijinja", 928 | "r0", 929 | "serde", 930 | "strum", 931 | "toml", 932 | "xtensa-lx", 933 | "xtensa-lx-rt-proc-macros", 934 | ] 935 | 936 | [[package]] 937 | name = "xtensa-lx-rt-proc-macros" 938 | version = "0.2.2" 939 | source = "registry+https://github.com/rust-lang/crates.io-index" 940 | checksum = "11277b1e4cbb7ffe44678c668518b249c843c81df249b8f096701757bc50d7ee" 941 | dependencies = [ 942 | "darling", 943 | "proc-macro2", 944 | "quote", 945 | "syn 2.0.79", 946 | ] 947 | -------------------------------------------------------------------------------- /examples/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "embedded-dht-rs-on-esp32" 3 | version = "0.1.0" 4 | authors = ["zeldan "] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | embedded-dht-rs = { version = "0.3.2", features = ["dht11", "dht20", "dht22"] } 10 | esp-backtrace = { version = "0.14.0", features = [ 11 | "esp32", 12 | "exception-handler", 13 | "panic-handler", 14 | "println", 15 | ] } 16 | esp-hal = { version = "0.20.1", features = [ "esp32" ] } 17 | esp-println = { version = "0.11.0", features = ["esp32", "log"] } 18 | fugit = "0.3.7" 19 | log = { version = "0.4.21" } 20 | [profile.dev] 21 | # Rust debug is too slow. 22 | # For debug builds always builds with some optimization 23 | opt-level = "s" 24 | 25 | [profile.release] 26 | codegen-units = 1 # LLVM can perform better optimizations using a single thread 27 | debug = 2 28 | debug-assertions = false 29 | incremental = false 30 | lto = 'fat' 31 | opt-level = 's' 32 | overflow-checks = false 33 | -------------------------------------------------------------------------------- /examples/LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /examples/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /examples/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("cargo:rustc-link-arg-bins=-Tlinkall.x"); 3 | } -------------------------------------------------------------------------------- /examples/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "esp" 3 | -------------------------------------------------------------------------------- /examples/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | use embedded_dht_rs::{dht11::Dht11, dht20::Dht20, dht22::Dht22}; 5 | use esp_backtrace as _; 6 | use esp_hal::{ 7 | clock::ClockControl, delay::Delay, gpio::{Io, Level, OutputOpenDrain, Pull}, i2c::I2C, peripherals::Peripherals, prelude::*, system::SystemControl 8 | }; 9 | use fugit::HertzU32; 10 | 11 | #[entry] 12 | fn main() -> ! { 13 | let peripherals = Peripherals::take(); 14 | let system = SystemControl::new(peripherals.SYSTEM); 15 | let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); 16 | let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); 17 | 18 | esp_println::logger::init_logger_from_env(); 19 | 20 | let delay = Delay::new(&clocks); 21 | 22 | let od_for_dht11 = OutputOpenDrain::new(io.pins.gpio4, Level::High, Pull::None); 23 | let od_for_dht22 = OutputOpenDrain::new(io.pins.gpio5, Level::High, Pull::None); 24 | let i2c_for_dht20 = I2C::new( 25 | peripherals.I2C0, 26 | io.pins.gpio21, 27 | io.pins.gpio22, 28 | HertzU32::kHz(400), 29 | &clocks 30 | ); 31 | 32 | let mut dht11 = Dht11::new(od_for_dht11, delay); 33 | let mut dht22 = Dht22::new(od_for_dht22, delay); 34 | let mut dht20 = Dht20::new(i2c_for_dht20, delay); 35 | 36 | loop { 37 | delay.delay(5000.millis()); 38 | 39 | match dht11.read() { 40 | Ok(sensor_reading) => log::info!( 41 | "DHT 11 Sensor - Temperature: {} °C, humidity: {} %", 42 | sensor_reading.temperature, 43 | sensor_reading.humidity 44 | ), 45 | Err(error) => log::error!("An error occurred while trying to read sensor: {:?}", error), 46 | } 47 | 48 | match dht22.read() { 49 | Ok(sensor_reading) => log::info!( 50 | "DHT 22 Sensor - Temperature: {} °C, humidity: {} %", 51 | sensor_reading.temperature, 52 | sensor_reading.humidity 53 | ), 54 | Err(error) => log::error!("An error occurred while trying to read sensor: {:?}", error), 55 | } 56 | 57 | match dht20.read() { 58 | Ok(sensor_reading) => log::info!( 59 | "DHT 20 Sensor - Temperature: {} °C, humidity: {} %", 60 | sensor_reading.temperature, 61 | sensor_reading.humidity 62 | ), 63 | Err(error) => log::error!("An error occurred while trying to read sensor: {:?}", error), 64 | } 65 | 66 | log::info!("-----"); 67 | } 68 | } -------------------------------------------------------------------------------- /src/dht.rs: -------------------------------------------------------------------------------- 1 | use embedded_hal::{ 2 | delay::DelayNs, 3 | digital::{InputPin, OutputPin, PinState}, 4 | }; 5 | 6 | use crate::SensorError; 7 | 8 | #[cfg(test)] 9 | const DEFAULT_MAX_ATTEMPTS: usize = 10; 10 | #[cfg(not(test))] 11 | const DEFAULT_MAX_ATTEMPTS: usize = 10_000; 12 | 13 | /// Common base struct for DHT11, DHT22 sensors. 14 | pub struct Dht { 15 | pub pin: P, 16 | pub delay: D, 17 | } 18 | 19 | impl Dht { 20 | pub fn new(pin: P, delay: D) -> Self { 21 | Self { pin, delay } 22 | } 23 | 24 | /// Reads a byte (8 bits) from the sensor. 25 | /// 26 | /// This method reads 8 bits sequentially from the sensor to construct a byte. 27 | /// It follows the communication protocol of the DHT11/DHT22 sensors: 28 | /// 29 | /// For each bit: 30 | /// - Waits for the pin to go **high** (start of bit transmission). 31 | /// - Delays for **30 microseconds** to sample the bit value. 32 | /// - If the pin is **high** after the delay, the bit is interpreted as **'1'**. 33 | /// - If the pin is **low**, the bit is interpreted as **'0'**. 34 | /// - Waits for the pin to go **low** (end of bit transmission). 35 | /// 36 | /// The bits are assembled into a byte, starting from the most significant bit (MSB). 37 | /// 38 | /// # Returns 39 | /// 40 | /// - `Ok(u8)`: The byte read from the sensor. 41 | /// - `Err(SensorError)`: If a pin error occurs. 42 | pub fn read_byte(&mut self) -> Result { 43 | let mut byte: u8 = 0; 44 | for n in 0..8 { 45 | match self.wait_until_state(PinState::High) { 46 | Ok(_) => {} 47 | Err(err) => return Err(err), 48 | }; 49 | 50 | self.delay.delay_us(30); 51 | let is_bit_1 = self.pin.is_high(); 52 | if is_bit_1.unwrap() { 53 | let bit_mask = 1 << (7 - (n % 8)); 54 | byte |= bit_mask; 55 | match self.wait_until_state(PinState::Low) { 56 | Ok(_) => {} 57 | Err(err) => return Err(err), 58 | }; 59 | } 60 | } 61 | Ok(byte) 62 | } 63 | 64 | /// Waits until the pin reaches the specified state. 65 | /// 66 | /// This helper function continuously polls the pin until it reaches the desired `PinState`. 67 | /// It introduces a **1-microsecond delay** between each poll to prevent excessive CPU usage. 68 | /// 69 | /// # Arguments 70 | /// 71 | /// - `state`: The target `PinState` to wait for (`PinState::High` or `PinState::Low`). 72 | /// 73 | /// # Returns 74 | /// 75 | /// - `Ok(())`: When the pin reaches the desired state. 76 | /// - `Err(SensorError::Timeout)`: If the desired state is not reached in time. 77 | /// - `Err(SensorError::Io(...))`: If a pin error occurs while reading the pin state. 78 | /// 79 | pub fn wait_until_state(&mut self, state: PinState) -> Result<(), SensorError> { 80 | for _ in 0..DEFAULT_MAX_ATTEMPTS { 81 | let is_state = match state { 82 | PinState::Low => self.pin.is_low(), 83 | PinState::High => self.pin.is_high(), 84 | }; 85 | 86 | match is_state { 87 | Ok(true) => return Ok(()), 88 | Ok(false) => self.delay.delay_us(1), 89 | Err(_) => return Err(SensorError::PinError), 90 | } 91 | } 92 | 93 | Err(SensorError::Timeout) 94 | } 95 | } 96 | 97 | #[cfg(test)] 98 | mod tests { 99 | extern crate std; 100 | use std::io::ErrorKind; 101 | 102 | use super::*; 103 | use embedded_hal_mock::eh1::delay::NoopDelay as MockNoop; 104 | use embedded_hal_mock::eh1::digital::{Mock, State, Transaction as PinTransaction}; 105 | use embedded_hal_mock::eh1::MockError; 106 | 107 | #[test] 108 | fn test_read_byte() { 109 | // Set up the pin transactions to mock the behavior of the sensor during the reading of a byte. 110 | // Each bit read from the sensor starts with a High state that lasts long enough 111 | // to signify the bit, followed by reading whether it stays High (bit 1) or goes Low (bit 0). 112 | let expectations = [ 113 | // Bit 1 - 0 114 | PinTransaction::get(State::High), 115 | PinTransaction::get(State::Low), 116 | // Bit 2 - 1 117 | PinTransaction::get(State::High), 118 | PinTransaction::get(State::High), 119 | PinTransaction::get(State::Low), 120 | // Bit 3 - 0 121 | PinTransaction::get(State::High), 122 | PinTransaction::get(State::Low), 123 | // Bit 4 - 1 124 | PinTransaction::get(State::High), 125 | PinTransaction::get(State::High), 126 | PinTransaction::get(State::Low), 127 | // Bit 5 - 0 128 | PinTransaction::get(State::High), 129 | PinTransaction::get(State::Low), 130 | // Bit 6 - 1 131 | PinTransaction::get(State::High), 132 | PinTransaction::get(State::High), 133 | PinTransaction::get(State::Low), 134 | // Bit 7 - 1 135 | PinTransaction::get(State::High), 136 | PinTransaction::get(State::High), 137 | PinTransaction::get(State::Low), 138 | // Bit 8 - 1 139 | PinTransaction::get(State::High), 140 | PinTransaction::get(State::High), 141 | PinTransaction::get(State::Low), 142 | ]; 143 | 144 | let mock_pin = Mock::new(&expectations); 145 | let mock_delay = MockNoop::new(); 146 | 147 | let mut dht = Dht::new(mock_pin, mock_delay); 148 | 149 | let result = dht.read_byte().unwrap(); 150 | assert_eq!(result, 0b01010111); 151 | 152 | dht.pin.done(); 153 | } 154 | 155 | #[test] 156 | fn test_wait_until_state() { 157 | let expectations = [ 158 | PinTransaction::get(State::Low), 159 | PinTransaction::get(State::Low), 160 | PinTransaction::get(State::High), 161 | ]; 162 | 163 | let mock_pin = Mock::new(&expectations); 164 | let mock_delay = MockNoop::new(); 165 | 166 | let mut dht = Dht::new(mock_pin, mock_delay); 167 | 168 | let result = dht.wait_until_state(PinState::High); 169 | assert!(result.is_ok()); 170 | 171 | dht.pin.done(); 172 | } 173 | 174 | #[test] 175 | fn test_wait_until_state_timeout_error() { 176 | let expectations: [PinTransaction; DEFAULT_MAX_ATTEMPTS] = [ 177 | PinTransaction::get(State::Low), 178 | PinTransaction::get(State::Low), 179 | PinTransaction::get(State::Low), 180 | PinTransaction::get(State::Low), 181 | PinTransaction::get(State::Low), 182 | PinTransaction::get(State::Low), 183 | PinTransaction::get(State::Low), 184 | PinTransaction::get(State::Low), 185 | PinTransaction::get(State::Low), 186 | PinTransaction::get(State::Low), 187 | ]; 188 | 189 | let mock_pin = Mock::new(&expectations); 190 | let mock_delay = MockNoop::new(); 191 | 192 | let mut dht = Dht::new(mock_pin, mock_delay); 193 | 194 | let result = dht.wait_until_state(PinState::High); 195 | assert!(matches!(result, Err(SensorError::Timeout))); 196 | 197 | dht.pin.done(); 198 | } 199 | 200 | #[test] 201 | fn test_wait_until_state_pin_error() { 202 | let err = MockError::Io(ErrorKind::NotConnected); 203 | let expectations = [PinTransaction::get(State::High).with_error(err)]; 204 | let mock_pin = Mock::new(&expectations); 205 | let mock_delay = MockNoop::new(); 206 | 207 | let mut dht = Dht::new(mock_pin, mock_delay); 208 | 209 | let result = dht.wait_until_state(PinState::High); 210 | assert!(matches!(result, Err(SensorError::PinError))); 211 | 212 | dht.pin.done(); 213 | } 214 | } 215 | -------------------------------------------------------------------------------- /src/dht11.rs: -------------------------------------------------------------------------------- 1 | use embedded_hal::{ 2 | delay::DelayNs, 3 | digital::{InputPin, OutputPin, PinState}, 4 | }; 5 | 6 | use crate::{dht::Dht, SensorError, SensorReading}; 7 | 8 | pub struct Dht11 { 9 | dht: Dht, 10 | } 11 | 12 | impl Dht11 { 13 | pub fn new(pin: P, delay: D) -> Self { 14 | Self { 15 | dht: Dht::new(pin, delay), 16 | } 17 | } 18 | 19 | pub fn read(&mut self) -> Result, SensorError> { 20 | // Start communication: pull pin low for 18ms, then release. 21 | let _ = self.dht.pin.set_low(); 22 | self.dht.delay.delay_ms(18); 23 | let _ = self.dht.pin.set_high(); 24 | 25 | // Wait for sensor to respond. 26 | self.dht.delay.delay_us(48); 27 | 28 | // Sync with sensor: wait for high then low signals. 29 | let _ = self.dht.wait_until_state(PinState::High); 30 | let _ = self.dht.wait_until_state(PinState::Low); 31 | 32 | // Start reading 40 bits 33 | let humidity_integer = self.dht.read_byte()?; 34 | let humidity_decimal = self.dht.read_byte()?; 35 | let temperature_integer = self.dht.read_byte()?; 36 | let temperature_decimal = self.dht.read_byte()?; 37 | let checksum = self.dht.read_byte()?; 38 | 39 | // Checksum 40 | let sum = humidity_integer 41 | .wrapping_add(humidity_decimal) 42 | .wrapping_add(temperature_integer) 43 | .wrapping_add(temperature_decimal); 44 | if sum != checksum { 45 | return Err(SensorError::ChecksumMismatch); 46 | } 47 | 48 | Ok(SensorReading { 49 | humidity: humidity_integer, 50 | temperature: temperature_integer, 51 | }) 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/dht20.rs: -------------------------------------------------------------------------------- 1 | use embedded_hal::{delay::DelayNs, i2c::I2c}; 2 | 3 | use crate::{SensorError, SensorReading}; 4 | 5 | pub struct Dht20 { 6 | pub i2c: I, 7 | pub delay: D, 8 | } 9 | 10 | impl Dht20 { 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, 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 | } 100 | -------------------------------------------------------------------------------- /src/dht22.rs: -------------------------------------------------------------------------------- 1 | use embedded_hal::{ 2 | delay::DelayNs, 3 | digital::{InputPin, OutputPin, PinState}, 4 | }; 5 | 6 | use crate::{dht::Dht, SensorError, SensorReading}; 7 | 8 | pub struct Dht22 { 9 | dht: Dht, 10 | } 11 | 12 | impl Dht22 { 13 | pub fn new(pin: P, delay: D) -> Self { 14 | Self { 15 | dht: Dht::new(pin, delay), 16 | } 17 | } 18 | 19 | pub fn read(&mut self) -> Result, SensorError> { 20 | // Start communication: pull pin low for 18ms, then release. 21 | let _ = self.dht.pin.set_low(); 22 | self.dht.delay.delay_ms(18); 23 | let _ = self.dht.pin.set_high(); 24 | 25 | // Wait for sensor to respond. 26 | self.dht.delay.delay_us(48); 27 | 28 | // Sync with sensor: wait for high then low signals. 29 | let _ = self.dht.wait_until_state(PinState::High); 30 | let _ = self.dht.wait_until_state(PinState::Low); 31 | 32 | // Start reading 40 bits 33 | let humidity_high = self.dht.read_byte()?; 34 | let humidity_low = self.dht.read_byte()?; 35 | let temperature_high = self.dht.read_byte()?; 36 | let temperature_low = self.dht.read_byte()?; 37 | let checksum = self.dht.read_byte()?; 38 | 39 | // Checksum 40 | let sum = humidity_high 41 | .wrapping_add(humidity_low) 42 | .wrapping_add(temperature_high) 43 | .wrapping_add(temperature_low); 44 | if sum != checksum { 45 | return Err(SensorError::ChecksumMismatch); 46 | } 47 | 48 | let humidity_value = ((humidity_high as u16) << 8) | (humidity_low as u16); 49 | let humidity_percentage = humidity_value as f32 / 10.0; 50 | 51 | let temperature_high_clean = temperature_high & 0x7F; // 0x7F = 0111 1111 52 | let temperature_value = ((temperature_high_clean as u16) << 8) | (temperature_low as u16); 53 | let mut temperature_percentage = temperature_value as f32 / 10.0; 54 | 55 | if temperature_high & 0x80 != 0 { 56 | temperature_percentage = -temperature_percentage; 57 | } 58 | 59 | 60 | Ok(SensorReading { 61 | humidity: humidity_percentage, 62 | temperature: temperature_percentage, 63 | }) 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![doc = include_str!("../README.md")] 2 | #![no_std] 3 | 4 | mod dht; 5 | 6 | #[cfg(feature = "dht11")] 7 | pub mod dht11; 8 | 9 | #[cfg(feature = "dht20")] 10 | pub mod dht20; 11 | 12 | #[cfg(feature = "dht22")] 13 | pub mod dht22; 14 | 15 | /// Represents a reading from the sensor. 16 | pub struct SensorReading { 17 | pub humidity: T, 18 | pub temperature: T, 19 | } 20 | 21 | /// Possible errors when interacting with the sensor. 22 | #[derive(Debug)] 23 | pub enum SensorError { 24 | ChecksumMismatch, 25 | Timeout, 26 | PinError 27 | } 28 | --------------------------------------------------------------------------------