Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save clive520/fe909c627300ee341ac45a6dda4853bb to your computer and use it in GitHub Desktop.
Save clive520/fe909c627300ee341ac45a6dda4853bb to your computer and use it in GitHub Desktop.
ESP8266 NodeMcu Lua & PM2.5/PMS5003/G5
// 1:紫色==>5V (V V)因為V V才是5V的電 2:橘色==>GND 4:藍色RX==>TX 5:綠色TX==>RX
#include <Wire.h>
#include <ESP8266HTTPClient.h>
long pmcf10=0;
long pmcf25=0;
long pmcf100=0;
long pmat10=0;
long pmat25=0;
long pmat100=0;
char buf[50];
void setup()
{
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");
}
else if(count == 7){
pmcf25 = 256*high + c;
Serial.print("CF=1, PM2.5=");
Serial.print(pmcf25);
Serial.println(" ug/m3");
}
else if(count == 9){
pmcf100 = 256*high + c;
Serial.print("CF=1, PM10=");
Serial.print(pmcf100);
Serial.println(" ug/m3");
}
else if(count == 11){
pmat10 = 256*high + c;
Serial.print("atmosphere, PM1.0=");
Serial.print(pmat10);
Serial.println(" ug/m3");
}
else if(count == 13){
pmat25 = 256*high + c;
Serial.print("atmosphere, PM2.5=");
Serial.print(pmat25);
Serial.println(" ug/m3");
}
else if(count == 15){
pmat100 = 256*high + c;
Serial.print("atmosphere, PM10=");
Serial.print(pmat100);
Serial.println(" ug/m3");
}
count++;
}
while(Serial.available()) Serial.read();
Serial.println();
delay(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment