#include <SoftwareSerial.h>
SoftwareSerial pmsSerial(2, 3);
// PM2.5(pin) <–> Arduino(pin)
// 2: TXD <–> Rx
// 3: RXD <–> Tx
void setup() {
Serial.begin(115200); // for 序列埠監控視窗
pmsSerial.begin(9600); // PM2.5 sensor baud rate = 9600
}
struct pms5003data {
uint16_t framelen; //框架長度
uint16_t pm10_standard, pm25_standard, pm100_standard; //標準 PM1.0, PM2.5, PM10 濃度
uint16_t pm10_env, pm25_env, pm100_env; //大氣環境下 PM1.0, PM2.5, PM10 濃度
uint16_t particles_03um, particles_05um, particles_10um, particles_25um, particles_50um, particles_100um;
uint16_t unused; //版本,錯誤代碼
uint16_t checksum; //校驗碼
};
struct pms5003data data; // 宣告結構變數 data
void loop() {
if (readPMSdata(&pmsSerial)) {
// reading data was successful!
Serial.println();
Serial.println(“——————————————“);
Serial.println(“Concentration Units (standard)”);
Serial.print(“PM 1.0: “); Serial.print(data.pm10_standard);
Serial.print(“tPM 2.5: “); Serial.print(data.pm25_standard);
Serial.print(“tPM 10: “); Serial.println(data.pm100_standard);
Serial.println(“——————————————“);
Serial.println(“Concentration Units (environmental)”);
Serial.print(“PM 1.0: “); Serial.print(data.pm10_env);
Serial.print(“tPM 2.5: “); Serial.print(data.pm25_env);
Serial.print(“tPM 10: “); Serial.println(data.pm100_env);
Serial.println(“——————————————“);
// Serial.print(“Particles > 0.3um / 0.1L air:”); Serial.println(data.particles_03um);
// Serial.print(“Particles > 0.5um / 0.1L air:”); Serial.println(data.particles_05um);
// Serial.print(“Particles > 1.0um / 0.1L air:”); Serial.println(data.particles_10um);
// Serial.print(“Particles > 2.5um / 0.1L air:”); Serial.println(data.particles_25um);
// Serial.print(“Particles > 5.0um / 0.1L air:”); Serial.println(data.particles_50um);
// Serial.print(“Particles > 10.0 um / 0.1L air:”); Serial.println(data.particles_100um);
// Serial.println(“——————————————“);
}
}
boolean readPMSdata(Stream *s) { // 從 pmsSerial Pin2 讀取 UART 資料
if (! s->available()) { // 取得 Serial Port 可讀取的資料位元組數目 (number of bytes), 如果 Serial port 有資料進來,available() 會回傳大於 0 的數值。
return false; // 若有 bytes 進來, 則往下判斷 起始位元值; 若無bytes 進來 return flase
}
// Read a byte at a time until we get to the special ‘0x42’ start-byte
if (s->peek() != 0x42) {
s->read();
return false;
}
// Now read all 32 bytes
if (s->available() < 32) {
return false;
}
uint8_t buffer[32];
uint16_t sum = 0;
s->readBytes(buffer, 32);
// get checksum ready
for (uint8_t i=0; i<30; i++) {
sum += buffer[i];
}
/* debugging
for (uint8_t i=2; i<32; i++) {
Serial.print(“0x”); Serial.print(buffer[i], HEX); Serial.print(“, “);
}
Serial.println();
*/
// The data comes in endian’d, this solves it so it works on all platforms
uint16_t buffer_u16[15];
for (uint8_t i=0; i<15; i++) {
buffer_u16[i] = buffer[2 + i*2 + 1];
buffer_u16[i] += (buffer[2 + i*2] << 8);
}
// put it into a nice struct 🙂
memcpy((void *)&data, (void *)buffer_u16, 30);
if (sum != data.checksum) {
Serial.println(“Checksum failure”);
return false;
}
// success!
return true;
}
- 環境空氣品質監控
- 無塵室, 實驗室, 手術室, 隔離病房
- 材料,半導體, 食品蔬果, 藥品化妝品 .等製造工廠
- 換氣系統
- 空調系統
- 空氣濾淨系統.
- 學生專題製作