Not quite logging to SD in this code.. that part is in there but is not in use. Still working on KWP2000 & Zeitronix. Zeitronix bit is done and working well.
Here’s the Arduino / C++ code for capturing the Zeitronix stuff. I’m not bothering with the 3 byte start sequence in the output
#include
#include
#include
#include
#include
#include
const int chipSelect = 10;
int gotPacket = 0;
char packet[11];
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
Serial1.begin(9600);
Serial1.setTimeout(100);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void getZeitronixPacket(char* packetpointer){
gotPacket = 0;
char buffer[32] = {0xF}; // buffer 32 bytes to make sure we find the 3 byte start sequence.
for (int i=0; i<32; i++){
Serial1.readBytes(&buffer[i],1);
if (i > 1 && buffer[i] == 2 && buffer[i-1] == 1 && buffer[i-2] == 0) {
gotPacket = 1;
Serial1.readBytes(packetpointer,11);
return;
}
}
}
void exampleStuff() {
// make a string for assembling the data to log:
String dataString = String(millis()) += ",";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
void loop() {
char dataString[80];
while (Serial1.available() > 32){ // wait til there's a nice chunk in the serial buffer
getZeitronixPacket(packet);
// this would be our logtosd() below:
if (gotPacket == 1) {
sprintf(dataString, "%lu,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u", millis(),packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7],packet[8],packet[9],packet[10]);
Serial.println(dataString);
}
}
// Serial.print(".");
}
Comments (0)