Skip to content

Instantly share code, notes, and snippets.

@rocketjosh
Last active October 28, 2017 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rocketjosh/f72584383d198f346938a57dade94c7f to your computer and use it in GitHub Desktop.
Save rocketjosh/f72584383d198f346938a57dade94c7f to your computer and use it in GitHub Desktop.
/*
* Example based on code found here.
* https://github.com/iwanders/OBD9141
*
* Put the "OBD9141 in Libraries folder.
*
*/
#include "OBD9141.h"
// The RX pin of the Serial1 connected to 9141 K RX of tranciever.
#define RX_PIN LIN_KRX
// The Tx pin of the Serial1 connected to 9141 K TX of tranciever.
#define TX_PIN LIN_KTX
// The Enable pin connected to 9141 SLP pin of tranciever.
#define EN_PIN LIN_KSLP
#define OBD9141_DEBUG
OBD9141 obd;
void setup(){
SerialUSB.begin(115200);
delay(2000);
pinMode(EN_PIN, OUTPUT);
digitalWrite(EN_PIN, HIGH);
pinMode(PS_J1850_9141, OUTPUT);
digitalWrite(PS_J1850_9141, HIGH);
obd.begin(Serial1, RX_PIN, TX_PIN);
}
void loop(){
SerialUSB.println("Looping");
// obd.set_port(false);
bool init_success = obd.init();
SerialUSB.print("init_success:");
SerialUSB.println(init_success);
// init_success = true;
// Uncomment this line if you use the simulator to force the init to be
// interpreted as successful. With an actual ECU; be sure that the init is
// succesful before trying to request PID's.
if (init_success){
bool res = true;
while(res){
SerialUSB.println(obd.readUint8());
res = obd.getCurrentPID(0x11, 1);
if (res){
SerialUSB.print("Result 0x11 (throttle): ");
SerialUSB.println(obd.readUint8());
}
res = obd.getCurrentPID(0x0C, 2);
if (res){
SerialUSB.print("Result 0x0C (RPM): ");
SerialUSB.println(obd.readUint16()/4);
}
res = obd.getCurrentPID(0x0D, 1);
if (res){
SerialUSB.print("Result 0x0D (speed): ");
SerialUSB.println(obd.readUint8());
}
SerialUSB.println();
delay(200);
}
}
delay(3000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment