ESPHome 2025.10.0-dev
Loading...
Searching...
No Matches
am2315c.cpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2023-2024 Rob Tillaart
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22#include "am2315c.h"
23#include "esphome/core/hal.h"
25#include "esphome/core/log.h"
26
27namespace esphome {
28namespace am2315c {
29
30static const char *const TAG = "am2315c";
31
32bool AM2315C::reset_register_(uint8_t reg) {
33 // code based on demo code sent by www.aosong.com
34 // no further documentation.
35 // 0x1B returned 18, 0, 4
36 // 0x1C returned 18, 65, 0
37 // 0x1E returned 18, 8, 0
38 // 18 seems to be status register
39 // other values unknown.
40 uint8_t data[3];
41 data[0] = reg;
42 data[1] = 0;
43 data[2] = 0;
44 ESP_LOGD(TAG, "Reset register: 0x%02x", reg);
45 if (this->write(data, 3) != i2c::ERROR_OK) {
46 ESP_LOGE(TAG, "Write failed!");
47 this->mark_failed();
48 return false;
49 }
50 delay(5);
51 if (this->read(data, 3) != i2c::ERROR_OK) {
52 ESP_LOGE(TAG, "Read failed!");
53 this->mark_failed();
54 return false;
55 }
56 delay(10);
57 data[0] = 0xB0 | reg;
58 if (this->write(data, 3) != i2c::ERROR_OK) {
59 ESP_LOGE(TAG, "Write failed!");
60 this->mark_failed();
61 return false;
62 }
63 delay(5);
64 return true;
65}
66
67bool AM2315C::convert_(uint8_t *data, float &humidity, float &temperature) {
68 uint32_t raw;
69 raw = (data[1] << 12) | (data[2] << 4) | (data[3] >> 4);
70 humidity = raw * 9.5367431640625e-5;
71 raw = ((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5];
72 temperature = raw * 1.9073486328125e-4 - 50;
73 return crc8(data, 6, 0xFF, 0x31, true) == data[6];
74}
75
77 // get status
78 uint8_t status = 0;
79 if (this->read(&status, 1) != i2c::ERROR_OK) {
80 ESP_LOGE(TAG, "Read failed!");
81 this->mark_failed();
82 return;
83 }
84
85 // reset registers if required, according to the datasheet
86 // this can be required after power on, although this was
87 // never required during testing
88 if ((status & 0x18) != 0x18) {
89 ESP_LOGD(TAG, "Resetting AM2315C registers");
90 if (!this->reset_register_(0x1B)) {
91 this->mark_failed();
92 return;
93 }
94 if (!this->reset_register_(0x1C)) {
95 this->mark_failed();
96 return;
97 }
98 if (!this->reset_register_(0x1E)) {
99 this->mark_failed();
100 return;
101 }
102 }
103}
104
106 // request measurement
107 uint8_t data[3];
108 data[0] = 0xAC;
109 data[1] = 0x33;
110 data[2] = 0x00;
111 if (this->write(data, 3) != i2c::ERROR_OK) {
112 ESP_LOGE(TAG, "Write failed!");
113 this->status_set_warning();
114 return;
115 }
116
117 // wait for hw to complete measurement
118 set_timeout(160, [this]() {
119 // check status
120 uint8_t status = 0;
121 if (this->read(&status, 1) != i2c::ERROR_OK) {
122 ESP_LOGE(TAG, "Read failed!");
123 this->status_set_warning();
124 return;
125 }
126 if ((status & 0x80) == 0x80) {
127 ESP_LOGE(TAG, "HW still busy!");
128 this->status_set_warning();
129 return;
130 }
131
132 // read
133 uint8_t data[7];
134 if (this->read(data, 7) != i2c::ERROR_OK) {
135 ESP_LOGE(TAG, "Read failed!");
136 this->status_set_warning();
137 return;
138 }
139
140 // check for all zeros
141 bool zeros = true;
142 for (uint8_t i : data) {
143 zeros = zeros && (i == 0);
144 }
145 if (zeros) {
146 ESP_LOGW(TAG, "Data all zeros!");
147 this->status_set_warning();
148 return;
149 }
150
151 // convert
152 float temperature = 0.0;
153 float humidity = 0.0;
154 if (this->convert_(data, humidity, temperature)) {
155 if (this->temperature_sensor_ != nullptr) {
156 this->temperature_sensor_->publish_state(temperature);
157 }
158 if (this->humidity_sensor_ != nullptr) {
159 this->humidity_sensor_->publish_state(humidity);
160 }
161 this->status_clear_warning();
162 } else {
163 ESP_LOGW(TAG, "CRC failed!");
164 this->status_set_warning();
165 }
166 });
167}
168
170 ESP_LOGCONFIG(TAG, "AM2315C:");
171 LOG_I2C_DEVICE(this);
172 if (this->is_failed()) {
173 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
174 }
175 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
176 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
177}
178
180
181} // namespace am2315c
182} // namespace esphome
uint8_t raw[35]
Definition bl0939.h:0
uint8_t status
Definition bl0942.h:8
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void update() override
Definition am2315c.cpp:105
void dump_config() override
Definition am2315c.cpp:169
void setup() override
Definition am2315c.cpp:76
sensor::Sensor * humidity_sensor_
Definition am2315c.h:46
float get_setup_priority() const override
Definition am2315c.cpp:179
bool convert_(uint8_t *data, float &humidity, float &temperature)
Definition am2315c.cpp:67
bool reset_register_(uint8_t reg)
Definition am2315c.cpp:32
sensor::Sensor * temperature_sensor_
Definition am2315c.h:45
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:184
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:153
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:73
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:33
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:49
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.
Definition helpers.cpp:44
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint16_t temperature
Definition sun_gtil2.cpp:12