ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
gcja5.cpp
Go to the documentation of this file.
1/* From snooping with a logic analyzer, the I2C on this sensor is broken. I was only able
2 * to receive 1's as a response from the sensor. I was able to get the UART working.
3 *
4 * The datasheet says the values should be divided by 1000, but this must only be for the I2C
5 * implementation. Comparing UART values with another sensor, there is no need to divide by 1000.
6 */
7#include "gcja5.h"
8#include "esphome/core/log.h"
10#include <cstring>
11
12namespace esphome::gcja5 {
13
14ESPHOME_LOG_TAG(TAG, "gcja5");
15
16void GCJA5Component::loop() {
18 if (now - this->last_transmission_ >= 500) {
19 // last transmission too long ago. Reset RX index.
20 this->rx_message_.clear();
21 }
22
23 const size_t avail = this->available();
24 if (avail == 0) {
25 return;
26 }
27
28 // There must now be data waiting
29 this->last_transmission_ = now;
30 this->read_available(
31 [this](uint8_t val) {
32 this->rx_message_.push_back(val);
33
34 // check if rx_message_ has 32 bytes of data
35 if (this->rx_message_.size() == 32) {
36 if (this->parse_data_()) {
38 PUBLISH_STATE(this->pm_1_0_sensor_, get_32_bit_uint_(1));
39 PUBLISH_STATE(this->pm_2_5_sensor_, get_32_bit_uint_(5));
40 PUBLISH_STATE(this->pm_10_0_sensor_, get_32_bit_uint_(9));
41 PUBLISH_STATE(this->pmc_0_3_sensor_, get_16_bit_uint_(13));
42 PUBLISH_STATE(this->pmc_0_5_sensor_, get_16_bit_uint_(15));
43 PUBLISH_STATE(this->pmc_1_0_sensor_, get_16_bit_uint_(17));
44 PUBLISH_STATE(this->pmc_2_5_sensor_, get_16_bit_uint_(21));
45 PUBLISH_STATE(this->pmc_5_0_sensor_, get_16_bit_uint_(23));
46 PUBLISH_STATE(this->pmc_10_0_sensor_, get_16_bit_uint_(25));
47 } else {
48 this->status_set_warning();
49 ESP_LOGV(TAG, "Have 32 bytes but not good data. Skipping.");
50 }
51
52 this->rx_message_.clear();
53 }
54 },
55 avail);
56}
57
59 uint8_t crc = xor_bytes(&this->rx_message_[1], 29);
60
61 ESP_LOGVV(TAG, "Checksum packet was (0x%02X), calculated checksum was (0x%02X)", this->rx_message_[30], crc);
62
63 return (crc == this->rx_message_[30]);
64}
65
67 ESP_LOGVV(TAG, "GCJA5 Data: ");
68 for (uint8_t i = 0; i < 32; i++) {
69 ESP_LOGVV(TAG, " %u: 0b" BYTE_TO_BINARY_PATTERN " (0x%02X)", i + 1, BYTE_TO_BINARY(this->rx_message_[i]),
70 this->rx_message_[i]);
71 }
72
73 if (this->rx_message_[0] != 0x02 || this->rx_message_[31] != 0x03 || !this->calculate_checksum_()) {
74 ESP_LOGVV(TAG, "Discarding bad packet - failed checks.");
75 return false;
76 }
77 ESP_LOGVV(TAG, "Good packet found.");
78
79 uint8_t status = this->rx_message_[29];
80 if (!this->first_status_log_) {
81 this->first_status_log_ = true;
82
83 ESP_LOGI(TAG,
84 "GCJA5 Status\n"
85 " Overall Status : %i\n"
86 " PD Status : %i\n"
87 " LD Status : %i\n"
88 " Fan Status : %i",
89 (status >> 6) & 0x03, (status >> 4) & 0x03, (status >> 2) & 0x03, (status >> 0) & 0x03);
90 }
91 return true;
92}
93
95
96} // namespace esphome::gcja5
uint8_t status
Definition bl0942.h:8
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual void dump_config()
void status_clear_warning()
Definition component.h:309
uint32_t get_32_bit_uint_(uint8_t start_index) const
Definition gcja5.h:32
uint16_t get_16_bit_uint_(uint8_t start_index) const
Definition gcja5.h:29
std::vector< uint8_t > rx_message_
Definition gcja5.h:37
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
mopeka_std_values val[3]
ESPHOME_LOG_TAG(TAG, "gcja5")
constexpr uint8_t xor_bytes(const uint8_t *data, size_t len, uint8_t init=0)
XOR the bytes of data with size len, starting from init.
Definition helpers.h:949
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t