Skip to content

Instantly share code, notes, and snippets.

@mi6crazyheart
Created October 16, 2023 20:42
Show Gist options
  • Save mi6crazyheart/d5da57df382067e5273a08f1a622e3dd to your computer and use it in GitHub Desktop.
Save mi6crazyheart/d5da57df382067e5273a08f1a622e3dd to your computer and use it in GitHub Desktop.
How to read humidity & temperature data using a DMT11 sensor in ESP32 through ESP-IDF?
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <dht.h>
#define SENSOR_TYPE DHT_TYPE_DHT11
#define DHT_GPIO_PIN GPIO_NUM_26
void dht_test(void *pvParameters)
{
float temperature, humidity;
while (1)
{
if (dht_read_float_data(SENSOR_TYPE, DHT_GPIO_PIN, &humidity, &temperature) == ESP_OK)
printf("Humidity: %.1f%% Temp: %.1fC\n", humidity, temperature);
else
printf("Could not read data from sensor\n");
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
void app_main(void)
{
xTaskCreate(dht_test, "dht_test", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment