0

I'm building an android app to act as an interface for an IOT controller. I already have the code for the chip, and it is connected to an HC-05 bluetooth module. I've tried using the bluetooth terminal from the app store and my phone successfully connects to the HC-05. I'm now building the mobile app to send/receive data from the chip. So I need to connect to HC-05 directly from node.js, and that's where I'm stuck.

I've been looking for npm modules that can help me, and so far I've found web-bluetooth-terminal, bluetooth-terminal, serialport, bluetooth-serial-port and johnny-five. Thing is, I'm not sure what the difference between these is and which one will actually work with HC-05. As far as I understand, johnny-five is for writing the code for the controller itself, not connecting to the bluetooth module, and I'm not sure whether web-bluetooth-terminal can connect to HC-05 at 9600 baudrate and different sites say different things. How can I make this work?

1 Answers1

0

I might be a bit late with answering but I'm working on connecting an arduino with an HC-05 module to a NodeJS app and I've stumbled upon the Node-bluetooth library. By using this I can connect with the HC-05 by doing the following:

router.post('/connect', function (req, res) {
    res.render('connect');

    const bluetooth = require('node-bluetooth');
    const device = new bluetooth.DeviceINQ();

    // Find devices
    device
        .on('finished', console.log.bind(console, 'finished'))
        .on('found', function found(address, name) {
            console.log('Found: ' + address + ' with name ' + name);

            // We know our Arduino bluetooth module is called 'HC-05', so we only want to connect to that.
            if (name === 'HC-05') {

                // find serial port channel
                device.findSerialPortChannel(address, function (channel) {
                    console.log('Found channel for serial port on %s: ', name, channel);

                    // make bluetooth connect to remote device
                    bluetooth.connect(address, channel, function (err, connection) {
                        if (err) return console.error(err);

                        // This is some example code from the library for writing, customize as you wish.
                        connection.delimiter = Buffer.from('/n', 'utf-8');
                        connection.on('data', (buffer) => {
                            console.log('received message: ', buffer.toString());
                        });

                        // This is some example code from the library for writing, customize as you wish.
                        connection.write(new Buffer('hello', 'utf-8'), () => {
                            console.log('wrote');
                        });
                    });
                });
            }
        });
    device.scan();
});

I know that checking for the "HC-05" string probably isn't great practice but it works for testing purposes.

Jobje325
  • 55
  • 12