ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
apc1.h
Go to the documentation of this file.
1#pragma once
2
5#include "esphome/core/hal.h"
8
9namespace esphome::apc1 {
10
11static constexpr uint8_t APC1_HEADER_1 = 0x42;
12static constexpr uint8_t APC1_HEADER_2 = 0x4D;
13static constexpr uint8_t APC1_FRAME_HEADER_SIZE = 4; // 2 start bytes (0x42, 0x4D) + 2 frame length bytes
14static constexpr uint8_t APC1_FRAME_SIZE_MEASUREMENT = 64;
15static constexpr uint8_t APC1_FRAME_SIZE_DEVICE_INFO = 23;
16static constexpr uint8_t APC1_FRAME_SIZE_COMMAND_RESPONSE = 8;
17static constexpr uint8_t APC1_COMMAND_FRAME_SIZE = 7;
18
25
26static constexpr uint16_t APC1_MEASUREMENT_MODE_PASSIVE = 0x0000;
27static constexpr uint16_t APC1_MEASUREMENT_MODE_ACTIVE = 0x0001;
28
29static constexpr uint16_t APC1_OPERATING_MODE_IDLE = 0x0000;
30static constexpr uint16_t APC1_OPERATING_MODE_STANDARD = 0x0001;
31
32class APC1Component final : public uart::UARTDevice, public Component {
33 SUB_SENSOR(pm_1_0)
34 SUB_SENSOR(pm_2_5)
35 SUB_SENSOR(pm_10_0)
36 SUB_SENSOR(pm_1_0_std)
37 SUB_SENSOR(pm_2_5_std)
38 SUB_SENSOR(pm_10_0_std)
39
40 SUB_SENSOR(pm_0_3um)
41 SUB_SENSOR(pm_0_5um)
42 SUB_SENSOR(pm_1_0um)
43 SUB_SENSOR(pm_2_5um)
44 SUB_SENSOR(pm_5_0um)
45 SUB_SENSOR(pm_10_0um)
46
47 SUB_SENSOR(tvoc)
48 SUB_SENSOR(eco2)
49 SUB_SENSOR(aqi)
50
51 SUB_SENSOR(temperature)
52 SUB_SENSOR(humidity)
53 SUB_SENSOR(raw_temperature)
54 SUB_SENSOR(raw_humidity)
55
56 SUB_SENSOR(rs0)
57 SUB_SENSOR(rs2)
58 SUB_SENSOR(rs3)
59
60 SUB_SENSOR(error_code)
61
62 public:
63 void setup() override;
64 void dump_config() override;
65 void loop() override;
66
67 void set_active_mode();
68 void set_passive_mode();
69 void request_measurement();
70 void set_idle_mode();
71 void set_measurement_mode();
72
73 void set_set_pin(GPIOPin *set_pin) { this->set_pin_ = set_pin; }
74 void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
75
76 protected:
77 void send_command_(APC1Command cmd, uint16_t data);
78 void initialize_device_();
80 void arm_watchdog_();
82 bool parse_frame_(const uint8_t *frame, uint8_t total_len);
83 void parse_measurement_frame_(const uint8_t *frame);
84 void parse_device_info_frame_(const uint8_t *frame);
85
86 GPIOPin *set_pin_{nullptr};
91 // 42 4D, payload length (2, BE), payload ending in its checksum (2)
93 using Frame =
96 uint8_t last_error_code_{0xFF};
97 bool active_mode_{true};
98 bool idle_{false};
99 bool gas_warming_up_{false};
100};
101
102} // namespace esphome::apc1
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
Collects binary frames that give their own length, of up to N bytes, in one or more FrameFormats; the...
void parse_device_info_frame_(const uint8_t *frame)
Definition apc1.cpp:200
void parse_measurement_frame_(const uint8_t *frame)
Definition apc1.cpp:124
uint32_t last_transmission_
Definition apc1.h:88
void send_command_(APC1Command cmd, uint16_t data)
Definition apc1.cpp:242
uint32_t last_init_attempt_
Definition apc1.h:90
void arm_watchdog_()
Restarts the no-data watchdog so a mode change gets the same grace period as a cold boot.
Definition apc1.cpp:209
PayloadLengthU16BE< LengthField{.offset=2}> PayloadLength
Definition apc1.h:92
LengthFrameReader< APC1_FRAME_SIZE_MEASUREMENT, Frame > reader_
Definition apc1.h:95
bool parse_frame_(const uint8_t *frame, uint8_t total_len)
Returns false for a frame that fails the checksum.
Definition apc1.cpp:99
static void uint32_t
One frame type for LengthFrameReader: a FrameHeader, a length rule from above, then any of the option...
FrameFormat option: the shortest frame, when that is more than its header and length field.
offset: where the length field starts, counted from the first header byte.
uint16_t temperature
Definition sun_gtil2.cpp:12