mirror of
https://github.com/esphome/esphome.git
synced 2024-12-03 13:34:01 +01:00
Add support for STS3x Temperature sensors (#669)
* Add support for Sensirion STS3x Temperature sensors * Removed humidty reading from STS3x sensor * Fixed line error and operand error * Fixed syntax * Add test snippet for STS3x sensor * Clean up * #550 Proactive fix for STS3x component reporting WARNING status and reinitialzing similar to SHT3xd * Flattened config. * Fixed missing temperature unit * Code formatting * Added marking for future commands * Cleanup * Removed whitespace * Cleanup * Cleanup
This commit is contained in:
parent
f9ca3f1c27
commit
89c1274d56
0
esphome/components/sts3x/__init__.py
Normal file
0
esphome/components/sts3x/__init__.py
Normal file
22
esphome/components/sts3x/sensor.py
Normal file
22
esphome/components/sts3x/sensor.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import esphome.codegen as cg
|
||||||
|
import esphome.config_validation as cv
|
||||||
|
from esphome.components import i2c, sensor
|
||||||
|
from esphome.const import CONF_ID, ICON_THERMOMETER, UNIT_CELSIUS
|
||||||
|
|
||||||
|
DEPENDENCIES = ['i2c']
|
||||||
|
|
||||||
|
sts3x_ns = cg.esphome_ns.namespace('sts3x')
|
||||||
|
|
||||||
|
STS3XComponent = sts3x_ns.class_('STS3XComponent', sensor.Sensor,
|
||||||
|
cg.PollingComponent, i2c.I2CDevice)
|
||||||
|
|
||||||
|
CONFIG_SCHEMA = sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1).extend({
|
||||||
|
cv.GenerateID(): cv.declare_id(STS3XComponent),
|
||||||
|
}).extend(cv.polling_component_schema('60s')).extend(i2c.i2c_device_schema(0x4A))
|
||||||
|
|
||||||
|
|
||||||
|
def to_code(config):
|
||||||
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
|
yield cg.register_component(var, config)
|
||||||
|
yield sensor.register_sensor(var, config)
|
||||||
|
yield i2c.register_i2c_device(var, config)
|
123
esphome/components/sts3x/sts3x.cpp
Normal file
123
esphome/components/sts3x/sts3x.cpp
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
#include "sts3x.h"
|
||||||
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
namespace sts3x {
|
||||||
|
|
||||||
|
static const char *TAG = "sts3x";
|
||||||
|
|
||||||
|
static const uint16_t STS3X_COMMAND_READ_SERIAL_NUMBER = 0x3780;
|
||||||
|
static const uint16_t STS3X_COMMAND_READ_STATUS = 0xF32D;
|
||||||
|
static const uint16_t STS3X_COMMAND_SOFT_RESET = 0x30A2;
|
||||||
|
static const uint16_t STS3X_COMMAND_POLLING_H = 0x2400;
|
||||||
|
|
||||||
|
/// Commands for future use
|
||||||
|
static const uint16_t STS3X_COMMAND_CLEAR_STATUS = 0x3041;
|
||||||
|
static const uint16_t STS3X_COMMAND_HEATER_ENABLE = 0x306D;
|
||||||
|
static const uint16_t STS3X_COMMAND_HEATER_DISABLE = 0x3066;
|
||||||
|
static const uint16_t STS3X_COMMAND_FETCH_DATA = 0xE000;
|
||||||
|
|
||||||
|
void STS3XComponent::setup() {
|
||||||
|
ESP_LOGCONFIG(TAG, "Setting up STS3x...");
|
||||||
|
if (!this->write_command_(STS3X_COMMAND_READ_SERIAL_NUMBER)) {
|
||||||
|
this->mark_failed();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t raw_serial_number[2];
|
||||||
|
if (!this->read_data_(raw_serial_number, 1)) {
|
||||||
|
this->mark_failed();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uint32_t serial_number = (uint32_t(raw_serial_number[0]) << 16);
|
||||||
|
ESP_LOGV(TAG, " Serial Number: 0x%08X", serial_number);
|
||||||
|
}
|
||||||
|
void STS3XComponent::dump_config() {
|
||||||
|
ESP_LOGCONFIG(TAG, "STS3x:");
|
||||||
|
LOG_I2C_DEVICE(this);
|
||||||
|
if (this->is_failed()) {
|
||||||
|
ESP_LOGE(TAG, "Communication with ST3x failed!");
|
||||||
|
}
|
||||||
|
LOG_UPDATE_INTERVAL(this);
|
||||||
|
|
||||||
|
LOG_SENSOR(" ", "STS3x", this);
|
||||||
|
}
|
||||||
|
float STS3XComponent::get_setup_priority() const { return setup_priority::DATA; }
|
||||||
|
void STS3XComponent::update() {
|
||||||
|
if (this->status_has_warning()) {
|
||||||
|
ESP_LOGD(TAG, "Retrying to reconnect the sensor.");
|
||||||
|
this->write_command_(STS3X_COMMAND_SOFT_RESET);
|
||||||
|
}
|
||||||
|
if (!this->write_command_(STS3X_COMMAND_POLLING_H)) {
|
||||||
|
this->status_set_warning();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->set_timeout(50, [this]() {
|
||||||
|
uint16_t raw_data[1];
|
||||||
|
if (!this->read_data_(raw_data, 1)) {
|
||||||
|
this->status_set_warning();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float temperature = 175.0f * float(raw_data[0]) / 65535.0f - 45.0f;
|
||||||
|
ESP_LOGD(TAG, "Got temperature=%.2f°C", temperature);
|
||||||
|
this->publish_state(temperature);
|
||||||
|
this->status_clear_warning();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool STS3XComponent::write_command_(uint16_t command) {
|
||||||
|
// Warning ugly, trick the I2Ccomponent base by setting register to the first 8 bit.
|
||||||
|
return this->write_byte(command >> 8, command & 0xFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t sts3x_crc(uint8_t data1, uint8_t data2) {
|
||||||
|
uint8_t bit;
|
||||||
|
uint8_t crc = 0xFF;
|
||||||
|
|
||||||
|
crc ^= data1;
|
||||||
|
for (bit = 8; bit > 0; --bit) {
|
||||||
|
if (crc & 0x80)
|
||||||
|
crc = (crc << 1) ^ 0x131;
|
||||||
|
else
|
||||||
|
crc = (crc << 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
crc ^= data2;
|
||||||
|
for (bit = 8; bit > 0; --bit) {
|
||||||
|
if (crc & 0x80)
|
||||||
|
crc = (crc << 1) ^ 0x131;
|
||||||
|
else
|
||||||
|
crc = (crc << 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return crc;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool STS3XComponent::read_data_(uint16_t *data, uint8_t len) {
|
||||||
|
const uint8_t num_bytes = len * 3;
|
||||||
|
auto *buf = new uint8_t[num_bytes];
|
||||||
|
|
||||||
|
if (!this->parent_->raw_receive(this->address_, buf, num_bytes)) {
|
||||||
|
delete[](buf);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < len; i++) {
|
||||||
|
const uint8_t j = 3 * i;
|
||||||
|
uint8_t crc = sts3x_crc(buf[j], buf[j + 1]);
|
||||||
|
if (crc != buf[j + 2]) {
|
||||||
|
ESP_LOGE(TAG, "CRC8 Checksum invalid! 0x%02X != 0x%02X", buf[j + 2], crc);
|
||||||
|
delete[](buf);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
data[i] = (buf[j] << 8) | buf[j + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
delete[](buf);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace sts3x
|
||||||
|
} // namespace esphome
|
24
esphome/components/sts3x/sts3x.h
Normal file
24
esphome/components/sts3x/sts3x.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "esphome/core/component.h"
|
||||||
|
#include "esphome/components/sensor/sensor.h"
|
||||||
|
#include "esphome/components/i2c/i2c.h"
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
namespace sts3x {
|
||||||
|
|
||||||
|
/// This class implements support for the ST3x-DIS family of temperature i2c sensors.
|
||||||
|
class STS3XComponent : public sensor::Sensor, public PollingComponent, public i2c::I2CDevice {
|
||||||
|
public:
|
||||||
|
void setup() override;
|
||||||
|
void dump_config() override;
|
||||||
|
float get_setup_priority() const override;
|
||||||
|
void update() override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool write_command_(uint16_t command);
|
||||||
|
bool read_data_(uint16_t *data, uint8_t len);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace sts3x
|
||||||
|
} // namespace esphome
|
@ -522,6 +522,9 @@ sensor:
|
|||||||
name: "Living Room Humidity 8"
|
name: "Living Room Humidity 8"
|
||||||
address: 0x44
|
address: 0x44
|
||||||
update_interval: 15s
|
update_interval: 15s
|
||||||
|
- platform: sts3x
|
||||||
|
name: "Living Room Temperature 9"
|
||||||
|
address: 0x4A
|
||||||
- platform: scd30
|
- platform: scd30
|
||||||
co2:
|
co2:
|
||||||
name: "Living Room CO2 9"
|
name: "Living Room CO2 9"
|
||||||
|
Loading…
Reference in New Issue
Block a user