Go Down

Topic: ELMduino - Library for Car Hacking (Read 101 times) previous topic - next topic

Power_Broker

If you want to start car hacking through your OBD-II port using a bluetooth scanner - ELMduino is for you.

ELMduino download and GitHub Link



You can use this library to interface with OBD-II scanners such as this common one and will work with any car that has an OBD-II port:



And you can use it to query any pieces of data as specified by the OBD-II public PIDs. Here is a list of PIDs supported. Some notables include speed, rpm, MAF pressure, etc.



Below is an example code that will print speed and rpm data from a car.

Code: [Select]

#include "ELMduino.h"




const float KMPH_MPH_CONVERT = 0.6213711922;
const uint8_t SAMPLE_PERIOD = 100;




ELM327 myELM327;

enum fsm{
  get_speed,
  get_rpm};
fsm state = get_rpm;




float rpm;
float speed_kmph;
float speed_mph;
uint64_t currentTime = millis();
uint64_t previousTime = currentTime;





void setup()
{
  Serial.begin(115200);
  Serial3.begin(115200);

  // wait a bit for the ELM327 to come online
  delay(2000);

  // connect to ELM327
  while(!myELM327.begin(Serial3))
  {
    Serial.println("Couldn't connect to ELM327, trying again...");
    delay(1000);
  }
}




void loop()
{
  // only query the ELM327 every so often
  currentTime = millis();
  if((currentTime - previousTime) >= SAMPLE_PERIOD)
  {
    previousTime += SAMPLE_PERIOD;

    switch(state)
    {
      case get_rpm:
        if(myELM327.queryRPM(rpm))
        {
          rpm = rpm / 4.0; // necessary conversion factor based off OBD-II standard
          Serial.print(rpm); Serial.print(" "); Serial.println(speed_mph);
        }
        else
          Serial.println("\tTimeout");
        state = get_speed;
        break;
       
      case get_speed:
        if(myELM327.querySpeed(speed_kmph))
        {
          speed_mph = speed_kmph * KMPH_MPH_CONVERT;
          Serial.print(rpm); Serial.print(" "); Serial.println(speed_mph);
        }
        else
          Serial.println("\tTimeout");
        state = get_rpm;
        break;
    }
  }

  // extra processing here:
}






Note that you will need to connect to the bluetooth scanner using an HC-05 or other bluetooth to UART converter.
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Go Up