Basic routines for interfacing a DHT sensor (DHT11, DHT22)
It is easy to port this library to any platform. But now it is ready for use in:
- STM32 (HAL)
- ESP32 (esp-idf)
- AVR (ATmega32)
- Add
DHT.h
andDHT.c
files to your project. It is optional to useDHT_platform.h
andDHT_platform.c
files (open and configDHT_platform.h
file). - Initialize platform-dependent part of handler.
- Call
DHT_Init()
. - Call other functions and enjoy.
Using DHT_platform files
#include <stdio.h>
#include "DHT.h"
#include "DHT_platform.h"
int main(void)
{
DHT_Handler_t Handler = {0};
DHT_Platform_Init(&Handler);
DHT_Init(&Handler, DHT_TYPE_DHT22);
while (1)
{
float Temp = 0
float Hum = 0;
DHT_ReadSample(&Handler, &Temp, &Hum);
printf("Temp: %f, Hum: %f\r\n", Temp, Hum);
}
return 0;
}
Without using DHT_platform files (ATmega32)
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdint.h>
#include "DHT.h"
#define DHT_PORT PORTB
#define DHT_PIN PINB
#define DHT_DDR DDRB
#define DHT_BIT 0
static void
PlatformSetDir(uint8_t Dir)
{
if (Dir)
DHT_DDR |= (1 << DHT_BIT);
else
DHT_DDR &= ~(1 << DHT_BIT);
}
static void
PlatformWrite(uint8_t Level)
{
if (Level)
DHT_PORT |= (1 << DHT_BIT);
else
DHT_PORT &= ~(1 << DHT_BIT);
}
static uint8_t
PlatformRead(void)
{
return ((DHT_PIN >> DHT_BIT) & 0x01);
}
static void
PlatformDelayMs(uint16_t Delay)
{
for (; Delay > 0; Delay--)
_delay_ms(1);
}
static void
PlatformDelayUs(uint16_t Delay)
{
for (; Delay > 0; Delay--)
_delay_us(1);
}
int main(void)
{
DHT_Handler_t Handler = {0};
Handler.PlatformSetDir = PlatformSetDir;
Handler.PlatformWrite = PlatformWrite;
Handler.PlatformRead = PlatformRead;
Handler.PlatformDelayMs = PlatformDelayMs;
Handler.PlatformDelayUs = PlatformDelayUs;
DHT_Init(&Handler, DHT_TYPE_DHT22);
while (1)
{
float Temp = 0
float Hum = 0;
DHT_ReadSample(&Handler, &Temp, &Hum);
// We consider that the UART is initialized and STDOUT is redirected to it
printf("Temp: %f, Hum: %f\r\n", Temp, Hum);
}
return 0;
}