PMS7003 Arduino Library by Soldered 1.0.0
This is a library for the PMS7003 Board by Soldered
Loading...
Searching...
No Matches
PMserial.h
Go to the documentation of this file.
1/* SerialPM
2 Arduino library for PM sensors with serial interface
3 PMS1003 aka G1
4 PMS3003 aka G2
5 PMS5003 aka G5
6 PMS7003 aka G7
7 PMSA003 aka G10
8*/
9#ifndef _SERIALPM_H
10#define _SERIALPM_H
11
12#include <Arduino.h>
13
14#define HAS_HW_SERIAL
15#ifdef HAS_HW_SERIAL
16#include <HardwareSerial.h>
17#endif
18
19#if defined(__AVR__) || defined(ESP8266)
20#define HAS_SW_SERIAL
21#include <SoftwareSerial.h>
22#endif
23
24#if defined(HAS_HWSERIAL1) || defined(BOARD_HAVE_USART1)
25#define HAS_HW_SERIAL1
26#endif
27
28// leonardo & maple_mini: Serial1 is HWserial
29#if defined(__AVR_ATmega32U4__) || defined(SERIAL_USB)
30#define HAS_USB_SERIAL
31#endif
32
33enum PMS
34{
35 PLANTOWER_AUTO, // self discovery
36 PLANTOWER_24B, // 24 byte long message, no count info (LBC)
37 PLANTOWER_32B, // 32 byte long message, w/count info (LBC)
38 PLANTOWER_32B_S, // 32 byte long message, w/count info and HCHO (LBC)
39 PLANTOWER_32B_T, // 32 byte long message, w/partial count info, temp and rhum (LBC)
40 PLANTOWER_40B, // 40 byte long message, w/count info, temp, rhum and HCHO (LBC)
41 PMSx003 = PLANTOWER_AUTO, // self discovery
48 // PMS7003 = PLANTOWER_32B, // G7 edited
49 PMSA003 = PLANTOWER_32B // G10
50};
51
53{
54 public:
55 union {
56 uint16_t data[9]; // all PM/NC data
57 struct
58 {
59 uint16_t pm[3]; // particulate matter [ug/m3]
60 uint16_t nc[6]; // number concentration [#/100cc]
61 };
62 struct
63 {
64 // pmX [ug/m3]: PM1.0, PM2.5 & PM10
65 uint16_t pm01, pm25, pm10;
66 // nXpY [#/100cc]: number concentrations under X.Y um
67 uint16_t n0p3, n0p5, n1p0, n2p5, n5p0, n10p0;
68 };
69 };
70 union {
71 float extra[3]; // T/RH/HCHO
72 struct
73 {
74 // temperature [°C], relative humidity [%], formaldehyde concentration [mg/m3]
75 float temp, rhum, hcho;
76 };
77 };
78
79 // manual mode - you need configure SerialPort manual and link to this object (function "setSerialPort")
80 SerialPM(PMS sensor) : pms(sensor)
81 {
82 uart = nullptr;
84 }
85
86#ifdef HAS_HW_SERIAL
87 SerialPM(PMS sensor, HardwareSerial &serial) : pms(sensor)
88 {
89 uart = &serial;
91 }
92#endif
93
94#ifdef HAS_SW_SERIAL
95 SerialPM(PMS sensor, uint8_t rx, uint8_t tx) : pms(sensor)
96 {
97 static SoftwareSerial serial(rx, tx);
98 uart = &serial;
100 }
101
102 SerialPM(PMS sensor, Stream &serial) : pms(sensor)
103 {
104 uart = &serial;
105 // TODO: WIP!!!
107 }
108#elif defined(ESP32)
109 SerialPM(PMS sensor, uint8_t rx, uint8_t tx) : pms(sensor), rx(rx), tx(tx)
110 {
111 uart = &Serial1;
113 }
114#endif
115
116 void init();
117
118#define PMS_ERROR_TIMEOUT "Sensor read timeout"
119#define PMS_ERROR_PMS_TYPE "Wrong PMSx003 sensor type"
120#define PMS_ERROR_MSG_UNKNOWN "Unknown message protocol"
121#define PMS_ERROR_MSG_HEADER "Incomplete message header"
122#define PMS_ERROR_MSG_BODY "Incomplete message body"
123#define PMS_ERROR_MSG_START "Wrong message start"
124#define PMS_ERROR_MSG_LENGTH "Message too long"
125#define PMS_ERROR_MSG_CKSUM "Wrong message checksum"
126
139
141 STATUS read(bool tsi_mode = false, bool truncated_num = false);
142 operator bool()
143 {
144 return status == OK;
145 }
146 void sleep();
147 void wake();
149 {
150 return status == OK;
151 }
153 {
154 return (status == OK) && (pms != PMS3003);
155 }
157 {
158 return (status == OK) && ((pms == PMS5003T) || (pms == PMS5003ST));
159 }
160 inline bool has_formaldehyde()
161 {
162 return (status == OK) && ((pms == PMS5003S) || (pms == PMS5003ST));
163 }
164 inline Stream *getSerialPort()
165 {
166 return uart;
167 }
168 inline void setSerialPort(Stream *serial)
169 {
170 uart = serial;
172 }
173
174 // adding offsets works well in normal range
175 // might introduce under- or overflow at the ends of the sensor range
176 inline void set_rhum_offset(float offset)
177 {
178 rhum_offset = offset;
179 };
180 inline void set_temp_offset(float offset)
181 {
182 temp_offset = offset;
183 };
184 inline float get_rhum_offset()
185 {
186 return rhum_offset;
187 };
188 inline float get_temp_offset()
189 {
190 return temp_offset;
191 };
192#ifdef PMS_DEBUG
193#ifdef HAS_HW_SERIAL
194 inline void print_buffer(Stream &term, const char *fmt)
195 {
196 char tmp[8];
197 for (uint8_t n = 0; n < BUFFER_LEN; n += 2)
198 {
199 sprintf(tmp, fmt, buff2word(n));
200 term.print(tmp);
201 }
202 }
203#endif
204 // debug timing
205 inline uint16_t waited_ms()
206 {
207 return wait_ms;
208 }
209 // debug readBytes
210 inline uint16_t bytes_read()
211 {
212 return nbytes;
213 }
214#endif
215
216 protected:
217 Stream *uart; // hardware/software serial
218 PMS pms; // sensor type/message protocol
219 enum
220 {
224 } hwSerial; // Is uart hardware serial? (or software serial)
225#ifdef ESP32
226 uint8_t rx, tx; // Serial1 pins on ESP32
227#endif
228
229 // Correct Temperature & Humidity
230 float temp_offset = 0.0;
231 float rhum_offset = 0.0;
232
233 // utility functions
235 bool checkBuffer(size_t bufferLen);
236 void decodeBuffer(bool tsi_mode, bool truncated_num);
237
238 // message timing
239 static const uint16_t max_wait_ms = 1000;
240 uint16_t wait_ms; // time spent waiting for new sample
241
242 // message buffer
243 static const uint8_t BUFFER_LEN = 40;
245 inline uint16_t buff2word(uint8_t n)
246 {
247 return (buffer[n] << 8) | buffer[n + 1];
248 }
249};
250
251#endif //_SERIALPM_H
PMS
Definition PMserial.h:34
@ PLANTOWER_32B
Definition PMserial.h:37
@ PLANTOWER_40B
Definition PMserial.h:40
@ PMSx003
Definition PMserial.h:41
@ PMS5003T
Definition PMserial.h:46
@ PMSA003
Definition PMserial.h:49
@ PMS5003ST
Definition PMserial.h:47
@ PLANTOWER_32B_S
Definition PMserial.h:38
@ PLANTOWER_32B_T
Definition PMserial.h:39
@ PLANTOWER_AUTO
Definition PMserial.h:35
@ PLANTOWER_24B
Definition PMserial.h:36
@ PMS5003S
Definition PMserial.h:45
@ PMS3003
Definition PMserial.h:43
@ PMS1003
Definition PMserial.h:42
@ PMS5003
Definition PMserial.h:44
Definition PMserial.h:53
SerialPM(PMS sensor, Stream &serial)
Definition PMserial.h:102
float rhum
Definition PMserial.h:75
uint8_t buffer[BUFFER_LEN]
Definition PMserial.h:244
STATUS status
Definition PMserial.h:140
uint16_t n0p5
Definition PMserial.h:67
float temp_offset
Definition PMserial.h:230
Stream * uart
Definition PMserial.h:217
void set_rhum_offset(float offset)
Definition PMserial.h:176
bool has_number_concentration()
Definition PMserial.h:152
uint16_t pm25
Definition PMserial.h:65
float extra[3]
Definition PMserial.h:71
bool checkBuffer(size_t bufferLen)
Definition PMserial.cpp:194
void init()
Definition PMserial.cpp:64
STATUS
Definition PMserial.h:128
@ ERROR_MSG_HEADER
Definition PMserial.h:133
@ ERROR_TIMEOUT
Definition PMserial.h:130
@ ERROR_MSG_LENGTH
Definition PMserial.h:136
@ OK
Definition PMserial.h:129
@ ERROR_PMS_TYPE
Definition PMserial.h:131
@ ERROR_MSG_CKSUM
Definition PMserial.h:137
@ ERROR_MSG_BODY
Definition PMserial.h:134
@ ERROR_MSG_START
Definition PMserial.h:135
@ ERROR_MSG_UNKNOWN
Definition PMserial.h:132
uint16_t buff2word(uint8_t n)
Definition PMserial.h:245
void decodeBuffer(bool tsi_mode, bool truncated_num)
Definition PMserial.cpp:205
bool has_formaldehyde()
Definition PMserial.h:160
PMS pms
Definition PMserial.h:218
uint16_t nc[6]
Definition PMserial.h:60
void sleep()
Definition PMserial.cpp:92
SerialPM(PMS sensor, uint8_t rx, uint8_t tx)
Definition PMserial.h:95
uint16_t pm01
Definition PMserial.h:65
uint8_t nbytes
Definition PMserial.h:244
enum SerialPM::@4 hwSerial
STATUS trigRead()
Definition PMserial.cpp:105
uint16_t wait_ms
Definition PMserial.h:240
uint16_t bytes_read()
Definition PMserial.h:210
STATUS read(bool tsi_mode=false, bool truncated_num=false)
Definition PMserial.cpp:250
static const uint8_t BUFFER_LEN
Definition PMserial.h:243
bool has_temperature_humidity()
Definition PMserial.h:156
uint8_t tx
Definition PMserial.h:226
bool has_particulate_matter()
Definition PMserial.h:148
uint16_t n2p5
Definition PMserial.h:67
static const uint16_t max_wait_ms
Definition PMserial.h:239
uint8_t rx
Definition PMserial.h:226
uint16_t data[9]
Definition PMserial.h:56
uint16_t n0p3
Definition PMserial.h:67
Stream * getSerialPort()
Definition PMserial.h:164
void print_buffer(Stream &term, const char *fmt)
Definition PMserial.h:194
void set_temp_offset(float offset)
Definition PMserial.h:180
uint16_t pm10
Definition PMserial.h:65
float rhum_offset
Definition PMserial.h:231
float hcho
Definition PMserial.h:75
@ serModeHardware
Definition PMserial.h:221
@ serModeSoftware
Definition PMserial.h:222
@ serModeManual
Definition PMserial.h:223
uint16_t waited_ms()
Definition PMserial.h:205
void wake()
Definition PMserial.cpp:98
float get_rhum_offset()
Definition PMserial.h:184
uint16_t pm[3]
Definition PMserial.h:59
float temp
Definition PMserial.h:75
SerialPM(PMS sensor)
Definition PMserial.h:80
uint16_t n10p0
Definition PMserial.h:67
uint16_t n5p0
Definition PMserial.h:67
void setSerialPort(Stream *serial)
Definition PMserial.h:168
float get_temp_offset()
Definition PMserial.h:188
SerialPM(PMS sensor, HardwareSerial &serial)
Definition PMserial.h:87
uint16_t n1p0
Definition PMserial.h:67