ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
apc1.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/hal.h"
7
8#include <array>
9
10namespace esphome::apc1 {
11
12static constexpr uint8_t APC1_HEADER_1 = 0x42;
13static constexpr uint8_t APC1_HEADER_2 = 0x4D;
14static constexpr uint8_t APC1_FRAME_HEADER_SIZE = 4; // 2 start bytes (0x42, 0x4D) + 2 frame length bytes
15static constexpr uint8_t APC1_FRAME_SIZE_MEASUREMENT = 64;
16static constexpr uint8_t APC1_FRAME_SIZE_DEVICE_INFO = 23;
17static constexpr uint8_t APC1_FRAME_SIZE_COMMAND_RESPONSE = 8;
18static constexpr uint8_t APC1_COMMAND_FRAME_SIZE = 7;
19
26
27static constexpr uint16_t APC1_MEASUREMENT_MODE_PASSIVE = 0x0000;
28static constexpr uint16_t APC1_MEASUREMENT_MODE_ACTIVE = 0x0001;
29
30static constexpr uint16_t APC1_OPERATING_MODE_IDLE = 0x0000;
31static constexpr uint16_t APC1_OPERATING_MODE_STANDARD = 0x0001;
32
33class APC1Component final : public uart::UARTDevice, public Component {
34 SUB_SENSOR(pm_1_0)
35 SUB_SENSOR(pm_2_5)
36 SUB_SENSOR(pm_10_0)
37 SUB_SENSOR(pm_1_0_std)
38 SUB_SENSOR(pm_2_5_std)
39 SUB_SENSOR(pm_10_0_std)
40
41 SUB_SENSOR(pm_0_3um)
42 SUB_SENSOR(pm_0_5um)
43 SUB_SENSOR(pm_1_0um)
44 SUB_SENSOR(pm_2_5um)
45 SUB_SENSOR(pm_5_0um)
46 SUB_SENSOR(pm_10_0um)
47
48 SUB_SENSOR(tvoc)
49 SUB_SENSOR(eco2)
50 SUB_SENSOR(aqi)
51
52 SUB_SENSOR(temperature)
53 SUB_SENSOR(humidity)
54 SUB_SENSOR(raw_temperature)
55 SUB_SENSOR(raw_humidity)
56
57 SUB_SENSOR(rs0)
58 SUB_SENSOR(rs2)
59 SUB_SENSOR(rs3)
60
61 SUB_SENSOR(error_code)
62
63 public:
64 void setup() override;
65 void dump_config() override;
66 void loop() override;
67
68 void set_active_mode();
69 void set_passive_mode();
70 void request_measurement();
71 void set_idle_mode();
72 void set_measurement_mode();
73
74 void set_set_pin(GPIOPin *set_pin) { this->set_pin_ = set_pin; }
75 void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
76
77 protected:
78 void send_command_(APC1Command cmd, uint16_t data);
79 void initialize_device_();
81 void arm_watchdog_();
82 void parse_frame_(uint16_t total_len);
85
86 GPIOPin *set_pin_{nullptr};
91 std::array<uint8_t, APC1_FRAME_SIZE_MEASUREMENT> rx_buffer_{};
92 uint8_t rx_index_{0};
93 uint8_t last_error_code_{0xFF};
94 bool active_mode_{true};
95 bool idle_{false};
96 bool gas_warming_up_{false};
97};
98
99} // namespace esphome::apc1
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
std::array< uint8_t, APC1_FRAME_SIZE_MEASUREMENT > rx_buffer_
Definition apc1.h:91
uint32_t last_transmission_
Definition apc1.h:88
void parse_frame_(uint16_t total_len)
Definition apc1.cpp:158
void send_command_(APC1Command cmd, uint16_t data)
Definition apc1.cpp:336
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:303
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12