Pofay's Blog

Setup wireless tethering for Johnny Five on Arduino using HC-05 Bluetooth Module

Before following along with this article please setup your Arduino for Johnny Five. The full setup guide is here

Johnny Five is a really cool framework that allows you to use Javascript code to control supported boards ranging from an arduino to a raspberry pi.

For Arduino the setup is pretty easy but the constraints are as follows:

The JavaScript program is executed on a host machine that runs Node.js. The program transmits basic IO instructions to the board via usb serial, which acts as a thin client. Requires tethering.

Which means your Arduino must be connected at all times to the host machine so that the code can run.

This is unlike the traditional method of upload and forget when using base Arduino.

Although this drawback can be mitigated by using a Raspberry Pi alongside the Arduino. But I want to use just the Arduino to do stuff using Javascript.

Thats when I found this link for using a HC-05 bluetooth module to act as a wireless serial port.

Although theres a number of things that the guide in the link omitted. Like where is the key pin on a HC-05.

And that you have to use a Bluetooth Manager to actually let the device be seen as a serial port.

Today I will explain to you how I set this up on my host computer running Ubuntu 18.04 LTS.

Its going to basically mirror what the guide has but with added content on whats missing.

Hardware Used

The base setup

So first off we need to send AT commands to the HC-05. In order for us to do that we need to do this connection:

With Bluetooth RX to PIN 11 and Bluetooth TX to PIN 10

connection

And also locate the KEY pin of the HC-05. This is important since this is where the code does its magic to send AT commands:

key-pin

Then open up your Arduino IDE and stick this code:

#define ROBOT_NAME "RandomBot" // This will be the Bluetooth name the HC-05 is set once setup is complete.
// You can change this to anything you like.
// If you haven't configured your device before use this
#define BLUETOOTH_SPEED 38400 //This is the default baudrate that HC-05 uses
// If you are modifying your existing configuration, use this:
// #define BLUETOOTH_SPEED 57600
#include <SoftwareSerial.h>
// Swap RX/TX connections on bluetooth chip
// Pin 10 --> Bluetooth TX
// Pin 11 --> Bluetooth RX
SoftwareSerial mySerial(10, 11); // RX, TX
/*
The possible baudrates are:
AT+UART=1200,0,0 -------1200
AT+UART=2400,0,0 -------2400
AT+UART=4800,0,0 -------4800
AT+UART=9600,0,0 -------9600 - Default for hc-06
AT+UART=19200,0,0 ------19200
AT+UART=38400,0,0 ------38400
AT+UART=57600,0,0 ------57600 - Johnny-five speed
AT+UART=115200,0,0 -----115200
AT+UART=230400,0,0 -----230400
AT+UART=460800,0,0 -----460800
AT+UART=921600,0,0 -----921600
AT+UART=1382400,0,0 ----1382400
*/
void setup() {
// I added a 10 second delay before it does the AT setup to the bluetooth module
// so I still have time to dip in a jumper wire from PIN 9 to the KEY PIN.
delay(10000);
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Starting config");
mySerial.begin(BLUETOOTH_SPEED);
delay(1000);
// Should respond with OK
mySerial.print("AT\r\n");
waitForResponse();
// Should respond with its version
mySerial.print("AT+VERSION\r\n");
waitForResponse();
// Set pin to 0000
mySerial.print("AT+PSWD=0000\r\n");
waitForResponse();
// Set the name to ROBOT_NAME
String rnc = String("AT+NAME=") + String(ROBOT_NAME) + String("\r\n");
mySerial.print(rnc);
waitForResponse();
// Set baudrate to 57600
mySerial.print("AT+UART=57600,0,0\r\n");
waitForResponse();
Serial.println("Done!");
}
void waitForResponse() {
delay(1000);
while (mySerial.available()) {
Serial.write(mySerial.read());
}
Serial.write("\n");
}
void loop() {}
view raw bluetooth-setup.ino hosted with ❤ by GitHub

Assuming you have done the required connection, run the code and manually connect a jumper wire to PIN 9 of your Arduino to the HC-05 KEY PIN.

You have 10 seconds to do that.

After 10 seconds the TX LED of your Arduino should blink every second for 6 seconds.

If you’ve done this right you should see an output in your Serial Monitor similar to this:

Starting config 
OK
OK[VERSION]
OK
OK
OK
Done!

Otherwise change the value of BLUETOOTH_SPEED in the code. In my case I had to change 38400 to 9600 since I formerly did stuff with my HC-05.

There should be no ERROR(0) shown in the Serial Monitor. If there is do the setup again.

Uploading the Firmata

Without changing any of the connections open another tab in your Arduino IDE and copy the code in this link

Then Upload as usual to you Arduino.

Changing the connection

Next setup this connection with Bluetooth RX to PIN 1(TX) and Bluetooth TX to PIN 0(RX)

hc-05-port

For extra measure press the reset button on your Arduino for 11 Seconds.

Bluetooth in Host (Linux Only)

First in your HOST machine download blueman:

sudo apt-get install blueman    

Then open blueman and you should see this window:

base

Click on Adapters —> Preferences and then set Visibility Setting to Always Visible

Adapter

Then select the name of the HC-05 Bluetooth. Its what you set in the ROBOT_NAME during the AT setup. In my case its POF_ROBOT.

Selection

Then click setup and pair with it. The default pin should be 0000:

Setup

The most important part is the connection. Select the Serial Port option:

Setup2

In Ubuntu 18.04 LTS and provided you didn’t switch to another desktop manager shortly after pressing next you should see a notificaiton telling that bluetooth device is now open in /dev/rfcomm#.

Usually in the first run it should be /dev/rfcomm0 unless you’ve got other bluetooth devices that are communicating over as a serial port.

Running some code

Run these commands:

mkdir j5
cd j5
npm init // Walk through the setup
npm install johnny-five

And then create a file called board.js with the following contents:

var five = require("johnny-five");
var board = new five.Board({
port: '/dev/rfcomm0'
})
// The board's pins will not be accessible until
// the board has reported that it is ready
board.on("ready", function() {
console.log("Ready!");
var led = new five.Led(13);
led.blink(500);
});
view raw bluetooth-board.js hosted with ❤ by GitHub

Remember to change the /dev/rfcomm0 if you’ve got other bluetooth as serial port devices connected.

The run using sudo node board.js. The LED in PIN 13 should keep on blinking until you press CTRL-C.

Notes: If it gives you a timeout error, press the reset button on your Arduino for 11 seconds. That should do the trick.

End

This is by no means an exhaustive guide, but rather on what I did to get HC-05 Bluetooth to work as a serial port in my case since the guide didn’t tell everything.


Written by Gian Carlo Gilos who lives in Cebu, Philippines. Mostly in the process of making stuff work. Github