#include <SoftwareSerial.h> #include <UTFT.h> SoftwareSerial PmsSerial(2, 3); // RX, TX UTFT myGLCD(QD220A, A2,A1,A5,A4,A3); extern uint8_t SmallFont[]; extern uint8_t BigFont[]; extern uint8_t SevenSegNumFont[]; static unsigned int pm_at_10; static unsigned int pm_at_25; static unsigned int pm_at_100; 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 int ucRxSum = 0; for(int i=0; i<30; i++) { ucRxSum += ucRxBuffer[i]; } if(ucRxSum != ((ucRxBuffer[30]<<8) + ucRxBuffer[31])) return; //校驗碼加總檢查 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]; sencorT=(int)ucRxBuffer[24] * 256 + (int)ucRxBuffer[25]; sencorH=(int)ucRxBuffer[26] * 256 + (int)ucRxBuffer[27]; sencorT=sencorT/10; sencorH=sencorH/10; if (pm_at_25 > 999) { ucRxCnt = 0; return; } ucRxCnt = 0; return; } } void setup() { PmsSerial.begin(9600); Serial.begin(9600); Serial.print("PM_AQI1.0");Serial.print(","); Serial.print("PM_AQI2.5");Serial.print(","); Serial.print("PM_AQI10");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("(%)"); //TFT myGLCD.InitLCD(); myGLCD.clrScr(); myGLCD.setFont(BigFont); myGLCD.print("PM2.5 Sencor", 0, 0); myGLCD.setFont(BigFont); myGLCD.setColor( 255 , 255 , 0 ); myGLCD.print("NNJH", CENTER , 25); myGLCD.setFont(SmallFont); myGLCD.setColor( 255 , 0 , 0 ); myGLCD.print("(pm2.5)", 30 ,45); myGLCD.setFont(SmallFont); myGLCD.setColor( 255 , 0 , 0 ); myGLCD.print("ug/m3", 40 , 110); myGLCD.setFont(SmallFont); myGLCD.setColor( 255 , 0 , 0 ); myGLCD.print("(Temp)", 130 ,105); myGLCD.setFont(SmallFont); myGLCD.setColor( 255 , 0 , 0 ); myGLCD.print("(Humandity)", 120 ,135); pinMode(12,OUTPUT); digitalWrite(12,HIGH); } void loop() { while (PmsSerial.available()) { getG5(PmsSerial.read()); } Serial.print(pm_at_10);Serial.print(","); Serial.print(pm_at_25);Serial.print(","); Serial.print(pm_at_100);Serial.print(","); Serial.print(sencorT);Serial.print(","); Serial.println(sencorH); //TFT myGLCD.setFont(SevenSegNumFont); if (pm_at_25 < 16){ myGLCD.setColor(0, 255, 0); } else if( (pm_at_25) > (16) & (pm_at_25)<(36)) { myGLCD.setColor(VGA_YELLOW); } else if ((pm_at_25) > (35) & (pm_at_25)<(55) ){ myGLCD.setColor(208,123, 0); } else if ((pm_at_25) > (54) & (pm_at_25)<(151) ) { myGLCD.setColor(VGA_RED); } else if ((pm_at_25) > (150) ) { myGLCD.setColor(VGA_PURPLE); } if ((pm_at_25) < (10) ) { myGLCD.print("00"+String(pm_at_25) , 5, 58 ); } else if ((pm_at_25) > (9) & (pm_at_25) < (100)) { myGLCD.print("0"+String(pm_at_25) , 5, 58 ); } else if ((pm_at_25) > (99) ) { myGLCD.print(String(pm_at_25) , 5, 58 ); } myGLCD.setFont(BigFont); if (sencorT < 10) { myGLCD.setColor(VGA_BLUE); myGLCD.print("0"+String(sencorT) , 115 , 115); } else if (sencorT > 9) { myGLCD.setColor(VGA_WHITE); myGLCD.print(String(sencorT) , 115 , 115); } if (sencorH < 10) { myGLCD.setColor(VGA_BLUE); myGLCD.print("0"+String(sencorH) , 115 , 150); } else if (sencorH > 9 & sencorH < 65) { myGLCD.setColor(VGA_WHITE); myGLCD.print(String(sencorH) , 115 , 150); } else if (sencorH >= 65) { myGLCD.setColor(VGA_RED); myGLCD.print(String(sencorH) , 115 , 150); } delay(2000); }