EricSmekens / node-bluetooth-obd

Node package for communication with Bluetooth OBD connectors.
Other
262 stars 72 forks source link

Problem Connecting on Raspberry PI Rasbian #7

Closed rubberbird closed 9 years ago

rubberbird commented 9 years ago

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

EricSmekens commented 9 years ago

Okay, atleast we know your device can be found, only connecting doesn't work.

rubberbird commented 9 years ago

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.

EricSmekens commented 9 years ago

You need to pair the device with OBD-II device. Which OS are you running, raspbian?

rubberbird commented 9 years ago

I have paired my phone with the obd2 unit. Also I have paired my phone with the pi. Yes, using rasbian.

rubberbird commented 9 years ago

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); });

/**

EricSmekens commented 9 years ago

Can you connect and send data to the OBD-II device by terminal? (With rfcomm connect or something like that).

rubberbird commented 9 years ago

@EricSmekens using which commands?

rubberbird commented 9 years ago

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); } }); } }); };

Below is the output from the function arguments toString

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

EricSmekens commented 9 years ago

http://askubuntu.com/questions/248817/how-to-i-connect-a-raw-serial-terminal-to-a-bluetooth-connection

Second answer is a good one I think, might be slightly different for raspbian.

rubberbird commented 9 years ago

Using hcitools I can connect.

EricSmekens commented 9 years ago

And are you able to send a command like '010D' and get a response from it?

rubberbird commented 9 years ago

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.

EricSmekens commented 9 years ago

Okay, sweet. Thanks for letting me know. I will probably ass this in the readme, if that's fine with you?

rubberbird commented 9 years ago

No problem. That's probably a good idea to add to read me. Cheers for your help.