7static const char *
const TAG =
"bh1750.sensor";
9static const uint8_t BH1750_COMMAND_POWER_ON = 0b00000001;
10static const uint8_t BH1750_COMMAND_MT_REG_HI = 0b01000000;
11static const uint8_t BH1750_COMMAND_MT_REG_LO = 0b01100000;
12static const uint8_t BH1750_COMMAND_ONE_TIME_L = 0b00100011;
13static const uint8_t BH1750_COMMAND_ONE_TIME_H = 0b00100000;
14static const uint8_t BH1750_COMMAND_ONE_TIME_H2 = 0b00100001;
41 ESP_LOGCONFIG(TAG,
"Running setup for '%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");
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, ESP_LOG_MSG_COMM_FAIL_FOR, this->
get_name().c_str());
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);
BedjetMode mode
BedJet operating mode.
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
const StringRef & get_name() const
constexpr const char * c_str() const
void read_lx_(BH1750Mode mode, uint8_t mtreg, const std::function< void(float)> &f)
void dump_config() override
float get_setup_priority() const override
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
void publish_state(float state)
Publish a new state to the front-end.
uint16_t i2ctohs(uint16_t i2cshort)
@ ERROR_OK
No error found during execution of method.
const float DATA
For components that import data from directly connected sensors like DHT.
Providing packet encoding functions for exchanging data with a remote host.