7 static const char *
const TAG =
"bh1750.sensor";
9 static const uint8_t BH1750_COMMAND_POWER_ON = 0b00000001;
10 static const uint8_t BH1750_COMMAND_MT_REG_HI = 0b01000000;
11 static const uint8_t BH1750_COMMAND_MT_REG_LO = 0b01100000;
12 static const uint8_t BH1750_COMMAND_ONE_TIME_L = 0b00100011;
13 static const uint8_t BH1750_COMMAND_ONE_TIME_H = 0b00100000;
14 static const uint8_t BH1750_COMMAND_ONE_TIME_H2 = 0b00100001;
41 ESP_LOGCONFIG(TAG,
"Setting up BH1750 '%s'...", this->
name_.
c_str());
42 uint8_t turn_on = BH1750_COMMAND_POWER_ON;
51 uint8_t turn_on = BH1750_COMMAND_POWER_ON;
53 ESP_LOGW(TAG,
"Turning on BH1750 failed");
60 uint8_t mtreg_hi = BH1750_COMMAND_MT_REG_HI | ((mtreg >> 5) & 0b111);
61 uint8_t mtreg_lo = BH1750_COMMAND_MT_REG_LO | ((mtreg >> 0) & 0b11111);
63 ESP_LOGW(TAG,
"Setting measurement time for BH1750 failed");
75 cmd = BH1750_COMMAND_ONE_TIME_L;
76 meas_time = 24 * mtreg / 69;
79 cmd = BH1750_COMMAND_ONE_TIME_H;
80 meas_time = 180 * mtreg / 69;
83 cmd = BH1750_COMMAND_ONE_TIME_H2;
84 meas_time = 180 * mtreg / 69;
91 ESP_LOGW(TAG,
"Starting measurement for BH1750 failed");
99 this->
set_timeout(
"read", meas_time, [
this, mode, mtreg, f]() {
102 ESP_LOGW(TAG,
"Reading BH1750 data failed");
108 float lx = float(raw_value) / 1.2f;
118 LOG_SENSOR(
"",
"BH1750",
this);
119 LOG_I2C_DEVICE(
this);
121 ESP_LOGE(TAG,
"Communication with BH1750 failed!");
124 LOG_UPDATE_INTERVAL(
this);
131 if (std::isnan(val)) {
148 int ideal_mtreg = 50000 * 10 * 69 / (12 * (int) val);
149 use_mtreg = std::min(254, std::max(31, ideal_mtreg));
151 ESP_LOGV(TAG,
"L result: %f -> Calculated mode=%d, mtreg=%d", val, (
int) use_mode, use_mtreg);
153 this->
read_lx_(use_mode, use_mtreg, [
this](
float val) {
154 if (std::isnan(val)) {
159 ESP_LOGD(TAG,
"'%s': Got illuminance=%.1flx", this->
get_name().c_str(), val);
const float DATA
For components that import data from directly connected sensors like DHT.
void read_lx_(BH1750Mode mode, uint8_t mtreg, const std::function< void(float)> &f)
uint16_t i2ctohs(uint16_t i2cshort)
void status_set_warning(const char *message="unspecified")
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
No error found during execution of method.
void status_clear_warning()
BedjetMode mode
BedJet operating mode.
void publish_state(float state)
Publish a new state to the front-end.
float get_setup_priority() const override
constexpr const char * c_str() const
void dump_config() override
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
const StringRef & get_name() const