Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added HDC2010 component #6674

Open
wants to merge 48 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
305dbe4
Added HDC2010 component
optimusprimespace May 3, 2024
bfc127c
Update sensor.py
optimusprimespace May 3, 2024
b7a7dcc
added tests
optimusprimespace May 3, 2024
fb046fd
Update sensor.py
optimusprimespace May 3, 2024
9c76910
Update sensor.py
optimusprimespace May 10, 2024
a34e6cc
Update hdc2010.cpp
optimusprimespace May 10, 2024
465839c
Update hdc2010.cpp
optimusprimespace May 10, 2024
9526d54
Update hdc2010.cpp
optimusprimespace May 10, 2024
f1b68b6
Merge branch 'dev' into dev
optimusprimespace May 10, 2024
cc147df
Update hdc2010.cpp
optimusprimespace May 10, 2024
5ec71ea
Merge branch 'dev' of https://github.com/optimusprimespace/esphome in…
optimusprimespace May 10, 2024
e836527
updated temp calc
optimusprimespace May 12, 2024
2d830d7
Update hdc2010.cpp
optimusprimespace May 12, 2024
6dc249e
Update hdc2010.cpp
optimusprimespace May 12, 2024
5d8dd7c
Update hdc2010.cpp
optimusprimespace May 12, 2024
49e25aa
Update hdc2010.cpp
optimusprimespace May 12, 2024
9b18ef9
Update hdc2010.cpp
optimusprimespace May 12, 2024
c7f37fb
Update hdc2010.cpp
optimusprimespace May 15, 2024
f03c0c7
Update hdc2010.cpp
optimusprimespace May 17, 2024
3354de2
Merge branch 'esphome:dev' into dev
optimusprimespace May 17, 2024
267aea4
Update hdc2010.cpp
optimusprimespace May 17, 2024
a13ad39
Update hdc2010.cpp
optimusprimespace May 17, 2024
32b892d
Update hdc2010.cpp
optimusprimespace May 17, 2024
49bc279
Updated function and header files for new methods
optimusprimespace May 17, 2024
dd4ed0f
clang and header file modification
optimusprimespace May 17, 2024
29394f8
added newline to function and header file
optimusprimespace May 17, 2024
b39e3ae
Update hdc2010.cpp
optimusprimespace May 17, 2024
c6c03b5
Update hdc2010.cpp
optimusprimespace May 17, 2024
d75745e
Update hdc2010.cpp
optimusprimespace May 17, 2024
5e71ddb
Update __init__.py
optimusprimespace May 17, 2024
9d7a86e
Update __init__.py
optimusprimespace May 17, 2024
9b56611
Updated code to fix execution bits
optimusprimespace May 17, 2024
15177a8
Update __init__.py
optimusprimespace May 17, 2024
a4c410e
Merge branch 'esphome:dev' into dev
optimusprimespace May 19, 2024
d33e5e2
Update hdc2010.cpp
optimusprimespace May 19, 2024
96930fb
Update hdc2010.cpp
optimusprimespace May 19, 2024
343581a
Update hdc2010.cpp
optimusprimespace May 19, 2024
378a153
Update hdc2010.cpp
optimusprimespace May 19, 2024
7e23c78
Update hdc2010.cpp
optimusprimespace May 26, 2024
8cc8b87
Merge branch 'esphome:dev' into dev
optimusprimespace May 26, 2024
7938a6e
Update hdc2010.cpp
optimusprimespace May 26, 2024
699e3c4
added codeowners and removed execution bit
optimusprimespace May 26, 2024
68f7e69
Merge branch 'esphome:dev' into dev
optimusprimespace Jun 5, 2024
3f87004
Update __init__.py
optimusprimespace Jun 5, 2024
b98a1ac
Merge branch 'dev' of https://github.com/optimusprimespace/esphome in…
optimusprimespace Jun 5, 2024
782f906
Update CODEOWNERS
optimusprimespace Jun 5, 2024
36ccbe3
Update CODEOWNERS
optimusprimespace Jun 5, 2024
5de9eab
Merge branch 'esphome:dev' into dev
optimusprimespace Jun 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions esphome/components/hdc2010/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CODEOWNERS = ["@optimusprimespace"]
CODEOWNERS = ["@ssieb"]
113 changes: 113 additions & 0 deletions esphome/components/hdc2010/hdc2010.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include "hdc2010.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 = 0x8F;
static const uint8_t HDC2010_CMD_START_MEASUREMENT = 0xF9;
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 CONFIG = 0x0E;
static const uint8_t MEASUREMENT_CONFIG = 0x0F;

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)) {
ESP_LOGW(TAG, "HDC2010 initial config instruction error");
this->status_set_warning();
return;
}

// Set measurement mode to temperature and humidity
uint8_t configContents;
read_register(MEASUREMENT_CONFIG, &configContents, 1);
configContents = (configContents & 0xF9); // Always set to TEMP_AND_HUMID mode
this->write_bytes(MEASUREMENT_CONFIG, &configContents, 1);

// Set rate to manual
read_register(CONFIG, &configContents, 1);
configContents &= 0x8F;
this->write_bytes(CONFIG, &configContents, 1);

// Set temperature resolution to 14bit
read_register(CONFIG, &configContents, 1);
configContents &= 0x3F;
this->write_bytes(CONFIG, &configContents, 1);

// Set humidity resolution to 14bit
read_register(CONFIG, &configContents, 1);
configContents &= 0xCF;
this->write_bytes(CONFIG, &configContents, 1);

delayMicroseconds(5000); // wait for 5ms
}

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() {
// Trigger measurement
uint8_t configContents;
read_register(CONFIG, &configContents, 1);
configContents |= 0x01;
this->write_bytes(MEASUREMENT_CONFIG, &configContents, 1);

delayMicroseconds(1000); // 1ms delay after triggering the sample

float temp = readTemp();
float humidity = readHumidity();

this->temperature_->publish_state(temp);
this->humidity_->publish_state(humidity);

ESP_LOGD(TAG, "Got temperature=%.1f°C humidity=%.1f%%", temp, humidity);
this->status_clear_warning();
}

float HDC2010Component::readTemp() {
uint8_t byte[2];
uint16_t temp;

read_register(HDC2010_CMD_TEMPERATURE_LOW, &byte[0], 1);
read_register(HDC2010_CMD_TEMPERATURE_HIGH, &byte[1], 1);

temp = (unsigned int) byte[1] << 8 | byte[0];
return (float) temp * 0.0025177f - 40.0f;
}

float HDC2010Component::readHumidity() {
uint8_t byte[2];
uint16_t humidity;

read_register(HDC2010_CMD_HUMIDITY_LOW, &byte[0], 1);
read_register(HDC2010_CMD_HUMIDITY_HIGH, &byte[1], 1);

humidity = (unsigned int) byte[1] << 8 | byte[0];
return (float) humidity * 0.001525879f;
}

float HDC2010Component::get_setup_priority() const { return setup_priority::DATA; }
} // namespace hdc2010
} // namespace esphome
35 changes: 35 additions & 0 deletions esphome/components/hdc2010/hdc2010.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#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 readTemp();

float readHumidity();

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
64 changes: 64 additions & 0 deletions esphome/components/hdc2010/sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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]))
12 changes: 12 additions & 0 deletions tests/components/hdc2010/test.esp32-c3-idf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
i2c:
- id: i2c_hdc1080
scl: 5
sda: 4

sensor:
- platform: hdc1080
temperature:
name: Temperature
humidity:
name: Humidity
update_interval: 15s
12 changes: 12 additions & 0 deletions tests/components/hdc2010/test.esp32-c3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
i2c:
- id: i2c_hdc2010
scl: 5
sda: 4

sensor:
- platform: hdc2010
temperature:
name: Temperature
humidity:
name: Humidity
update_interval: 30s
12 changes: 12 additions & 0 deletions tests/components/hdc2010/test.esp32-idf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
i2c:
- id: i2c_hdc2010
scl: 16
sda: 17

sensor:
- platform: hdc2010
temperature:
name: Temperature
humidity:
name: Humidity
update_interval: 30s
12 changes: 12 additions & 0 deletions tests/components/hdc2010/test.esp32.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
i2c:
- id: i2c_hdc2010
scl: 16
sda: 17

sensor:
- platform: hdc2010
temperature:
name: Temperature
humidity:
name: Humidity
update_interval: 15s
12 changes: 12 additions & 0 deletions tests/components/hdc2010/test.esp8266.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
i2c:
- id: i2c_hdc2010
scl: 5
sda: 4

sensor:
- platform: hdc2010
temperature:
name: Temperature
humidity:
name: Humidity
update_interval: 30s
12 changes: 12 additions & 0 deletions tests/components/hdc2010/test.rp2040.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
i2c:
- id: i2c_hdc2010
scl: 5
sda: 4

sensor:
- platform: hdc2010
temperature:
name: Temperature
humidity:
name: Humidity
update_interval: 30s