diff --git a/examples/dht_sensor/Makefile b/examples/dht_sensor/Makefile index cfdde69..79b8d3a 100644 --- a/examples/dht_sensor/Makefile +++ b/examples/dht_sensor/Makefile @@ -1,4 +1,4 @@ -PROGRAM=dht_sensor +PROGRAM = dht_sensor EXTRA_COMPONENTS = extras/dht include ../../common.mk diff --git a/examples/dht_sensor/dht_sensor.c b/examples/dht_sensor/dht_sensor.c index b031672..32988b1 100644 --- a/examples/dht_sensor/dht_sensor.c +++ b/examples/dht_sensor/dht_sensor.c @@ -8,7 +8,7 @@ #include "esp/uart.h" #include "FreeRTOS.h" #include "task.h" -#include "dht.h" +#include #include "esp8266.h" /* An example using the ubiquitous DHT** humidity sensors @@ -16,6 +16,7 @@ * from a sensor attached to GPIO pin 4. */ uint8_t const dht_gpio = 4; +const dht_sensor_type_t sensor_type = DHT_TYPE_DHT22; void dhtMeasurementTask(void *pvParameters) { @@ -28,7 +29,7 @@ void dhtMeasurementTask(void *pvParameters) gpio_set_pullup(dht_gpio, false, false); while(1) { - if (dht_read_data(dht_gpio, &humidity, &temperature)) { + if (dht_read_data(sensor_type, dht_gpio, &humidity, &temperature)) { printf("Humidity: %d%% Temp: %dC\n", humidity / 10, temperature / 10); diff --git a/extras/dht/component.mk b/extras/dht/component.mk index 0948e4d..f7ec19e 100644 --- a/extras/dht/component.mk +++ b/extras/dht/component.mk @@ -1,10 +1,10 @@ # Component makefile for extras/dht -INC_DIRS += $(ROOT)extras/dht +# include it as 'dht/dht.h' +INC_DIRS += $(dht_ROOT).. # args for passing into compile rule generation -extras/dht_INC_DIR = $(ROOT)extras/dht -extras/dht_SRC_DIR = $(ROOT)extras/dht +dht_SRC_DIR = $(dht_ROOT) -$(eval $(call component_compile_rules,extras/dht)) +$(eval $(call component_compile_rules,dht)) diff --git a/extras/dht/dht.c b/extras/dht/dht.c index 3930735..dee04f2 100644 --- a/extras/dht/dht.c +++ b/extras/dht/dht.c @@ -14,11 +14,10 @@ #include // sdk_os_delay_us // DHT timer precision in microseconds -#define DHT_TIMER_INTERVAL 2 -#define DHT_DATA_BITS 40 +#define DHT_TIMER_INTERVAL 2 +#define DHT_DATA_BITS 40 // #define DEBUG_DHT - #ifdef DEBUG_DHT #define debug(fmt, ...) printf("%s" fmt "\n", "dht: ", ## __VA_ARGS__); #else @@ -116,7 +115,7 @@ static inline bool dht_fetch_data(uint8_t pin, bool bits[DHT_DATA_BITS]) debug("LOW bit timeout\n"); return false; } - if (!dht_await_pin_state(pin, 75, false, &high_duration)){ + if (!dht_await_pin_state(pin, 75, false, &high_duration)) { debug("HIGHT bit timeout\n"); return false; } @@ -128,27 +127,26 @@ static inline bool dht_fetch_data(uint8_t pin, bool bits[DHT_DATA_BITS]) /** * Pack two data bytes into single value and take into account sign bit. */ -static inline int16_t dht_convert_data(uint8_t msb, uint8_t lsb) +static inline int16_t dht_convert_data(dht_sensor_type_t sensor_type, uint8_t msb, uint8_t lsb) { int16_t data; -#if DHT_TYPE == DHT22 - data = msb & 0x7F; - data <<= 8; - data |= lsb; - if (msb & BIT(7)) { - data = 0 - data; // convert it to negative + if (sensor_type == DHT_TYPE_DHT22) { + data = msb & 0x7F; + data <<= 8; + data |= lsb; + if (msb & BIT(7)) { + data = 0 - data; // convert it to negative + } + } + else { + data = msb * 10; } -#elif DHT_TYPE == DHT11 - data = msb * 10; -#else -#error "Unsupported DHT type" -#endif return data; } -bool dht_read_data(uint8_t pin, int16_t *humidity, int16_t *temperature) +bool dht_read_data(dht_sensor_type_t sensor_type, uint8_t pin, int16_t *humidity, int16_t *temperature) { bool bits[DHT_DATA_BITS]; uint8_t data[DHT_DATA_BITS/8] = {0}; @@ -175,19 +173,19 @@ bool dht_read_data(uint8_t pin, int16_t *humidity, int16_t *temperature) return false; } - *humidity = dht_convert_data(data[0], data[1]); - *temperature = dht_convert_data(data[2], data[3]); + *humidity = dht_convert_data(sensor_type, data[0], data[1]); + *temperature = dht_convert_data(sensor_type, data[2], data[3]); debug("Sensor data: humidity=%d, temp=%d\n", *humidity, *temperature); return true; } -bool dht_read_float_data(uint8_t pin, float *humidity, float *temperature) +bool dht_read_float_data(dht_sensor_type_t sensor_type, uint8_t pin, float *humidity, float *temperature) { int16_t i_humidity, i_temp; - if (dht_read_data(pin, &i_humidity, &i_temp)) { + if (dht_read_data(sensor_type, pin, &i_humidity, &i_temp)) { *humidity = (float)i_humidity / 10; *temperature = (float)i_temp / 10; return true; diff --git a/extras/dht/dht.h b/extras/dht/dht.h index ab22269..664e95c 100644 --- a/extras/dht/dht.h +++ b/extras/dht/dht.h @@ -11,16 +11,19 @@ #include #include -#define DHT11 11 -#define DHT22 22 - -// Type of sensor to use -#define DHT_TYPE DHT22 - #ifdef __cplusplus extern "C" { #endif +/** + * Sensor type + */ +typedef enum +{ + DHT_TYPE_DHT11 = 0, //!< DHT11 + DHT_TYPE_DHT22 //!< DHT22 +} dht_sensor_type_t; + /** * Read data from sensor on specified pin. * @@ -29,7 +32,7 @@ extern "C" { * temperature=24.4 is 24.4 degrees Celsius * */ -bool dht_read_data(uint8_t pin, int16_t *humidity, int16_t *temperature); +bool dht_read_data(dht_sensor_type_t sensor_type, uint8_t pin, int16_t *humidity, int16_t *temperature); /** @@ -37,7 +40,7 @@ bool dht_read_data(uint8_t pin, int16_t *humidity, int16_t *temperature); * * Return values as floating point values. */ -bool dht_read_float_data(uint8_t pin, float *humidity, float *temperature); +bool dht_read_float_data(dht_sensor_type_t sensor_type, uint8_t pin, float *humidity, float *temperature); #ifdef __cplusplus }