ESPHome  2024.12.2
sen0321.cpp
Go to the documentation of this file.
1 #include "sen0321.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/hal.h"
4 
5 namespace esphome {
6 namespace sen0321_sensor {
7 
8 static const char *const TAG = "sen0321_sensor.sensor";
9 
11  ESP_LOGCONFIG(TAG, "Setting up sen0321...");
12  if (!this->write_byte(SENSOR_MODE_REGISTER, SENSOR_MODE_AUTO)) {
13  ESP_LOGW(TAG, "Error setting measurement mode.");
14  this->mark_failed();
15  };
16 }
17 
18 void Sen0321Sensor::update() { this->read_data_(); }
19 
21  ESP_LOGCONFIG(TAG, "DF Robot Ozone Sensor sen0321:");
22  LOG_I2C_DEVICE(this);
23  if (this->is_failed()) {
24  ESP_LOGE(TAG, "Communication with sen0321 failed!");
25  }
26  LOG_UPDATE_INTERVAL(this);
27 }
28 
30  uint8_t result[2];
31  this->read_bytes(SENSOR_AUTO_READ_REG, result, (uint8_t) 2);
32  this->publish_state(((uint16_t) (result[0] << 8) + result[1]));
33 }
34 
35 } // namespace sen0321_sensor
36 } // namespace esphome
bool is_failed() const
Definition: component.cpp:143
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition: i2c.h:212
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7