{"id":24091480,"url":"https://github.com/luhuadong/rtt-dhtxx","last_synced_at":"2025-07-27T19:05:32.743Z","repository":{"id":109656460,"uuid":"235070812","full_name":"luhuadong/rtt-dhtxx","owner":"luhuadong","description":"Digital Humidity and Temperature sensor (communicate via single bus), including DHT11, DHT21 and DHT22","archived":false,"fork":false,"pushed_at":"2023-10-09T17:38:56.000Z","size":1461,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-10-09T18:38:00.301Z","etag":null,"topics":["rt-thread"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luhuadong.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-01-20T10:03:28.000Z","updated_at":"2022-08-03T08:03:00.000Z","dependencies_parsed_at":"2023-07-29T12:00:53.102Z","dependency_job_id":null,"html_url":"https://github.com/luhuadong/rtt-dhtxx","commit_stats":null,"previous_names":[],"tags_count":3,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luhuadong%2Frtt-dhtxx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luhuadong%2Frtt-dhtxx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luhuadong%2Frtt-dhtxx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luhuadong%2Frtt-dhtxx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luhuadong","download_url":"https://codeload.github.com/luhuadong/rtt-dhtxx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233312100,"owners_count":18657022,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rt-thread"],"created_at":"2025-01-10T07:44:28.869Z","updated_at":"2025-01-10T07:44:33.047Z","avatar_url":"https://github.com/luhuadong.png","language":"C","funding_links":[],"categories":["Packages"],"sub_categories":["Peripherals"],"readme":"# rtt-dhtxx\nDigital Humidity and Temperature sensor (communicate via single bus), including DHT11, DHT12, DHT21, DHT22 ...\n\n\n\n## 1、介绍\n\ndhtxx 是基于 RT-Thread 的 DHTxx 系列传感器驱动软件包，适用于单总线式数字温湿度传感器，包括 DHT11、DHT12、DHT21 和 DHT22 等型号，并提供两种 API 操作接口。\n\n\n\n### 1.1 特性\n\n- 支持多实例。\n- 支持静态和动态分配内存。\n- 支持 sensor 设备驱动框架。\n- 线程安全。\n\n\n\n### 1.2 工作模式\n\n| 传感器       | 温度 | 湿度 |\n| :----------- | :--- | :--- |\n| **通信接口** |      |      |\n| 单总线       | √    | √    |\n| **工作模式** |      |      |\n| 轮询         | √    | √    |\n| 中断         |      |      |\n| FIFO         |      |      |\n\n\n\n### 1.3 目录结构\n\n| 名称     | 说明                           |\n| -------- | ------------------------------ |\n| docs     | 文档目录                       |\n| examples | 例子目录（提供两种操作示例）   |\n| inc      | 头文件目录                     |\n| src      | 源代码目录（提供两种驱动接口） |\n\n驱动源代码提供两种接口，分别是自定义接口，以及RT-Thread设备驱动接口（open/read/control/close）。\n\n\n\n### 1.4 许可证\n\ndhtxx 软件包遵循 Apache license v2.0 许可，详见 `LICENSE` 文件。\n\n\n\n### 1.5 依赖\n\n- RT-Thread 4.0+\n- 使用动态创建方式需要开启动态内存管理模块\n- 使用 sensor 设备接口需要开启 sensor 设备驱动框架模块\n\n\n\n## 2、获取 dhtxx 软件包\n\n使用 dhtxx package 需要在 RT-Thread 的包管理器中选择它，具体路径如下：\n\n```\nRT-Thread online packages ---\u003e\n    peripheral libraries and drivers ---\u003e\n        [*] sensors drivers  ---\u003e\n            [*] DHTxx one-wire digital temperature and humidity sensor.\n```\n\n然后让 RT-Thread 的包管理器自动更新，或者使用 `pkgs --update` 命令更新包到 BSP 中。\n\n\n\n## 3、使用 dhtxx 软件包\n\n### 3.1 版本说明\n\n| 版本   | 说明                                                         |\n| ------ | ------------------------------------------------------------ |\n| v0.8.x | 支持自定义接口和 sensor 框架，通过传入 type 参数支持多个不同型号的传感器设备 |\n| v0.9.0 | 使用 Kconfig 方式配置类型，取消 type 参数和 dht_info 结构体，使得接口更简洁且更符合 sensor 框架逻辑 |\n| latest | 进一步优化                                                   |\n\n注意：如果您有在一台设备上接入多个不同型号 DHT 传感器的需求，请继续使用 v0.8.x 版本。其余开发阶段，建议使用 latest 版本。\n\n\n\n### 3.2 配置选项\n\n- 选择传感器类型（目前可选 DHT11、DHT12、DHT21、DHT22，请根据规格书选择兼容型号）\n- 是否开启调试输出（将会打印更多调试信息）\n- 是否使用示例程序（添加 msh 示例程序 `cat_dhtxx` 和 `dhtxx_read_sample`，亦可通过 sensor 命令进行测试）\n  - 示例程序使用的 data 引脚号\n\n\n\n## 4、API 说明\n\n### 4.1 自定义接口\n\n#### 创建和删除对象\n\n要操作传感器模块，首先需要创建一个传感器对象。\n\n```c\ndht_device_t dht_create(const rt_base_t pin);\n```\n\n调用这个函数时，会从动态堆内存中分配一个 dht_device_t 句柄，并按给定参数初始化。\n\n| 参数         | 描述                         |\n| ------------ | ---------------------------- |\n| pin          | 数据输出引脚                 |\n| **返回**     | ——                           |\n| dht_device_t | 创建成功，返回传感器对象句柄 |\n| RT_NULL      | 创建失败                     |\n\n对于使用 `dht_create()` 创建出来的对象，当不需要使用，或者运行出错时，请使用下面的函数接口将其删除，避免内存泄漏。\n\n```c\nvoid dht_delete(dht_device_t dev);\n```\n\n| **参数**     | **描述**               |\n| ------------ | ---------------------- |\n| dht_device_t | 要删除的传感器对象句柄 |\n| **返回**     | ——                     |\n| 无           |                        |\n\n\n\n#### 初始化对象\n\n如果需要使用静态内存分配，则可调用 `dht_init()` 函数。\n\n```c\nrt_err_t   dht_init(struct dht_device *dev, const rt_base_t pin);\n```\n\n使用该函数前需要先创建 dht_device 结构体。\n\n| 参数      | 描述             |\n| --------- | ---------------- |\n| dev       | 传感器对象结构体 |\n| pin       | 数据输出引脚     |\n| **返回**  | ——               |\n| RT_EOK    | 初始化成功       |\n| -RT_ERROR | 初始化失败       |\n\n\n\n#### 读传感器\n\n读取温湿度值，并将数据保存在传感器对象中。\n\n```c\nrt_bool_t  dht_read(dht_device_t dev);\n```\n\n| 参数     | 描述           |\n| -------- | -------------- |\n| dev      | 传感器对象句柄 |\n| **返回** | ——             |\n| RT_TRUE  | 读取成功       |\n| RT_FALSE | 读取失败       |\n\n由于 DHTxx 系列传感器模块内包含温度和湿度传感器，并且一次访问即可获取温度和湿度值，因此获取具体的温度或湿度值需要调用下面两个函数。\n\n\n\n#### 获取湿度值\n\n```c\nrt_int32_t dht_get_humidity(dht_device_t dev);\n```\n\n| 参数     | 描述                 |\n| -------- | -------------------- |\n| dev      | 传感器对象句柄       |\n| **返回** | ——                   |\n| 湿度值   | 数值为真实湿度的十倍 |\n\n\n\n#### 获取温度值\n\n```c\nrt_int32_t dht_get_temperature(dht_device_t dev);\n```\n\n| 参数     | 描述                 |\n| -------- | -------------------- |\n| dev      | 传感器对象句柄       |\n| **返回** | ——                   |\n| 温度值   | 数值为真实温度的十倍 |\n\n\n\n### 4.2 Sensor 接口\n\ndhtxx 软件包已对接 sensor 驱动框架，操作传感器模块之前，只需调用下面接口注册传感器设备即可。\n\n```c\nrt_err_t rt_hw_dht_init(const char *name, struct rt_sensor_config *cfg);\n```\n\n| 参数      | 描述            |\n| --------- | --------------- |\n| name      | 传感器设备名称  |\n| cfg       | sensor 配置信息 |\n| **返回**  | ——              |\n| RT_EOK    | 创建成功        |\n| -RT_ERROR | 创建失败        |\n\n初始化示例：\n\n```c\nstatic int rt_hw_dht_port(void)\n{\n    struct rt_sensor_config cfg;\n    \n    cfg.intf.type = RT_SENSOR_INTF_ONEWIRE;\n    cfg.intf.user_data = (void *)DATA_PIN;\n    rt_hw_dht_init(\"dht\", \u0026cfg);\n    \n    return RT_EOK;\n}\nINIT_COMPONENT_EXPORT(rt_hw_dht_port);\n```\n\nv0.9.0 以上版本不再需要构建 `dht_info` 结构体，在 menuconfig 或 studio 中配置传感器类型即可。\n\n\n\n### 4.3 其他\n\n另外，在 dhtxx 软件包中还提供了下面一些接口。\n\n#### 温度转换\n\n将摄氏温度转为开氏温度\n\n```c\nfloat convert_c2k(float c);\n```\n\n将摄氏温度转为华氏温度\n\n```c\nfloat convert_c2f(float c);\n```\n\n将华氏温度转为摄氏温度\n\n```c\nfloat convert_f2c(float f);\n```\n\n\n\n#### 数值转换\n\n将一个数按倍数关系拆分成整数和小数部分。\n\n```c\nrt_int32_t split_int(const rt_int32_t num, rt_int32_t *integer, \n                     rt_int32_t *decimal, const rt_uint32_t times);\n```\n\n| 参数     | 描述              |\n| -------- | ----------------- |\n| num      | 待转换的数值      |\n| integer  | 转换后的整数部分  |\n| decimal  | 转换后的小数部分  |\n| times    | 倍数              |\n| **返回** | ——                |\n| 0        | 正数（大于等于0） |\n| 1        | 负数（小于0）     |\n\n针对 dhtxx 软件包中输出的温湿度都是真实值的十倍，为了保持精度，可以这样输出：\n\n```c\nrt_int32_t integer;\nrt_int32_t decimal;\nrt_int32_t flag = split_int(dht_get_temperature(dev), \u0026integer, \u0026decimal, 10);\nrt_kprintf(\"Temp: %s%d.%d\\n\", flag \u003e 0 ? \"-\" : \"\", integer, decimal);\n```\n\n\n\n## 5、注意事项\n\n1. 为传感器对象提供静态创建和动态创建两种方式，如果使用动态创建，请记得在使用完毕释放对应的内存空间。\n\n2. 由于 DHTxx 模块包含一个温度传感器和一个湿度传感器，因此在 sensor 框架中会注册两个设备，内部提供 1 位 FIFO 缓存进行同步，缓存空间在调用 `rt_device_open` 函数时创建，因此 read 之前务必确保两个设备都开启成功。\n\n3. v0.9.0 以上版本的初始化函数取消了 type 参数，以及 `dht_info` 结构体，传感器类型的配置移到 Kconfig，如果需要在一台设备上接入多个不同型号 DHT 传感器，请继续使用 v0.8.x 版本。其余开发阶段，建议使用新版本。\n\n   \u003e v0.8.x 版本：dhtxx 软件包为支持 DHT11、DHT22 等多种传感器类型，在 sensor 接口部分定义了 `dht_info` 结构体，因此在调用 `rt_hw_dht_init` 初始化前需要构建并传入该结构体指针。\n\n   \n\n4. 获取的温度和湿度值均为真实值的十倍，因此应用层需要进行转换，软件包提供了 `split_int` 函数进行处理，可根据您的实际情况选择使用。\n\n\n\n## 6、相关文档\n\n见 docs 目录。\n\n\n\n## 7、联系方式\n\n- 维护：luhuadong@163.com\n- 主页：\u003chttps://github.com/luhuadong/rtt-dhtxx\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluhuadong%2Frtt-dhtxx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluhuadong%2Frtt-dhtxx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluhuadong%2Frtt-dhtxx/lists"}