ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
pmsx003.h
Go to the documentation of this file.
1#pragma once
2
8
9namespace esphome::pmsx003 {
10
11static constexpr uint8_t START_CHARACTER_1 = 0x42;
12static constexpr uint8_t START_CHARACTER_2 = 0x4D;
13// PMS5003ST: 36 byte payload after the 2 start and 2 length bytes
14static constexpr uint8_t MAX_FRAME_SIZE = 40;
15// 42 4D, payload length (2, BE), payload ending in its checksum (2)
18void warn_too_long(size_t len);
21
22enum class Type : uint8_t {
23 PMS1003 = 0,
24 PMS3003,
25 PMSX003, // PMS5003, PMS6003, PMS7003, PMSA003 (NOT PMSA003I - see `pmsa003i` component)
30 LUFTMY, // Luftmy LD10, LD11, LD13, LD15, LD16: standard PM + particle counts, no atmospheric PM values
31};
32
33enum class Command : uint8_t {
34 MEASUREMENT_MODE = 0xE1, // Data Options: `CMD_MEASUREMENT_MODE_PASSIVE`, `CMD_MEASUREMENT_MODE_ACTIVE`
35 MANUAL_MEASUREMENT = 0xE2,
36 SLEEP_MODE = 0xE4, // Data Options: `CMD_SLEEP_MODE_SLEEP`, `CMD_SLEEP_MODE_WAKEUP`
37};
38
39enum class State : uint8_t {
40 IDLE = 0,
42 WAITING,
43};
44
45class PMSX003Component final : public uart::UARTDevice, public Component {
46 public:
47 PMSX003Component() = default;
48 void setup() override;
49 void dump_config() override;
50 void loop() override;
51
52 void set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; }
53
54 void set_type(Type type) { this->type_ = type; }
55
56 // "Standard Particle"
57 SUB_SENSOR(pm_1_0_std)
58 SUB_SENSOR(pm_2_5_std)
59 SUB_SENSOR(pm_10_0_std)
60
61 // "Under Atmospheric Pressure"
62 SUB_SENSOR(pm_1_0)
63 SUB_SENSOR(pm_2_5)
64 SUB_SENSOR(pm_10_0)
65
66 // Particle counts by size
67 SUB_SENSOR(pm_particles_03um)
68 SUB_SENSOR(pm_particles_05um)
69 SUB_SENSOR(pm_particles_10um)
70 SUB_SENSOR(pm_particles_25um)
71 SUB_SENSOR(pm_particles_50um)
72 SUB_SENSOR(pm_particles_100um)
73
74 // Formaldehyde
75 SUB_SENSOR(formaldehyde)
76
77 // Temperature and Humidity
78 SUB_SENSOR(temperature)
79 SUB_SENSOR(humidity)
80
81 protected:
82 bool check_frame_(const uint8_t *frame, size_t total_size);
83 void parse_data_(const uint8_t *frame);
84 bool check_payload_length_(uint16_t payload_length);
85 void send_command_(Command cmd, uint16_t data);
86
89 bool initialised_{false};
95};
96
97} // namespace esphome::pmsx003
Collects binary frames that give their own length, of up to N bytes, in one or more FrameFormats; the...
void set_update_interval(uint32_t update_interval)
Definition pmsx003.h:52
LengthFrameReader< MAX_FRAME_SIZE, PmsFrame > reader_
Definition pmsx003.h:90
void send_command_(Command cmd, uint16_t data)
Definition pmsx003.cpp:152
bool check_payload_length_(uint16_t payload_length)
Definition pmsx003.cpp:130
SUB_SENSOR(pm_1_0_std) SUB_SENSOR(pm_2_5_std) SUB_SENSOR(pm_10_0_std) SUB_SENSOR(pm_1_0) SUB_SENSOR(pm_2_5) SUB_SENSOR(pm_10_0) SUB_SENSOR(pm_particles_03um) SUB_SENSOR(pm_particles_05um) SUB_SENSOR(pm_particles_10um) SUB_SENSOR(pm_particles_25um) SUB_SENSOR(pm_particles_50um) SUB_SENSOR(pm_particles_100um) SUB_SENSOR(formaldehyde) SUB_SENSOR(temperature) SUB_SENSOR(humidity) protected void parse_data_(const uint8_t *frame)
Definition pmsx003.cpp:173
uint8_t type
void warn_too_long(size_t len)
The wrong-type warning, for a frame longer than any supported type sends.
Definition pmsx003.cpp:108
PayloadLengthU16BE< LengthField{.offset=2}> PayloadLength
Definition pmsx003.h:16
const void size_t len
Definition hal.h:68
static void uint32_t
One frame type for LengthFrameReader: a FrameHeader, a length rule from above, then any of the option...
offset: where the length field starts, counted from the first header byte.
FrameFormat option: a frame longer than MAX calls WARN(length) and is skipped; a replay can warn agai...
uint16_t temperature
Definition sun_gtil2.cpp:12