ESPHome 2026.6.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
13static const char *const 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();
41 return;
42 }
43}
45 ESP_LOGD(TAG, "AM2320:");
46 LOG_I2C_DEVICE(this);
47 if (this->is_failed()) {
48 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
49 }
50 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
51 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
52}
53
54bool AM2320Component::read_bytes_(uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion) {
55 if (!this->write_bytes(a_register, data, 2)) {
56 ESP_LOGW(TAG, "Writing bytes for AM2320 failed!");
57 return false;
58 }
59
60 if (conversion > 0)
61 delay(conversion);
62 return this->read(data, len) == i2c::ERROR_OK;
63}
64
65bool AM2320Component::read_data_(uint8_t *data) {
66 // Wake up
67 this->write_bytes(0, data, 0);
68
69 // Write instruction 3, 2 bytes, get 8 bytes back (2 preamble, 2 bytes temperature, 2 bytes humidity, 2 bytes CRC)
70 if (!this->read_bytes_(3, data, 8, 2)) {
71 ESP_LOGW(TAG, "Updating AM2320 failed!");
72 return false;
73 }
74
75 uint16_t checksum;
76
77 checksum = data[7] << 8;
78 checksum += data[6];
79
80 if (crc16(data, 6) != checksum) {
81 ESP_LOGW(TAG, "AM2320 Checksum invalid!");
82 return false;
83 }
84
85 return true;
86}
87
88} // namespace esphome::am2320
uint8_t checksum
Definition bl0906.h:3
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
void status_clear_warning()
Definition component.h:289
bool read_bytes_(uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion=0)
Definition am2320.cpp:54
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:65
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:68
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
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:86
const void size_t len
Definition hal.h:64
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12