Arduino-汉王PM2.5检测模组B1
汉王PM2.5检测模组B1,接入Arduino,使用I2C1602显示屏显示
1 #include <Arduino.h> 2 #include <Wire.h> 3 #include <LiquidCrystal_I2C.h> 4 float pm25 = 0; 5 #define DATALEN 7 6 LiquidCrystal_I2C lcd(0x3F,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display 7 void setup() { 8 // put your setup code here, to run once: 9 Serial.begin(115200); 10 lcd.init(); // initialize the lcd 11 12 // Print a message to the LCD. 13 lcd.backlight(); 14 lcd.print("PM2.5"); 15 16 } 17 void ProcessSerialData()//读取PM2.5的数据。 18 { 19 uint8_t mData = 0; 20 uint8_t i = 0; 21 uint8_t mPkt[DATALEN] = {0}; 22 int mCheck = 0; 23 while (Serial.available() > 0) 24 { 25 mData = Serial.read(); 26 delay(2);//wait until packet is received 27 if(mData == 0xAA)//head1 ok 28 { 29 mPkt[0] = mData; 30 mData = Serial.read(); 31 { 32 for(int i=1;i < DATALEN;i++)//data recv and crc calc 33 { 34 mPkt[i] = Serial.read(); 35 Serial.print(mPkt[i],HEX); 36 Serial.print("---"); 37 delay(2); 38 } 39 delay(1); 40 Serial.println(); 41 // Serial.println(mPkt[3]*256+mPkt[2]); 42 // Serial.print(mPkt[2]); 43 // Serial.println(" "); 44 // Serial.print("---"); 45 // Serial.print(mPkt[5]); 46 if(mPkt[DATALEN-1] == 0xFF) 47 { 48 pm25 = (mPkt[1]<<8)|mPkt[4]; 49 Serial.println(float(pm25/100)); 50 lcd.setCursor(0,1); 51 lcd.print(float(pm25/100)); 52 Serial.flush(); 53 return; 54 } 55 } 56 } 57 } 58 } 59 60 void loop() { 61 ProcessSerialData(); 62 }
作者:Dozeoo
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。