8static const char *
const TAG =
"hmc5883l";
9static const uint8_t HMC5883L_ADDRESS = 0x1E;
10static const uint8_t HMC5883L_REGISTER_CONFIG_A = 0x00;
11static const uint8_t HMC5883L_REGISTER_CONFIG_B = 0x01;
12static const uint8_t HMC5883L_REGISTER_MODE = 0x02;
13static const uint8_t HMC5883L_REGISTER_DATA_X_MSB = 0x03;
14static const uint8_t HMC5883L_REGISTER_DATA_X_LSB = 0x04;
15static const uint8_t HMC5883L_REGISTER_DATA_Z_MSB = 0x05;
16static const uint8_t HMC5883L_REGISTER_DATA_Z_LSB = 0x06;
17static const uint8_t HMC5883L_REGISTER_DATA_Y_MSB = 0x07;
18static const uint8_t HMC5883L_REGISTER_DATA_Y_LSB = 0x08;
19static const uint8_t HMC5883L_REGISTER_STATUS = 0x09;
20static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_A = 0x0A;
21static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_B = 0x0B;
22static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_C = 0x0C;
25 ESP_LOGCONFIG(TAG,
"Setting up HMC5583L...");
27 if (!this->
read_byte(HMC5883L_REGISTER_IDENTIFICATION_A, &
id[0]) ||
28 !this->
read_byte(HMC5883L_REGISTER_IDENTIFICATION_B, &
id[1]) ||
29 !this->
read_byte(HMC5883L_REGISTER_IDENTIFICATION_C, &
id[2])) {
39 if (
id[0] != 0x48 ||
id[1] != 0x34 ||
id[2] != 0x33) {
49 if (!this->
write_byte(HMC5883L_REGISTER_CONFIG_A, config_a)) {
56 config_b |= this->
range_ << 5;
57 if (!this->
write_byte(HMC5883L_REGISTER_CONFIG_B, config_b)) {
73 ESP_LOGCONFIG(TAG,
"HMC5883L:");
76 ESP_LOGE(TAG,
"Communication with HMC5883L failed!");
78 ESP_LOGE(TAG,
"The ID registers don't match - Is this really an HMC5883L?");
80 LOG_UPDATE_INTERVAL(
this);
82 LOG_SENSOR(
" ",
"X Axis", this->
x_sensor_);
83 LOG_SENSOR(
" ",
"Y Axis", this->
y_sensor_);
84 LOG_SENSOR(
" ",
"Z Axis", this->
z_sensor_);
89 uint16_t raw_x, raw_y, raw_z;
90 if (!this->
read_byte_16(HMC5883L_REGISTER_DATA_X_MSB, &raw_x) ||
91 !this->
read_byte_16(HMC5883L_REGISTER_DATA_Y_MSB, &raw_y) ||
92 !this->
read_byte_16(HMC5883L_REGISTER_DATA_Z_MSB, &raw_z)) {
128 const float x = int16_t(raw_x) * mg_per_bit * 0.1f;
129 const float y = int16_t(raw_y) * mg_per_bit * 0.1f;
130 const float z = int16_t(raw_z) * mg_per_bit * 0.1f;
132 float heading = atan2f(0.0f -
x,
y) * 180.0f / M_PI;
133 ESP_LOGD(TAG,
"Got x=%0.02fµT y=%0.02fµT z=%0.02fµT heading=%0.01f°",
x,
y,
z, heading);
BedjetMode mode
BedJet operating mode.
uint32_t get_loop_interval() const
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message="unspecified")
void start()
Start running the loop continuously.
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
void dump_config() override
HMC5883LDatarate datarate_
sensor::Sensor * z_sensor_
HighFrequencyLoopRequester high_freq_
sensor::Sensor * heading_sensor_
HMC5883LOversampling oversampling_
float get_setup_priority() const override
sensor::Sensor * y_sensor_
sensor::Sensor * x_sensor_
enum esphome::hmc5883l::HMC5883LComponent::ErrorCode error_code_
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
bool read_byte_16(uint8_t a_register, uint16_t *data)
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
void publish_state(float state)
Publish a new state to the front-end.
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.
Application App
Global storage of Application pointer - only one Application can exist.