evanshortiss / obd-parser

Write commands to a generic OBDII connection and parse responses
Other
37 stars 17 forks source link

Bluetooth Connector #3

Open dominicrico opened 7 years ago

dominicrico commented 7 years ago

Hey,

great work with the OBD parser!

I have written a bluetooth connector for your module.

https://github.com/dominicrico/obd-parser-bluetooth-connection

Cheers!

evanshortiss commented 7 years ago

This is great! I still haven't developed all the other tooling enough to use on my raspberry pi yet, but was going to need bluetooth once I'm happy with the CLI and parser.

I'm hoping to push a decent size update for obd-parser and obd-parser-cli soon. When I do I will add a link to your module and probably end up using it 😄

dominicrico commented 7 years ago

You are very welcome!

I'm also using it for a Pi project. Great work, good to hear that you will update it!

evanshortiss commented 7 years ago

@dominicrico nice, let me know if you will blog it somewhere!

I pushed a new release last night for the CLI and parser. Both now v0.3.0. The "monitor" (poller.startPolling) command should be working better now. I think I may have broken the "poll" command since I was too aggressive with the timeout for it, will patch in 0.3.1 this evening hopefully.

dominicrico commented 7 years ago

Awesome! The monitor stopped working after some calls in my process. Would be nice if that will work now!

evanshortiss commented 7 years ago

@dominicrico yeah if you use the new CLI it definitely works. CLI now uses obd-parser@0.3.0 so if you use 0.3.0 it should be good 👍

Tested last night on a drive and collected ~10 mins of data 🚗

npm i -g obd-parser-cli

# will print speed to stdout every ~1000ms
obd monitor 0D:1000 -c serial -i /dev/tty.usbserial -b 38400

This example above uses usb serial cable, but if you make a PR to the CLI we can add bluetooth support using your module too.

evanshortiss commented 7 years ago

Also, just saw this repo in your GitHub and this commit https://github.com/dominicrico/node-obd/commit/606e6e9828c64d47e949d09a7ce7247bd59b9881

This library works as vanilla javascript so you should not need to do this. Let me know if the library is not working as vanillajs since it should be just fine.

mkdir my-obd-proj
cd my-obd-proj
npm init
npm i obd-parser --save

Then in a file such as app.js you can copy what's done in /example/poll-rpm.js

dominicrico commented 7 years ago

Yes, you are totally right. I had an issue with my electron app and an angular module loading the obd-parser so I tried it with my plain js variant. Now I change the way of requiring the module and it now works like expected!

dominicrico commented 7 years ago

@evanshortiss sometimes my command queue is getting longer and longer. Do you know why? I'm currently running two pollers at the same time with a different interval. Thanks for your help!

evanshortiss commented 7 years ago

@dominicrico before running the program can you do the following in the terminal:

export DEBUG=*

or in Windows I think it is:

set DEBUG=*

Alternatively, add the following at the start of your node.js program BEFORE you do require('obd-parser')

// make sure this is the first line in your application!
process.env.DEBUG='*'

var OBD = require('obd-parser')

Either of these will enable log output, then you can share those logs here and I can take a look 👍

dominicrico commented 7 years ago

Shure will do it later when I'm in my car ;)

evanshortiss commented 7 years ago

@dominicrico so I think I've found this same problem! Here's how I created it:

  1. Connect Raspberry Pi to car OBD using USB
  2. Power Raspberry Pi using 12V car adapter
  3. Wait...
  4. Wait...
  5. Give up

Even if I try using my Macbook USB to OBD I get the same issue. To fix it I must turn off the car, then turn it on again, then try with my Macbook and it works!

My guess is that maybe the Pi is drawing to much power, since if I power the Pi via USB using my Macbook it works! The problem seems to be completely related to using the 12V outlet in my car with the Pi 😢

I'm testing using a MK6 (2012) VW, what about you?

dominicrico commented 7 years ago

@evanshortiss I'm using my Macbook connecting to the OBD via my Bluetooth connector. For me, it starts working again after reloading my Electron application.

I haven't tested it with a Pi in my car.

Have you tested it with two pollers at the same time?

My car is a VW T5 (2004)

obd-parser (Vehicle Speed) 2017-03-20T17:08:09.724Z - poll timer triggered after 757ms +199ms obd-parser (Vehicle Speed) 2017-03-20T17:08:09.725Z - adding new timeout event with delay of 1000ms +1ms obd-parser (Vehicle Speed) 2017-03-20T17:08:09.727Z - got connection. writing data "010D" +2ms obd-parser (connection) 2017-03-20T17:08:09.728Z - queue is unlocked, writing message "010D" +1ms obd-parser (connection) 2017-03-20T17:08:09.728Z - writing "010D". queue state [] +0ms obd-parser (connection) 2017-03-20T17:08:09.729Z - send count 48. receive count 47 +1ms obd-parser (connection) 2017-03-20T17:08:09.729Z - writing message 010D . connection will lock +0ms obd-parser (connection) 2017-03-20T17:08:09.731Z - Send Message to Bluetooth OBD +2ms obd-parser (connection) 2017-03-20T17:08:09.738Z - received data "010D " +7ms obd-parser (OBDStreamParser) 2017-03-20T17:08:09.739Z - received data 010D +1ms obd-parser (OBDStreamParser) 2017-03-20T17:08:09.740Z - current buffer: "010D\r" +1ms obd-parser (OBDStreamParser) 2017-03-20T17:08:09.741Z - data was not a complete output +1ms obd-parser (Engine RPM) 2017-03-20T17:08:10.688Z - poll timer triggered after 1164ms +948ms obd-parser (Engine RPM) 2017-03-20T17:08:10.689Z - adding new timeout event with delay of 1000ms +0ms obd-parser (Engine RPM) 2017-03-20T17:08:10.690Z - got connection. writing data "010C" +1ms obd-parser (connection) 2017-03-20T17:08:10.691Z - queue is locked. queued message "010C". entries are 1 +1ms ["010C"] obd-parser (Vehicle Speed) 2017-03-20T17:08:10.726Z - poll operation timed out. trigger supplied callback +35ms obd-parser (Vehicle Speed) 2017-03-20T17:08:10.726Z - timeout 1 sec after requesting data. retrying now +0ms obd-parser (Vehicle Speed) 2017-03-20T17:08:10.727Z - queueing next poll for 500ms from now +1ms obd-parser (Vehicle Speed) 2017-03-20T17:08:10.727Z - doNextPoll called before previous was cleared. possible timeout. clearing +0ms obd-parser (Vehicle Speed) 2017-03-20T17:08:11.229Z - poll timer was triggered, but we are already waiting on data. skipping +502ms obd-parser (Vehicle Speed) 2017-03-20T17:08:11.230Z - poll timer triggered after 500ms +1ms obd-parser (Vehicle Speed) 2017-03-20T17:08:11.230Z - poller cannot add multiple listeners. removing cur listener +0ms obd-parser (Vehicle Speed) 2017-03-20T17:08:11.231Z - removing "data" listener from parser until new poll is sent +1ms obd-parser (Vehicle Speed) 2017-03-20T17:08:11.232Z - cleared existing poller timeout event +1ms obd-parser (Vehicle Speed) 2017-03-20T17:08:11.233Z - adding new timeout event with delay of 1000ms +1ms obd-parser (Vehicle Speed) 2017-03-20T17:08:11.233Z - got connection. writing data "010D" +0ms obd-parser (connection) 2017-03-20T17:08:11.234Z - queue is locked. queued message "010D". entries are 2 +1ms ["010C","010D"] obd-parser (Engine RPM) 2017-03-20T17:08:11.690Z - poll operation timed out. trigger supplied callback +456ms obd-parser (Engine RPM) 2017-03-20T17:08:11.691Z - timeout 1 sec after requesting data. retrying now +1ms obd-parser (Engine RPM) 2017-03-20T17:08:11.691Z - queueing next poll for 500ms from now +0ms obd-parser (Engine RPM) 2017-03-20T17:08:11.692Z - doNextPoll called before previous was cleared. possible timeout. clearing +1ms obd-parser (Engine RPM) 2017-03-20T17:08:12.192Z - poll timer was triggered, but we are already waiting on data. skipping +500ms obd-parser (Engine RPM) 2017-03-20T17:08:12.193Z - poll timer triggered after 500ms +1ms obd-parser (Engine RPM) 2017-03-20T17:08:12.193Z - poller cannot add multiple listeners. removing cur listener +0ms obd-parser (Engine RPM) 2017-03-20T17:08:12.194Z - removing "data" listener from parser until new poll is sent +1ms obd-parser (Engine RPM) 2017-03-20T17:08:12.194Z - cleared existing poller timeout event +0ms obd-parser (Engine RPM) 2017-03-20T17:08:12.195Z - adding new timeout event with delay of 1000ms +1ms obd-parser (Engine RPM) 2017-03-20T17:08:12.195Z - got connection. writing data "010C" +0ms obd-parser (connection) 2017-03-20T17:08:12.196Z - queue is locked. queued message "010C". entries are 3 +1ms ["010C","010D","010C"] obd-parser (Vehicle Speed) 2017-03-20T17:08:12.234Z - poll operation timed out. trigger supplied callback +38ms obd-parser (Vehicle Speed) 2017-03-20T17:08:12.235Z - timeout 1 sec after requesting data. retrying now +1ms obd-parser (Vehicle Speed) 2017-03-20T17:08:12.235Z - queueing next poll for 500ms from now +0ms obd-parser (Vehicle Speed) 2017-03-20T17:08:12.236Z - doNextPoll called before previous was cleared. possible timeout. clearing +1ms obd-parser (Vehicle Speed) 2017-03-20T17:08:12.737Z - poll timer was triggered, but we are already waiting on data. skipping +501ms obd-parser (Vehicle Speed) 2017-03-20T17:08:12.738Z - poll timer triggered after 500ms +1ms obd-parser (Vehicle Speed) 2017-03-20T17:08:12.739Z - poller cannot add multiple listeners. removing cur listener +1ms obd-parser (Vehicle Speed) 2017-03-20T17:08:12.740Z - removing "data" listener from parser until new poll is sent +1ms obd-parser (Vehicle Speed) 2017-03-20T17:08:12.741Z - cleared existing poller timeout event +1ms obd-parser (Vehicle Speed) 2017-03-20T17:08:12.741Z - adding new timeout event with delay of 1000ms +0ms obd-parser (Vehicle Speed) 2017-03-20T17:08:12.742Z - got connection. writing data "010D" +1ms obd-parser (connection) 2017-03-20T17:08:12.743Z - queue is locked. queued message "010D". entries are 4 +1ms ["010C","010D","010C","010D"]

From now on the queue is getting longer and longer and no more data will be received.

evanshortiss commented 7 years ago

Have you tested it with two pollers at the same time?

Yep! Like so obd monitor 05:1000 0d:1000 05:5000 -c serial -i /dev/tty.usbserial - b 38400

(^ this uses the CLI, but I can try do it manually too)

I'm surprised by the log line obd-parser (OBDStreamParser) 2017-03-20T17:08:09.739Z - received data 010D, this is not expected. The module should send a payload like "010D1" (connection.js appends the "1" to "010D") and receive something like "410D91" so the OBDStreamParser log should never contain data starting with "01, unless the vehicle does not support ATE0 (see here)? To check if your vehicle supports ATE0 the connect initialise logs should print "OK" when it is sent.

When I run I get:

  obd-parser (connection) 2017-03-21T16:25:33.481Z - queue is unlocked, writing message "010D" +0ms
  obd-parser (connection) 2017-03-21T16:25:33.481Z - writing "010D". queue state [] +0ms
  obd-parser (connection) 2017-03-21T16:25:33.481Z - send count 8. receive count 7 +0ms
. connection will lock +0ms017-03-21T16:25:33.481Z - writing message 010D1

Note that my output has "writing message 010D1" with the "1" on the end and yours seems to be missing it. Also I don't recognise this log obd-parser (connection) 2017-03-20T17:08:09.731Z - Send Message to Bluetooth OBD, perhaps you need the latest version of connection.js

From now on the queue is getting longer and longer and no more data will be received.

Ultimately, the main reason for this is that it seems like your car OBD system returned "010D\r" for a query. The parser expects format "410DXX\r>". The "41" tells us this is a response to a query and "0D" tells us what PID it is. The "XX" is the value. The ">" tells us the system has finished the query and is ready for more. In your logs things are locked because a complete response never came back (one with a ">"). I don't know why this might, be, it could be the car, bluetooth, or maybe a file you've modified? Can you make sure the "1" is being appended also, so 010D should become 010D1 see here .

Could you provide all logs from the time the program started? And we might see if ATE0 worked.

dominicrico commented 7 years ago

The poller works like expected for about 40 to 50 messages. Then things starting to get messed up...

I also noticed a similar behaviour in a Bluetooth Handsfree (HFP) module I'm currently writing for my Pi project. Maybe it could have something to do with the bluetooth connection and the missing bluetooth hci socket support for Mac OS....

The OBD Bluetooth log is something I wrote in my bluetooth connector. It's the same like in your serial connector.

evanshortiss commented 7 years ago

the bluetooth connection and the missing bluetooth hci socket support for Mac OS....

I've no idea @dominicrico, but I ordered a Bluetooth connector while travelling this week. I will test and see if I get the same issue with Bluetooth sometime this week or next week.

dominicrico commented 7 years ago

@evanshortiss any update on this?

dominicrico commented 7 years ago

@evanshortiss with the raspberry pi and a bluetooth connection to the OBD device it works without any issues. It must be an Apple thing...

evanshortiss commented 7 years ago

@dominicrico hey, that's interesting. So the Apple driver or system at some level just chokes?

I just recently got a bluetooth adapter, but it did not appear when plugged in my vehicle and I scanned for a device. I'm not sure if the bluetooth device is broken or my car is not delivering enough power to it - I'll need to try again or get another adapter!

dominicrico commented 7 years ago

@evanshortiss yes, thats very interesting. But I was not able to figure it out...

Some OBDII adapters do not work with some cars. Had the same issue and had to buy another one.

evanshortiss commented 7 years ago

Hmm, when I get a new bluetooth adapter I'll try again and see but I've been busy with work and travel so there's big gaps in when I can do it 😢

Yeah! It's a PITA really, but seems vendors have some specific codes/specs and not all codes are implemented by vendors. For example, my VW for instance does not support the 2F - Fuel Level Input 😞 I think if you can get on the car CANBUS it's possible to get this but it's also more involved AFAIK.