-
Notifications
You must be signed in to change notification settings - Fork 2
/
CO2_ABC.ino
194 lines (167 loc) · 6.18 KB
/
CO2_ABC.ino
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifdef TGS4161
#define CO2_FIRST_PROCESS_TIME 60L*60*1000 //how much to wait before first storing of the data usually 15 min
//#define CO2_FIRST_PROCESS_TIME 60L*15*1000 //how much to wait before first storing of the data usually 15 min
int cfg_lowest_co2_ppm = 400;
byte cfg_abc_resetHours = 72;
unsigned long lastEEPROMWrite = 0;
unsigned long lastCO2Read = 0;
#define ONE_HOUR 60L*60*1000
#define THREE_DAYS (ONE_HOUR*72)
#define CO2_READ_TIMEOUT 15000
//unsigned long timeOneHourStarted = 0;
uint32_t time3daysStarted = 0;
void initCO2ABC() {
currentCO2MaxMv = 0;
currentMaxMvTemp = 0;
EEPROM.get(EE_FLT_PREV_PERIOD_CO2_HIGHESTMV, prevCO2MaxMv);
if (isnan(prevCO2MaxMv)) prevCO2MaxMv = 0;
EEPROM.get(EE_FLT_PREV_PERIOD_CO2_ATTEMP, prevMaxMvTemp);
if (isnan(prevMaxMvTemp)) prevMaxMvTemp = 0;
}
void processCO2() {
if (timePassed(lastCO2Read, CO2_READ_TIMEOUT) == false) return;
lastCO2Read = millis();
readSensorData();
if (startedCO2Monitoring || (millis() > CO2_FIRST_PROCESS_TIME)) {
processABC();
startedCO2Monitoring = true;
}
computeCO2PPM();
if (timePassed(time3daysStarted, THREE_DAYS)) {
time3daysStarted = millis();
storeABC();
}
}
void readSensorData() {
raTempC.addValue(getTermistorC(getVolts(1023 - analogReadFine(TEMP_PIN, ANALOG_READ_PRECISION))));
double dd = getVolts(analogReadFine(CO2_PIN, ANALOG_READ_PRECISION));
raCO2mv.addValue(getCO2_Mv(dd, true));
raCO2mvNoTempCorr.addValue(getCO2_Mv(dd, false));
}
//checkForMaxMV()
//uint32_t maxCO2RecheckTimeout = 0;
void storeABC() {
EEPROM.put(EE_FLT_PREV_PERIOD_CO2_HIGHESTMV, currentCO2MaxMv);
EEPROM.put(EE_FLT_PREV_PERIOD_CO2_ATTEMP, currentMaxMvTemp);
prevCO2MaxMv = currentCO2MaxMv;
prevMaxMvTemp = currentMaxMvTemp;
currentCO2MaxMv = 0;
currentMaxMvTemp = 0;
}
double raDeviation(RunningAverage &ra) {
double avg = ra.getAverage();
double dev = 0;
for (int i=0; !isnan(ra.getElement(i)); i++) {
dev += abs(avg-ra.getElement(i));
}
return dev;
}
void processABC() {
if (currentCO2MaxMv == 0 || ((raCO2mv.getAverage() > currentCO2MaxMv) &&
(raDeviation(raCO2mv) < 0.06F && raDeviation(raTempC) < 0.06F))) {
currentCO2MaxMv = raCO2mv.getAverage();
currentMaxMvTemp = raTempC.getAverage();
}
}
double getCO2MaxMv() {
return (prevCO2MaxMv > 1) ? prevCO2MaxMv : currentCO2MaxMv;
// return max(prevCO2MaxMv, currentCO2MaxMv);
}
void computeCO2PPM() {
//Serial << "computeco2ppm:" << getCO2MaxMv() <<","<<raCO2mv.getAverage() << endl;
sPPM = mv2ppm(getCO2MaxMv() - raCO2mv.getAverage(), GRADIENT);
}
#endif
void debugInfoCO2ABC() {
#ifdef TGS4161
Serial << F("cmv:") << raCO2mv.getAverage() << endl;
Serial << F("cNT:") << raCO2mvNoTempCorr.getAverage() << endl;
Serial << F("dif:") << raCO2mvNoTempCorr.getAverage() - raCO2mv.getAverage() << endl;
Serial << F("cMv:") << currentCO2MaxMv << endl;
Serial << F("pMv:") << prevCO2MaxMv << endl;
Serial << F("cMT:") << currentMaxMvTemp << endl;
Serial << F("pMT:") << prevMaxMvTemp << endl;
Serial << F("tem:") << raTempC.getAverage() << endl;
// Serial << F("upt:") << millis2min() << endl;
Serial << F("mnt:") << millis()/(1000L*60) << endl;
#endif
Serial << F("sPP:") << sPPM << endl;
}
//void processCO2SensorData() {
// //the default ppm is 350, that is - we need to adjust the values as if they had
// //hit the max ppm of 350. We assume that 400 ppm is the lowest that can be received
// double co2MvAdj = raCO2mv.getAverage() + ppm2mv((double)cfg_lowest_co2_ppm, GRADIENT);
// if (co2MvAdj > currentCO2MaxMv) {
// if (maxCO2RecheckTimeout == 0) {
// maxCO2RecheckTimeout = millis();
// } else if (timePassed(maxCO2RecheckTimeout, 1L*60L*1000L)) {
// //apparently quick calibration by putting the device outside is not stable, so it is best to leave the device
// //continously on. Now at least there is a 1 minute filter, so that if there is some very quick change - to avoid it
// /*
// * if a new high is found, wait for 1 minutes and only then process it
// * this will solve the problem, that may appear if the temperature changes rapidly
// * together with CO2 concentration
// */
// //update the current max CO2, and store it. Since the Read Timeout is 60 seconds,
// // this is will ensure that there will be only a limited stores to the Flash memory
// // until the treshold is reached. Maximum should be 5000 times, until the sensor warms
// // for a few days, and only if it sits outside
//
// //if the device is exposed to sudden change of temperature e.g. from outside bring it in
// // or vice versa
// // the temperature measurement and the CO2 measurement may not catch up this is why we check
// // if the running average of the tempeature has min and max less than 2 degrees, this means
// // that the temperature has already settled, which means that the device may need to stay out longer
// currentCO2MaxMv = co2MvAdj;
// //storeCurrentCO2MaxMv();
// maxCO2RecheckTimeout = 0;
// }
// }
//}
//void storeCurrentCO2MaxMv() {
// EEPROM.put(EE_FLT_CURRENT_PERIOD_CO2_HIGHESTMV, currentCO2MaxMv);
//}
//
//void co2OnOneHour() {
// storeCurrentCO2MaxMv();
// if (eeAddHourAndReturn() == cfg_abc_resetHours) {
// for (byte i=0; i < 4; i++) EEPROM.put(EE_4B_HOUR + i, (byte)0);
// EEPROM.put(EE_FLT_PREV_PERIOD_CO2_HIGHESTMV, currentCO2MaxMv);
// prevCO2MaxMv = currentCO2MaxMv;
// currentCO2MaxMv = 0;
// }
//}
//
//double getEECurrentCO2MaxMv() {
// double d;
// EEPROM.get(EE_FLT_CURRENT_PERIOD_CO2_HIGHESTMV, d);
// return d;
//}
//
//double getEEPrevCO2MaxMv() {
// double d;
// EEPROM.get(EE_FLT_PREV_PERIOD_CO2_HIGHESTMV, d);
// return d;
//}
//byte eeGetHours() {
// byte maxH = 0, pos = 0, r = 0, i = 0;
// for (; i < 4; i++) {
// if (EEPROM.get(i + EE_4B_HOUR, r) && r > maxH && r != 255) {
// maxH = r;
// pos = i;
// }
// }
// return maxH;
//}
//
//byte eeAddHourAndReturn() {
// byte maxH = 0, pos = 0, r = 0, i = 0;
// for (; i < 4; i++) {
// if (EEPROM.get(i + EE_4B_HOUR, r) && r > maxH && r != 255) {
// maxH = r;
// pos = i;
// }
// }
// EEPROM.put(EE_4B_HOUR + ((i+1) % 4), maxH + 1);
// return maxH + 1;
//}