forked from rocketscream/TraLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VehicleTracking.ino
340 lines (310 loc) · 8.9 KB
/
VehicleTracking.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
/*******************************************************************************
* Example of Vehicle Tracking and Logging using TraLog
* Version: 1.00
* Date: 04-10-2012
* Company: Rocket Scream Electronics
* Author: Lim Phang Moh
* Website: www.rocketscream.com
*
* This is an example on how to use all 3 feature (GPRS-GPS-SD) of the TraLog
* shield. We will update a Cosm feed using HTTP PUT request over GPRS, writing
* to a text file on the microSD card, and receiving GPS NMEA data. Due to the
* limitation on the RAM size on ATMega328, all the task must be executed in a
* modest approach by allowing all task to run one at a time rather than doing
* everything at one go. Avoid using excessive amount of RAM
* (example: Serial.print("Hello") should be avoided and store the string in
* Flash memory like for example: Serial.print(F("Hello))).
*
* ============
* Requirements
* ============
* 1. UART selection switch to SW position (uses pin D5 (RX) & D6 (TX)).
* 2. Jumper J14 is closed to allow usage of pin A2 to control on-off state of
* WISMO228 module. This is the default factory setting.
* 3. You need to know your service provider APN name, username, and password.
* If they don't specify the username and password, you can use " ". Notice
* the space in between the quote mark.
* 4. The Cosm server name & port.
* 5. Cosm Api Key.
* 6. Cosm data stream names.
* 7. A microSD card pre-formatted in FAT32 format.
* 8. Both GSM & GPS antenna attached to the TraLog shield.
*
* ============
* Instructions
* ============
* 1. Use the RTC example (on WISMO228 library) to set the GSM RTC to your
* desired time prior to using this example. Or you can uncomment the RTC_SET
* setting section in the setup code but time will be set to the value
* specified in this firmware every time it power up.
* 2. Ensure a CR1220 coin cell is used to provide battery backup to the
* WISMO228 internal RTC in the even of power inavailability.
* 3. Please create your own Cosm/Pachube datastream and replace necessary
* parameter(s) to suit your setup.
*
* This example is licensed under Creative Commons Attribution-ShareAlike 3.0
* Unported License.
*
* Revision Description
* ======== ===========
* 1.00 Initial public release. Tested with L22 & L23 of WISMO228 firmware.
* Only works with Arduino IDE 1.0 & 1.0.1. Uses v1.10 of the WISMO228
* library.
*******************************************************************************/
// ***** COMPILE OPTIONS *****
// Uncomment to allow serial debug messages
#define DEBUG
// Uncomment to set the GSM RTC using the time & date specified in newClock[]
#define RTC_SET
// ***** INCLUDES *****
#include <SD.h>
#include "SoftwareSerial.h"
#include <WISMO228.h>
#include <TinyGPS.h>
// ***** PIN ASSIGNMENT *****
const uint8_t gsmRxPin = 5;
const uint8_t gsmTxPin = 6;
const uint8_t gsmOnOffPin = A2;
const uint8_t gpsRxPin = 7;
const uint8_t gpsTxPin = 8;
const uint8_t sdCsPin = 4;
// ***** CONSTANTS *****
// ***** GPRS PARAMATERS *****
const char apn[] = "apn";
const char username[] = "username";
const char password[] = "password";
// ***** COSM PARAMATERS *****
const char server[] = "api.cosm.com";
const char path[] = "/v2/feeds/InsertYourFeedNumberHere.csv";
const char port[] = "80";
const char host[] = "api.cosm.com";
const char controlKey[] = "X-PachubeApiKey: InsertYourPachubeApiKeyHere";
const char contentType[] = "text/csv";
const char stream1[] = "0"; // Cosm feeds data stream name
const char stream2[] = "1"; // Cosm feeds data stream name
// ***** RTC *****
// Clock in YY/MM/DD,HH:MM:SS*ZZ format
// ZZ represent time difference between local & GMT time expressed in quarters
// of an hour. *ZZ has a range of -48 to +48.
// Example: 12/10/16,17:30:00+32 (GMT+8 local time).
const char newClock[CLOCK_COUNT_MAX + 1] = "12/10/16,17:30:00+32";
#define LOG_INTERVAL 60000
// ***** CLASSES *****
// File class
File logFile;
// GPS Parser Class
TinyGPS gps;
// Software serial for the GPS
SoftwareSerial ss(gpsRxPin, gpsTxPin);
// WISMO228 class
WISMO228 wismo(gsmRxPin, gsmTxPin, gsmOnOffPin);
// ***** VARIABLES *****
char clock[CLOCK_COUNT_MAX + 1];
char data[32];
unsigned long scheduler;
boolean newGpsData = false;
void setup()
{
// Use hardware serial to track the progress of task execution
#ifdef DEBUG
Serial.begin(9600);
Serial.println(F("===Vehicle Tracking & Logging using TraLog==="));
#endif
// Initialize the SD card
if (!SD.begin(sdCsPin))
{
#ifdef DEBUG
Serial.println(F("SD card init failed!"));
#endif
return;
}
Serial.println(F("SD card init OK."));
// Initialize WISMO228
wismo.init();
// Perform WISMO228 power up sequence
if (wismo.powerUp())
{
#ifdef DEBUG
Serial.println(F("GSM is awake."));
#endif
#ifdef RTC_SET
if (wismo.setClock(newClock))
{
#ifdef DEBUG
Serial.println(F("New clock set."));
#endif
// Let's check the time & date
if (wismo.getClock(clock))
{
#ifdef DEBUG
Serial.print(F("RTC: "));
Serial.println(clock);
#endif
}
}
#endif
}
// Running 2 software serial at 1 time makes the application unpredictable
// due to excessive RAM usage and multiple fast edge interrupt
wismo.shutdown();
#ifdef DEBUG
Serial.println(F("GSM back to sleep."));
#endif
// Set tracking, feed update & logging interval
scheduler = millis() + LOG_INTERVAL;
}
void loop()
{
if (millis() > scheduler)
{
scheduler = millis() + LOG_INTERVAL;
// Get GPS data for current location
processGps();
// New valid GPS data available
if (newGpsData)
{
// Feed current location to COSM datastream
processGsm();
// Log current location into microSD card
processLog();
// Latest GPS data logged
newGpsData = false;
}
}
}
void processGps(void)
{
ss.begin(4800);
#ifdef DEBUG
Serial.println(F("GPS gathering data..."));
#endif
// Parse GPS data for 2 second
for (unsigned long start = millis(); millis() - start < 2000;)
{
while (ss.available())
{
char c = ss.read();
// New valid NMEA data available
if (gps.encode(c))
{
newGpsData = true;
}
}
}
ss.end();
#ifdef DEBUG
if (newGpsData)
{
Serial.println(F("New GPS data available."));
}
else
{
Serial.println(F("No GPS fix available."));
}
#endif
}
void processGsm(void)
{
if (wismo.powerUp())
{
#ifdef DEBUG
Serial.println(F("GSM wakes up."));
#endif
// Retrieve time & date
if (wismo.getClock(clock))
{
#ifdef DEBUG
Serial.print("Current clock: ");
Serial.println(clock);
#endif
}
float flat, flon;
unsigned long age;
char coordinate[8];
gps.f_get_position(&flat, &flon, &age);
// Prepare data for Cosm feed
strcpy(data, stream1);
strcat(data, ",");
dtostrf(flat, 2, 4, coordinate);
strcat(data, coordinate);
strcat(data, "\r\n");
strcat(data, stream2);
strcat(data, ",");
dtostrf(flon, 2, 4, coordinate);
strcat(data, coordinate);
strcat(data, "\r\n");
// Terminate the string
strcat(data, "\0");
// Connect to GPRS network
if (wismo.openGPRS(apn, username, password))
{
#ifdef DEBUG
Serial.println(F("GPRS OK."));
Serial.println(F("Sending feed to Cosm."));
#endif
// Send current coordinate as Cosm feed through HTTP PUT request
if (wismo.putHttp(server, path, port, host, data, controlKey,
contentType))
{
#ifdef DEBUG
Serial.println(F("Feed sent!"));
#endif
}
else
{
#ifdef DEBUG
Serial.println(F("Unable to send feed."));
#endif
}
// Close the GPRS connection
if (wismo.closeGPRS())
{
#ifdef DEBUG
Serial.println(F("GPRS closed."));
#endif
}
}
else
{
#ifdef DEBUG
Serial.println(F("GPRS failed."));
#endif
}
}
wismo.shutdown();
}
void processLog()
{
// Open the file
logFile = SD.open("log.txt", FILE_WRITE);
// File is successfully opened
if (logFile)
{
#ifdef DEBUG
Serial.println(F("Logging..."));
#endif
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
// Log current time
logFile.print(clock);
logFile.print(",");
// Log latitude
logFile.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 2);
logFile.print(",");
// Log longitude
logFile.println(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 2);
// Close file
logFile.close();
#ifdef DEBUG
Serial.println(F("Logging complete."));
#endif
}
// File open failed
else
{
#ifdef DEBUG
Serial.println(F("Logging error."));
#endif
}
}