-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPS Datalogger.ino
143 lines (116 loc) · 2.56 KB
/
GPS Datalogger.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
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#include <DHT20.h>
DHT20 DHT;
bool debug = true;
static const int GPSrx = 4, GPStx = 3;
static const uint32_t GPSBaud = 9600;
static const int TempPin = 2;
static const int LM60TempPin = 6;
const int SDcs = 5;
TinyGPSPlus gps;
SoftwareSerial ss(GPSrx, GPStx);
double lat;
double lng;
double alt;
TinyGPSTime time;
int status;
double temp;
double hum;
int valid;
int LM60read;
double extTemp;
void setup() {
Serial.begin(115200);
ss.begin(GPSBaud);
DHT.begin();
delay(1000);
pinMode(LM60TempPin, OUTPUT);
if (!SD.begin(SDcs)) {
Serial.println("Card failed, or not present");
while (1);
}
Serial.println("SD card initialized.");
File log = SD.open("datalog.txt", FILE_WRITE);
log.println("time,valid,lat,lng,alt,temp,hum,LM60temp");
log.close();
}
void loop() {
logGPSInfo();
if (debug) printDebug();
smartDelay(1000);
}
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
static void logGPSInfo() {
File log = SD.open("datalog.txt", FILE_WRITE);
lat = gps.location.lat();
lng = gps.location.lng();
alt = gps.altitude.meters();
time = gps.time;
status = DHT.read();
temp = DHT.getTemperature();
hum = DHT.getHumidity();
digitalWrite(LM60TempPin, HIGH);
analogReference(INTERNAL);
analogRead(A0);
delay(10);
LM60read = analogRead(A0);
digitalWrite(LM60TempPin, LOW);
extTemp = ((1100. * LM60read / 1023.) - 424) / 6.25;
if (gps.location.isValid()) {
valid = 1;
} else {
valid = 0;
}
log.print(time.value());
log.print(",");
log.print(valid);
log.print(",");
log.print(lat, 4);
log.print(",");
log.print(lng, 4);
log.print(",");
log.print(alt, 4);
log.print(",");
log.print(temp, 1);
log.print(",");
log.print(hum, 1);
log.print(",");
log.print(extTemp, 1);
log.println();
log.close();
}
static void printDebug() {
String formattedTime = formatTime(time);
Serial.print(formattedTime);
Serial.print(",");
Serial.print(valid);
Serial.print(",");
Serial.print(lat, 4);
Serial.print(",");
Serial.print(lng, 4);
Serial.print(",");
Serial.print(alt, 4);
Serial.print(",");
Serial.print(temp, 1);
Serial.print(",");
Serial.print(hum, 1);
Serial.print(",");
Serial.print(extTemp, 1);
Serial.println();
}
static String formatTime(TinyGPSTime &t) {
char sz[32];
sprintf(sz, "%02d:%02d:%02d", t.hour(), t.minute(), t.second());
return sz;
}