ESPHome 2026.11.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
28
29ESPHOME_LOG_TAG(TAG, "am2315c");
30
31bool AM2315C::reset_register_(uint8_t reg) {
32 // code based on demo code sent by www.aosong.com
33 // no further documentation.
34 // 0x1B returned 18, 0, 4
35 // 0x1C returned 18, 65, 0
36 // 0x1E returned 18, 8, 0
37 // 18 seems to be status register
38 // other values unknown.
39 uint8_t data[3];
40 data[0] = reg;
41 data[1] = 0;
42 data[2] = 0;
43 ESP_LOGD(TAG, "Reset register: 0x%02x", reg);
44 if (this->write(data, 3) != i2c::ERROR_OK) {
45 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
46 return false;
47 }
48 delay(5);
49 if (this->read(data, 3) != i2c::ERROR_OK) {
50 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
51 return false;
52 }
53 delay(10);
54 data[0] = 0xB0 | reg;
55 if (this->write(data, 3) != i2c::ERROR_OK) {
56 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
57 return false;
58 }
59 delay(5);
60 return true;
61}
62
63bool AM2315C::convert_(uint8_t *data, float &humidity, float &temperature) {
65 raw = (data[1] << 12) | (data[2] << 4) | (data[3] >> 4);
66 humidity = raw * 9.5367431640625e-5;
67 raw = encode_uint24(data[3] & 0x0F, data[4], data[5]);
68 temperature = raw * 1.9073486328125e-4 - 50;
69 return crc8(data, 6, 0xFF, 0x31, true) == data[6];
70}
71
73 // get status
74 uint8_t status = 0;
75 if (this->read(&status, 1) != i2c::ERROR_OK) {
76 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
77 return;
78 }
79
80 // reset registers if required, according to the datasheet
81 // this can be required after power on, although this was
82 // never required during testing
83 if ((status & 0x18) != 0x18) {
84 ESP_LOGD(TAG, "Resetting AM2315C registers");
85 // reset_register_() marks the component failed with its own reason
86 if (!this->reset_register_(0x1B) || !this->reset_register_(0x1C) || !this->reset_register_(0x1E))
87 return;
88 }
89}
90
92 // request measurement
93 uint8_t data[3];
94 data[0] = 0xAC;
95 data[1] = 0x33;
96 data[2] = 0x00;
97 if (this->write(data, 3) != i2c::ERROR_OK) {
98 ESP_LOGE(TAG, "Write failed!");
99 this->status_set_warning();
100 return;
101 }
102
103 // wait for hw to complete measurement
104 set_timeout(160, [this]() {
105 // check status
106 uint8_t status = 0;
107 if (this->read(&status, 1) != i2c::ERROR_OK) {
108 ESP_LOGE(TAG, "Read failed!");
109 this->status_set_warning();
110 return;
111 }
112 if ((status & 0x80) == 0x80) {
113 ESP_LOGE(TAG, "HW still busy!");
114 this->status_set_warning();
115 return;
116 }
117
118 // read
119 uint8_t data[7];
120 if (this->read(data, 7) != i2c::ERROR_OK) {
121 ESP_LOGE(TAG, "Read failed!");
122 this->status_set_warning();
123 return;
124 }
125
126 // check for all zeros
127 bool zeros = true;
128 for (uint8_t i : data) {
129 zeros = zeros && (i == 0);
130 }
131 if (zeros) {
132 ESP_LOGW(TAG, "Data all zeros!");
133 this->status_set_warning();
134 return;
135 }
136
137 // convert
138 float temperature = 0.0;
139 float humidity = 0.0;
140 if (this->convert_(data, humidity, temperature)) {
141 if (this->temperature_sensor_ != nullptr) {
142 this->temperature_sensor_->publish_state(temperature);
143 }
144 if (this->humidity_sensor_ != nullptr) {
145 this->humidity_sensor_->publish_state(humidity);
146 }
147 this->status_clear_warning();
148 } else {
149 ESP_LOGW(TAG, "CRC failed!");
150 this->status_set_warning();
151 }
152 });
153}
154
156 ESP_LOGCONFIG(TAG, "AM2315C:");
157 LOG_I2C_DEVICE(this);
158 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
159 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
160}
161
162} // namespace esphome::am2315c
uint8_t raw[35]
Definition bl0939.h:0
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void status_clear_warning()
Definition component.h:296
void update() override
Definition am2315c.cpp:91
void dump_config() override
Definition am2315c.cpp:155
void setup() override
Definition am2315c.cpp:72
sensor::Sensor * humidity_sensor_
Definition am2315c.h:44
bool convert_(uint8_t *data, float &humidity, float &temperature)
Definition am2315c.cpp:63
bool reset_register_(uint8_t reg)
Definition am2315c.cpp:31
sensor::Sensor * temperature_sensor_
Definition am2315c.h:43
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:175
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
ESPHOME_LOG_TAG(TAG, "am2315c")
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
Definition helpers.h:1002
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:68
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12