diff --git a/drivers/Makefile.include b/drivers/Makefile.include index b4c590257..c2374e6b5 100644 --- a/drivers/Makefile.include +++ b/drivers/Makefile.include @@ -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 diff --git a/drivers/dht/Makefile b/drivers/dht/Makefile new file mode 100644 index 000000000..48422e909 --- /dev/null +++ b/drivers/dht/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/drivers/dht/dht.c b/drivers/dht/dht.c new file mode 100644 index 000000000..60808009c --- /dev/null +++ b/drivers/dht/dht.c @@ -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 + * @author Christian Mehlis + * + * @} + */ + +#include + +#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; +} diff --git a/drivers/include/dht.h b/drivers/include/dht.h new file mode 100644 index 000000000..1c06cfe2b --- /dev/null +++ b/drivers/include/dht.h @@ -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 + */ + +#ifndef DHT_H +#define DHT_H + +#include + +#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 */ +/** @} */