ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
am2320.cpp
Go to the documentation of this file.
1// Implementation based on:
2// - ESPEasy: https://github.com/letscontrolit/ESPEasy/blob/mega/src/_P034_DHT12.ino
3// - DHT12_sensor_library: https://github.com/xreef/DHT12_sensor_library/blob/master/DHT12.cpp
4// - Arduino - AM2320: https://github.com/EngDial/AM2320/blob/master/src/AM2320.cpp
5
6#include "am2320.h"
7#include "esphome/core/hal.h"
9#include "esphome/core/log.h"
10
11namespace esphome::am2320 {
12
13ESPHOME_LOG_TAG(TAG, "am2320");
14
15void AM2320Component::update() {
16 uint8_t data[8];
17 data[0] = 0;
18 data[1] = 4;
19 if (!this->read_data_(data)) {
20 this->status_set_warning();
21 return;
22 }
23
24 float temperature = (((data[4] & 0x7F) << 8) + data[5]) / 10.0f;
25 temperature = (data[4] & 0x80) ? -temperature : temperature;
26 float humidity = ((data[2] << 8) + data[3]) / 10.0f;
27
28 ESP_LOGD(TAG, "Got temperature=%.1f°C humidity=%.1f%%", temperature, humidity);
29 PUBLISH_STATE(this->temperature_sensor_, temperature);
30 PUBLISH_STATE(this->humidity_sensor_, humidity);
32}
34 uint8_t data[8];
35 data[0] = 0;
36 data[1] = 4;
37 if (!this->read_data_(data)) {
38 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
39 return;
40 }
41}
42void AM2320Component::dump_config() {
43 ESP_LOGD(TAG, "AM2320:");
44 LOG_I2C_DEVICE(this);
45 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
46 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
47}
48
49bool AM2320Component::read_bytes_(uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion) {
50 if (!this->write_bytes(a_register, data, 2)) {
51 ESP_LOGW(TAG, "Writing bytes for AM2320 failed!");
52 return false;
53 }
54
55 if (conversion > 0)
56 delay(conversion);
57 return this->read(data, len) == i2c::ERROR_OK;
58}
59
60bool AM2320Component::read_data_(uint8_t *data) {
61 // Wake up
62 this->write_bytes(0, data, 0);
63
64 // Write instruction 3, 2 bytes, get 8 bytes back (2 preamble, 2 bytes temperature, 2 bytes humidity, 2 bytes CRC)
65 if (!this->read_bytes_(3, data, 8, 2)) {
66 ESP_LOGW(TAG, "Updating AM2320 failed!");
67 return false;
68 }
69
70 if (crc16(data, 6) != encode_value_le<uint16_t>(&data[6])) {
71 ESP_LOGW(TAG, "AM2320 Checksum invalid!");
72 return false;
73 }
74
75 return true;
76}
77
78} // namespace esphome::am2320
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void status_clear_warning()
Definition component.h:309
bool read_bytes_(uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion=0)
Definition am2320.cpp:49
bool read_data_(uint8_t *data)
Definition am2320.cpp:60
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:175
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
ESPHOME_LOG_TAG(TAG, "am2320")
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.
Definition helpers.cpp:95
const void size_t len
Definition hal.h:68
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1084
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12