-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCO2_DATA_logger_MHZ14.ino
260 lines (205 loc) · 7.63 KB
/
CO2_DATA_logger_MHZ14.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
#include <SPI.h>
#define CHILD_ID 0
#define CO2_SENSOR_PWM_PIN 3
unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
float valAIQ =0.0;
float lastAIQ =0.0;
unsigned long duration;
long ppm;
/*MySensor gw;
MyMessage msg(CHILD_ID, V_LEVEL);
*/
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------MH-Z14
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
// A simple data logger for the Arduino analog pins
// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL 10000 // mills between entries (reduce to take more/faster data)
// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 10000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()
/* the digital pins that connect to the LEDs
#define redLEDpin 2
#define greenLEDpin 3
// The analog pins that connect to the sensors
#define photocellPin 0 // analog 0
#define tempPin 1 // analog 1
#define BANDGAPREF 14 // special indicator that we want to measure the bandgap
#define aref_voltage 3.3 // we tie 3.3V to ARef and measure it with a multimeter!
#define bandgap_voltage 1.1 // this is not super guaranteed but its not -too- off
*/
RTC_DS1307 RTC; // define the Real Time Clock object
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
// the logging file
File logfile;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Logger
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------BME/BMP280
//int led = 9;
int carboni = 0;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------LED/Variables
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
/* red LED indicates error
digitalWrite(redLEDpin, HIGH);
*/
while(1);
}
void setup() {
// Run serial to connect to a computer
Serial.begin(9600);
pinMode(CO2_SENSOR_PWM_PIN, INPUT);
// initialize the SD card
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
Serial.println(F("BME280 test"));
bool status;
// default settings
status = bme.begin();
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
Serial.println("-- Default Test --");
Serial.println();
delay(100); // let sensor boot up
pinMode(10, OUTPUT);
// pinMode(led, OUTPUT);
// see if the card is present and can be initialized:
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
error("Card failed, or not present");
}
Serial.println("card initialized.");
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
/* if (! logfile) {
error("couldnt create file");
}
*/
Serial.print("Logging to: ");
Serial.println(filename);
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
logfile.println("datetime,CO2_ppm,Corrected_CO2,temp,pressure");
#if ECHO_TO_SERIAL
Serial.println("datetime,CO2_ppm,Corrected_CO2,temp,pressure");
#endif //ECHO_TO_SERIAL
}
void loop(void)
{
DateTime now;
// delay for the amount of time we want between readings
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
// log milliseconds since starting
uint32_t m = millis();
// logfile.print(m); // milliseconds since start
//logfile.print(", ");
//#if ECHO_TO_SERIAL
//Serial.print(m); // milliseconds since start
//Serial.print(", ");
//#endif
// fetch the time
now = RTC.now();
// log time
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
logfile.print('"');
#if ECHO_TO_SERIAL
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.print('"');
#endif //ECHO_TO_SERIAL
while(digitalRead(CO2_SENSOR_PWM_PIN) == HIGH) {;}
//wait for the pin to go HIGH and measure HIGH time
duration = pulseIn(CO2_SENSOR_PWM_PIN, HIGH, 2000000);
ppm = 5000 * ((duration/1000) - 2)/1000;
//Serial.print(ppm);
float carboni = ppm * 100.0F * ((bme.readTemperature() + 273) * (1013))/(bme.readPressure() * (298));
logfile.print(", ");
logfile.print(ppm); //this is useful for writing the data onto the logger
logfile.print(", ");
logfile.print(carboni); //this is useful for writing the data onto the logger
logfile.print(", ");
logfile.print(bme.readTemperature());
logfile.print(", ");
logfile.print(bme.readPressure() / 100.0F);
#if ECHO_TO_SERIAL
Serial.print(", ");
Serial.print(ppm);
Serial.print(", ");
Serial.print(carboni);
Serial.print(", ");
Serial.print(bme.readTemperature());
Serial.print(", ");
Serial.print(bme.readPressure()/ 100.0F);
#endif //ECHO_TO_SERIAL
logfile.println();
#if ECHO_TO_SERIAL
Serial.println();
#endif // ECHO_TO_SERIAL
//digitalWrite(greenLEDpin, LOW);
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();
logfile.flush();
/*
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
*/
}