-
Notifications
You must be signed in to change notification settings - Fork 4
/
co2-monitor.ino
386 lines (290 loc) · 9.9 KB
/
co2-monitor.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// A simple eCO2 meter using the Adafruit SCD30 breakout and the Adafruit 128x32 OLEDs
#include <Adafruit_SCD30.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <SD.h>
#include "RTClib.h"
RTC_PCF8523 rtc;
#include <Adafruit_NeoPixel.h>
#define NEO_PIN 8
Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, NEO_PIN, NEO_GRB + NEO_KHZ800);
int verNumber = 6;
int bad_co2 = 1100;
Adafruit_SCD30 scd30;
//---OLED display
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire);
#define BUTTON_A 9
#define BUTTON_B 6
#define BUTTON_C 5
#define VBATPIN A6 //A13 //14
boolean initDone;
unsigned long initTimer;
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)
unsigned long writeInterval = 30000;
unsigned long writeTimer;
unsigned long checkInterval = 2000;
unsigned long checkTimer;
float cur_co2 = 0;
float cur_humidity = 0;
float cur_temperature = 0;
float cur_measuredvbat = 0;
boolean vbat_toggleShow;
unsigned long sdCard_checkTimer;
bool sdCard_found;
void setup(void) {
Serial.begin(115200);
// while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens
delay(500);
Serial.println("SCD30 OLED eCO2 meter!");
// Try to initialize!
if (!scd30.begin()) {
Serial.println("Failed to find SCD30 chip");
while (1) {
delay(10);
}
}
Serial.println("SCD30 Found!");
delay(2000);
setup_rtc();
//---OLED display
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
pinMode(BUTTON_A, INPUT_PULLUP);
pinMode(BUTTON_B, INPUT_PULLUP);
pinMode(BUTTON_C, INPUT_PULLUP);
// bSetupMode = false;
// setupStage = -1;
if (!scd30.setMeasurementInterval(2)) {
Serial.println("Failed to set measurement interval");
while (1) {
delay(10);
}
}
//only set those once, NOT every time we start the device
// scd30.setAltitudeOffset(233); //in Montreal 233 m above sea level
scd30.setTemperatureOffset(0);
// 1015 => 10.15 degrees C
// 31337 => 313.37 degrees C
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
strip.begin();
strip.setBrightness(50);
strip.show(); // Initialize all pixels to 'off'
display.display();
delay(500); // Pause for half second
display.setTextSize(1);
display.setTextColor(WHITE);
display.setRotation(0);
sdCard_found = setup_sd();
initDone = false;
initTimer = millis();
checkTimer = millis();
writeTimer = millis();
Serial.println("setup done");
}
void loop() {
//------print initial info
if ( initDone == false) {
if (millis() - initTimer > 5000) {
initDone = true;
Serial.print("version ");
Serial.println(verNumber);
Serial.print("date: ");
Serial.print(F(__DATE__));
Serial.print(", time: ");
Serial.print(F(__TIME__));
Serial.println();
String dataString = "";
DateTime now = rtc.now();
dataString += String(now.year(), DEC);
dataString += '/';
dataString += String(now.month(), DEC);
dataString += '/';
dataString += String(now.day(), DEC);
dataString += ",";
dataString += String(now.hour(), DEC);
dataString += ':';
dataString += String(now.minute(), DEC);
dataString += ':';
dataString += String(now.second(), DEC);
Serial.println(dataString);
if (scd30.selfCalibrationEnabled()) {
Serial.print("Self calibration enabled");
} else {
Serial.print("Self calibration disabled");
}
Serial.println();
Serial.print("Measurement Interval: ");
Serial.print(scd30.getMeasurementInterval());
Serial.println(" seconds");
Serial.print("Forced Recalibration reference: ");
Serial.print(scd30.getForcedCalibrationReference());
Serial.println(" ppm");
Serial.print("Altitude offset: ");
Serial.print(scd30.getAltitudeOffset());
Serial.println(" meters");
Serial.print("Temperature offset: ");
Serial.print((float)scd30.getTemperatureOffset() / 100.0);
Serial.println(" degrees C");
} else {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 0);
display.print("version");
display.setCursor(90, 0);
display.println(verNumber);
cur_measuredvbat = analogRead(VBATPIN);
cur_measuredvbat *= 2; // we divided by 2, so multiply back
cur_measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage
cur_measuredvbat /= 1024; // convert to voltage
display.setCursor(0, 15);
display.print("battery");
display.setCursor(90, 15);
display.println(cur_measuredvbat, 1);
display.display();
strip.setPixelColor(0, strip.Color(0, 0, 255));
strip.show();
}
} else { //else if ( initDone == false)
//---check every 5 seconds if SD card is present
if (millis() - sdCard_checkTimer > 5000) {
sdCard_checkTimer = millis();
sdCard_found = setup_sd();
}
bool isInMenu = checkMenu();
// bool isInMenu = false;
if (isInMenu == false) {
//--check SCD30 sensor every n seconds
if (millis() - checkTimer > checkInterval) {
checkTimer = millis();
// loop_rtc();
// loop_sd();
display.clearDisplay();
// display.setCursor(0, 0);
display.setTextSize(1);
// display.setCursor(0, 0);
// display.print("v:");
// display.setCursor(10, 0);
// display.println(verNumber, 1);
DateTime now = rtc.now();
display.setCursor(0, 0);
display.print(now.timestamp(DateTime::TIMESTAMP_TIME));
if (scd30.dataReady()) {
Serial.println("Data available!");
if (!scd30.read()) {
Serial.println("Error reading sensor data");
display.println("READ ERR");
display.display();
return;
}
cur_temperature = scd30.temperature;
cur_co2 = scd30.eCO2;
cur_humidity = scd30.relative_humidity;
Serial.print("eCO2: ");
Serial.print(cur_co2, 3);
Serial.println(" ppm");
Serial.println("");
cur_measuredvbat = analogRead(VBATPIN);
cur_measuredvbat *= 2; // we divided by 2, so multiply back
cur_measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage
cur_measuredvbat /= 1024; // convert to voltage
if (cur_co2 > 0 && millis() - writeTimer > writeInterval) {
writeTimer = millis();
logTo_sd(cur_measuredvbat, cur_co2, cur_temperature, cur_humidity);
}
} //end if (scd30.dataReady())
if (sdCard_found == true) {
if (cur_measuredvbat < 3.67 ) {
if (vbat_toggleShow == true) {
display.setCursor(55, 0);
display.print(cur_measuredvbat);
display.setCursor(80, 0);
display.print("V");
} else {
display.setCursor(55, 0);
display.println("LOW!");
}
vbat_toggleShow = !vbat_toggleShow;
} else {
vbat_toggleShow = true;
if (millis() % 20000 < 10000) {
display.setCursor(55, 0);
display.print(cur_humidity, 1);
display.setCursor(80, 0);
display.print("%");
} else {
display.setCursor(55, 0);
display.println(cur_measuredvbat);
display.setCursor(80, 0);
display.print("V");
}
}
display.setCursor(95, 0);
display.print(cur_temperature, 1);
display.setCursor(120, 0);
display.print("C");
} else {
display.setCursor(55, 0);
display.println("NO SD-CARD!");
}
display.setCursor(100, 10);
display.println(" CO2");
display.setCursor(100, 20);
display.println(" ppm");
display.setTextSize(2);
display.setCursor(15, 15);
display.print(cur_co2, 1);
display.display();
} //end if (millis() - writeTimer > writeInterval)
if (cur_co2 > bad_co2) {
fadeNeoPixel(true);
} else {
fadeNeoPixel(false);
}
}//end if isInMenu == false
}//else end if initDone == false
//--blink onboard LED to indicate code is running
blink_it();
}
void blink_it()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
void fadeNeoPixel(bool _doIt) {
if (_doIt) {
int temp_percent = map(millis() % 10000, 0, 10000, -255, 255);
// Serial.print("temp_percent ");
// Serial.print(temp_percent);
// Serial.println();
strip.setPixelColor(0, strip.Color(abs(temp_percent), 0, 0));
strip.show();
} else {
strip.setPixelColor(0, strip.Color(0, 0, 0));
strip.show();
}
}