mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-18 12:52:44 +01:00
Merge pull request #2990 from LudwigOrtmann/pr/dht
drivers/dht: initial import
This commit is contained in:
commit
b771e91644
@ -10,6 +10,9 @@ endif
|
||||
ifneq (,$(filter cc110x_legacy,$(USEMODULE)))
|
||||
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/cc110x_legacy/include
|
||||
endif
|
||||
ifneq (,$(filter dht,$(USEMODULE)))
|
||||
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/dht/include
|
||||
endif
|
||||
ifneq (,$(filter at86rf231,$(USEMODULE)))
|
||||
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/at86rf231/include
|
||||
endif
|
||||
|
1
drivers/dht/Makefile
Normal file
1
drivers/dht/Makefile
Normal file
@ -0,0 +1 @@
|
||||
include $(RIOTBASE)/Makefile.base
|
199
drivers/dht/dht.c
Normal file
199
drivers/dht/dht.c
Normal file
@ -0,0 +1,199 @@
|
||||
/*
|
||||
* Copyright 2015 Ludwig Ortmann
|
||||
* Copyright 2015 Christian Mehlis
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup driver_dht
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Device driver implementation for the DHT 11 and 22
|
||||
* temperature and humidity sensor
|
||||
*
|
||||
* @author Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
|
||||
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "hwtimer.h"
|
||||
#include "timex.h"
|
||||
#include "periph/gpio.h"
|
||||
|
||||
#include "dht.h"
|
||||
|
||||
#define ENABLE_DEBUG (0)
|
||||
#include "debug.h"
|
||||
|
||||
/***********************************************************************
|
||||
* internal API declaration
|
||||
**********************************************************************/
|
||||
|
||||
static void dht_read_data(gpio_t dev, uint32_t *data, uint8_t *checksum);
|
||||
static int dht_test_checksum(uint32_t data, uint8_t checksum);
|
||||
void dht_parse_11(dht_data_t *data, float *outhum, float *outtemp);
|
||||
void dht_parse_22(dht_data_t *data, float *outhum, float *outtemp);
|
||||
|
||||
/***********************************************************************
|
||||
* internal API implementation
|
||||
**********************************************************************/
|
||||
|
||||
void dht_parse_11(dht_data_t *data, float *outhum, float *outtemp)
|
||||
{
|
||||
*outhum = data->humidity >> 8;
|
||||
*outtemp = data->temperature >> 8;
|
||||
}
|
||||
|
||||
void dht_parse_22(dht_data_t *data, float *outhum, float *outtemp)
|
||||
{
|
||||
*outhum = data->humidity / 10;
|
||||
|
||||
/* the highest bit indicates a negative value */
|
||||
if (data->temperature & 0x8000) {
|
||||
*outtemp = (data->temperature & 0x7FFF) / -10;
|
||||
}
|
||||
else {
|
||||
*outtemp = data->temperature / 10;
|
||||
}
|
||||
}
|
||||
|
||||
static int dht_test_checksum(uint32_t data, uint8_t checksum)
|
||||
{
|
||||
uint8_t sum;
|
||||
sum = (data >> 0) & 0x000000FF;
|
||||
sum += (data >> 8) & 0x000000FF;
|
||||
sum += (data >> 16) & 0x000000FF;
|
||||
sum += (data >> 24) & 0x000000FF;
|
||||
|
||||
return ((checksum == sum) && (checksum != 0));
|
||||
}
|
||||
|
||||
static void dht_read_data(gpio_t dev, uint32_t *data, uint8_t *checksum)
|
||||
{
|
||||
/* send init signal to device */
|
||||
gpio_clear(dev);
|
||||
hwtimer_wait(HWTIMER_TICKS(20 * MS_IN_USEC));
|
||||
gpio_set(dev);
|
||||
hwtimer_wait(HWTIMER_TICKS(40));
|
||||
|
||||
/* sync on device */
|
||||
gpio_init_in(dev, GPIO_PULLUP);
|
||||
while (!gpio_read(dev)) ;
|
||||
while (gpio_read(dev)) ;
|
||||
|
||||
/*
|
||||
* data is read in sequentially, highest bit first:
|
||||
* 40 .. 24 23 .. 8 7 .. 0
|
||||
* [humidity][temperature][checksum]
|
||||
*/
|
||||
|
||||
/* read all the bits */
|
||||
uint64_t les_bits = 0x00000000000000;
|
||||
for (int c = 0; c < 40; c++) {
|
||||
les_bits <<= 1; /* this is a nop in the first iteration, but
|
||||
we must not shift the last bit */
|
||||
/* wait for start of bit */
|
||||
while (!gpio_read(dev)) ;
|
||||
unsigned long start = hwtimer_now();
|
||||
/* wait for end of bit */
|
||||
while (gpio_read(dev)) ;
|
||||
/* calculate bit length (long 1, short 0) */
|
||||
unsigned long stop = hwtimer_now();
|
||||
/* compensate for overflow if needed */
|
||||
if (stop < start) {
|
||||
stop = HWTIMER_MAXTICKS - stop;
|
||||
start = 0;
|
||||
}
|
||||
if ((stop - start) > 40) {
|
||||
/* read 1, set bit */
|
||||
les_bits |= 0x0000000000000001;
|
||||
}
|
||||
else {
|
||||
/* read 0, don't set bit */
|
||||
}
|
||||
}
|
||||
|
||||
*checksum = les_bits & 0x00000000000000FF;
|
||||
*data = (les_bits >> 8) & 0x00000000FFFFFFFF;
|
||||
|
||||
gpio_init_out(dev, GPIO_PULLUP);
|
||||
gpio_set(dev);
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* public API implementation
|
||||
**********************************************************************/
|
||||
|
||||
int dht_init(dht_t *dev, dht_type_t type, gpio_t gpio)
|
||||
{
|
||||
DEBUG("dht_init\n");
|
||||
|
||||
dev->gpio = gpio;
|
||||
dev->type = type;
|
||||
|
||||
if (gpio_init_out(gpio, GPIO_PULLUP) == -1) {
|
||||
return -1;
|
||||
}
|
||||
gpio_set(gpio);
|
||||
|
||||
hwtimer_wait(HWTIMER_TICKS(2000 * MS_IN_USEC));
|
||||
|
||||
DEBUG("dht_init: success\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int dht_read_raw(dht_t *dev, dht_data_t *outdata)
|
||||
{
|
||||
uint32_t data;
|
||||
uint8_t checksum;
|
||||
|
||||
/* read raw data */
|
||||
dht_read_data(dev->gpio, &data, &checksum);
|
||||
|
||||
/* check checksum */
|
||||
int ret = dht_test_checksum(data, checksum);
|
||||
if (!ret) {
|
||||
DEBUG("checksum fail\n");
|
||||
}
|
||||
|
||||
outdata->humidity = data >> 16;
|
||||
outdata->temperature = data & 0x0000FFFF;
|
||||
|
||||
return (ret - 1); /* take that logic! */
|
||||
}
|
||||
|
||||
void dht_parse(dht_t *dev, dht_data_t *data, float *outrelhum, float *outtemp)
|
||||
{
|
||||
switch (dev->type) {
|
||||
case (DHT11):
|
||||
dht_parse_11(data, outrelhum, outtemp);
|
||||
break;
|
||||
|
||||
case DHT22:
|
||||
dht_parse_22(data, outrelhum, outtemp);
|
||||
break;
|
||||
|
||||
default:
|
||||
DEBUG("unknown DHT type\n");
|
||||
}
|
||||
}
|
||||
|
||||
int dht_read(dht_t *dev, float *outrelhum, float *outtemp)
|
||||
{
|
||||
/* read data, fail on error */
|
||||
dht_data_t data;
|
||||
if (dht_read_raw(dev, &data) == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
dht_parse(dev, &data, outrelhum, outtemp);
|
||||
return 0;
|
||||
}
|
113
drivers/include/dht.h
Normal file
113
drivers/include/dht.h
Normal file
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright 2015 Ludwig Ortmann, Christian Mehlis
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup driver_dht DHT Family of Humidity and Temperature Sensors
|
||||
* @ingroup drivers
|
||||
* @brief Device driver for the DHT Family of humidity
|
||||
* and temperature sensors
|
||||
*
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Device driver interface for the DHT family of humidity
|
||||
* and temperature sensors
|
||||
*
|
||||
* @author Ludwig Ortmann <ludwig.ortmann@fu-berlin.de
|
||||
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
||||
*/
|
||||
|
||||
#ifndef DHT_H
|
||||
#define DHT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "periph/gpio.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief data type for storing DHT sensor readings
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t humidity; /**< relative deca-humidity */
|
||||
uint16_t temperature; /**< temperature in deca-Celsius */
|
||||
} dht_data_t;
|
||||
|
||||
/**
|
||||
* @brief device type of the DHT device
|
||||
*/
|
||||
typedef enum {
|
||||
DHT11, /**< DHT11 device identifier */
|
||||
DHT22 /**< DHT22 device identifier */
|
||||
} dht_type_t;
|
||||
|
||||
/**
|
||||
* @brief device descriptor for DHT sensor devices
|
||||
*/
|
||||
typedef struct {
|
||||
gpio_t gpio; /**< GPIO pin of the device's data pin */
|
||||
dht_type_t type; /**< type of the DHT device */
|
||||
} dht_t;
|
||||
|
||||
/**
|
||||
* @brief initialize a new DHT device
|
||||
*
|
||||
* @param[in] dev device descriptor of a DHT device
|
||||
* @param[in] type type of the DHT device
|
||||
* @param[in] gpio GPIO pin the device's data pin is connected to
|
||||
*
|
||||
* @return 0 on success
|
||||
* @return -1 on error
|
||||
*/
|
||||
int dht_init(dht_t *dev, dht_type_t type, gpio_t gpio);
|
||||
|
||||
/**
|
||||
* @brief read sensor data from device
|
||||
*
|
||||
* @param[in] dev device descriptor of a DHT device
|
||||
* @param[out] relhum pointer to relative humidity in g/m^3
|
||||
* @param[out] temp pointer to temperature in degrees Celsius
|
||||
*
|
||||
* @return 0 on success
|
||||
* @return -1 on error
|
||||
*/
|
||||
int dht_read(dht_t *dev, float *relhum, float *temp);
|
||||
|
||||
/**
|
||||
* @brief read sensor data from device
|
||||
*
|
||||
* @note When reading fails and the function indicates an error,
|
||||
* data will contain garbage.
|
||||
*
|
||||
* @param[in] dev device descriptor of a DHT device
|
||||
* @param[in] data pointer to DHT data object
|
||||
*
|
||||
* @return 0 on success
|
||||
* @return -1 on checksum error
|
||||
*/
|
||||
int dht_read_raw(dht_t *dev, dht_data_t *data);
|
||||
|
||||
/**
|
||||
* @brief parse raw sensor data into relative humidity and Celsius
|
||||
*
|
||||
* @param[in] dev device descriptor of a DHT device
|
||||
* @param[in] data sensor data
|
||||
* @param[out] relhum pointer to relative humidity in g/m^3
|
||||
* @param[out] temp pointer to temperature in degrees Celsius
|
||||
*/
|
||||
void dht_parse(dht_t *dev, dht_data_t *data, float *relhum, float *temp);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DHT_H */
|
||||
/** @} */
|
27
tests/driver_dht/Makefile
Normal file
27
tests/driver_dht/Makefile
Normal file
@ -0,0 +1,27 @@
|
||||
APPLICATION = driver_dht
|
||||
include ../Makefile.tests_common
|
||||
|
||||
FEATURES_REQUIRED = periph_gpio
|
||||
|
||||
# define default pin mappings for some boards:
|
||||
ifneq (,$(filter stm32f4discovery,$(BOARD)))
|
||||
export DHT_GPIO ?= GPIO_4
|
||||
endif
|
||||
|
||||
USEMODULE += dht
|
||||
|
||||
ifneq (,$(DHT_TYPE))
|
||||
CFLAGS += -DDHT_TYPE=$(DHT_TYPE)
|
||||
else
|
||||
# set random default
|
||||
CFLAGS += -DDHT_TYPE=DHT11
|
||||
endif
|
||||
|
||||
ifneq (,$(DHT_GPIO))
|
||||
CFLAGS += -DDHT_GPIO=$(DHT_GPIO)
|
||||
else
|
||||
# set random default
|
||||
CFLAGS += -DDHT_GPIO=GPIO_0
|
||||
endif
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
66
tests/driver_dht/main.c
Normal file
66
tests/driver_dht/main.c
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Ludwig Ortmann, Christian Mehlis
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup tests
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Test application for the dht humidity and temperature sensor driver
|
||||
*
|
||||
* @author Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
|
||||
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifndef DHT_TYPE
|
||||
#error "DHT_TYPE not defined"
|
||||
#endif
|
||||
|
||||
#ifndef DHT_GPIO
|
||||
#error "DHT_GPIO not defined"
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "hwtimer.h"
|
||||
#include "timex.h"
|
||||
|
||||
#include "dht.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
dht_t dev;
|
||||
|
||||
puts("DHT temperature and humidity sensor test application\n");
|
||||
|
||||
printf("Initializing DHT sensor at GPIO_%i... ", DHT_GPIO);
|
||||
if (dht_init(&dev, DHT_TYPE, DHT_GPIO) == 0) {
|
||||
puts("[OK]\n");
|
||||
}
|
||||
else {
|
||||
puts("[Failed]");
|
||||
return 1;
|
||||
}
|
||||
|
||||
dht_data_t data;
|
||||
float temp, hum;
|
||||
while (1) {
|
||||
|
||||
if (dht_read_raw(&dev, &data) == -1) {
|
||||
puts("error reading data");
|
||||
}
|
||||
dht_parse(&dev, &data, &hum, &temp);
|
||||
printf("raw relative humidity: %i\nraw temperature: %i C\n", data.humidity, data.temperature);
|
||||
printf("relative humidity: %i\ntemperature: %i C\n", (int) hum, (int) temp);
|
||||
hwtimer_wait(HWTIMER_TICKS(2000 * MS_IN_USEC));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user