PowerBroker2 / ELMduino

Arduino OBD-II Bluetooth Scanner Interface Library for Car Hacking Projects
MIT License
649 stars 124 forks source link

Odometer value #173

Open vizziniroberto71 opened 1 year ago

vizziniroberto71 commented 1 year ago

HI, I'm using your library for a project and it's been very useful, really nice work! I also wanted to enter the value of the kilometers traveled (odometer), but I can't find the entry in the library. Am I looking wrong or hasn't been implemented? I checked on Wikipedia the formula for the calculation (PID 166) but not being an expert I don't know how to implement it. Is there a way to extract the value using the library, or is this a function you would have to implement from scratch? Thank you. Robert.

patfelst commented 1 year ago

you'll have to code it yourself. Just copy one of the other PID functions, and replace the PID with 166. Something like this (I've not tested it):

uint8_t ELM327::odometer()
{
    return (uint8_t)processPID(SERVICE_01, 166, 1, 4, 0.1);

}

Note that it returns 4 bytes, and you need to divide the result by 10 (that's what the 0.1 scale factor is for)

vizziniroberto71 commented 1 year ago

Hi Patrick, thanks for your suggestion, I modified the library to also read the odometer value, but when compiling it returns the following error: c:\Users\Administrator\Documents\Arduino\libraries\ELMDuino\src\ELMduino.cpp: In member function 'uint32_t ELM327::supportedPIDs_161_192()': c:\Users\Administrator\Documents\Arduino\libraries\ELMDuino\src\ELMduino.cpp:2261:42: error: 'SUPPORTED_PIDS_161_192' was not declared in this scope return (uint32_t)processPID(SERVICE_01, SUPPORTED_PIDS_161_192, 1, 4); ^~~~~~ c:\Users\Administrator\Documents\Arduino\libraries\ELMDuino\src\ELMduino.cpp:2261:42: note: suggested alternative: 'SUPPORTED_PIDS_61_80' return (uint32_t)processPID(SERVICE_01, SUPPORTED_PIDS_161_192, 1, 4); ^~~~~~ SUPPORTED_PIDS_61_80 c:\Users\Administrator\Documents\Arduino\libraries\ELMDuino\src\ELMduino.cpp: In member function 'uint32_t ELM327::odometer()': c:\Users\Administrator\Documents\Arduino\libraries\ELMDuino\src\ELMduino.cpp:2284:42: error: 'ODOMETER' was not declared in this scope return (uint32_t)processPID(SERVICE_01, ODOMETER, 1, 4, 1.0 / 10.0);

this error is returned at compile time. The changes made on the two library files are: `uint32_t supportedPIDs_161_192();

uint32_t odometer();` these lines have been inserted in the elmduino.h file!

In the elmduino.cpp file i inserted:

uint32_t ELM327::supportedPIDs_161_192() { return (uint32_t)processPID(SERVICE_01, SUPPORTED_PIDS_161_192, 1, 4); }

uint32_t ELM327::odometer() { return (uint32_t)processPID(SERVICE_01, ODOMETER, 1, 4, 1.0 / 10.0); }

I'm sorry if I bother you for this triviality, but I can't understand the error and it is the last step of the project that is still missing. Thank you.

Roberto.

patfelst commented 1 year ago

I wouldn't worry about the uint32_t supportedPIDs_161_192(), it's not needed, for the moment, lets just assume your vehicle supports reporting of odometer.

Also you've referenced ODOMETER when it hasn't been defined in the .h file. To start with, just hard code everything as per my example, if it works, then maybe it's worth defining ODOMETER constant etc. I assumed you knew you needed to add the function prototype to the class.

In the .h file where all the other functions are defined - it doesn't really matter where - but I suggest here https://github.com/PowerBroker2/ELMduino/blob/eabbb1a2e8e82d8fed1b758c0d893bc46efbe171/src/ELMduino.h#L410 add uint8_t odometer();

and paste in the original function I provided in the .cpp file.

that should be all you need to do.

vizziniroberto71 commented 1 year ago

Hi Patrick, thank you very much for your support, I compiled the sketch according to your suggestions and the error disappeared, I can't see the data on the display but there must be some error in the Nextion code. Thank you. Roberto.

Il giorno lun 17 lug 2023 alle ore 14:10 Patrick Felstead < @.***> ha scritto:

I wouldn't worry about the uint32_t supportedPIDs_161_192(), it's not needed, for the moment, lets just assume your vehicle supports reporting of odometer.

Also you've referenced ODOMETER when it hasn't been defined in the .h file. To start with, just hard code everything as per my example, if it works, then maybe it's worth defining ODOMETER constant etc. I assumed you knew you needed to add the function prototype to the class.

In the .h file where all the other functions are defined - it doesn't really matter where - but I suggest here

https://github.com/PowerBroker2/ELMduino/blob/eabbb1a2e8e82d8fed1b758c0d893bc46efbe171/src/ELMduino.h#L410 add uint8_t odometer();

and paste in the original function I provided in the .cpp file.

that should be all you need to do.

— Reply to this email directly, view it on GitHub https://github.com/PowerBroker2/ELMduino/issues/173#issuecomment-1638000558, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE4HNSUBY6VKODSTD2RLB53XQUTUXANCNFSM6AAAAAA2LZM3G4 . You are receiving this because you are subscribed to this thread.Message ID: @.***>

patfelst commented 1 year ago

try turning debug on to see what your vehicle is reporting - this will be independent of your Nextion display.

have a look at the multiple PIDs example for how to turn debug on https://github.com/PowerBroker2/ELMduino/blob/master/examples/multiple_pids/multiple_pids.ino

vizziniroberto71 commented 1 year ago

Thanks again Patrick, I will try your suggestion these days. See you soon.

Roberto.

Il giorno mer 26 lug 2023 alle ore 14:18 Patrick Felstead @.***> ha scritto:

try turning debug on to see what your vehicle is reporting - this will be independent of your Nextion display.

have a look at the multiple PIDs example for how to turn debug on https://github.com/PowerBroker2/ELMduino/blob/master/examples/multiple_pids/multiple_pids.ino

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

jimwhitelaw commented 10 months ago

I have created a new method to get the odometer reading and an example program to run it. It should be working (works with an emulator) but I'm unable to properly test it as I don't have a vehicle that supports the OBDII spec PID ("A6") to test it with. If someone wants to have a go at it, it's on the "odometer" branch of my ELMduino fork. https://github.com/jimwhitelaw/ELMduino.git