2016-04-20 22:03:18 +00:00
|
|
|
/*
|
|
|
|
* Part of esp-open-rtos
|
|
|
|
* Copyright (C) 2016 Jonathan Hartsuiker (https://github.com/jsuiker)
|
|
|
|
* BSD Licensed as described in the file LICENSE
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dht.h"
|
2016-07-06 12:57:00 +00:00
|
|
|
#include "FreeRTOS.h"
|
2016-04-20 22:03:18 +00:00
|
|
|
#include "string.h"
|
|
|
|
#include "task.h"
|
|
|
|
#include "esp/gpio.h"
|
|
|
|
|
|
|
|
#include <espressif/esp_misc.h> // sdk_os_delay_us
|
|
|
|
|
2016-07-06 12:57:00 +00:00
|
|
|
// DHT timer precision in microseconds
|
2016-10-26 13:41:08 +00:00
|
|
|
#define DHT_TIMER_INTERVAL 2
|
|
|
|
#define DHT_DATA_BITS 40
|
2016-07-06 12:57:00 +00:00
|
|
|
|
|
|
|
// #define DEBUG_DHT
|
|
|
|
#ifdef DEBUG_DHT
|
2016-04-20 22:03:18 +00:00
|
|
|
#define debug(fmt, ...) printf("%s" fmt "\n", "dht: ", ## __VA_ARGS__);
|
|
|
|
#else
|
|
|
|
#define debug(fmt, ...) /* (do nothing) */
|
|
|
|
#endif
|
|
|
|
|
2016-07-06 12:57:00 +00:00
|
|
|
/*
|
2016-04-20 22:03:18 +00:00
|
|
|
* Note:
|
|
|
|
* A suitable pull-up resistor should be connected to the selected GPIO line
|
|
|
|
*
|
|
|
|
* __ ______ _______ ___________________________
|
|
|
|
* \ A / \ C / \ DHT duration_data_low / \
|
|
|
|
* \_______/ B \______/ D \__________________________/ DHT duration_data_high \__
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Initializing communications with the DHT requires four 'phases' as follows:
|
2016-07-06 12:57:00 +00:00
|
|
|
*
|
2016-04-20 22:03:18 +00:00
|
|
|
* Phase A - MCU pulls signal low for at least 18000 us
|
|
|
|
* Phase B - MCU allows signal to float back up and waits 20-40us for DHT to pull it low
|
|
|
|
* Phase C - DHT pulls signal low for ~80us
|
|
|
|
* Phase D - DHT lets signal float back up for ~80us
|
2016-07-06 12:57:00 +00:00
|
|
|
*
|
2016-04-20 22:03:18 +00:00
|
|
|
* After this, the DHT transmits its first bit by holding the signal low for 50us
|
|
|
|
* and then letting it float back high for a period of time that depends on the data bit.
|
|
|
|
* duration_data_high is shorter than 50us for a logic '0' and longer than 50us for logic '1'.
|
|
|
|
*
|
2016-07-06 12:57:00 +00:00
|
|
|
* There are a total of 40 data bits transmitted sequentially. These bits are read into a byte array
|
2016-04-20 22:03:18 +00:00
|
|
|
* of length 5. The first and third bytes are humidity (%) and temperature (C), respectively. Bytes 2 and 4
|
|
|
|
* are zero-filled and the fifth is a checksum such that:
|
|
|
|
*
|
|
|
|
* byte_5 == (byte_1 + byte_2 + byte_3 + btye_4) & 0xFF
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-07-06 12:57:00 +00:00
|
|
|
/**
|
|
|
|
* Wait specified time for pin to go to a specified state.
|
|
|
|
* If timeout is reached and pin doesn't go to a requested state
|
|
|
|
* false is returned.
|
|
|
|
* The elapsed time is returned in pointer 'duration' if it is not NULL.
|
|
|
|
*/
|
|
|
|
static bool dht_await_pin_state(uint8_t pin, uint32_t timeout,
|
|
|
|
bool expected_pin_state, uint32_t *duration)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < timeout; i += DHT_TIMER_INTERVAL) {
|
2016-07-06 18:01:44 +00:00
|
|
|
// need to wait at least a single interval to prevent reading a jitter
|
|
|
|
sdk_os_delay_us(DHT_TIMER_INTERVAL);
|
2016-07-06 12:57:00 +00:00
|
|
|
if (gpio_read(pin) == expected_pin_state) {
|
|
|
|
if (duration) {
|
|
|
|
*duration = i;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-04-20 22:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-06 12:57:00 +00:00
|
|
|
/**
|
|
|
|
* Request data from DHT and read raw bit stream.
|
|
|
|
* The function call should be protected from task switching.
|
|
|
|
* Return false if error occurred.
|
|
|
|
*/
|
2019-07-21 21:21:27 +00:00
|
|
|
static inline bool dht_fetch_data(dht_sensor_type_t sensor_type, uint8_t pin, bool bits[DHT_DATA_BITS])
|
2016-07-06 12:57:00 +00:00
|
|
|
{
|
|
|
|
uint32_t low_duration;
|
|
|
|
uint32_t high_duration;
|
2016-04-20 22:03:18 +00:00
|
|
|
|
|
|
|
// Phase 'A' pulling signal low to initiate read sequence
|
|
|
|
gpio_write(pin, 0);
|
2019-07-21 21:21:27 +00:00
|
|
|
sdk_os_delay_us(sensor_type == DHT_TYPE_SI7021 ? 500 : 20000);
|
2016-04-20 22:03:18 +00:00
|
|
|
gpio_write(pin, 1);
|
|
|
|
|
2016-07-06 12:57:00 +00:00
|
|
|
// Step through Phase 'B', 40us
|
|
|
|
if (!dht_await_pin_state(pin, 40, false, NULL)) {
|
|
|
|
debug("Initialization error, problem in phase 'B'\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step through Phase 'C', 88us
|
|
|
|
if (!dht_await_pin_state(pin, 88, true, NULL)) {
|
|
|
|
debug("Initialization error, problem in phase 'C'\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step through Phase 'D', 88us
|
|
|
|
if (!dht_await_pin_state(pin, 88, false, NULL)) {
|
|
|
|
debug("Initialization error, problem in phase 'D'\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read in each of the 40 bits of data...
|
|
|
|
for (int i = 0; i < DHT_DATA_BITS; i++) {
|
|
|
|
if (!dht_await_pin_state(pin, 65, true, &low_duration)) {
|
|
|
|
debug("LOW bit timeout\n");
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-26 13:41:08 +00:00
|
|
|
if (!dht_await_pin_state(pin, 75, false, &high_duration)) {
|
2016-07-06 12:57:00 +00:00
|
|
|
debug("HIGHT bit timeout\n");
|
|
|
|
return false;
|
2016-04-20 22:03:18 +00:00
|
|
|
}
|
2016-07-06 12:57:00 +00:00
|
|
|
bits[i] = high_duration > low_duration;
|
2016-04-20 22:03:18 +00:00
|
|
|
}
|
2016-07-06 12:57:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pack two data bytes into single value and take into account sign bit.
|
|
|
|
*/
|
2016-10-26 13:41:08 +00:00
|
|
|
static inline int16_t dht_convert_data(dht_sensor_type_t sensor_type, uint8_t msb, uint8_t lsb)
|
2016-07-06 12:57:00 +00:00
|
|
|
{
|
|
|
|
int16_t data;
|
|
|
|
|
2016-10-26 13:41:08 +00:00
|
|
|
if (sensor_type == DHT_TYPE_DHT22) {
|
|
|
|
data = msb & 0x7F;
|
|
|
|
data <<= 8;
|
|
|
|
data |= lsb;
|
|
|
|
if (msb & BIT(7)) {
|
|
|
|
data = 0 - data; // convert it to negative
|
|
|
|
}
|
|
|
|
}
|
2019-11-29 08:38:51 +00:00
|
|
|
else if (sensor_type == DHT_TYPE_SI7021) {
|
|
|
|
data = msb * 256 + (lsb & ~0x3);
|
|
|
|
}
|
2016-10-26 13:41:08 +00:00
|
|
|
else {
|
|
|
|
data = msb * 10;
|
2016-07-06 12:57:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-10-26 13:41:08 +00:00
|
|
|
bool dht_read_data(dht_sensor_type_t sensor_type, uint8_t pin, int16_t *humidity, int16_t *temperature)
|
2016-07-06 12:57:00 +00:00
|
|
|
{
|
|
|
|
bool bits[DHT_DATA_BITS];
|
|
|
|
uint8_t data[DHT_DATA_BITS/8] = {0};
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
gpio_enable(pin, GPIO_OUT_OPEN_DRAIN);
|
|
|
|
|
|
|
|
taskENTER_CRITICAL();
|
2019-07-21 21:21:27 +00:00
|
|
|
result = dht_fetch_data(sensor_type, pin, bits);
|
2016-04-20 22:03:18 +00:00
|
|
|
taskEXIT_CRITICAL();
|
2016-07-06 12:57:00 +00:00
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < DHT_DATA_BITS; i++) {
|
|
|
|
// Read each bit into 'result' byte array...
|
|
|
|
data[i/8] <<= 1;
|
|
|
|
data[i/8] |= bits[i];
|
|
|
|
}
|
|
|
|
|
2016-07-06 18:01:44 +00:00
|
|
|
if (data[4] != ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
|
2016-07-06 12:57:00 +00:00
|
|
|
debug("Checksum failed, invalid data received from sensor\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-26 13:41:08 +00:00
|
|
|
*humidity = dht_convert_data(sensor_type, data[0], data[1]);
|
|
|
|
*temperature = dht_convert_data(sensor_type, data[2], data[3]);
|
2016-07-06 12:57:00 +00:00
|
|
|
|
|
|
|
debug("Sensor data: humidity=%d, temp=%d\n", *humidity, *temperature);
|
|
|
|
|
|
|
|
return true;
|
2016-04-20 22:03:18 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 13:41:08 +00:00
|
|
|
bool dht_read_float_data(dht_sensor_type_t sensor_type, uint8_t pin, float *humidity, float *temperature)
|
2016-07-06 12:57:00 +00:00
|
|
|
{
|
|
|
|
int16_t i_humidity, i_temp;
|
|
|
|
|
2016-10-26 13:41:08 +00:00
|
|
|
if (dht_read_data(sensor_type, pin, &i_humidity, &i_temp)) {
|
2016-07-06 12:57:00 +00:00
|
|
|
*humidity = (float)i_humidity / 10;
|
|
|
|
*temperature = (float)i_temp / 10;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|