ESPHome 2026.11.0-dev
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
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 if (this->temperature_sensor_ != nullptr)
30 this->temperature_sensor_->publish_state(temperature);
31 if (this->humidity_sensor_ != nullptr)
32 this->humidity_sensor_->publish_state(humidity);
34}
36 uint8_t data[8];
37 data[0] = 0;
38 data[1] = 4;
39 if (!this->read_data_(data)) {
40 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
41 return;
42 }
43}
45 ESP_LOGD(TAG, "AM2320:");
46 LOG_I2C_DEVICE(this);
47 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
48 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
49}
50
51bool AM2320Component::read_bytes_(uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion) {
52 if (!this->write_bytes(a_register, data, 2)) {
53 ESP_LOGW(TAG, "Writing bytes for AM2320 failed!");
54 return false;
55 }
56
57 if (conversion > 0)
58 delay(conversion);
59 return this->read(data, len) == i2c::ERROR_OK;
60}
61
62bool AM2320Component::read_data_(uint8_t *data) {
63 // Wake up
64 this->write_bytes(0, data, 0);
65
66 // Write instruction 3, 2 bytes, get 8 bytes back (2 preamble, 2 bytes temperature, 2 bytes humidity, 2 bytes CRC)
67 if (!this->read_bytes_(3, data, 8, 2)) {
68 ESP_LOGW(TAG, "Updating AM2320 failed!");
69 return false;
70 }
71
72 if (crc16(data, 6) != encode_value_le<uint16_t>(&data[6])) {
73 ESP_LOGW(TAG, "AM2320 Checksum invalid!");
74 return false;
75 }
76
77 return true;
78}
79
80} // namespace esphome::am2320
void mark_failed()
Mark this component as failed.
void status_clear_warning()
Definition component.h:296
bool read_bytes_(uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion=0)
Definition am2320.cpp:51
sensor::Sensor * temperature_sensor_
Definition am2320.h:22
sensor::Sensor * humidity_sensor_
Definition am2320.h:23
bool read_data_(uint8_t *data)
Definition am2320.cpp:62
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
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
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:1039
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12