Giter Site home page Giter Site logo

node-bluetooth-obd's Introduction

NPM

bluetooth-obd - 0.2.5

Bluetooth communication for OBD-II ELM327 devices.

This node module lets you communicate over a bluetooth serial port with OBD-II ELM327 Connectors using Node.js.

Limitations

  • Only tested on ELM327 devices.
  • Not all OBD-II Commands are implemented yet.

Pre-Requirements

  • If it's a Bluetooth ELM327, then it should already be paired! If this hasn't been done, it will cause a connection error.
  • bluetooth-serial-port (module that is used by this module, thanks to Eelco) requires libbluetooth-dev package: $ sudo apt-get install libbluetooth-dev

Serial

  • If you're looking for serial RS23 connection, look into serial-obd.

Install

npm install bluetooth-obd

Documentation

Basic usage

var OBDReader = require('bluetooth-obd');
var btOBDReader = new OBDReader();
var dataReceivedMarker = {};

btOBDReader.on('connected', function () {
    //this.requestValueByName("vss"); //vss = vehicle speed sensor

    this.addPoller("vss");
    this.addPoller("rpm");
    this.addPoller("temp");
    this.addPoller("load_pct");
    this.addPoller("map");
    this.addPoller("frp");

    this.startPolling(1000); //Request all values each second.
});

btOBDReader.on('dataReceived', function (data) {
    console.log(data);
    dataReceivedMarker = data;
});

// Use first device with 'obd' in the name
btOBDReader.autoconnect('obd');

API

OBDReader

Event: ('dataReceived', data)

Emitted when data is read from the OBD-II connector.

  • data - the data that was read and parsed to a reply object

Event: ('connected')

Emitted when the connection is set up (port is open).

Event: ('error', message)

Emitted when an error is encountered.

Event: ('debug', message)

Emitted with debugging information.

OBDReader()

Creates an instance of OBDReader.

getPIDByName(Name)

Find a PID-value by name.

Params:
  • name Name of the PID you want the hexadecimal (in ASCII text) value of.
Return:
  • string PID in hexadecimal ASCII

parseOBDCommand(hexString)

Parses a hexadecimal string to a reply object. Uses PIDS. (obdInfo.js)

Params:
  • string hexString Hexadecimal value in string that is received over the serialport.
Return:
  • Object reply - The reply.
  • string reply.value - The value that is already converted. This can be a PID converted answer or "OK" or "NO DATA".
  • string reply.name - The name. --! Only if the reply is a PID.
  • string reply.mode - The mode of the PID. --! Only if the reply is a PID.
  • string reply.pid - The PID. --! Only if the reply is a PID.

autoconnect(query)

Attempt discovery of the device based on a query string, and call connect() on the first match.

Params:
  • string query (Optional) string to be matched against address/channel (fuzzy-ish)

connect(address, channel)

Connect/Open the serial port and add events to serialport. Also starts the intervalWriter that is used to write the queue.

Params:
  • string address MAC-address of device that will be connected to.
  • number channel Channel that the serial port service runs on.

disconnect()

Disconnects/closes the port.

write(message, replies)

Writes a message to the port. (Queued!) All write functions call this function.

Params:
  • string message The PID or AT Command you want to send. Without \r or \n!
  • number replies The number of replies that are expected. Default = 0. 0 --> infinite

requestValueByName(name)

Writes a PID value by entering a pid supported name.

Params:
  • string name Look into obdInfo.js for all PIDS.

addPoller(name)

Adds a poller to the poller-array.

Params:
  • string name Name of the poller you want to add.

removePoller(name)

Removes an poller.

Params:
  • string name Name of the poller you want to remove.

removeAllPollers()

Removes all pollers.

writePollers()

Writes all active pollers.

startPolling()

Starts polling. Lower interval than activePollers * 50 will probably give buffer overflows.

Params:
  • number interval Frequency how often all variables should be polled. (in ms) If no value is given, then for each activePoller 75ms will be added.

stopPolling()

Stops polling.

LICENSE

This module is available under a Apache 2.0 license, see also the LICENSE file for details.

node-bluetooth-obd's People

Contributors

bryant1410 avatar damoclark avatar ericsmekens avatar germanbluefox avatar joouis avatar kauegimenes avatar szaboge avatar ulizama avatar yimengzhu 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-bluetooth-obd's Issues

VIN

I'm not having much luck getting the VIN. I made it a little further by adding this in obd.js

else if (valueArray[0] === "49") {
        reply.mode = valueArray[0];
        for (var i = 0; i < PIDS.length; i++) {
			console.log('pid length:', PIDS.name);
            if (PIDS[i].mode == "09") {
                reply.name = PIDS[i].name;
				reply.value = PIDS[i].convertToUseful(hexString);
				console.log('value:', reply.value);
            }
        }
    } 

Any suggestions as it stands I do not believe the methods for getting VIN actually work... or am I mistaken?

does it control the signals?

Hello guys, i have a question does it control the signals? because I would like to play with them and make a little program for car to "dance" to music :))

Get Error: Error finding serialport: undefined

Hi Eric, Thank you for the excellent work. Now i can connect my OBD with my server. But i always get the Error: Error finding serialport: undefined before i can receive the data. As soon as this error is thrown the OBD will disconnect. Do you know how can i fix this Problem?

cant install

when i do

npm install bluetooth-obd

it try to compile something and throws error.

Please add as a Request in README.md

Pre-requests on Linux

Needs Bluetooth development packages to build

$ sudo apt-get install libbluetooth-dev

It delayed the test for a hole day.

thank in advance.

Returned data from OBD split up into chunks

Hi Eric,

Amazing library really getting to grips with it, however there is one strange issue I'm having.
I have adapted the code to work in a hybrid app but whenever I send a command to the OBD device it returns the data in an inconsistent chunk of results, example:

Me: 010C
OBDII:
OBDII: 4
OBDII: 3C
OBDII: 3
OBDII: 3C C
OBDII: 3C
OBDII:
OBDII:
OBDII: >

It is a similar issue to this stackoverflow post: http://stackoverflow.com/questions/25374953/bluetoothchat-to-elm327-split-response-message

Although the solution there appears to be in Java.

I have a work around where I loop over the results adding the relevant parts together then when i receive a ">" command I stop as i know its finished.

Have you ever come across this before?

Thanks again for the library and any help,

Chris

Node Red

Hi,

I have been experimenting with this in Node Red trying to utilise it using the Node Red NPM node.
Having little to no luck at all and was wondering have you experimented with this and been able to get it to work?

Data contains nothing

Hi,

I've a ELM327 connected to a raspberry. Succesfully paired and trusted.
When I'm running the demo code, data is always empty. Console shows rows with {} What's going wrong?

Thanks in advance!

install issues

Upon installation on raspbian the following error occurs

pi@raspi-car-01:~/testodb2 $ npm install --save bluetooth-obd

> [email protected] install /home/pi/testodb2/node_modules/bluetooth-serial-port
> node-gyp configure build

make: Entering directory '/home/pi/testodb2/node_modules/bluetooth-serial-port/build'
  CXX(target) Release/obj.target/BluetoothSerialPort/src/linux/BluetoothSerialPort.o
  CXX(target) Release/obj.target/BluetoothSerialPort/src/linux/DeviceINQ.o
../src/linux/DeviceINQ.cc:35:37: fatal error: bluetooth/bluetooth.h: No such file or directory
     #include <bluetooth/bluetooth.h>
                                     ^
compilation terminated.
BluetoothSerialPort.target.mk:105: recipe for target 'Release/obj.target/BluetoothSerialPort/src/linux/DeviceINQ.o' failed
make: *** [Release/obj.target/BluetoothSerialPort/src/linux/DeviceINQ.o] Error 1
make: Leaving directory '/home/pi/testodb2/node_modules/bluetooth-serial-port/build'
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:262:23)
gyp ERR! stack     at ChildProcess.emit (events.js:182:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12)
gyp ERR! System Linux 4.14.79+
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "build"
gyp ERR! cwd /home/pi/testodb2/node_modules/bluetooth-serial-port
gyp ERR! node -v v10.14.2
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok

Please add to the installation that libbluetooth-dev is required as found on stackoverflow.com

Connection to VEEPEAK BLE+

Hi,
I'm a little confused on how to connect to a device, I have this one:

info 34:81:F4:D2:43:2C
Device 34:81:F4:D2:43:2C (public)
	Name: VEEPEAK
	Alias: VEEPEAK
	Class: 0x00240404
	Icon: audio-card
	Paired: yes
	Trusted: yes
	Blocked: no
	Connected: no
	LegacyPairing: yes
	UUID: Serial Port               (00001101-0000-1000-8000-00805f9b34fb)
	UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
	Modalias: bluetooth:v0039p5056d0120
	RSSI: -84

I have tried the commands:
btOBDReader.autoconnect('34:81:F4:D2:43:2C');
btOBDReader.autoconnect('VEEPEAK');
btOBDReader.autoconnect('veepeak'); - just in case it was case sensitive
btOBDReader.connect('34:81:F4:D2:43:2C', 1); - as I have no idea what the port is...

Any ideas?

Thanks,
Jamie

Commented ATZ command returns no data

When testing the module as-is, and setting a poll on rpm, I get OKs for the sets, and then empty objects for all of the poll results:

{ value: 'OK' }
{ value: 'OK' }
{ value: 'OK' }
{ value: 'OK' }
{ value: 'OK' }
{ value: 'OK' }
{}
{}
{}
...

Based on testing/reading, it looks like the problem is the ATZ (ELM327 chip reset) being commented out. Is there a reason for that? Things work for me when it's un-commented.

        //self.write('ATZ');
        //Turns off extra line feed and carriage return
        self.write('ATL0');
        //This disables spaces in in output, which is faster!
        self.write('ATS0');
        //Turns off headers and checksum to be sent.
        self.write('ATH0');
        //Turns off echo.
        self.write('ATE0');
        //Turn adaptive timing to 2. This is an aggressive learn curve for adjusting the timeout. Will make huge difference on slow systems.
        self.write('ATAT2');
        //Set timeout to 10 * 4 = 40msec, allows +20 queries per second. This is the maximum wait-time. ATAT will decide if it should wait shorter or not.
        //self.write('ATST0A');
        //Set the protocol to automatic.
        self.write('ATSP0');

Messages failing after disconnect

Hi,
I am creating a graphing app based off your library. The first time I connect to a device it works perfectly (Other than I had to remove the "replies" part of the string that gets added to the Queue. It seems like it always appends a 1 to every PID which would just make my ELM Simulator return nothing. Removing this makes it work.

However back to the issue, once I disconnect and reconnect it seems there are really weird concatenated messages being sent to the BT device.

Seems like something weird goes on after a disconnect that doesn't properly clean up. I have a hunch it is something to do with the intervalWriter which may go deeper into the way the btSerial lib is handling the buffers. But am struggling to debug that. I've tried just adding all the functions I could find that detach listeners, detach pollers, clear queue,etc..etc. None of it helped. If you disconnect again and reconnect the problem only compounds. See my 3rd screenshot.

Screenshot of Good Calls on first connect on OBD Device

Screenshot of Data receieved back in front end from OBD Device

Screenshot of calls being sent after disconnect and reconnect.

Screenshot of Data receieved back in front end from OBD Device after disconnect and reconnect.

After second disconnect and reconnect

You can find my project here which should demonstrate the issue.
https://github.com/Reiss-Cashmore/obdii-grapher

I am using Mac OSX if that helps.

Timing

Timing new responses should be improved.

Problem Installing for Android use

Log

PS C:\Users\fjsolance\Desktop\MotorBikeControl\MotorBikeControlApp> npm install --save bluetooth-serial-port
npm ERR! code 1
npm ERR! path C:\Users\fjsolance\Desktop\MotorBikeControl\MotorBikeControlApp\node_modules\bluetooth-serial-port
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp configure build
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | win32 | x64
npm ERR! gyp info find Python using Python version 3.10.4 found at "C:\Users\fjsolance\AppData\Local\Programs\Python\Python310\python.exe"
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS msvs_version not set from command line or npm config
npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
npm ERR! gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details
npm ERR! gyp ERR! find VS looking for Visual Studio 2015
npm ERR! gyp ERR! find VS - not found
npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio
npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload.
npm ERR! gyp ERR! find VS For more information consult the documentation at:
npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Could not find any Visual Studio installation to use
npm ERR! gyp ERR! stack at VisualStudioFinder.fail (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:122:47)
npm ERR! gyp ERR! stack at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:75:16
npm ERR! gyp ERR! stack at VisualStudioFinder.findVisualStudio2013 (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:363:14)
npm ERR! gyp ERR! stack at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:71:14
npm ERR! gyp ERR! stack at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:384:16
npm ERR! gyp ERR! stack at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\util.js:54:7
npm ERR! gyp ERR! stack at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\util.js:33:16
npm ERR! gyp ERR! stack at ChildProcess.exithandler (node:child_process:406:5)
npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:526:28)
npm ERR! gyp ERR! stack at maybeClose (node:internal/child_process:1092:16)
npm ERR! gyp ERR! System Windows_NT 10.0.19042
npm ERR! gyp ERR! command "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" "configure" "build"
npm ERR! gyp ERR! cwd C:\Users\fjsolance\Desktop\MotorBikeControl\MotorBikeControlApp\node_modules\bluetooth-serial-port
npm ERR! gyp ERR! node -v v16.14.2
npm ERR! gyp ERR! node-gyp -v v8.4.1
npm ERR! gyp ERR! not ok

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\fjsolance\AppData\Local\npm-cache_logs\2022-05-20T11_48_07_556Z-debug-0.log

Problem Connecting on Raspberry PI Rasbian

Using the code in your sample. Device is being found, but connection is failing.

Error Below:

Debug: Found device: OBDII (00:0D:18:06:00:00)
Debug: Found device channel: 16
connected
Error: Error with OBD-II device: Error: Cannot connect

WiFi

Can this be adapted to the wifi serial interface? 192.168.0.10:35000

Npm install failing

Unable to use this for the moment. Please help
Ubuntu 14 is the OS used

Details:
_modules/bluetooth-serial-port/build'
CXX(target) Release/obj.target/BluetoothSerialPort/src/linux/BluetoothSerialPort.o
CXX(target) Release/obj.target/BluetoothSerialPort/src/linux/DeviceINQ.o
../src/linux/DeviceINQ.cc:35:37: fatal error: bluetooth/bluetooth.h: No such file or directory
#include <bluetooth/bluetooth.h>
^
compilation terminated.
make: *** [Release/obj.target/BluetoothSerialPort/src/linux/DeviceINQ.o] Error 1
make: Leaving directory /home/home/node_sundar/node_modules/bluetooth-obd/node_modules/bluetooth-serial-port/build' gyp ERR! build error gyp ERR! stack Error:make` failed with exit code: 2

Parameters Supported By This Library

This library supports which parameters (such as rpm, speed) in a car for an obd to get values from?
I have already tested rpm, the speed with ELM327 OBD-II.
I also tried to test the fuel level input, but I got no response using this library. It showed no data!
Please let me know the parameters, soon.
Thanks

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.