mirror of
https://github.com/esphome/esphome.git
synced 2024-12-11 16:04:54 +01:00
Added HDC2010 component
This commit is contained in:
parent
c7c0d97a5e
commit
305dbe4817
4 changed files with 219 additions and 0 deletions
1
esphome/components/hdc2010/__init__.py
Executable file
1
esphome/components/hdc2010/__init__.py
Executable file
|
@ -0,0 +1 @@
|
|||
CODEOWNERS = ["@optimusprimespace"]
|
122
esphome/components/hdc2010/hdc2010.cpp
Executable file
122
esphome/components/hdc2010/hdc2010.cpp
Executable file
|
@ -0,0 +1,122 @@
|
|||
#include "hdc2010.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/hal.h"
|
||||
// https://github.com/vigsterkr/homebridge-hdc2010/blob/main/src/hdc2010.js
|
||||
// https://github.com/lime-labs/HDC2080-Arduino/blob/master/src/HDC2080.cpp
|
||||
namespace esphome {
|
||||
namespace hdc2010 {
|
||||
|
||||
static const char *const TAG = "hdc2010";
|
||||
|
||||
static const uint8_t HDC2010_ADDRESS = 0x40; // 0b1000000 or 0b1000001 from datasheet
|
||||
static const uint8_t HDC2010_CMD_CONFIGURATION_MEASUREMENT = 0x0F;
|
||||
static const uint8_t HDC2010_CMD_CONFIG = 0x0E;
|
||||
static const uint8_t HDC2010_CMD_TEMPERATURE_LOW = 0x00;
|
||||
static const uint8_t HDC2010_CMD_TEMPERATURE_HIGH = 0x01;
|
||||
static const uint8_t HDC2010_CMD_HUMIDITY_LOW = 0x02;
|
||||
static const uint8_t HDC2010_CMD_HUMIDITY_HIGH = 0x03;
|
||||
static const uint8_t HDC2010_CMD_HEATER_HEAT0 = 0x5A;
|
||||
// static const uint8_t HDC2010_TEMP_OFFSET_ADJUST = 0x08;
|
||||
// static const uint8_t HDC2010_HUM_OFFSET_ADJUST = 0x09;
|
||||
|
||||
void HDC2010Component::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up HDC2010...");
|
||||
|
||||
const uint8_t data[2] = {
|
||||
0b00000000, // resolution 14bit for both humidity and temperature
|
||||
0b00000000 // reserved
|
||||
};
|
||||
|
||||
if (!this->write_bytes(HDC2010_CMD_CONFIGURATION_MEASUREMENT, data, 2)) {
|
||||
// as instruction is same as powerup defaults (for now), interpret as warning if this fails
|
||||
ESP_LOGW(TAG, "HDC2010 initial config instruction error");
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
}
|
||||
void HDC2010Component::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "HDC2010:");
|
||||
LOG_I2C_DEVICE(this);
|
||||
if (this->is_failed()) {
|
||||
ESP_LOGE(TAG, "Communication with HDC2010 failed!");
|
||||
}
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
LOG_SENSOR(" ", "Temperature", this->temperature_);
|
||||
LOG_SENSOR(" ", "Humidity", this->humidity_);
|
||||
}
|
||||
void HDC2010Component::update() {
|
||||
// Temperature
|
||||
uint16_t raw_temp;
|
||||
if (this->write(&HDC2010_CMD_TEMPERATURE_LOW, 1) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
if (this->write(&HDC2010_CMD_TEMPERATURE_HIGH, 1) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
delay(20);
|
||||
if (this->read(reinterpret_cast<uint8_t *>(&raw_temp), 2) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
raw_temp = (unsigned int) (HDC2010_CMD_TEMPERATURE_HIGH) << 8 | (unsigned int) (HDC2010_CMD_TEMPERATURE_LOW);
|
||||
raw_temp = i2c::i2ctohs(raw_temp);
|
||||
|
||||
float temp = raw_temp * 0.0025177001953125f - 40.0f; // raw * 2^-16 * 165 - 40
|
||||
this->temperature_->publish_state(temp);
|
||||
|
||||
// Humidity
|
||||
|
||||
uint16_t raw_humidity;
|
||||
if (this->write(&HDC2010_CMD_HUMIDITY_LOW, 1) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
if (this->write(&HDC2010_CMD_HUMIDITY_HIGH, 1) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
delay(20);
|
||||
if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
raw_humidity = (unsigned int) HDC2010_CMD_HUMIDITY_HIGH << 8 | HDC2010_CMD_HUMIDITY_LOW;
|
||||
raw_humidity = i2c::i2ctohs(raw_humidity);
|
||||
float humidity = raw_humidity * 0.00152587890625f; // raw * 2^-16 * 100
|
||||
this->humidity_->publish_state(humidity);
|
||||
|
||||
ESP_LOGD(TAG, "Got temperature=%.1f°C humidity=%.1f%%", temp, humidity);
|
||||
this->status_clear_warning();
|
||||
}
|
||||
|
||||
// void HDC2010::enableHeater()
|
||||
// {
|
||||
// uint16_t configContents; //Stores current contents of config register
|
||||
|
||||
// configContents = readReg(CONFIG);
|
||||
|
||||
// //set bit 3 to 1 to enable heater
|
||||
// configContents = (configContents | 0x08);
|
||||
|
||||
// writeReg(CONFIG, configContents);
|
||||
|
||||
// }
|
||||
|
||||
// void HDC2010::disableHeater()
|
||||
// {
|
||||
// uint16_t configContents; //Stores current contents of config register
|
||||
|
||||
// configContents = readReg(CONFIG);
|
||||
|
||||
// //set bit 3 to 0 to disable heater (all other bits 1)
|
||||
// configContents = (configContents & 0xF7);
|
||||
// writeReg(CONFIG, configContents);
|
||||
|
||||
// }
|
||||
|
||||
float HDC2010Component::get_setup_priority() const { return setup_priority::DATA; }
|
||||
|
||||
} // namespace hdc2010
|
||||
} // namespace esphome
|
31
esphome/components/hdc2010/hdc2010.h
Executable file
31
esphome/components/hdc2010/hdc2010.h
Executable file
|
@ -0,0 +1,31 @@
|
|||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace hdc2010 {
|
||||
|
||||
class HDC2010Component : public PollingComponent, public i2c::I2CDevice {
|
||||
public:
|
||||
void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; }
|
||||
void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; }
|
||||
|
||||
/// Setup the sensor and check for connection.
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
/// Retrieve the latest sensor values. This operation takes approximately 16ms.
|
||||
void update() override;
|
||||
|
||||
float get_setup_priority() const override;
|
||||
|
||||
protected:
|
||||
sensor::Sensor *temperature_{nullptr};
|
||||
sensor::Sensor *humidity_{nullptr};
|
||||
uint16_t heater_temperature_{100};
|
||||
uint16_t heater_duration_{30};
|
||||
};
|
||||
|
||||
} // namespace hdc2010
|
||||
} // namespace esphome
|
65
esphome/components/hdc2010/sensor.py
Executable file
65
esphome/components/hdc2010/sensor.py
Executable file
|
@ -0,0 +1,65 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import i2c, sensor
|
||||
from esphome.const import (
|
||||
CONF_HUMIDITY,
|
||||
CONF_ID,
|
||||
CONF_TEMPERATURE,
|
||||
DEVICE_CLASS_HUMIDITY,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_CELSIUS,
|
||||
UNIT_PERCENT,
|
||||
# CONF_HEATER,
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["i2c"]
|
||||
|
||||
hdc2010_ns = cg.esphome_ns.namespace("hdc2010")
|
||||
HDC2010Component = hdc2010_ns.class_(
|
||||
"HDC2010Component", cg.PollingComponent, i2c.I2CDevice
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(HDC2010Component),
|
||||
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_CELSIUS,
|
||||
accuracy_decimals=1,
|
||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_PERCENT,
|
||||
accuracy_decimals=0,
|
||||
device_class=DEVICE_CLASS_HUMIDITY,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
.extend(i2c.i2c_device_schema(0x40))
|
||||
)
|
||||
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await i2c.register_i2c_device(var, config)
|
||||
|
||||
if CONF_TEMPERATURE in config:
|
||||
sens = await sensor.new_sensor(config[CONF_TEMPERATURE])
|
||||
cg.add(var.set_temperature(sens))
|
||||
|
||||
if CONF_HUMIDITY in config:
|
||||
sens = await sensor.new_sensor(config[CONF_HUMIDITY])
|
||||
cg.add(var.set_humidity(sens))
|
||||
|
||||
# if CONF_HEATER in config:
|
||||
# conf = config[CONF_HEATER]
|
||||
# if not conf:
|
||||
# cg.add(var.set_heater(0, 0))
|
||||
# else:
|
||||
# cg.add(var.set_heater(conf[CONF_TEMPERATURE], conf[CONF_DURATION]))
|
Loading…
Reference in a new issue