#include SoftwareSerial PmsSerial(2, 3); // RX, TX static unsigned int pm_cf_10; //定義全域變數 static unsigned int pm_cf_25; static unsigned int pm_cf_100; static unsigned int pm_at_10; static unsigned int pm_at_25; static unsigned int pm_at_100; static unsigned int particulate03; static unsigned int particulate05; static unsigned int particulate10; static unsigned int particulate25; static float sencorT; static float sencorH; void getG5(unsigned char ucData)//獲取G5的值 { static unsigned int ucRxBuffer[250]; static unsigned int ucRxCnt = 0; ucRxBuffer[ucRxCnt++] = ucData; if (ucRxBuffer[0] != 0x42 && ucRxBuffer[1] != 0x4D)//資料頭判斷 { ucRxCnt = 0; return; } if (ucRxCnt > 32)//資料位元數判斷//G5T為32 { pm_cf_10=(int)ucRxBuffer[4] * 256 + (int)ucRxBuffer[5]; //大氣環境下PM2.5濃度計算 pm_cf_25=(int)ucRxBuffer[6] * 256 + (int)ucRxBuffer[7]; pm_cf_100=(int)ucRxBuffer[8] * 256 + (int)ucRxBuffer[9]; pm_at_10=(int)ucRxBuffer[10] * 256 + (int)ucRxBuffer[11]; pm_at_25=(int)ucRxBuffer[12] * 256 + (int)ucRxBuffer[13]; pm_at_100=(int)ucRxBuffer[14] * 256 + (int)ucRxBuffer[15]; particulate03=(int)ucRxBuffer[16] * 256 + (int)ucRxBuffer[17]; particulate05=(int)ucRxBuffer[18] * 256 + (int)ucRxBuffer[19]; particulate10=(int)ucRxBuffer[20] * 256 + (int)ucRxBuffer[21]; particulate25=(int)ucRxBuffer[22] * 256 + (int)ucRxBuffer[23]; sencorT=((int)ucRxBuffer[24] * 256 + (int)ucRxBuffer[25])/10; sencorH=((int)ucRxBuffer[26] * 256 + (int)ucRxBuffer[27])/10; if (pm_cf_25 > 999)//如果PM2.5數值>1000,返回重新計算 { ucRxCnt = 0; return; } ucRxCnt = 0; return; } } void setup() { PmsSerial.begin(9600); PmsSerial.setTimeout(1500); Serial.begin(9600); Serial.print("PM_CF1.0");Serial.print(","); Serial.print("PM_CF2.5");Serial.print(","); Serial.print("PM_CF10");Serial.print(","); Serial.print("PM_AQI1.0");Serial.print(","); Serial.print("PM_AQI2.5");Serial.print(","); Serial.print("PM_AQI10");Serial.print(","); Serial.print("PM_count03");Serial.print(","); Serial.print("PM_count05");Serial.print(","); Serial.print("PM_count10");Serial.print(","); Serial.print("PM_count25");Serial.print(","); Serial.print("Temperature");Serial.print(","); Serial.println("Humandity"); Serial.print("(ug/m3)");Serial.print(","); Serial.print("(ug/m3)");Serial.print(","); Serial.print("(ug/m3)");Serial.print(","); Serial.print("(ug/m3)");Serial.print(","); Serial.print("(ug/m3)");Serial.print(","); Serial.print("(ug/m3)");Serial.print(","); Serial.print("(pcs/0.01cf)");Serial.print(","); Serial.print("(pcs/0.01cf)");Serial.print(","); Serial.print("(pcs/0.01cf)");Serial.print(","); Serial.print("(pcs/0.01cf)");Serial.print(","); Serial.print("(C)");Serial.print(","); Serial.println("(%)"); } void loop() { while (PmsSerial.available()) { getG5(PmsSerial.read()); } Serial.print(pm_cf_10);Serial.print(","); Serial.print(pm_cf_25);Serial.print(","); Serial.print(pm_cf_100);Serial.print(","); Serial.print(pm_at_10);Serial.print(","); Serial.print(pm_at_25);Serial.print(","); Serial.print(pm_at_100);Serial.print(","); Serial.print(particulate03);Serial.print(","); Serial.print(particulate05);Serial.print(","); Serial.print(particulate10);Serial.print(","); Serial.print(particulate25);Serial.print(","); Serial.print(sencorT);Serial.print(","); Serial.println(sencorH); delay(2000); } ```
進階搜尋