-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSimpleArduinoObd.ino
More file actions
112 lines (93 loc) · 2.36 KB
/
SimpleArduinoObd.ino
File metadata and controls
112 lines (93 loc) · 2.36 KB
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
// Connect to BT OBDII
// Check for connection
// Start getting data
#define DEBUG 1
#include <SD.h>
#include <SPI.h>
#include "FileIO.h"
#include "GpsHelper.h"
#include "ObdHelper.h"
#define SD_SS_PIN 53
#define DELAY 1000 // delay between commands in milliseconds
static bool sendToGraph = false;
unsigned long timer = 0;
// Reset function
void (* ResetFunction) (void) = 0;
void setup()
{
// Setup Comms
Serial.begin(115200);
// Setup SdCard
InitialiseSdCard(SD_SS_PIN);
// Setup Gps
InitialiseGps();
// Connect to OBD
if(!InitBluetoothComms())
{
#if DEBUG
Serial.print(F("Failed to connect to OBD"));
#endif
// Sometimes works if it is reset
ResetFunction();
while(1);
}
#if DEBUG
Serial.println(F("Bluetooth OK"));
Serial.println(F("Waiting for GPS signal..."));
#endif
// Wait for valid GPS signal
while(!IsGpsDataAvailable(false));
// Get GPS data
#if DEBUG
Serial.println(F("Acquiring GPS data..."));
#endif
GetGpsData(¤tGpsData);
// Set file name
#if DEBUG
Serial.println(F("Setting filename..."));
#endif
SetFileName(
currentGpsData.year,
currentGpsData.month,
currentGpsData.day,
currentGpsData.hour,
currentGpsData.minute);
#if DEBUG
Serial.println(F("Now let's rock and roll!"));
#endif
timer = millis();
// Bluetooth - ok
// SD - ok
// FileName - ok
// Now start collecting data
}
void loop()
{
// Wait for valid GPS data - Should be much less than 1 sec
while(!IsGpsDataAvailable(false));
GetGpsData(¤tGpsData);
// Get OBD data
GetObdReadings(¤tObdReading);
// Save to file
memset(entryArray, 0, sizeof(entryArray));
char latChar[10] = {0};
dtostrf(currentGpsData.lat, 4, 6, latChar);
char lonChar[10] = {0};
dtostrf(currentGpsData.lon, 4, 6, lonChar);
sprintf(entryArray, entryFormat,
currentGpsData.day, currentGpsData.month, currentGpsData.year,
currentGpsData.hour, currentGpsData.minute, currentGpsData.second,
latChar, lonChar,
currentObdReading.RPM, currentObdReading.Speed, currentObdReading.Throttle);
bool entrySuccess = WriteToFile(entryArray, sizeof(entryArray));
#if DEBUG
Serial.println(entryArray);
if(entrySuccess)
Serial.println("Entry recorded.");
else
Serial.println("Entry failed");
#endif
// Wait until next log interval
while(millis() - timer < DELAY);
timer = millis();
}