8 static const char *
const TAG =
"hmc5883l";
9 static const uint8_t HMC5883L_ADDRESS = 0x1E;
10 static const uint8_t HMC5883L_REGISTER_CONFIG_A = 0x00;
11 static const uint8_t HMC5883L_REGISTER_CONFIG_B = 0x01;
12 static const uint8_t HMC5883L_REGISTER_MODE = 0x02;
13 static const uint8_t HMC5883L_REGISTER_DATA_X_MSB = 0x03;
14 static const uint8_t HMC5883L_REGISTER_DATA_X_LSB = 0x04;
15 static const uint8_t HMC5883L_REGISTER_DATA_Z_MSB = 0x05;
16 static const uint8_t HMC5883L_REGISTER_DATA_Z_LSB = 0x06;
17 static const uint8_t HMC5883L_REGISTER_DATA_Y_MSB = 0x07;
18 static const uint8_t HMC5883L_REGISTER_DATA_Y_LSB = 0x08;
19 static const uint8_t HMC5883L_REGISTER_STATUS = 0x09;
20 static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_A = 0x0A;
21 static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_B = 0x0B;
22 static 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)) {
66 if (!this->
write_byte(HMC5883L_REGISTER_MODE, mode)) {
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);
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
bool read_byte_16(uint8_t a_register, uint16_t *data)
const float DATA
For components that import data from directly connected sensors like DHT.
sensor::Sensor * heading_sensor_
void status_set_warning(const char *message="unspecified")
void dump_config() override
HighFrequencyLoopRequester high_freq_
sensor::Sensor * x_sensor_
void start()
Start running the loop continuously.
BedjetMode mode
BedJet operating mode.
HMC5883LOversampling oversampling_
void publish_state(float state)
Publish a new state to the front-end.
Application App
Global storage of Application pointer - only one Application can exist.
uint32_t get_loop_interval() const
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
enum esphome::hmc5883l::HMC5883LComponent::ErrorCode error_code_
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
sensor::Sensor * z_sensor_
float get_setup_priority() const override
HMC5883LDatarate datarate_
sensor::Sensor * y_sensor_