Created
January 11, 2019 01:46
-
-
Save clive520/4d3bf337550d95468f34e963a3c6416b to your computer and use it in GitHub Desktop.
PMS5003T空氣品質感測器_顯示資料於LCD_NodeMCU_ESP8266
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Sketch was generated by motoblockly | |
Website: http://www.motoblockly.com | |
Author: www.motoduino.com | |
Date: Fri Jan 11 2019 08:34:04 GMT+0800 | |
*/ | |
//LCD接線方式{GND ==> GND VCC==>5V SDA==>D2(04) SCL==>D1(05)} | |
//PMS5003T空氣品質感測器接線方式1:紫色==>5V 2:橘色==>GND 4:藍色RX==>D7(13) 5:綠色TX==>D8(15) | |
#include <Wire.h> | |
#include <esp_LiquidCrystal_I2C.h> | |
#include <SoftwareSerial.h> | |
esp_LiquidCrystal_I2C mylcd(0x27, 16, 2); | |
long PM10; | |
long PM2_5; | |
long PM1_0; | |
long temperature; | |
long humidity; | |
SoftwareSerial pms5003t_Serial(13,15); | |
#define PMS5003T_PM10 1 | |
#define PMS5003T_PM25 2 | |
#define PMS5003T_PM100 3 | |
#define PMS5003T_TEMPER 4 | |
#define PMS5003T_HUMI 5 | |
long pms_pmcf10=0; | |
long pms_pmcf25=0; | |
long pms_pmcf100=0; | |
long pms_pmat10=0; | |
long pms_pmat25=0; | |
long pms_pmat100=0; | |
long pms_Temperature=0; | |
long pms_Humidity=0; | |
long pms5003t_read_(int datatype) { | |
int count = 0; | |
unsigned char c; | |
unsigned char high; | |
pms5003t_Serial.listen(); | |
while (pms5003t_Serial.available()) { | |
c = pms5003t_Serial.read(); | |
if((count==0 && c!=0x42) || (count==1 && c!=0x4d)){ | |
Serial.println("pms5003t check failed"); | |
break; | |
} | |
if(count > 27){ | |
Serial.println("pms5003t completed"); | |
break; | |
} | |
else if(count == 4 || count == 6 || count == 8 || count == 10 || count == 12 || count == 14 || count == 24 || count == 26){ | |
high = c; } | |
else if(count == 5){ | |
pms_pmcf10 = 256*high + c; | |
} | |
else if(count == 7){ | |
pms_pmcf25 = 256*high + c; | |
} | |
else if(count == 9){ | |
pms_pmcf100 = 256*high + c; | |
} | |
else if(count == 11){ | |
pms_pmat10 = 256*high + c; | |
} | |
else if(count == 13){ | |
pms_pmat25 = 256*high + c; | |
} | |
else if(count == 15){ | |
pms_pmat100 = 256*high + c; | |
} | |
else if(count == 25){ | |
pms_Temperature = (256*high + c)/10; | |
} | |
else if(count == 27){ | |
pms_Humidity = (256*high + c)/10; | |
} | |
count++; | |
} | |
while(pms5003t_Serial.available()) pms5003t_Serial.read(); | |
if(datatype == PMS5003T_PM10) | |
return pms_pmcf10; | |
else if(datatype == PMS5003T_PM25) | |
return pms_pmcf25; | |
else if(datatype == PMS5003T_PM100) | |
return pms_pmcf100; | |
else if(datatype == PMS5003T_TEMPER) | |
return pms_Temperature; | |
else if(datatype == PMS5003T_HUMI) | |
return pms_Humidity; | |
} | |
void setup() | |
{ | |
mylcd.init(); | |
mylcd.backlight(); | |
pms5003t_Serial.begin(9600); | |
PM10 = 0; | |
PM2_5 = 0; | |
PM1_0 = 0; | |
temperature = 0; | |
humidity = 0; | |
} | |
void loop() | |
{ | |
PM10 = pms5003t_read_(PMS5003T_PM100); | |
PM2_5 = pms5003t_read_(PMS5003T_PM25); | |
PM1_0 = pms5003t_read_(PMS5003T_PM10); | |
temperature = pms5003t_read_(PMS5003T_TEMPER); | |
humidity = pms5003t_read_(PMS5003T_HUMI); | |
mylcd.clear(); | |
mylcd.setCursor(0,0); | |
mylcd.print(String("PM1=") + String(PM1_0) + String(" PM2.5=") + String(PM2_5)); | |
mylcd.setCursor(0,1); | |
mylcd.print(String("PM10=") + String(PM10)); | |
delay(3000); | |
mylcd.clear(); | |
mylcd.setCursor(0,0); | |
mylcd.print(String("Temperature=") + String(temperature) + String("C")); | |
mylcd.setCursor(0,1); | |
mylcd.print(String("Humidity=") + String(humidity) + String("%")); | |
delay(3000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment