· 6 years ago · Nov 20, 2019, 09:46 PM
1include "ESP8266WiFi.h" // replace with your channel's thingspeak API key and wifi information,
2 const char* ssid = "PUT HERE YOUR SSID WIFI";
3 const char* password = "PUT HERE YOUR WIFI PASSWORD";
4
5//Set variables for PM10 and PM2,5 readings
6 unsigned long starttime;
7 unsigned long sampletime_ms = 60000; // TIME BETWEEN MEASURES AND UPDATES
8
9unsigned long triggerOnP2;
10 unsigned long triggerOffP2;
11 unsigned long pulseLengthP2;
12 unsigned long durationP2;
13 boolean valP2 = HIGH;
14 boolean triggerP2 = false;
15 float ratioP2 = 0;
16 float countP2;
17 float concLarge;
18
19unsigned long triggerOnP1;
20 unsigned long triggerOffP1;
21 unsigned long pulseLengthP1;
22 unsigned long durationP1;
23 boolean valP1 = HIGH;
24 boolean triggerP1 = false;
25 float ratioP1 = 0;
26 float countP1;
27 float concSmall;
28
29WiFiClient client;
30
31void setup() {
32 pinMode(D4, INPUT); //PM2,5
33 pinMode(D5, INPUT);// PM10
34 Serial.begin(115200);
35 delay(10);
36
37WiFi.begin(ssid, password);
38
39Serial.println();
40 Serial.println();
41 Serial.print("Connecting to ");
42 Serial.println(ssid);
43
44while (WiFi.status() != WL_CONNECTED) {
45 delay(500);
46 Serial.print(".");
47 }
48 Serial.println("");
49 Serial.println("WiFi connected");
50 }
51
52void measure(){
53 valP1 = digitalRead(4); // Small ( pm2.5)
54 valP2 = digitalRead(5); // Large ( pm10 )
55
56//--------PM2,5-------------
57
58if(valP1 == LOW && triggerP1 == false){
59 triggerP1 = true;
60 triggerOnP1 = micros();
61 }
62
63if (valP1 == HIGH && triggerP1 == true){
64 triggerOffP1 = micros();
65 pulseLengthP1 = triggerOffP1 - triggerOnP1;
66 durationP1 = durationP1 + pulseLengthP1;
67 triggerP1 = false;
68 }
69
70//-----------PM10------------
71
72if(valP2 == LOW && triggerP2 == false){
73 triggerP2 = true;
74 triggerOnP2 = micros();
75 }
76
77if (valP2 == HIGH && triggerP2 == true){
78 triggerOffP2 = micros();
79 pulseLengthP2 = triggerOffP2 - triggerOnP2;
80 durationP2 = durationP2 + pulseLengthP2;
81 triggerP2 = false;
82 }
83
84//----------Calcolo-----------
85
86if ((millis() - starttime) > sampletime_ms) {
87
88// Integer percentage 0=>100
89 ratioP1 = durationP1/(sampletime_ms*10.0); // Integer percentage 0=>100
90 ratioP2 = durationP2/(sampletime_ms*10.0);
91 countP1 = 1.1*pow(ratioP1,3)-3.8*pow(ratioP1,2)+520*ratioP1+0.62;
92 countP2 = 1.1*pow(ratioP2,3)-3.8*pow(ratioP2,2)+520*ratioP2+0.62;
93 float PM10count = countP2;
94 float PM25count = countP1 - countP2;
95
96//PM10 count to mass concentration conversion
97 double r10 = 2.6*pow(10,-6);
98 double pi = 3.14159;
99 double vol10 = (4/3)*pi*pow(r10,3);
100 double density = 1.65*pow(10,12);
101 double mass10 = density*vol10;
102 double K = 3531.5;
103 concLarge = (PM10count)*K*mass10;
104
105//PM2.5 count to mass concentration conversion
106 double r25 = 0.44*pow(10,-6);
107 double vol25 = (4/3)*pi*pow(r25,3);
108 double mass25 = density*vol25;
109 concSmall = (PM25count)*K*mass25;
110
111//----Debug of Values on Serial Port----
112 Serial.print("PM10Conc: ");
113 Serial.print(concLarge);
114 Serial.print("ug/m3 , PM2.5Conc: ");
115 Serial.print(concSmall);
116 Serial.println(" ug/m3");
117
118//Reset Values
119 durationP1 = 0;
120 durationP2 = 0;
121 starttime = millis();
122
123}
124 }
125
126void loop() {
127
128measure(); // Recall of the measure program
129 }