ESPHome 2026.3.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 {
12namespace am2320 {
13
14static const char *const TAG = "am2320";
15
17 uint8_t data[8];
18 data[0] = 0;
19 data[1] = 4;
20 if (!this->read_data_(data)) {
21 this->status_set_warning();
22 return;
23 }
24
25 float temperature = (((data[4] & 0x7F) << 8) + data[5]) / 10.0f;
26 temperature = (data[4] & 0x80) ? -temperature : temperature;
27 float humidity = ((data[2] << 8) + data[3]) / 10.0f;
28
29 ESP_LOGD(TAG, "Got temperature=%.1f°C humidity=%.1f%%", temperature, humidity);
30 if (this->temperature_sensor_ != nullptr)
31 this->temperature_sensor_->publish_state(temperature);
32 if (this->humidity_sensor_ != nullptr)
33 this->humidity_sensor_->publish_state(humidity);
35}
37 uint8_t data[8];
38 data[0] = 0;
39 data[1] = 4;
40 if (!this->read_data_(data)) {
41 this->mark_failed();
42 return;
43 }
44}
46 ESP_LOGD(TAG, "AM2320:");
47 LOG_I2C_DEVICE(this);
48 if (this->is_failed()) {
49 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
50 }
51 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
52 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
53}
54
55bool AM2320Component::read_bytes_(uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion) {
56 if (!this->write_bytes(a_register, data, 2)) {
57 ESP_LOGW(TAG, "Writing bytes for AM2320 failed!");
58 return false;
59 }
60
61 if (conversion > 0)
62 delay(conversion);
63 return this->read(data, len) == i2c::ERROR_OK;
64}
65
66bool AM2320Component::read_data_(uint8_t *data) {
67 // Wake up
68 this->write_bytes(0, data, 0);
69
70 // Write instruction 3, 2 bytes, get 8 bytes back (2 preamble, 2 bytes temperature, 2 bytes humidity, 2 bytes CRC)
71 if (!this->read_bytes_(3, data, 8, 2)) {
72 ESP_LOGW(TAG, "Updating AM2320 failed!");
73 return false;
74 }
75
76 uint16_t checksum;
77
78 checksum = data[7] << 8;
79 checksum += data[6];
80
81 if (crc16(data, 6) != checksum) {
82 ESP_LOGW(TAG, "AM2320 Checksum invalid!");
83 return false;
84 }
85
86 return true;
87}
88
89} // namespace am2320
90} // namespace esphome
uint8_t checksum
Definition bl0906.h:3
void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
bool read_bytes_(uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion=0)
Definition am2320.cpp:55
sensor::Sensor * temperature_sensor_
Definition am2320.h:23
sensor::Sensor * humidity_sensor_
Definition am2320.h:24
bool read_data_(uint8_t *data)
Definition am2320.cpp:66
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:251
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:73
std::string size_t len
Definition helpers.h:817
void HOT delay(uint32_t ms)
Definition core.cpp:27
uint16_t temperature
Definition sun_gtil2.cpp:12