mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-19 12:43:28 +02:00
6682c43dfa
## Description: Move esphome-core codebase into esphome (and a bunch of other refactors). See https://github.com/esphome/feature-requests/issues/97 Yes this is a shit ton of work and no there's no way to automate it :( But it will be worth it 👍 Progress: - Core support (file copy etc): 80% - Base Abstractions (light, switch): ~50% - Integrations: ~10% - Working? Yes, (but only with ported components). Other refactors: - Moves all codegen related stuff into a single class: `esphome.codegen` (imported as `cg`) - Rework coroutine syntax - Move from `component/platform.py` to `domain/component.py` structure as with HA - Move all defaults out of C++ and into config validation. - Remove `make_...` helpers from Application class. Reason: Merge conflicts with every single new integration. - Pointer Variables are stored globally instead of locally in setup(). Reason: stack size limit. Future work: - Rework const.py - Move all `CONF_...` into a conf class (usage `conf.UPDATE_INTERVAL` vs `CONF_UPDATE_INTERVAL`). Reason: Less convoluted import block - Enable loading from `custom_components` folder. **Related issue (if applicable):** https://github.com/esphome/feature-requests/issues/97 **Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here> ## Checklist: - [ ] The code change is tested and works locally. - [ ] Tests have been added to verify that the new code works (under `tests/` folder). If user exposed functionality or configuration variables are added/changed: - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
195 lines
5.7 KiB
C++
195 lines
5.7 KiB
C++
#include "esphome/components/dht/dht.h"
|
|
#include "esphome/core/log.h"
|
|
#include "esphome/core/helpers.h"
|
|
|
|
namespace esphome {
|
|
namespace dht {
|
|
|
|
static const char *TAG = "dht";
|
|
|
|
DHT::DHT(const std::string &temperature_name, const std::string &humidity_name, GPIOPin *pin, uint32_t update_interval)
|
|
: PollingComponent(update_interval),
|
|
pin_(pin),
|
|
temperature_sensor_(new sensor::Sensor(temperature_name)),
|
|
humidity_sensor_(new sensor::Sensor(humidity_name)) {}
|
|
|
|
void DHT::setup() {
|
|
ESP_LOGCONFIG(TAG, "Setting up DHT...");
|
|
this->pin_->digital_write(true);
|
|
this->pin_->setup();
|
|
this->pin_->digital_write(true);
|
|
}
|
|
void DHT::dump_config() {
|
|
ESP_LOGCONFIG(TAG, "DHT:");
|
|
LOG_PIN(" Pin: ", this->pin_);
|
|
if (this->is_auto_detect_) {
|
|
ESP_LOGCONFIG(TAG, " Auto-detected model: %s", this->model_ == DHT_MODEL_DHT11 ? "DHT11" : "DHT22");
|
|
} else if (this->model_ == DHT_MODEL_DHT11) {
|
|
ESP_LOGCONFIG(TAG, " Model: DHT11");
|
|
} else {
|
|
ESP_LOGCONFIG(TAG, " Model: DHT22 (or equivalent)");
|
|
}
|
|
|
|
LOG_UPDATE_INTERVAL(this);
|
|
|
|
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
|
|
LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
|
|
}
|
|
|
|
void DHT::update() {
|
|
float temperature, humidity;
|
|
bool error;
|
|
if (this->model_ == DHT_MODEL_AUTO_DETECT) {
|
|
this->model_ = DHT_MODEL_DHT22;
|
|
error = this->read_sensor_(&temperature, &humidity, false);
|
|
if (error) {
|
|
this->model_ = DHT_MODEL_DHT11;
|
|
return;
|
|
}
|
|
} else {
|
|
error = this->read_sensor_(&temperature, &humidity, true);
|
|
}
|
|
|
|
if (error) {
|
|
ESP_LOGD(TAG, "Got Temperature=%.1f°C Humidity=%.1f%%", temperature, humidity);
|
|
|
|
this->temperature_sensor_->publish_state(temperature);
|
|
this->humidity_sensor_->publish_state(humidity);
|
|
this->status_clear_warning();
|
|
} else {
|
|
const char *str = "";
|
|
if (this->is_auto_detect_) {
|
|
str = " and consider manually specifying the DHT model using the model option";
|
|
}
|
|
ESP_LOGW(TAG, "Invalid readings! Please check your wiring (pull-up resistor, pin number)%s.", str);
|
|
this->status_set_warning();
|
|
}
|
|
}
|
|
|
|
float DHT::get_setup_priority() const { return setup_priority::DATA; }
|
|
void DHT::set_dht_model(DHTModel model) {
|
|
this->model_ = model;
|
|
this->is_auto_detect_ = model == DHT_MODEL_AUTO_DETECT;
|
|
}
|
|
sensor::Sensor *DHT::get_temperature_sensor() const { return this->temperature_sensor_; }
|
|
sensor::Sensor *DHT::get_humidity_sensor() const { return this->humidity_sensor_; }
|
|
bool HOT DHT::read_sensor_(float *temperature, float *humidity, bool report_errors) {
|
|
*humidity = NAN;
|
|
*temperature = NAN;
|
|
|
|
disable_interrupts();
|
|
this->pin_->digital_write(false);
|
|
this->pin_->pin_mode(OUTPUT);
|
|
this->pin_->digital_write(false);
|
|
|
|
if (this->model_ == DHT_MODEL_DHT11) {
|
|
delayMicroseconds(18000);
|
|
} else if (this->model_ == DHT_MODEL_SI7021) {
|
|
delayMicroseconds(500);
|
|
this->pin_->digital_write(true);
|
|
delayMicroseconds(40);
|
|
} else {
|
|
delayMicroseconds(800);
|
|
}
|
|
this->pin_->pin_mode(INPUT_PULLUP);
|
|
delayMicroseconds(40);
|
|
|
|
uint8_t data[5] = {0, 0, 0, 0, 0};
|
|
uint8_t bit = 7;
|
|
uint8_t byte = 0;
|
|
|
|
for (int8_t i = -1; i < 40; i++) {
|
|
uint32_t start_time = micros();
|
|
|
|
// Wait for rising edge
|
|
while (!this->pin_->digital_read()) {
|
|
if (micros() - start_time > 90) {
|
|
enable_interrupts();
|
|
if (report_errors) {
|
|
if (i < 0) {
|
|
ESP_LOGW(TAG, "Waiting for DHT communication to clear failed!");
|
|
} else {
|
|
ESP_LOGW(TAG, "Rising edge for bit %d failed!", i);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
start_time = micros();
|
|
uint32_t end_time = start_time;
|
|
|
|
// Wait for falling edge
|
|
while (this->pin_->digital_read()) {
|
|
if ((end_time = micros()) - start_time > 90) {
|
|
enable_interrupts();
|
|
if (report_errors) {
|
|
if (i < 0) {
|
|
ESP_LOGW(TAG, "Requesting data from DHT failed!");
|
|
} else {
|
|
ESP_LOGW(TAG, "Falling edge for bit %d failed!", i);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (i < 0)
|
|
continue;
|
|
|
|
if (end_time - start_time >= 40) {
|
|
data[byte] |= 1 << bit;
|
|
}
|
|
if (bit == 0) {
|
|
bit = 7;
|
|
byte++;
|
|
} else
|
|
bit--;
|
|
}
|
|
enable_interrupts();
|
|
|
|
ESP_LOGVV(TAG,
|
|
"Data: Hum=0b" BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN
|
|
", Temp=0b" BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN ", Checksum=0b" BYTE_TO_BINARY_PATTERN,
|
|
BYTE_TO_BINARY(data[0]), BYTE_TO_BINARY(data[1]), BYTE_TO_BINARY(data[2]), BYTE_TO_BINARY(data[3]),
|
|
BYTE_TO_BINARY(data[4]));
|
|
|
|
uint8_t checksum_a = data[0] + data[1] + data[2] + data[3];
|
|
// On the DHT11, two algorithms for the checksum seem to be used, either the one from the DHT22,
|
|
// or just using bytes 0 and 2
|
|
uint8_t checksum_b = this->model_ == DHT_MODEL_DHT11 ? (data[0] + data[2]) : checksum_a;
|
|
|
|
if (checksum_a != data[4] && checksum_b != data[4]) {
|
|
if (report_errors) {
|
|
ESP_LOGE(TAG, "Checksum invalid: %u!=%u", checksum_a, data[4]);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
if (this->model_ == DHT_MODEL_DHT11) {
|
|
*humidity = data[0];
|
|
*temperature = data[2];
|
|
} else {
|
|
uint16_t raw_humidity = (uint16_t(data[0] & 0xFF) << 8) | (data[1] & 0xFF);
|
|
uint16_t raw_temperature = (uint16_t(data[2] & 0xFF) << 8) | (data[3] & 0xFF);
|
|
*humidity = raw_humidity * 0.1f;
|
|
|
|
if ((raw_temperature & 0x8000) != 0)
|
|
raw_temperature = ~(raw_temperature & 0x7FFF);
|
|
|
|
*temperature = int16_t(raw_temperature) * 0.1f;
|
|
}
|
|
|
|
if (*temperature == 0.0f && (*humidity == 1.0f || *humidity == 2.0f)) {
|
|
if (report_errors) {
|
|
ESP_LOGE(TAG, "DHT reports invalid data. Is the update interval too high or the sensor damaged?");
|
|
}
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
} // namespace dht
|
|
} // namespace esphome
|