Lee Vincent Wilson
Published © MIT

Bluetooth, Node and Arduino

This project allows your Arduino to talk to your Node server via Bluetooth.

IntermediateFull instructions provided1,246
Bluetooth, Node and Arduino

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×1

Story

Read more

Schematics

Arduino Bluetooth Wiring

Code

Arduino Code

Arduino
#include <SoftwareSerial.h>

SoftwareSerial BTserial(10, 11); // Setup of Bluetooth module on pins 10 (TXD) and 11 (RXD);

void setup() {
  BTserial.begin(9600); // Bluetooth at baud 9600 for talking to the node server
  Serial.begin(4800); // Default Serial on Baud 4800 for printing out some messages in the Serial Monitor
}

void loop() {
    
  // Calls on BTSerial and sends the string to any connected devices.
  BTserial.print("From Arduino with mild ambivalence\n"); 

  // readStringUntil()
  // Reads all bytes off of the the Serial buffer until it finds the escape character '/n'
  // And then removes these bytes from the buffer
  // Returns the value as a string, which we print to the Serial monitor

  Serial.println(BTserial.readStringUntil('\n'));

  //Just so the Serial Monitor on Arduino and console on the Node server don't get too spammed
  delay(500);

}

Node Code

JavaScript
This code listens for an Arduino Board with a Bluetooth module and responds.
import BTSerialPort from 'bluetooth-serial-port';
const btSerial = new BTSerialPort.BluetoothSerialPort();

//Generic Error Handler for the BT Serial Port library as requires error functions
const errFunction = (err) =>{
   if(err){
       console.log('Error', err);
   }
};

// Connecting to BT device can take a few seconds so a little console.log to keep you entertained.
console.log("Starting Server");
// Are you not entertained?

/*
  For this to work you will have to connect to the Bluetooth device on your computer in the normal way
  I.e via Bluetooth settings: Default password is usually 0000 or 1234
*/

// Once BtSerial.inquire finds a device it will call this code
// BtSerial.inquire will find all devices currently connected with your computer
btSerial.on('found', function(address, name) {
    // If a device is found and the name contains 'HC' we will continue
    // This is so that it doesn't try to send data to all your other connected BT devices
    if(name.toLowerCase().includes('hc')){

      btSerial.findSerialPortChannel(address, function(channel) {
        // Finds then serial port channel and then connects to it
        btSerial.connect(address, channel, function() {
          // Now the magic begins, bTSerial.on('data', callbackFunc) listens to the bluetooth device.
          // If any data is received from it the call back function is used
          btSerial.on('data', function(bufferData) {
            // The data is encoded so we convert it to a string using Nodes Buffer.from func
            console.log(Buffer.from(bufferData).toString());

            // Now we have received some data from the Arduino we talk to it.
            // We Create a Buffered string using Nodes Buffer.from function
            // It needs to be buffered so the entire string is sent together
            // We also add an escape character '\n' to the end of the string
            // This is so Arduino knows that we've sent everything we want
            btSerial.write(Buffer.from('From Node With Love\n'), errFunction);
          });
        }, errFunction);
      },errFunction);
    }else{
      console.log('Not connecting to: ', name);
    }
});

// Starts looking for Bluetooth devices and calls the function btSerial.on('found'
btSerial.inquire();

Credits

Lee Vincent Wilson

Lee Vincent Wilson

1 project • 0 followers

Comments