Code for Project

In this page there is the Arduino IDE Code for Pollution Project with PPD42NS

include < ESP8266WiFi.h > // replace with your channel's thingspeak API key and wifi information,
 String apiKey = "PUT HERE YOUR API KEY";
 const char* ssid = "PUT HERE YOUR SSID WIFI";
 const char* password = "PUT HERE YOUR WIFI PASSWORD";
 const char* server = "api.thingspeak.com";

//Set variables for PM10 and PM2,5 readings
 unsigned long starttime;
 unsigned long sampletime_ms = 60000; // TIME BETWEEN MEASURES AND UPDATES

unsigned long triggerOnP2;
 unsigned long triggerOffP2;
 unsigned long pulseLengthP2;
 unsigned long durationP2;
 boolean valP2 = HIGH;
 boolean triggerP2 = false;
 float ratioP2 = 0;
 float countP2;
 float concLarge;

unsigned long triggerOnP1;
 unsigned long triggerOffP1;
 unsigned long pulseLengthP1;
 unsigned long durationP1;
 boolean valP1 = HIGH;
 boolean triggerP1 = false;
 float ratioP1 = 0;
 float countP1;
 float concSmall;

WiFiClient client;

void setup() {
 pinMode(4, INPUT); //PM2,5
 pinMode(5, INPUT);// PM10
 Serial.begin(115200);
 delay(10);

WiFi.begin(ssid, password);

Serial.println();
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);

while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected");
 }

void measure(){
 valP1 = digitalRead(4); // Small ( pm2.5)
 valP2 = digitalRead(5); // Large ( pm10 )

//--------PM2,5-------------

if(valP1 == LOW && triggerP1 == false){
 triggerP1 = true;
 triggerOnP1 = micros();
 }

if (valP1 == HIGH && triggerP1 == true){
 triggerOffP1 = micros();
 pulseLengthP1 = triggerOffP1 - triggerOnP1;
 durationP1 = durationP1 + pulseLengthP1;
 triggerP1 = false;
 }

//-----------PM10------------

if(valP2 == LOW && triggerP2 == false){
 triggerP2 = true;
 triggerOnP2 = micros();
 }

if (valP2 == HIGH && triggerP2 == true){
 triggerOffP2 = micros();
 pulseLengthP2 = triggerOffP2 - triggerOnP2;
 durationP2 = durationP2 + pulseLengthP2;
 triggerP2 = false;
 }

//----------Calcolo-----------

if ((millis() - starttime) > sampletime_ms) {

// Integer percentage 0=>100
 ratioP1 = durationP1/(sampletime_ms*10.0); // Integer percentage 0=>100
 ratioP2 = durationP2/(sampletime_ms*10.0);
 countP1 = 1.1*pow(ratioP1,3)-3.8*pow(ratioP1,2)+520*ratioP1+0.62;
 countP2 = 1.1*pow(ratioP2,3)-3.8*pow(ratioP2,2)+520*ratioP2+0.62;
 float PM10count = countP2;
 float PM25count = countP1 - countP2;

//PM10 count to mass concentration conversion
 double r10 = 2.6*pow(10,-6);
 double pi = 3.14159;
 double vol10 = (4/3)*pi*pow(r10,3);
 double density = 1.65*pow(10,12);
 double mass10 = density*vol10;
 double K = 3531.5;
 concLarge = (PM10count)*K*mass10;

//PM2.5 count to mass concentration conversion
 double r25 = 0.44*pow(10,-6);
 double vol25 = (4/3)*pi*pow(r25,3);
 double mass25 = density*vol25;
 concSmall = (PM25count)*K*mass25;

//------Send data to Thingspeak------
 client.connect(server,80); // "184.106.153.149" or api.thingspeak.com
 String postStr = apiKey;
 postStr +="&field1=";
 postStr += String(concLarge);
 postStr += "\r\n";
 postStr +="&field2=";
 postStr += String(concSmall);
 postStr += "\r\n";

client.print("POST /update HTTP/1.1\n");
 client.print("Host: api.thingspeak.com\n");
 client.print("Connection: close\n");
 client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
 client.print("Content-Type: application/x-www-form-urlencoded\n");
 client.print("Content-Length: ");
 client.print(postStr.length());
 client.print("\n\n");
 client.print(postStr);
 client.stop();

//----Debug of Values on Serial Port----
 Serial.print("PM10Conc: ");
 Serial.print(concLarge);
 Serial.print("ug/m3 , PM2.5Conc: ");
 Serial.print(concSmall);
 Serial.println(" ug/m3");

//Reset Values
 durationP1 = 0;
 durationP2 = 0;
 starttime = millis();

}
 }

void loop() {

measure(); // Recall of the measure program
 }