Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save clive520/6f0d00b492c278603eeb0d6e19ec070a to your computer and use it in GitHub Desktop.
Save clive520/6f0d00b492c278603eeb0d6e19ec070a to your computer and use it in GitHub Desktop.
ESP8266_PM2.5_G5_LCD
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.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];
LiquidCrystal_I2C lcd(0x27,16,2);
WiFiClient client;
String thingSpeakAddress= "http://api.thingspeak.com/update?";
String writeAPIKey;
String tsfield1Name;
String request_string;
HTTPClient http;
void setup()
{
lcd.begin();
Serial.begin(9600);
WiFi.disconnect();
delay(3000);
lcd.setCursor(0, 0);
lcd.print("START");
WiFi.begin("clive_1F","ruby3217");
while ((!(WiFi.status() == WL_CONNECTED))){
delay(300);
lcd.setCursor(0, 1);
lcd.print("......");
}
lcd.setCursor(0, 0);
lcd.print("I am connected!");
lcd.setCursor(0, 1);
lcd.print((WiFi.localIP()));
delay(1000);
}
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");
lcd.setCursor(0,0);
lcd.print(String("check failed "));
break;
}
if(count > 15){
if (client.connect("api.thingspeak.com",80)) {
writeAPIKey = "key=E29JR0R2RIV4D49D";
tsfield1Name = "&field1=pmat10&field2=pmat25&field3=pmat100";
request_string = thingSpeakAddress;
request_string += "key=";
request_string += "E29JR0R2RIV4D49D";
request_string += "&";
request_string += "field1";
request_string += "=";
request_string += pmat10;
request_string += "&";
request_string += "field2";
request_string += "=";
request_string += pmat25;
request_string += "&";
request_string += "field3";
request_string += "=";
request_string += pmat100;
http.begin(request_string);
http.GET();
http.end();
}
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.setCursor(0,0);
lcd.print(String("CF ")+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(8,0);
lcd.print(pmcf25 + String(","));
}
else if(count == 9){
pmcf100 = 256*high + c;
Serial.print("CF=1, PM10=");
Serial.print(pmcf100);
Serial.println(" ug/m3");
lcd.setCursor(12,0);
lcd.print(pmcf100);
}
else if(count == 11){
pmat10 = 256*high + c;
Serial.print("atmosphere, PM1.0=");
Serial.print(pmat10);
Serial.println(" ug/m3");
lcd.setCursor(0,1);
lcd.print(String("At ")+pmat10 + String(","));
}
else if(count == 13){
pmat25 = 256*high + c;
Serial.print("atmosphere, PM2.5=");
Serial.print(pmat25);
Serial.println(" ug/m3");
lcd.setCursor(8,1);
lcd.print(pmat25 + String(","));
}
else if(count == 15){
pmat100 = 256*high + c;
Serial.print("atmosphere, PM10=");
Serial.print(pmat100);
Serial.println(" ug/m3");
lcd.setCursor(12,1);
lcd.print(pmat100);
}
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