Giter Site home page Giter Site logo

eltonvs / kotlin-obd-api Goto Github PK

View Code? Open in Web Editor NEW
193.0 12.0 33.0 222 KB

:blue_car: A Kotlin OBD-II API for reading engine data

License: Apache License 2.0

Kotlin 100.00%
obd obd2 onboard-diagnostics car kotlin kotlin-library ioiv iot hacktoberfest

kotlin-obd-api's Introduction

Kotlin OBD API

GitHub release CI Status Maintainability codebeat badge GitHub license Open Source

A lightweight and developer-driven API to query and parse OBD commands.

Written in pure Kotlin and platform agnostic with a simple and easy to use interface, so you can hack your car without any hassle. ๐Ÿš™

This is a flexible API that allows developers to plug-in to any connection interface (Bluetooth, Wifi, USB...). By default we use an ObdDeviceConnection that receives an InputStream and an OutputStream as parameters (so if you can get this from your connection interface, you're good to go ๐Ÿ‘).

Installation

Gradle

In your root build.gradle file, at the end of repositories:

repositories {
  ...
  maven { url 'https://jitpack.io' }
}

Add the dependency

dependencies {
  ...

  // Kolin OBD API
  implementation 'com.github.eltonvs:kotlin-obd-api:1.3.0'
}

Maven

Add jitpack to the repositories section

<repositories>
  <repository>
      <id>jitpack.io</id>
      <url>https://jitpack.io</url>
  </repository>
</repositories>

Add the dependency

<dependency>
  <groupId>com.github.eltonvs</groupId>
  <artifactId>kotlin-obd-api</artifactId>
  <version>1.3.0</version>
</dependency>

Manual

You can download a jar from GitHub's releases page.

Basic Usage

Get an InputStream and an OutputStream from your connection interface and create an ObdDeviceConnection instance.

// Create ObdDeviceConnection instance
val obdConnection = ObdDeviceConnection(inputStream, outputStream)

With this, you're ready to run any command you want, just pass the command instance to the .run method. This command accepts 3 parameters: command, useCache (default = false) and delayTime (default = 0).

// Retrieving OBD Speed Command
val response = obdConnection.run(SpeedCommand())

// Using cache (use with caution)
val cachedResponse = obdConnection.run(VINCommand(), useCache = true)

// With a delay time - with this, the API will wait 500ms after executing the command
val delayedResponse = obdConnection(RPMCommand(), delayTime = 500L)

The retuned object is a ObdResponse and has the following attributes:

Attribute Type Description
command ObdCommand The command passed to the run method
rawResponse ObdRawResponse This class holds the raw data returned from the car
value String The parsed value
unit String The unit from the parsed value (e.g.: Km/h, RPM, ...

The ObdRawResponse has the following attributes:

Attribute Type Description
value String The raw value (hex)
elapsedTime Long The elapsed time (in milliseconds) to run the command
processedValue String The raw (hex) value without whitespaces, colons or any other "noise"
bufferedValue IntArray The raw (hex) value as a IntArray

Extending the library

It's easy to add a custom command using this library, all you need to do is create a class extending the ObdCommand class and overriding the following methods:

class CustomCommand : ObdCommand() {
    // Required
    override val tag = "CUSTOM_COMMAND"
    override val name "Custom Command"
    override val mode = "01"
    override val pid = "FF"

    // Optional
    override val defaultUnit = ""
    override val handler = { it: ObdRawResponse -> "Calculations to parse value from ${it.processedValue}" }
}

Commands

Here are a handul list of the main supported commands (sensors). For a full list, see here.

  • Available Commands
  • Vehicle Speed
  • Engine RPM
  • DTC Number
  • Trouble Codes (Current, Pending and Permanent)
  • Throttle Position
  • Fuel Pressure
  • Timing Advance
  • Intake Air Temperature
  • Mass Air Flow Rate (MAF)
  • Engine Run Time
  • Fuel Level Input
  • MIL ON/OFF
  • Vehicle Identification Number (VIN)

NOTE: Support for those commands will vary from car to car.

Contributing

Want to help or have something to add to the repo? problem on a specific feature?

  • Open an issue to explain the issue you want to solve Open an issue
  • After discussion to validate your ideas, you can open a PR or even a draft PR if the contribution is a big one Current PRs

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Elton Viana - Initial work - Also created the java-obd-api

See also the list of contributors who participated in this project.

License

This project is licensed under the Apache 2.0 License - See the LICENCE file for more details.

Acknowledgments

  • Paulo Pires - Creator of the obd-java-api, on which the initial steps were based.
  • SmartMetropolis Project (Digital Metropolis Institute - UFRN, Brazil) - Backed and sponsored the project development during the initial steps.
  • Ivanovitch Silva - Helped a lot during the initial steps and with the OBD research.

kotlin-obd-api's People

Contributors

eltonvs avatar meriemi-git avatar ojacquemart avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kotlin-obd-api's Issues

Issue with troublecodes

Hi when i query my car (Jaguar F-Pace) with mode 03 and mode 07 i get following response.

03
4300 4300
07
4700 4700

I am not sure but i belive that car responds 2 frames from 2 ecus (7E8,7E9).
Seems that kotlin-OBD-api cannot handle that well as it converts result into DTC's bul obviously there are no DTC's.

TroubleCodesCommand() turns 43004300 into C0300 DTC
PendingTroubleCodesCommand() turns 47004700 into C0700 DTC

Would there be an easy fix for it or i will need to figure out whole new commands to decode response properly ?

Need for an initialization of the ELM to setup expected settings

Hello,

My vehicle follow the CAN protocol ISO 15765-4 (according to what is detected by a commercial obd app that successfully read my VIN)

When reading the VIN code by using kotlin-obd-api (connection via a bluetooth obd adapter), the VIN fails to be correctly decoded.
I get the formatted value: @ tcD$4DTdt๏ฟฝ#3
instead of expected VIN: WF0DABCDEFGH01234

The raw data transmitted are

09 02\r
014\r
0: 49 02 01 57 46 30\r
1: 44 41 42 43 44 45 46\r
2: 47 48 30 31 32 33 34\r

and the value in the ObdRawResponse is: 09 020140: 49 02 01 57 46 301: 44 41 42 43 44 45 462: 47 48 30 31 32 33 34

I notice that in the implementation Control.kt: parseVIN, there is nothing that discards the first two rows that corresponds to the command and the message bytes count.(if I change the replacement criteria to "^.{7}0:49020.".toRegex(), I get the expected VIN.

I didn't find any documentation that talks about the repeated command (first line 09 02 of the received message), either in the examples of the ELM327 chip documentation there is no mention to that, except that the message format can vary from one vehicle to another.

There is always the command repeated in the received message from my bluetooth interface.
Should I implement this case in this lib or maybe this is specific to my interface so I have to manage it on the bluetooth adapter side?

OBDRawResponse -> value: Wrong conversion?

Hello guys,
The output of my OBD Commands dont match the reality when requesting the value of it. In case of the OBDRawResponses however the output seems correct.
Here is a comparison of the value and raw response for RPM and Vehicle Speed.

val obdConnection = ObdDeviceConnection(inputStream, outputStream)
val response = obdConnection.run(RPMCommand())
val output = response.value
Log.i("data", "RPM: $output")

RPM with real RPM of 0
response.value:
RPM: 272826368
response.rawResponse:
RPM: ObdRawResponse(value=01 0C41 0C 00 00, elapsedTime=320)

VehicleSpeed with real speed of 0
response.value:
Speed: 65
response.rawResponse:
Speed: ObdRawResponse(value=01 0D41 0D 00, elapsedTime=319)

When manually converting the raw response this seems to be correct. But for response.value i cant figure out why i get those strange values. Seems like the API doesnt convert the raw response correctly.
Can someone help me out to fix this issue?

1.2.0 always return same data?

After I up the version to 1.2.0.
SpeedCommand always return first data, same value .
Can you check this.
I already check the rawResponse, it not the same, looks like some issue from bytetoInt(). It always return first byte or something ?

Sample app?

Please could you put a sample app? Im trying to use the sample at the presentation page:

        val obdConnection = ObdDeviceConnection(socket.inputStream, socket.outputStream)
        val response = obdConnection.run(SpeedCommand())

But it looks like wrong by compiler for the "obdConnection.run()". Error showed is:
Suspend function 'run' should be called only from a coroutine or another suspend function

NonNumericResponseException while executing command [SPEED], response []

Hi Elton,
i'm trying to use your library in my android project. my project is in java, but i created a kotlin class for your library.
here is my kotlin class:
obdThread

everytime i run this code it gives me a NonNumericResponseException. it happens with all of the commands.
can you please help me?

Thanks, Doron.

BLE SUPPORT

Can this library make ble support? I saw an ios obd library which is supports ble. Can we send obd command and get result without input and output streams.

TroubleCode list not working properly - FATAL EXCEPTION: DefaultDispatcher-worker-2 Process: com.example.kotlinobd, PID: 21452 java.lang.NumberFormatException: For input string: " "

FATAL EXCEPTION: DefaultDispatcher-worker-2 Process: com.example.kotlinobd, PID: 21452 java.lang.NumberFormatException: For input string: " " at java.lang.Integer.parseInt(Integer.java:733) at com.github.eltonvs.obd.command.control.BaseTroubleCodesCommand$parseTroubleCodesList$troubleCodesList$1.invoke(TroubleCodes.kt:88) at com.github.eltonvs.obd.command.control.BaseTroubleCodesCommand$parseTroubleCodesList$troubleCodesList$1.invoke(TroubleCodes.kt:87) at kotlin.text.StringsKt___StringsKt.windowed(_Strings.kt:2359) at kotlin.text.StringsKt___StringsKt.chunked(_Strings.kt:2236) at com.github.eltonvs.obd.command.control.BaseTroubleCodesCommand.parseTroubleCodesList(TroubleCodes.kt:87) at com.github.eltonvs.obd.command.control.BaseTroubleCodesCommand.access$parseTroubleCodesList(TroubleCodes.kt:54) at com.github.eltonvs.obd.command.control.BaseTroubleCodesCommand$handler$1.invoke(TroubleCodes.kt:57) at com.github.eltonvs.obd.command.control.BaseTroubleCodesCommand$handler$1.invoke(TroubleCodes.kt:57) at com.github.eltonvs.obd.command.ObdCommand.handleResponse(ObdCommand.kt:23) at com.github.eltonvs.obd.connection.ObdDeviceConnection$run$2.invokeSuspend(ObdDeviceConnection.kt:36) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:279) at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85) at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59) at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source:1) at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38) at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source:1) at com.github.eltonvs.obd.connection.ObdDeviceConnection.run(ObdDeviceConnection.kt:28) at com.github.eltonvs.obd.connection.ObdDeviceConnection.run$default(ObdDeviceConnection.kt:23) at com.example.kotlinobd.MainConnectionActivity$retriveData$latestNews$1.invokeSuspend(MainConnectionActivity.kt:505) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:749) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664) Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@3b8b203, Dispatchers.Default]

readable values

how to make values of Vehicle Speed readable values
Vehicle Speed="1091308888Km/h" !!

Where is the release jar file?

i can't find a jar file from Github's releases page,there is only source code in there.And also, i can't find any user interface code in source code ,Did I make a mistake? Thanks.

New release 1.2.0 is missing from repositories

I'm trying to use 1.2.0

here is the error I'm getting


Execution failed for task ':app:checkDebugDuplicateClasses'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
   > Could not find com.github.eltonvs:kotlin-obd-api:1.2.0.
     Searched in the following locations:
       - https://dl.google.com/dl/android/maven2/com/github/eltonvs/kotlin-obd-api/1.2.0/kotlin-obd-api-1.2.0.pom
       - https://jcenter.bintray.com/com/github/eltonvs/kotlin-obd-api/1.2.0/kotlin-obd-api-1.2.0.pom
       - https://repo.maven.apache.org/maven2/com/github/eltonvs/kotlin-obd-api/1.2.0/kotlin-obd-api-1.2.0.pom
     Required by:
         project :app

Thanks!

NonNumericResponse Exception 1.2.0

Im getting the same error that was fix in version 1.1.2 when trying the speed command.
Just to make sure im installing the 1.2.0 version great im implementing on dependencies implementation 'com.github.eltonvs:kotlin-obd-api:1.2.0' and on setting.graddle im establishing maven { url 'https://jitpack.io' }.
The bluetooth connection is established correctly but when i send this commands it throws the exception.

Did i install the libraries correctly? I also tried to add the jar file and delete the line 'com.github.eltonvs:kotlin-obd-api:1.2.0' but it happens the same exception so im a bit lost.

@eltonvs

Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.