summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStanley Huang <stanleyhuangyc@gmail.com>2014-06-06 12:32:44 +1000
committerStanley Huang <stanleyhuangyc@gmail.com>2014-06-06 12:32:44 +1000
commit86fc8c5f37a90fa5991aa74053523a6e3b2e66b4 (patch)
treeee8c16c37de2020ddd775c79dff82ea0fb47dd97
parenta62d57e05b3396e1e5af126e6afa3b44f467a765 (diff)
download2021-arduino-obd-86fc8c5f37a90fa5991aa74053523a6e3b2e66b4.tar.gz
2021-arduino-obd-86fc8c5f37a90fa5991aa74053523a6e3b2e66b4.tar.bz2
2021-arduino-obd-86fc8c5f37a90fa5991aa74053523a6e3b2e66b4.zip
Update Mega Logger for new Kit with 3.2" display
-rw-r--r--megalogger/config.h41
-rw-r--r--megalogger/datalogger.h322
-rw-r--r--megalogger/megalogger.ino44
3 files changed, 107 insertions, 300 deletions
diff --git a/megalogger/config.h b/megalogger/config.h
index 45e1398..52e2e4f 100644
--- a/megalogger/config.h
+++ b/megalogger/config.h
@@ -8,14 +8,38 @@
#define OBD_PROTOCOL 0 /* 0 for auto */
/**************************************
-* Choose SD pin here
+* Data logging options
**************************************/
+// enable(1)/disable(0) data logging (if SD card is present)
+#define ENABLE_DATA_LOG 1
+
//#define SD_CS_PIN 4 // ethernet shield
-//#define SD_CS_PIN 7 // microduino
//#define SD_CS_PIN 10 // SD breakout
#define SD_CS_PIN SS
/**************************************
+* Data streaming options
+**************************************/
+// enable(1)/disable(0) data streaming
+#define ENABLE_DATA_OUT 1
+
+// uses software(1)/hardware(0) serial for data streaming
+#define USE_SOFTSERIAL 0
+
+// this defines the format of data streaming
+// FORMAT_BIN is required by Freematics OBD iOS App
+#define STREAM_FORMAT FORMAT_CSV
+
+/* Default streaming baudrates:
+ 9600bps for BLE
+ 38400bps for BT 2.1
+*/
+#define STREAM_BAUDRATE 9600
+
+// outputs debug information
+#define VERBOSE 0
+
+/**************************************
* Config GPS here
**************************************/
#define USE_GPS
@@ -31,22 +55,15 @@
/**************************************
* Timeout/interval options
**************************************/
-//#define OBD_MIN_INTERVAL 200 /* ms */
+#define OBD_MIN_INTERVAL 50 /* ms */
#define ACC_DATA_INTERVAL 200 /* ms */
#define GPS_DATA_TIMEOUT 2000 /* ms */
/**************************************
-* Data logging/streaming options
-**************************************/
-#define ENABLE_DATA_OUT 0
-#define ENABLE_DATA_LOG 0
-#define LOG_FORMAT FORMAT_CSV /* options: FORMAT_CSV, FORMAT_BIN */
-
-/**************************************
* LCD module (uncomment only one)
**************************************/
-//LCD_SSD1289 lcd; /* 3.2" SSD12389 based TFT LCD */
-LCD_ILI9325D lcd; /* 2.8" ILI9325 based TFT LCD */
+LCD_SSD1289 lcd; /* 3.2" SSD12389 based TFT LCD */
+//LCD_ILI9325D lcd; /* 2.8" ILI9325 based TFT LCD */
//LCD_ILI9341 lcd; /* 2.4" ILI9341 based SPI TFT LCD */
//LCD_Null lcd;
diff --git a/megalogger/datalogger.h b/megalogger/datalogger.h
index 8009a80..aae4034 100644
--- a/megalogger/datalogger.h
+++ b/megalogger/datalogger.h
@@ -6,76 +6,24 @@ typedef struct {
uint16_t pid;
uint8_t flags;
uint8_t checksum;
- float value;
-} LOG_DATA;
-
-typedef struct {
- uint32_t time;
- uint16_t pid;
- uint8_t flags;
- uint8_t checksum;
float value[3];
} LOG_DATA_COMM;
-typedef struct {
- uint32_t time; /* e.g. 1307281259 */
- uint16_t pid;
- uint8_t message;
- uint8_t checksum;
- uint16_t fileIndex;
- uint16_t fileSize; /* KB */
- uint16_t logFlags;
- uint8_t logType;
- uint8_t data[5];
-} LOG_DATA_FILE_INFO;
-
-typedef struct {
- uint32_t time;
- uint16_t pid;
- uint8_t message;
- uint8_t checksum;
- uint8_t data[12];
-} LOG_DATA_COMMAND;
-
-typedef struct {
- uint32_t id;
- uint32_t dataOffset;
- uint8_t ver;
- uint8_t logType;
- uint16_t flags;
- uint32_t dateTime; //4, YYMMDDHHMM, e.g. 1305291359
- /*
- uint8_t devid[8];
- uint8_t vin[24];
- uint8_t unused[84];
- */
-} HEADER;
-
#define HEADER_LEN 128 /* bytes */
-#define PID_GPS_COORDINATES 0xF00A
-#define PID_GPS_ALTITUDE 0xF00C
-#define PID_GPS_SPEED 0xF00D
-#define PID_GPS_HEADING 0xF00E
-#define PID_GPS_SAT_COUNT 0xF00F
-#define PID_GPS_TIME 0xF010
-
-#define PID_ACC 0xF020
-#define PID_GYRO 0xF021
+#define PID_GPS_LATITUDE 0xA
+#define PID_GPS_LONGITUDE 0xB
+#define PID_GPS_ALTITUDE 0xC
+#define PID_GPS_SPEED 0xD
+#define PID_GPS_HEADING 0xE
+#define PID_GPS_SAT_COUNT 0xF
+#define PID_GPS_TIME 0x10
+#define PID_GPS_DATE 0x11
-#define PID_MESSAGE 0xFE00
-#define PID_HEART_BEAT 0xFFEE
+#define PID_ACC 0x20
+#define PID_GYRO 0x21
-#define MSG_FILE_LIST_BEGIN 0x1
-#define MSG_FILE_LIST_END 0x2
-#define MSG_FILE_INFO 0x3
-#define MSG_FILE_REQUEST 0x4
-
-#if LOG_FORMAT == FORMAT_BIN
-#define FILE_NAME_FORMAT "/DAT%05d.LOG"
-#else
#define FILE_NAME_FORMAT "/DAT%05d.CSV"
-#endif
#if ENABLE_DATA_OUT
@@ -89,254 +37,105 @@ typedef struct {
SoftwareSerial SerialBLE(A2, A3); /* for BLE Shield on UNO/leonardo*/
#endif
-#define OUTPUT_BAUDRATE 9600
-
#else
-#define SerialBLE Serial
-#define OUTPUT_BAUDRATE 115200
+#define SerialBLE Serial3
+
+#endif
#endif
+#if ENABLE_DATA_LOG
+static File sdfile;
#endif
-void btInit(int baudrate);
-void btSend(byte* data, byte length);
+static const char* idstr = "FREEMATICS\r";
class CDataLogger {
public:
void initSender()
{
#if ENABLE_DATA_OUT
- SerialBLE.begin(OUTPUT_BAUDRATE);
+ SerialBLE.begin(STREAM_BAUDRATE);
+ SerialBLE.print(idstr);
#endif
-#if ENABLE_DATA_LOG && LOG_FORMAT == FORMAT_CSV
+#if ENABLE_DATA_LOG
m_lastDataTime = 0;
#endif
}
-#if ENABLE_DATA_OUT
- void sendFileInfo(File& file)
+ void logTimeElapsed()
{
- if (file.size() < HEADER_LEN) return;
-
- LOG_DATA_FILE_INFO info = {0};
- info.fileIndex = atol(file.name() + 3);
- if (info.fileIndex == 0) return;
-
- HEADER hdr;
- if (file.readBytes((char*)&hdr, sizeof(hdr)) != sizeof(hdr)) return;
-
- info.pid = PID_MESSAGE;
- info.message = MSG_FILE_INFO;
- info.fileSize = file.size();
- info.time = hdr.dateTime;
- info.logType = hdr.logType;
- info.logFlags = hdr.flags;
- info.checksum = getChecksum((char*)&info, sizeof(info));
- SerialBLE.write((uint8_t*)&info, sizeof(info));
- }
- void sendCommand(byte message, void* data = 0, byte bytes = 0)
- {
- LOG_DATA_COMMAND msg = {0, PID_MESSAGE, message};
- if (data) memcpy(msg.data, data, bytes);
- msg.checksum = getChecksum((char*)&msg, sizeof(msg));
- SerialBLE.write((uint8_t*)&msg, sizeof(msg));
- }
- bool receiveCommand(LOG_DATA_COMMAND& msg)
- {
- if (!SerialBLE.available())
- return false;
-
- if (SerialBLE.readBytes((char*)&msg, sizeof(msg)) != sizeof(msg))
- return false;
-
- uint8_t checksum = msg.checksum;
- msg.checksum = 0;
- if (getChecksum((char*)&msg, sizeof(msg)) != msg.checksum) {
- return false;
- }
- return true;
- }
-#endif
- void logData(uint16_t pid, int value)
- {
-#if LOG_FORMAT == FORMAT_BIN || STREAM_FORMAT == FORMAT_BIN
- LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value};
- ld.checksum = getChecksum((char*)&ld, 12);
-#endif
-#if ENABLE_DATA_OUT
-#if STREAM_FORMAT == FORMAT_BIN
- SerialBLE.write((uint8_t*)&ld, 12);
-#else
- SerialBLE.print(pid, HEX);
- SerialBLE.write(',');
- SerialBLE.print(value);
- SerialBLE.write('\n');
-#endif
-#endif
#if ENABLE_DATA_LOG
- if (!sdfile) return;
-#if LOG_FORMAT == FORMAT_BIN
- sdfile.write((uint8_t*)&ld, 12);
- dataSize += 12;
-#else
dataSize += sdfile.print(dataTime - m_lastDataTime);
sdfile.write(',');
- dataSize += sdfile.print(pid, HEX);
- sdfile.write(',');
- dataSize += sdfile.print(value);
- sdfile.write('\n');
- dataSize += 3;
+ dataSize++;
m_lastDataTime = dataTime;
#endif
-#endif
}
- void logData(uint16_t pid, float value)
+ void logData(char c)
{
-#if LOG_FORMAT == FORMAT_BIN || STREAM_FORMAT == FORMAT_BIN
- LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value};
- ld.checksum = getChecksum((char*)&ld, 12);
-#endif
-#if ENABLE_DATA_OUT
-#if STREAM_FORMAT == FORMAT_BIN
- SerialBLE.write((uint8_t*)&ld, 12);
-#else
- SerialBLE.print(pid, HEX);
- SerialBLE.write(',');
- SerialBLE.print(value);
- SerialBLE.write('\n');
-#endif
+#if ENABLE_DATA_OUT && STREAM_FORMAT == FORMAT_CSV
+ SerialBLE.write(c);
#endif
#if ENABLE_DATA_LOG
- if (!sdfile) return;
-#if LOG_FORMAT == FORMAT_BIN
- sdfile.write((uint8_t*)&ld, 12);
- dataSize += 12;
-#else
- dataSize += sdfile.print(dataTime - m_lastDataTime);
- sdfile.write(',');
- dataSize += sdfile.print(pid, HEX);
- sdfile.write(',');
- dataSize += sdfile.print(value);
- sdfile.write('\n');
- dataSize += 3;
- m_lastDataTime = dataTime;
-#endif
+ if (c >= ' ') {
+ sdfile.write(c);
+ dataSize++;
+ }
#endif
}
- void logData(uint16_t pid, float value1, float value2)
+ void logData(uint16_t pid, int value)
{
-#if LOG_FORMAT == FORMAT_BIN || STREAM_FORMAT == FORMAT_BIN
- LOG_DATA_COMM ld = {dataTime, pid, 2, 0, {value1, value2}};
- ld.checksum = getChecksum((char*)&ld, 16);
-#endif
+ char buf[16];
+ byte n = sprintf(buf, "%X,%d\r", pid, value);
#if ENABLE_DATA_OUT
#if STREAM_FORMAT == FORMAT_BIN
- SerialBLE.write((uint8_t*)&ld, 16);
+ LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value};
+ ld.checksum = getChecksum((char*)&ld, 12);
+ SerialBLE.write((uint8_t*)&ld, 12);
#else
- SerialBLE.print(pid, HEX);
- SerialBLE.write(',');
- SerialBLE.print(value1, 6);
- SerialBLE.write(',');
- SerialBLE.print(value2, 6);
- SerialBLE.write('\n');
+ SerialBLE.write((uint8_t*)buf, n);
#endif
#endif
#if ENABLE_DATA_LOG
- if (!sdfile) return;
-#if LOG_FORMAT == FORMAT_BIN
- sdfile.write((uint8_t*)&ld, 16);
- dataSize += 16;
-#else
- dataSize += sdfile.print(dataTime - m_lastDataTime);
- sdfile.write(',');
- dataSize += sdfile.print(pid, HEX);
- sdfile.write(',');
- dataSize += sdfile.print(value1, 6);
- sdfile.write(',');
- dataSize += sdfile.print(value2, 6);
- sdfile.write('\n');
- dataSize += 4;
- m_lastDataTime = dataTime;
-#endif
+ logTimeElapsed();
+ dataSize += sdfile.write((uint8_t*)buf, n);
#endif
}
- void logData(uint16_t pid, uint32_t value1, uint32_t value2)
+ void logData(uint16_t pid, int32_t value)
{
-#if LOG_FORMAT == FORMAT_BIN || STREAM_FORMAT == FORMAT_BIN
- LOG_DATA_COMM ld = {dataTime, pid, 2, 0, {value1, value2}};
- ld.checksum = getChecksum((char*)&ld, 16);
-#endif
+ char buf[20];
+ byte n = sprintf(buf, "%X,%ld\r", pid, value);
#if ENABLE_DATA_OUT
#if STREAM_FORMAT == FORMAT_BIN
- SerialBLE.write((uint8_t*)&ld, 16);
+ LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value};
+ ld.checksum = getChecksum((char*)&ld, 12);
+ SerialBLE.write((uint8_t*)&ld, 12);
#else
- SerialBLE.print(pid, HEX);
- SerialBLE.write(',');
- SerialBLE.print(value1);
- SerialBLE.write(',');
- SerialBLE.print(value2);
- SerialBLE.write('\n');
+ SerialBLE.write((uint8_t*)buf, n);
#endif
#endif
#if ENABLE_DATA_LOG
- if (!sdfile) return;
-#if LOG_FORMAT == FORMAT_BIN
- sdfile.write((uint8_t*)&ld, 16);
- dataSize += 16;
-#else
- dataSize += sdfile.print(dataTime - m_lastDataTime);
- sdfile.write(',');
- dataSize += sdfile.print(pid, HEX);
- sdfile.write(',');
- dataSize += sdfile.print(value1);
- sdfile.write(',');
- dataSize += sdfile.print(value2);
- sdfile.write('\n');
- dataSize += 4;
- m_lastDataTime = dataTime;
-#endif
+ logTimeElapsed();
+ dataSize += sdfile.write((uint8_t*)buf, n);
#endif
}
void logData(uint16_t pid, int value1, int value2, int value3)
{
-#if LOG_FORMAT == FORMAT_BIN || STREAM_FORMAT == FORMAT_BIN
- LOG_DATA_COMM ld = {dataTime, pid, 3, 0, {value1, value2, value3}};
- ld.checksum = getChecksum((char*)&ld, 20);
-#endif
+ char buf[24];
+ byte n = sprintf(buf, "%X,%d,%d,%d\r", pid, value1, value2, value3);
#if ENABLE_DATA_OUT
#if STREAM_FORMAT == FORMAT_BIN
+ LOG_DATA_COMM ld = {dataTime, pid, 3, 0, {value1, value2, value3}};
+ ld.checksum = getChecksum((char*)&ld, 20);
SerialBLE.write((uint8_t*)&ld, 20);
#else
- SerialBLE.print(pid, HEX);
- SerialBLE.write(',');
- SerialBLE.print(value1);
- SerialBLE.write(',');
- SerialBLE.print(value2);
- SerialBLE.write(',');
- SerialBLE.print(value3);
- SerialBLE.write('\n');
+ SerialBLE.write((uint8_t*)buf, n);
#endif
#endif
#if ENABLE_DATA_LOG
- if (!sdfile) return;
-#if LOG_FORMAT == FORMAT_BIN
- sdfile.write((uint8_t*)&ld, 20);
- dataSize += 20;
-#else
- dataSize += sdfile.print(dataTime - m_lastDataTime);
- sdfile.write(',');
- dataSize += sdfile.print(pid, HEX);
- sdfile.write(',');
- dataSize += sdfile.print(value1);
- sdfile.write(',');
- dataSize += sdfile.print(value2);
- sdfile.write(',');
- dataSize += sdfile.print(value3);
- sdfile.write('\n');
- dataSize += 5;
- m_lastDataTime = dataTime;
-#endif
+ logTimeElapsed();
+ dataSize += sdfile.write((uint8_t*)buf, n);
#endif
}
#if ENABLE_DATA_LOG
@@ -365,13 +164,7 @@ public:
return 0;
}
-#if LOG_FORMAT == FORMAT_BIN
- HEADER hdr = {'UDUS', HEADER_LEN, 1, 0, logFlags, dateTime};
- sdfile.write((uint8_t*)&hdr, sizeof(hdr));
- for (byte i = 0; i < HEADER_LEN - sizeof(hdr); i++)
- sdfile.write((uint8_t)0);
- dataSize = HEADER_LEN;
-#endif
+ dataSize = sdfile.print(idstr);
return fileIndex;
}
void closeFile()
@@ -380,13 +173,13 @@ public:
}
void flushFile()
{
- if (sdfile) sdfile.flush();
+ sdfile.flush();
}
#endif
uint32_t dataTime;
uint32_t dataSize;
private:
- static byte getChecksum(char* buffer, byte len)
+ byte getChecksum(char* buffer, byte len)
{
uint8_t checksum = 0;
for (byte i = 0; i < len; i++) {
@@ -395,9 +188,6 @@ private:
return checksum;
}
#if ENABLE_DATA_LOG
- File sdfile;
-#if LOG_FORMAT == FORMAT_CSV
uint32_t m_lastDataTime;
#endif
-#endif
};
diff --git a/megalogger/megalogger.ino b/megalogger/megalogger.ino
index 4962c12..387ce9e 100644
--- a/megalogger/megalogger.ino
+++ b/megalogger/megalogger.ino
@@ -124,13 +124,15 @@ public:
lcd.printInt(index);
#endif
+#if 0
showECUCap();
delay(1000);
-
- read(PID_DISTANCE, (int&)startDistance);
+#endif
initScreen();
+ read(PID_DISTANCE, (int&)startDistance);
+
lastRefreshTime = millis() >> 10;
}
void loop()
@@ -299,17 +301,16 @@ private:
return;
// parsed GPS data is ready
- static uint32_t lastAltTime = 0;
uint32_t time;
uint32_t date;
dataTime = millis();
gps.get_datetime(&date, &time, 0);
- logData(PID_GPS_TIME, time, date);
+ logData(PID_GPS_TIME, (int32_t)time);
- float speed = gps.speed() * 1852 / 100000;
- logData(PID_GPS_SPEED, speed);
+ int kph = gps.speed() * 1852 / 100000;
+ logData(PID_GPS_SPEED, kph);
// no need to log GPS data when vehicle has not been moving
// that's when previous speed is zero and current speed is also zero
@@ -318,13 +319,11 @@ private:
// lastSpeed will be updated
//ShowSensorData(PID_SPEED, speed);
- long lat, lon;
+ int32_t lat, lon;
gps.get_position(&lat, &lon, 0);
- logData(PID_GPS_COORDINATES, (float)lat / 100000, (float)lon / 100000);
-
- if (dataTime - lastAltTime > 10000) {
- logData(PID_GPS_ALTITUDE, (float)gps.altitude());
- }
+ logData(PID_GPS_LATITUDE, lat);
+ logData(PID_GPS_LONGITUDE, lon);
+ logData(PID_GPS_ALTITUDE, (int)(gps.altitude() / 100));
lcd.setFontSize(FONT_SIZE_MEDIUM);
char buf[16];
@@ -397,7 +396,9 @@ private:
// read OBD-II data
int value;
- if (!read(pid, value)) {
+ sendQuery(pid);
+ pid = 0;
+ if (!getResult(pid, value)) {
return;
}
@@ -618,8 +619,13 @@ void btSend(byte* data, byte length)
void setup()
{
- Serial.begin(115200);
- Serial.println("MegaLogger");
+ lcd.begin();
+ lcd.setFontSize(FONT_SIZE_MEDIUM);
+ lcd.backlight(true);
+ lcd.setColor(0xFFE0);
+ lcd.print("MEGA LOGGER - OBD-II/GPS/G-FORCE");
+ lcd.setColor(RGB16_WHITE);
+
#ifdef GPSUART
#ifdef GPS_OPEN_BAUDRATE
GPSUART.begin(GPS_OPEN_BAUDRATE);
@@ -635,13 +641,7 @@ void setup()
logger.begin();
logger.initSender();
-
- lcd.begin();
- lcd.setFontSize(FONT_SIZE_MEDIUM);
- lcd.backlight(true);
- lcd.setColor(0xFFE0);
- lcd.print("MEGA LOGGER - OBD-II/GPS/G-FORCE");
- lcd.setColor(RGB16_WHITE);
+ delay(500);
logger.checkSD();
logger.setup();