-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataLoggingBMP.ino
133 lines (109 loc) · 3.14 KB
/
DataLoggingBMP.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
#include <SD.h> //sd library
#include <SPI.h> //Serial Peripheral Interface library
#include <RTClib.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp1(4); // hardware SPI
RTC_PCF8523 rtc;
File myFile;
void setup() {
pinMode(10, OUTPUT);
Serial.begin(115200);
Serial.println("Starting BMP280 device 1...");
// BMP Initialization
if (!bmp1.begin()) {
Serial.println("Sensor BMP280 device 1 was not found.");
while (1);
}
Serial.println("Initialize BMP280 1 completed.");
delay(2000);
// RTC Initialization
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(3);
}
if (! rtc.initialized() || rtc.lostPower()) {
Serial.println("RTC is NOT initialized, let's set the time!");
delay(2500);
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
rtc.start();
// SD Card Initialization
if (SD.begin())
{
Serial.println("SD card is ready to use.");
if (SD.exists("dataLog.csv")) //check to see if dataLog.csv exists
{
Serial.println("dataLog.csv already exists");
}
else
{
//If dataLog.csv does not exist, create it and write the column headings
Serial.println("dataLog.csv created during setup");
myFile = SD.open("dataLog.csv", FILE_WRITE);
myFile.print("TEMPERATURE");
myFile.print(",");
myFile.print("PRESSURE");
myFile.print(",");
myFile.println("TIME");
myFile.close();
}
//SET UP COLUMN HEADINGS IN SERIAL MONITOR
Serial.print("TEMPERATURE");
Serial.print("\t");
Serial.print("PRESSURE");
Serial.print("\t");
Serial.println("TIME");
}
else
{
Serial.println("SD card initialization failed");
return;
}
}
void loop() {
DateTime now = rtc.now();
float pressure = bmp1.readPressure()/100;
float temperature = bmp1.readTemperature();
Serial.print(temperature);
Serial.print("\t\t");
Serial.print(pressure);
Serial.print("\t\t");
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.println(now.second(), DEC);
//PRINT TO FILE
myFile = SD.open("dataLog.csv", FILE_WRITE);
if (myFile) {
myFile.print(temperature);
myFile.print(",");
myFile.print(pressure);
myFile.print(",");
myFile.print(now.year(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.day(), DEC);
myFile.print(' ');
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(':');
myFile.println(now.second(), DEC);
myFile.close(); // close the file
}
// if the file didn't open, print an error:
else {
Serial.println("error opening file");
}
delay(1000);
}