7 static const char *
const TAG =
"hmc5883l";
8 static const uint8_t HMC5883L_ADDRESS = 0x1E;
9 static const uint8_t HMC5883L_REGISTER_CONFIG_A = 0x00;
10 static const uint8_t HMC5883L_REGISTER_CONFIG_B = 0x01;
11 static const uint8_t HMC5883L_REGISTER_MODE = 0x02;
12 static const uint8_t HMC5883L_REGISTER_DATA_X_MSB = 0x03;
13 static const uint8_t HMC5883L_REGISTER_DATA_X_LSB = 0x04;
14 static const uint8_t HMC5883L_REGISTER_DATA_Z_MSB = 0x05;
15 static const uint8_t HMC5883L_REGISTER_DATA_Z_LSB = 0x06;
16 static const uint8_t HMC5883L_REGISTER_DATA_Y_MSB = 0x07;
17 static const uint8_t HMC5883L_REGISTER_DATA_Y_LSB = 0x08;
18 static const uint8_t HMC5883L_REGISTER_STATUS = 0x09;
19 static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_A = 0x0A;
20 static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_B = 0x0B;
21 static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_C = 0x0C;
24 ESP_LOGCONFIG(TAG,
"Setting up HMC5583L...");
26 if (!this->
read_byte(HMC5883L_REGISTER_IDENTIFICATION_A, &
id[0]) ||
27 !this->
read_byte(HMC5883L_REGISTER_IDENTIFICATION_B, &
id[1]) ||
28 !this->
read_byte(HMC5883L_REGISTER_IDENTIFICATION_C, &
id[2])) {
34 if (
id[0] != 0x48 ||
id[1] != 0x34 ||
id[2] != 0x33) {
44 if (!this->
write_byte(HMC5883L_REGISTER_CONFIG_A, config_a)) {
51 config_b |= this->
range_ << 5;
52 if (!this->
write_byte(HMC5883L_REGISTER_CONFIG_B, config_b)) {
61 if (!this->
write_byte(HMC5883L_REGISTER_MODE, mode)) {
68 ESP_LOGCONFIG(TAG,
"HMC5883L:");
71 ESP_LOGE(TAG,
"Communication with HMC5883L failed!");
73 ESP_LOGE(TAG,
"The ID registers don't match - Is this really an HMC5883L?");
75 LOG_UPDATE_INTERVAL(
this);
77 LOG_SENSOR(
" ",
"X Axis", this->
x_sensor_);
78 LOG_SENSOR(
" ",
"Y Axis", this->
y_sensor_);
79 LOG_SENSOR(
" ",
"Z Axis", this->
z_sensor_);
84 uint16_t raw_x, raw_y, raw_z;
85 if (!this->
read_byte_16(HMC5883L_REGISTER_DATA_X_MSB, &raw_x) ||
86 !this->
read_byte_16(HMC5883L_REGISTER_DATA_Y_MSB, &raw_y) ||
87 !this->
read_byte_16(HMC5883L_REGISTER_DATA_Z_MSB, &raw_z)) {
123 const float x = int16_t(raw_x) * mg_per_bit * 0.1f;
124 const float y = int16_t(raw_y) * mg_per_bit * 0.1f;
125 const float z = int16_t(raw_z) * mg_per_bit * 0.1f;
127 float heading = atan2f(0.0f - x, y) * 180.0f / M_PI;
128 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
sensor::Sensor * x_sensor_
BedjetMode mode
BedJet operating mode.
HMC5883LOversampling oversampling_
void publish_state(float state)
Publish a new state to the front-end.
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.
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
sensor::Sensor * z_sensor_
float get_setup_priority() const override
HMC5883LDatarate datarate_
sensor::Sensor * y_sensor_