ESPHome 2025.5.0
Loading...
Searching...
No Matches
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
5namespace esphome {
6namespace sen0321_sensor {
7
8static 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
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
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
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:216
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7