112 lines
3.4 KiB
C
112 lines
3.4 KiB
C
#ifndef DHT22_SENSOR_H
|
|
#define DHT22_SENSOR_H
|
|
|
|
#include "driver/gpio.h"
|
|
#include "esp_err.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C"
|
|
{
|
|
#endif
|
|
|
|
/**
|
|
* @brief DHT22 result codes
|
|
*/
|
|
typedef enum
|
|
{
|
|
DHT22_OK = 0, /**< Reading successful */
|
|
DHT22_TIMEOUT_ERROR = -1, /**< Timeout during communication */
|
|
DHT22_CHECKSUM_ERROR = -2 /**< Checksum verification failed */
|
|
} dht22_result_t;
|
|
|
|
/**
|
|
* @brief DHT22 sensor configuration
|
|
*/
|
|
typedef struct
|
|
{
|
|
int gpio_pin; /**< GPIO pin number where DHT22 is connected */
|
|
uint32_t start_signal_us; /**< Start signal duration in microseconds (default: 18000) */
|
|
uint32_t response_timeout_us; /**< Response timeout in microseconds (default: 500) */
|
|
uint32_t data_timeout_us; /**< Data bit timeout in microseconds (default: 150) */
|
|
uint32_t bit_threshold_us; /**< Threshold for distinguishing 0/1 bits (default: 40) */
|
|
} dht22_config_t;
|
|
|
|
/**
|
|
* @brief DHT22 sensor data structure
|
|
*/
|
|
typedef struct
|
|
{
|
|
float temperature; /**< Temperature in degrees Celsius */
|
|
float humidity; /**< Relative humidity percentage */
|
|
} dht22_data_t;
|
|
|
|
/**
|
|
* @brief Initialize DHT22 sensor with default configuration
|
|
*
|
|
* @param gpio_pin GPIO pin number where DHT22 is connected
|
|
* @return esp_err_t ESP_OK on success, error code on failure
|
|
*/
|
|
esp_err_t dht22_init(int gpio_pin);
|
|
|
|
/**
|
|
* @brief Initialize DHT22 sensor with custom configuration
|
|
*
|
|
* @param config DHT22 configuration parameters
|
|
* @return esp_err_t ESP_OK on success, error code on failure
|
|
*/
|
|
esp_err_t dht22_init_with_config(const dht22_config_t *config);
|
|
|
|
/**
|
|
* @brief Read temperature and humidity from DHT22 sensor
|
|
*
|
|
* @param temperature Pointer to store temperature value (in Celsius)
|
|
* @param humidity Pointer to store humidity value (in percentage)
|
|
* @return dht22_result_t DHT22_OK on success, error code on failure
|
|
*/
|
|
dht22_result_t dht22_read(float *temperature, float *humidity);
|
|
|
|
/**
|
|
* @brief Read temperature and humidity into data structure
|
|
*
|
|
* @param data Pointer to DHT22 data structure to fill
|
|
* @return dht22_result_t DHT22_OK on success, error code on failure
|
|
*/
|
|
dht22_result_t dht22_read_data(dht22_data_t *data);
|
|
|
|
/**
|
|
* @brief Read from specific GPIO pin (for multiple sensors)
|
|
*
|
|
* @param gpio_pin GPIO pin number
|
|
* @param temperature Pointer to store temperature value
|
|
* @param humidity Pointer to store humidity value
|
|
* @return dht22_result_t DHT22_OK on success, error code on failure
|
|
*/
|
|
dht22_result_t dht22_read_from_pin(int gpio_pin, float *temperature, float *humidity);
|
|
|
|
/**
|
|
* @brief Get last error message as string
|
|
*
|
|
* @param result DHT22 result code
|
|
* @return const char* Human-readable error message
|
|
*/
|
|
const char *dht22_get_error_string(dht22_result_t result);
|
|
|
|
/**
|
|
* @brief Check if DHT22 sensor is properly connected and responding
|
|
*
|
|
* @return true if sensor responds correctly, false otherwise
|
|
*/
|
|
bool dht22_test_connection(void);
|
|
|
|
/**
|
|
* @brief Deinitialize DHT22 sensor
|
|
*
|
|
* @return esp_err_t ESP_OK on success, error code on failure
|
|
*/
|
|
esp_err_t dht22_deinit(void);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif // DHT22_SENSOR_H
|