Skip to content

Instantly share code, notes, and snippets.

@clive520
Last active April 22, 2021 10:57
Show Gist options
  • Save clive520/8ce89a0bf3bcd519d798ac071a16f2ad to your computer and use it in GitHub Desktop.
Save clive520/8ce89a0bf3bcd519d798ac071a16f2ad to your computer and use it in GitHub Desktop.
ESP8266 NodeMcu Lua & PM2.5_PMS5003_G5 & LCD
// 1:紫色==>5V (V V)因為V V才是5V的電 2:橘色==>GND 4:藍色RX==>TX 5:綠色TX==>RX
// 本程式除了PM2.5 G5外,在多接上一個2*16的LCD當作顯示
// LCD接腳 GND==>GND VCC==>5V SDA==>D2 SCL==>D1
// 利用以上程式可以得到PM1.0、PM2.5、PM10分別在CF=1為標準顆粒物、atmosphere為大氣環境下
// CF=1 根據美國TSI公司的儀器校準
// 大氣環境下 根據中國氣象局的數據校準
#include <LiquidCrystal_I2C.h> //加入LCD函數庫
#include <Wire.h>
#include <ESP8266HTTPClient.h>
LiquidCrystal_I2C lcd(0x27,16,2); //LCD的address為0x27,每排16個字元,共2排
long pmcf10=0;
long pmcf25=0;
long pmcf100=0;
long pmat10=0;
long pmat25=0;
long pmat100=0;
char buf[50];
void setup()
{
lcd.begin(); //啟動LCD
Serial.begin(9600);
}
void loop()
{
// put your main code here, to run repeatedly:
int count = 0;
unsigned char c;
unsigned char high;
while (Serial.available()) {
c = Serial.read();
if((count==0 && c!=0x42) || (count==1 && c!=0x4d)){
Serial.println("check failed");
break;
}
if(count > 15){
Serial.println("complete");
break;
}
else if(count == 4 || count == 6 || count == 8 || count == 10 || count == 12 || count == 14) high = c;
else if(count == 5){
pmcf10 = 256*high + c;
Serial.print("CF=1, PM1.0=");
Serial.print(pmcf10);
Serial.println(" ug/m3");
lcd.clear(); //先清除LCD上的文字
lcd.setCursor(0,0);
lcd.print(String("CF=1, PM1=")+pmcf10 + String(","));
}
else if(count == 7){
pmcf25 = 256*high + c;
Serial.print("CF=1, PM2.5=");
Serial.print(pmcf25);
Serial.println(" ug/m3");
lcd.setCursor(0,1);
lcd.print(String("PM2.5=")+pmcf25 + String(","));
}
else if(count == 9){
pmcf100 = 256*high + c;
Serial.print("CF=1, PM10=");
Serial.print(pmcf100);
Serial.println(" ug/m3");
lcd.setCursor(9,1);
lcd.print(String("PM10=")+pmcf100 + String(","));
delay(5000);
}
else if(count == 11){
pmat10 = 256*high + c;
Serial.print("atmosphere, PM1.0=");
Serial.print(pmat10);
Serial.println(" ug/m3");
lcd.clear(); //先清除LCD上的文字
lcd.setCursor(0,0);
lcd.print(String("AT, PM1=")+pmat10 + String(","));
}
else if(count == 13){
pmat25 = 256*high + c;
Serial.print("atmosphere, PM2.5=");
Serial.print(pmat25);
Serial.println(" ug/m3");
lcd.setCursor(0,1);
lcd.print(String("PM2.5=")+pmat25 + String(","));
}
else if(count == 15){
pmat100 = 256*high + c;
Serial.print("atmosphere, PM10=");
Serial.print(pmat100);
Serial.println(" ug/m3");
lcd.setCursor(9,1);
lcd.print(String("PM10=")+pmat100 + String(","));
delay(5000);
}
count++;
}
while(Serial.available()) Serial.read();
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment