-
Notifications
You must be signed in to change notification settings - Fork 0
/
Esp8266AirQuality.cpp
136 lines (125 loc) · 3.8 KB
/
Esp8266AirQuality.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
AirQuality library v1.0
2010 Copyright (c) Seeed Technology Inc. All right reserved.
Original Author: Bruce.Qin
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include"Arduino.h"
#include"Esp8266AirQuality.h"
//Get the avg voltage in 5 minutes.
void Esp8266AirQuality::avgVoltage()
{
if(i==150)//sum 5 minutes
{
vol_standard=temp/150;
temp=0;
Serial.print("Vol_standard in 5 minutes:");
Serial.println(vol_standard);
i=0;
}
else
{
temp+=first_vol;
i++;
}
}
void Esp8266AirQuality::init(int pin)
{
// _pin=pin;
// pinMode(_pin,INPUT);
unsigned char i=0;
delay(3000);
Serial.println();
Serial.println("Air Quality Sensor is starting: Please wait 10 seconds to calibrate...");
for (int j=0; j <= 10; j++) {
delay(1000);
Serial.print(".");
}
Serial.println();
// init_voltage=analogRead(_pin);
init_voltage=analogRead(A0);
Serial.println("The init voltage is: " + String(init_voltage));
while(init_voltage)
{
if(init_voltage<798 && init_voltage>10)// the init voltage is ok
{
first_vol=analogRead(A0);//initialize first value
last_vol=first_vol;
vol_standard=last_vol;
Serial.println("Sensor ready.");
Serial.println();
Serial.println("Setup of ESP8266 with Air Quality Sensor finished");
Serial.println("-------------------------------------------------");
error=false;;
break;
}
else if(init_voltage>798||init_voltage<=10)
{
i++;
Serial.println("waiting sensor init..(it takes 60 seconds to init)");
delay(60000);//60000
init_voltage=analogRead(A0);
if(i==5)
{
i=0;
error=true;
Serial.println("Sensor Error!");
}
}
else
break;
}
}
int Esp8266AirQuality::slope(void)
{
while(timer_index)
{
Serial.println();
if(first_vol-last_vol>400||first_vol>700)
{
Serial.println("High pollution! Force signal active.");
timer_index=0;
avgVoltage();
return 0;
}
else if((first_vol-last_vol>400&&first_vol<700)||first_vol-vol_standard>150)
{
Serial.print("sensor_value=");
Serial.print(first_vol);
Serial.println("\t High pollution!");
timer_index=0;
avgVoltage();
return 1;
}
else if((first_vol-last_vol>200&&first_vol<700)||first_vol-vol_standard>50)
{
//Serial.println(first_vol-last_vol);
Serial.print("sensor_value=");
Serial.print(first_vol);
Serial.println("\t Low pollution!");
timer_index=0;
avgVoltage();
return 2;
}
else
{
avgVoltage();
Serial.print("sensor_value=");
Serial.print(first_vol);
Serial.println("\t Air fresh");
timer_index=0;
return 3;
}
}
return -1;
}