Closed rubberbird closed 9 years ago
Okay, atleast we know your device can be found, only connecting doesn't work.
1 - Yes, using a EM327 bluetooth device in a car with ignition on. Car wasn't running. 2 - I haven't pair the device already, do I need to add the MAC to the hcitools trusted list? 3 - I have pair with another device (iphone) succesfully usign bluez simple agent and hcitools.
You need to pair the device with OBD-II device. Which OS are you running, raspbian?
I have paired my phone with the obd2 unit. Also I have paired my phone with the pi. Yes, using rasbian.
I added some console outs and it seems to be failing below where I have places the <----
That is the last console message outputed before.
Error: Error with OBD-II device: Error: Cannot connect
Err in the console from below.
}, function (err) { //Error callback!
self.emit('error', 'Error with OBD-II device: ' + err);
});
/**
@this {OBDReader} */ OBDReader.prototype.connect = function (address, channel) { var self = this; //Enclosure var btSerial = new (require('bluetooth-serial-port')).BluetoothSerialPort();
btSerial.connect(address, channel, function () { console.log("ODB Attemting to generate AT commands"); self.connected = true;
//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');
//Event connected
self.emit('connected');
btSerial.on('data', function (data) {
var currentString, indexOfEnd, arrayOfCommands;
currentString = self.receivedData + data.toString('utf8'); // making sure it's a utf8 string
arrayOfCommands = currentString.split('>');
var forString;
if(arrayOfCommands.length < 2) {
self.receivedData = arrayOfCommands[0];
} else {
for(var commandNumber = 0; commandNumber < arrayOfCommands.length; commandNumber++) {
forString = arrayOfCommands[commandNumber];
if(forString === '') {
continue;
}
var multipleMessages = forString.split('\r');
for(var messageNumber = 0; messageNumber < multipleMessages.length; messageNumber++) {
var messageString = multipleMessages[messageNumber];
if(messageString === '') {
continue;
}
var reply;
reply = parseOBDCommand(messageString);
//Event dataReceived.
self.emit('dataReceived', reply);
console.log("Connection Reply Output: " + reply);
self.receivedData = '';
}
}
}
});
btSerial.on('failure', function(error) {
self.emit('error', 'Error with OBD-II device Serial Connection: ' + error);
});
}, function (err) { //Error callback! self.emit('error', 'Error with OBD-II device: ' + err); });
Can you connect and send data to the OBD-II device by terminal? (With rfcomm connect or something like that).
@EricSmekens using which commands?
I have looked a little deeper and it is failing with the below function in bluetooth-serial-port.js in your dependant npm plugin
BluetoothSerialPort.prototype.connect = function(address, channel, successCallback, errorCallback) {
var self = this;
console.log("Bt Serial Connection" + "address: " + address.toString() + "channel: " + channel.toString() + "success Callback: " + successCallback.toString() + "error callback: " + errorCallback.toString());
var read = function() {
console.log("Bt Serial Connection -read function");
process.nextTick(function() {
console.log("Bt Serial Connection- Process next tick");
if (self.connection) {
self.connection.read(function(err, buffer) {
console.log("Bt Serial Connection -read" + err.toString() + buffer.toString());
if (!err && buffer) {
self.emit('data', buffer);
} else {
self.close();
self.emit('failure', err);
}
});
}
});
};
Bt Serial Connectionaddress: 00:0D:18:06:00:00channel: 16success Callback: function () {
console.log("ODB Attemting to generate AT commands");
self.connected = true;
//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');
//Event connected
self.emit('connected');
btSerial.on('data', function (data) {
var currentString, indexOfEnd, arrayOfCommands;
currentString = self.receivedData + data.toString('utf8'); // making sure it's a utf8 string
arrayOfCommands = currentString.split('>');
var forString;
if(arrayOfCommands.length < 2) {
self.receivedData = arrayOfCommands[0];
} else {
for(var commandNumber = 0; commandNumber < arrayOfCommands.length; commandNumber++) {
forString = arrayOfCommands[commandNumber];
if(forString === '') {
continue;
}
var multipleMessages = forString.split('\r');
for(var messageNumber = 0; messageNumber < multipleMessages.length; messageNumber++) {
var messageString = multipleMessages[messageNumber];
if(messageString === '') {
continue;
}
var reply;
reply = parseOBDCommand(messageString);
//Event dataReceived.
self.emit('dataReceived', reply);
console.log("Connection Reply Output: " + reply);
self.receivedData = '';
}
}
}
});
btSerial.on('failure', function(error) {
self.emit('error', 'Error with OBD-II device Serial Connection: ' + error);
});
}error callback: function (err) { //Error callback!
self.emit('error', 'Error with OBD-II device: ' + err);
}
Error: Error with OBD-II device: Error: Cannot connect
Second answer is a good one I think, might be slightly different for raspbian.
Using hcitools I can connect.
And are you able to send a command like '010D' and get a response from it?
Ok, I got it working. I just needed to do the initial pair manually first.
Scan for the device.
hcitool scan
Connect to the device. (Enter 1234 as the pin)
bluez-simple-agent hci0 00:0D:18:A0:4E:35
Add the MAC to trusted device to avoid pairing again.
bluez-test-device trusted 00:0D:18:A0:4E:35 yes
Once this was done, all is working well.
Okay, sweet. Thanks for letting me know. I will probably ass this in the readme, if that's fine with you?
No problem. That's probably a good idea to add to read me. Cheers for your help.
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