Adding DHT22 library

This commit is contained in:
Ray Jones 2019-09-15 12:55:02 +10:00
parent d30536e939
commit 36d1b06ba2
14 changed files with 524 additions and 0 deletions

8
lib/esp32DHT/.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
.pioenvs
.vscode
lib
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
platformio.ini
.piolibdeps

25
lib/esp32DHT/.travis.yml Normal file
View file

@ -0,0 +1,25 @@
language: python
python:
- "2.7"
# Cache PlatformIO packages using Travis CI container-based infrastructure
sudo: false
cache:
directories:
- "~/.platformio"
env:
- PLATFORMIO_CI_SRC=examples/DHT22/DHT22.ino
- CPPLINT=true
install:
- pip install -U platformio
- pip install -U cpplint
script:
- if [[ "$CPPLINT" ]]; then cpplint --repository=. --recursive --linelength=200 --filter=-build/include ./src; else platformio ci --lib="." --board=lolin32; fi
notifications:
email:
on_success: change
on_failure: change

21
lib/esp32DHT/LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018 Bert Melis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

53
lib/esp32DHT/README.md Normal file
View file

@ -0,0 +1,53 @@
# esp32DHT
[![Build Status](https://travis-ci.com/bertmelis/esp32DHT.svg?branch=master)](https://travis-ci.com/bertmelis/esp32DHT)
This is a DHT11/22 library for ESP32 using the RMT peripheral, for use in the Arduino framework.
For ESP8266, please look into this repo: [DHT](https://github.com/bertmelis/DHT)
The library is non blocking, doesn't use delay and is usable in async frameworks. The library is kept simple on purpose. You are responsible yourself to follow the sensor's constraints (like polling frequency) and logical programming errors. Supplementary functions like dew point calculation are not included.
## Installation
* For Arduino IDE: see [the Arduino Guide](https://www.arduino.cc/en/Guide/Libraries#toc4)
* For Platformio: see the [Platfomio guide](http://docs.platformio.org/en/latest/projectconf/section_env_library.html)
## Usage
```C++
#include <Arduino.h>
#include <esp32DHT.h>
DHT22 sensor;
void setup() {
Serial.begin(112500);
sensor.setup(23);
sensor.setCallback([](int8_t result) {
if (result > 0) {
Serial.printf("Temp: %.1f°C\nHumid: %.1f%%\n", sensor.getTemperature(), sensor.getHumidity());
} else {
Serial.printf("Sensor error: %s", sensor.getError());
}
});
}
void loop() {
static uint32_t lastMillis = 0;
if (millis() - lastMillis > 30000) {
lastMillis = millis();
sensor.read();
Serial.print("Read DHT...\n");
}
}
```
> Note: `setup(uint8_t, rmt_channel_t channel = RMT_CHANNEL_0);` taks 2 arguments: the pin connected to the DHT sensor and the RMT channel[0-7]. The library uses 2 channels and defaults to (starting) channel 0. This means that by default channel 0 and channel 1 are occupied by the DHT and you should not use channel 7. If you're also using other RMT channels (for IR devices, extra DHT sensors, Neopixels...) you have to keep this in mind.
>
> Read more about RMT in the docs: [ESP-IDF documentation](https://esp-idf.readthedocs.io/en/latest/api-reference/peripherals/rmt.html)
## History
Whatever can be done using hardware should not be done by software. ESP32 has a RMT peripheral device which is remarkably versatile. As the DHT sensors rely on tight timing, the RMT device is perfect to accomplish reliable communication. I didn't find any other Arduino library that uses the RMT and/or doesn't block during communication. So I created my own one.
> This library won't exist without the examples for RMT. Credits go to the team and contributors of ESP-IDF and @nkolban!

View file

@ -0,0 +1 @@
platformio ci --lib="." --board=lolin32 examples/DHT22/DHT22.ino

BIN
lib/esp32DHT/docs/DHT11.pdf Normal file

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,52 @@
/*
Copyright 2018 Bert Melis
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <Arduino.h>
#include <Ticker.h>
#include <esp32DHT.h>
Ticker ticker;
DHT22 sensor;
// DHT11 sensor; // DHT11 also works!
void readDHT() {
sensor.read();
}
void setup() {
Serial.begin(74880);
sensor.setup(23); // pin 23 is DATA, RMT channel defaults to channel 0 and 1
sensor.onData([](float humidity, float temperature) {
Serial.printf("Temp: %g°C\nHumid: %g%%\n", temperature,humidity);
});
sensor.onError([](uint8_t error) {
Serial.printf("Sensor error: %s", sensor.getError());
});
ticker.attach(30, readDHT);
}
void loop() {
}

24
lib/esp32DHT/keywords.txt Normal file
View file

@ -0,0 +1,24 @@
#######################################
# Datatypes (KEYWORD1)
#######################################
DHT11 KEYWORD1
DHT22 KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
setPin KEYWORD2
setCallback KEYWORD2
read KEYWORD2
ready KEYWORD2
getTemperature KEYWORD2
getHumidity KEYWORD2
getError KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
#yourLITERAL LITERAL1

22
lib/esp32DHT/library.json Normal file
View file

@ -0,0 +1,22 @@
{
"name": "esp32DHT",
"version": "1.0.1",
"keywords": "DHT, DTH11, DHT22, RMT, callback, Arduino, ESP32",
"description": "DHT sensor library for ESP32 using the RMT peripheral",
"homepage": "https://github.com/bertmelis/esp32DHT",
"license": "MIT",
"authors": {
"name": "Bert Melis",
"url": "https://github.com/bertmelis",
"maintainer": true
},
"repository": {
"type": "git",
"url": "https://github.com/bertmelis/esp32DHT.git",
"branch": "master"
},
"frameworks": "arduino",
"platforms": [
"espressif32"
]
}

View file

@ -0,0 +1,9 @@
name=esp32DHT
version=1.0.1
author=Bert Melis
maintainer=Bert Melis
sentence=DHT sensor library for ESP32 using the RMT pheripheral
paragraph=
category=Sensors
url=https://github.com/bertmelis/esp32DHT
architectures=esp32

View file

@ -0,0 +1,197 @@
/*
Copyright 2018 Bert Melis
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONDHTTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "esp32DHT.hpp" // NOLINT
#define RMT_CLK_DIV 80
DHT::DHT() :
_status(0),
_data{0},
_pin(0),
_channel(RMT_CHANNEL_0),
_onData(nullptr),
_onError(nullptr),
_timer(nullptr),
_task(nullptr) {}
DHT::~DHT() {
if (_timer) { // if _timer is true, setup() has been called
// so RMT driver is loaded and the aux task is
// running
esp_timer_delete(_timer);
rmt_driver_uninstall(_channel);
vTaskDelete(_task);
}
}
void DHT::setup(uint8_t pin, rmt_channel_t channel) {
_pin = pin;
_channel = channel;
esp_timer_create_args_t _timerConfig;
_timerConfig.arg = static_cast<void*>(this);
_timerConfig.callback = reinterpret_cast<esp_timer_cb_t>(_handleTimer);
_timerConfig.dispatch_method = ESP_TIMER_TASK;
_timerConfig.name = "esp32DHTTimer";
esp_timer_create(&_timerConfig, &_timer);
rmt_config_t config;
config.rmt_mode = RMT_MODE_RX;
config.channel = _channel;
config.gpio_num = static_cast<gpio_num_t>(_pin);
config.mem_block_num = 2;
config.rx_config.filter_en = 1;
config.rx_config.filter_ticks_thresh = 10;
config.rx_config.idle_threshold = 1000;
config.clk_div = RMT_CLK_DIV;
rmt_config(&config);
rmt_driver_install(_channel, 400, 0); // 400 words for ringbuffer containing pulse trains from DHT
rmt_get_ringbuf_handle(_channel, &_ringBuf);
xTaskCreate((TaskFunction_t)&_handleData, "esp32DHT", 2048, this, 5, &_task);
pinMode(_pin, OUTPUT);
digitalWrite(_pin, HIGH);
}
void DHT::onData(esp32DHTInternals::OnData_CB callback) {
_onData = callback;
}
void DHT::onError(esp32DHTInternals::OnError_CB callback) {
_onError = callback;
}
void DHT::read() {
// _pin should be set to OUTPUT and HIGH
digitalWrite(_pin, LOW);
esp_timer_start_once(_timer, 18 * 1000); // timer is in microseconds
_data[0] = _data[1] = _data[2] = _data[3] = _data[4] = 0;
_status = 0;
}
const char* DHT::getError() const {
if (_status == 0) {
return "OK";
} else if (_status == 1) {
return "TO";
} else if (_status == 2) {
return "NACK";
} else if (_status == 3) {
return "DATA";
} else if (_status == 4) {
return "CS";
} else if (_status == 5) {
return "UNDERFLOW";
} else if (_status == 6) {
return "OVERFLOW";
}
return "UNKNOWN";
}
void DHT::_handleTimer(DHT* instance) {
pinMode(instance->_pin, INPUT);
rmt_rx_start(instance->_channel, 1);
rmt_set_pin(instance->_channel, RMT_MODE_RX, static_cast<gpio_num_t>(instance->_pin)); // reset after using pin as output
xTaskNotifyGive(instance->_task);
}
void DHT::_handleData(DHT* instance) {
size_t rx_size = 0;
while (1) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // block and wait for notification
// blocks until data is available or timeouts after 1000
rmt_item32_t* items = static_cast<rmt_item32_t*>(xRingbufferReceive(instance->_ringBuf, &rx_size, 1000));
if (items) {
instance->_decode(items, rx_size/sizeof(rmt_item32_t));
vRingbufferReturnItem(instance->_ringBuf, static_cast<void*>(items));
rmt_rx_stop(instance->_channel);
pinMode(instance->_pin, OUTPUT);
digitalWrite(instance->_pin, HIGH);
} else {
instance->_status = 1; // timeout error
rmt_rx_stop(instance->_channel);
pinMode(instance->_pin, OUTPUT);
digitalWrite(instance->_pin, HIGH);
}
instance->_tryCallback();
}
}
void DHT::_decode(rmt_item32_t* data, int numItems) {
if (numItems < 42) {
_status = 5;
} else if (numItems > 42) {
_status = 6;
} else if ((data[0].duration0 + data[0].duration1) < 140 && (data[0].duration0 + data[0].duration1) > 180) {
_status = 2;
} else {
for (uint8_t i = 1; i < numItems - 1; ++i) { // don't include tail
uint8_t pulse = data[i].duration0 + data[i].duration1;
if (pulse > 55 && pulse < 145) {
_data[(i - 1) / 8] <<= 1; // shift left
if (pulse > 120) {
_data[(i - 1) / 8] |= 1;
}
} else {
_status = 3; // DATA error
return;
}
}
if (_data[4] == ((_data[0] + _data[1] + _data[2] + _data[3]) & 0xFF)) {
_status = 0;
} else {
_status = 4; // checksum error
}
}
}
void DHT::_tryCallback() {
if (_status == 0) {
if (_onData) _onData(_getHumidity(), _getTemperature());
} else {
if (_onError) _onError(_status);
}
}
float DHT11::_getTemperature() {
if (_status != 0) return NAN;
return static_cast<float>(_data[2]);
}
float DHT11::_getHumidity() {
if (_status != 0) return NAN;
return static_cast<float>(_data[0]);
}
float DHT22::_getTemperature() {
if (_status != 0) return NAN;
float temp = (((_data[2] & 0x7F) << 8) | _data[3]) * 0.1;
if (_data[2] & 0x80) { // negative temperature
temp = -temp;
}
return temp;
}
float DHT22::_getHumidity() {
if (_status != 0) return NAN;
return ((_data[0] << 8) | _data[1]) * 0.1;
}

View file

@ -0,0 +1,27 @@
/*
Copyright 2018 Bert Melis
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include "esp32DHT.hpp"

View file

@ -0,0 +1,85 @@
/*
Copyright 2018 Bert Melis
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
extern "C" {
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp32-hal-gpio.h>
#include <driver/rmt.h>
#include <esp_timer.h>
}
#include <functional>
namespace esp32DHTInternals {
typedef std::function<void(float, float)> OnData_CB;
typedef std::function<void(uint8_t)> OnError_CB;
} // end namespace esp32DHTInternals
class DHT {
public:
DHT();
~DHT();
void setup(uint8_t pin, rmt_channel_t channel = RMT_CHANNEL_0); // setPin does complete setup of DHT lib
void onData(esp32DHTInternals::OnData_CB callback);
void onError(esp32DHTInternals::OnError_CB callback);
void read();
const char* getError() const;
protected:
uint8_t _status;
uint8_t _data[5];
private:
static void _handleTimer(DHT* instance);
static void _handleData(DHT* instance);
void _decode(rmt_item32_t* data, int numItems);
void _tryCallback();
virtual float _getTemperature() = 0;
virtual float _getHumidity() = 0;
private:
uint8_t _pin;
rmt_channel_t _channel;
esp32DHTInternals::OnData_CB _onData;
esp32DHTInternals::OnError_CB _onError;
esp_timer_handle_t _timer;
TaskHandle_t _task;
RingbufHandle_t _ringBuf;
};
class DHT11 : public DHT {
private:
float _getTemperature();
float _getHumidity();
};
class DHT22 : public DHT {
private:
float _getTemperature();
float _getHumidity();
};