-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGPS.ino
294 lines (259 loc) · 7.04 KB
/
GPS.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
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <SD.h>
#include <SPI.h>
#include "util.h";
#include "ublox.h";
SoftwareSerial GPS=SoftwareSerial(D1, D2);
Ublox ublox=Ublox(&GPS);
TinyGPSPlus nmea;
char secret[33];
void setup() {
Serial.begin(9600);
Serial.println();
Serial.println("Debugging started");
// Input for user button
pinMode(D3, INPUT_PULLUP);
// Softserial pins for GPS module
pinMode(D1, INPUT);
pinMode(D2, OUTPUT);
// LED on the ESP-12e
pinMode(D4, OUTPUT);
// Start Softserial for GPS
GPS.begin(9600);
GPS.enableRx(true);
Serial.println("SoftSerial started");
Serial.println("Sending gps settings");
ublox.setupGPS();
Serial.println("Setttings done");
// Check if button is pressed on boot
if(!digitalRead(D3)){
while(true){
if(Serial.available()){
GPS.write(Serial.read());
}
if(GPS.available()){
Serial.write(GPS.read());
}
delay(0);
}
}
Serial.println("Starting SD");
if(SD.begin(SS)){
Serial.println("SD Init success");
blink(4);
}else{
Serial.println("SD Init failure");
blink(8);
ESP.deepSleep(0);
}
File secretFile = SD.open("SYSTEM/SECRET.TXT");
secret[32]= '\0';
secretFile.read(secret, 32);
secretFile.close();
Serial.printf("Secret: %s\n", secret);
SD.mkdir("system");
}
bool connect_wifi(){
Serial.println("Connecting to wifi network");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
Serial.println("Scanning nearby networks");
int scanCount = WiFi.scanNetworks();
Serial.println("Loading possible networks");
File wifiFile = SD.open("SYSTEM/WIFI.TXT");
char fileSSID[40];
char fileKEY[40];
int charIndex = 0;
bool inSSID = true;
bool matchFound = false;
while(!matchFound){
int fileChar = wifiFile.read();
if(fileChar < 0){
break;
}
if(fileChar == '\n'){
fileKEY[charIndex]=(char)0;
charIndex = 0;
inSSID = true;
for(int i=0;i<scanCount;i++){
if(strcmp(fileSSID, WiFi.SSID(i).c_str())==0){
matchFound = true;
WiFi.begin(fileSSID, fileKEY);
break;
}
}
}else if(fileChar == ':'){
inSSID = false;
fileSSID[charIndex]=(char)0;
charIndex = 0;
}else{
if(inSSID){
fileSSID[charIndex++]=(char)fileChar;
}else{
fileKEY[charIndex++]=(char)fileChar;
}
}
}
if(matchFound){
int timeout = 2*30;
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
if(--timeout == 0){
Serial.println("Couldn't connect to the SSID");
return false;
}
}
Serial.println(WiFi.localIP());
}else{
Serial.println("No network available");
}
return matchFound;
}
void queueFile(const char* name){
Serial.printf("Queueing %s\n", name);
File queue = SD.open("system/queue.txt", FILE_WRITE);
if(!queue){
Serial.printf("Error opening queue file");
}
queue.seek(queue.size());
queue.printf("%s\n", name);
queue.flush();
queue.close();
}
void uploadTrack(char* name){
File log = SD.open(name);
WiFiClient client;
int tracker = ESP.getChipId();
String trackerId = String(tracker);
Serial.printf("Uploading %s\n", name);
if(client.connect("tracker.brixit.nl", 80)){
Serial.println("Sending headers");
String length = String(log.size());
String headers = String("POST /upload.php HTTP/1.0\r\n")+
"Host: tracker.brixit.nl\r\n" +
"Connection: close\r\n" +
"Content-Type: text/csv\r\n" +
"Content-Length: " + length + "\r\n" +
"X-Tracker: " + trackerId + "\r\n" +
"X-Secret: " + secret + "\r\n" +
"X-Track: " + name + "\r\n" +
"\r\n";
client.print(headers);
Serial.println("Sending data");
char buffer[1470];
while(true){
int bytes = log.read(buffer,1470);
if(bytes < 1){
break;
}
client.write_P(buffer, bytes);
delay(0);
}
Serial.println("Upload done");
client.stop();
}
}
void uploadQueue(){
digitalWrite(D4, HIGH);
delay(100);
digitalWrite(D4, LOW);
File queue = SD.open("system/queue.txt");
char filename[17];
char charIndex = 0;
Serial.println("Starting upload queue");
while(true){
int fileChar = queue.read();
if(fileChar < 0){
Serial.println("Reached EOF");
break;
}
if(fileChar == '\n'){
filename[charIndex] = (char)0;
charIndex = 0;
uploadTrack(filename);
}else{
filename[charIndex++]=(char)fileChar;
}
}
queue.close();
SD.remove("system/queue.txt");
}
void loop() {
blink(4);
Serial.println("Starting to parse nmea messages");
bool locked = false;
int lastSats = 0;
File logfile;
bool driving = true;
char name[22] = {0};
while(driving){
delay(10);
while(GPS.available() && driving){
if(!digitalRead(D3)){
driving=false;
}
int gpsChar = GPS.read();
//Serial.write(gpsChar);
if(nmea.encode(gpsChar) && nmea.location.isUpdated()){
if(!locked && nmea.location.isValid() && nmea.date.isValid()){
Serial.println("Got GPS fix");
char directory[9]={0};
sprintf(directory, "%d%d%d", nmea.date.year(), nmea.date.month(), nmea.date.day());
sprintf(name, "%s/%d.log", directory, nmea.time.value());
queueFile(name);
Serial.printf("Creating directory %s\n", directory);
SD.mkdir(directory);
logfile = SD.open(name, FILE_WRITE);
if(!logfile){
Serial.println("Error creating logfile");
}else{
Serial.printf("Logging to %s\n", name);
}
logfile.print("date,time,latitude,longitude,speed,checksum\n");
locked = true;
}
if(locked && !nmea.location.isValid()){
Serial.println("Lost GPS fix");
logfile.close();
locked = false;
}
if(locked){
digitalWrite(D4, LOW);
char lat[20];
char lng[20];
char speed[20];
fmtDouble(nmea.location.lat(), 10, lat);
fmtDouble(nmea.location.lng(), 10, lng);
fmtDouble(nmea.speed.kmph(),2, speed);
char logline[100] = {0};
sprintf(logline, "%d,%d,%s,%s,%s", nmea.date.value(), nmea.time.value(), lat, lng, speed);
int checksum = calculateChecksum(logline);
logfile.printf("%s,%d\n", logline, checksum);
logfile.flush();
if(!driving){
logfile.close();
}
digitalWrite(D4, HIGH);
}else{
if(nmea.satellites.value() > lastSats){
lastSats = nmea.satellites.value();
Serial.printf("Got new sattelite. Now %d\n", lastSats);
}
}
}
}
}
GPS.enableRx(false);
Serial.println("Track done");
digitalWrite(D4, LOW);
if(connect_wifi()){
uploadQueue();
}
ublox.sleepyGPS();
digitalWrite(D4, HIGH);
ESP.deepSleep(0);
}