pbLua Bluetooth Console

Introduction

The pbLua firmware lets you compile programs right on the NXT. In most cases, the USB cable is used for what’s called the console connection. This is where your terminal program (Hyperterminal, minicom, screen, etc) connects to the NXT.

Sometimes it’s nice to have a wireless conenction to the NXT, and that’s where the Bluetooth interface comes in really handy. This tutorial covers using the NXT in console mode for both Windows and a generic Linux host computer. I don’t have access to a Mac, but Mark Crosbie has a Bluetooth tutorial for Mac users.

I’m using plain Debian as my Linux host, so the instructions will work for most Debian based Linux machines (Ubuntu etc). If you’re not using a Debian based system, the package installation might be a bit different.

By the end of this tutorial you’ll know how to:

  1. Use the USB connection to do the pairing process with your host computer – Windows/Mac/Linux
  2. Connect to the NXT console using Bluetooth
  3. Send commands and programs to the NXT via Bluetooth

Contents

Setting up the NXT for Bluetooth

No matter what host machine you want to pair your NXT with, setting up the NXT Bluetooth system is the first step. Once you’ve done this, then move on the host-specific section for your operating system.

The next step assumes that we’re starting as if you’ve never used the NXT Bluetooth connection at all. While this step is not necessary, if you want to start out with a completely fresh and clean Bluetooth subsystem on your NXT, just rest to factory defaults:

Connect to your NXT through the USB cable, and start up your terminal:

> nxt.BtFactoryReset()

Then turn on the Bluetooth radio:

> nxt.BtPower(1)

Make your NXT visible, or searchable:

> nxt.BtVisible(1)

And finally, give it a name:

> nxt.BtSetName("OGEL")

Pairing the NXT with Linux

The following code samples run from a terminal session (command line) on the Linux host. First things first, let’s get the most up to date Bluetooth packages for our machine. For Debian, the command is:

rhempel@debian:~$ sudo apt-get install bluez bluez-hcidump

Unpacking bluez (from .../archives/bluez_4.57-1_i386.deb) ...

When editing files on your Linux system, use whatever editor you like, but some of the files must be edited using sudo (superuser powers) because they are owned by the system.

I’ll indicate which file to edit in the first line of the example. You’ll have to look for the section to change yourself…

There is one file we may have to edit, for the case of Bluetooth adapters that are used for certain keyboards and mice. These adapters wake up in HID (Human Input Device) mode so that you can use your keyboard to get into the BIOS of your machine at boot time, before the Bluetooth stack is up and runnning.

-- /etc/default/bluetooth

HID2HCI_ENABLED=1
HID2HCI_UNDO=1

Now, plug in your Bluetooth dongle if it’s a USB device. If it’s built in to your computer, it will be recognized automatically. Be aware that many internal Bluetooth devices in laptops show up as USB devices.

To see if your host has recognized your Bluetooth device:

-- Only run this if your Bluetooth interface is connected to the USB bus
rhempel@debian:~$ lsusb
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 050d:016a Belkin Components
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

-- Check if your Bluetooth interface is recognized
rhempel@debian:~$ hcitool dev
Devices:
        hci0    aa:bb:cc:dd:ee:ff

Of course, you’ll see different results for the MAC address of your device, the aa:bb:cc:dd:ee:ff is just an example.

Keep the NXT connected to your USB port for a little while longer. We’re going to set up the Bluetooth system on the Linux machine first, then pair the NXT with your computer. You’ll need to be able to enter the PIN code on the NXT, or use a little pbLua script to do it automatically.

It’s also a great idea to run a tool called hcidump. I usually run this in its own terminal window when I’m debugging Bluetooth stuff so that I can keep an eye on what’s happening. Here’s how to invoke it, and a bit of sample output..

rhempel@debian:~$ sudo hcidump
HCI sniffer - Bluetooth packet analyzer ver 1.42
device: hci0 snap_len: 1028 filter: 0xffffffff
< HCI Command: Create Connection (0x01|0x0005) plen 13
> HCI Event: Command Status (0x0f) plen 4
> HCI Event: Connect Complete (0x03) plen 11
...
> HCI Event: Link Key Request (0x17) plen 6
< HCI Command: Link Key Request Reply (0x01|0x000b) plen 22
> HCI Event: Command Complete (0x0e) plen 10
> HCI Event: Connect Complete (0x03) plen 11
< HCI Command: Read Remote Supported Features (0x01|0x001b) plen 2
< ACL data: handle 11 flags 0x02 dlen 10
    L2CAP(s): Info req: type 2
> ACL data: handle 11 flags 0x02 dlen 16
    L2CAP(s): Info rsp: type 2 result 0
      Extended feature mask 0x0000
...
... You get the idea 

Let’s see if we can find out a bit more about the Bluetooth device on the host using hciconfig:

rhempel@debian:~$ /usr/sbin/hciconfig
hci0:   Type: USB
        BD Address: aa:bb:cc:dd:ee:ff ACL MTU: 1021:8 SCO MTU: 64:1
        UP RUNNING PSCAN
        RX bytes:2145 acl:0 sco:0 events:82 errors:0
        TX bytes:2040 acl:0 sco:0 commands:82 errors:0

And now let’s scan for external Bluettoth devices, like our NXT. In this example, I’ve replaced that actual MAC of my NXT with nn:xx:tt:mm:aa:cc

rhempel@debian:~$ hcitool scan
Scanning ...
        nn:xx:tt:mm:aa:cc       OGEL

There’s OGEL in the list. Just by scanning and recognizing the NXT, the Bluetooth system will create a number of files to keep track of the devices. You can have a look at them like this (using your BT MAC):

rhempel@debian:~$ ls /var/lib/bluetooth/aa:bb:cc:dd:ee:ff/
classes  config  lastseen  names

If you’re curious, you can have a look in the files, but since they are owned by the system you’ll need to use sudo to get at them. We will have to create two files so that we can pair up with the NXT. Normally your GUI Bluetooth manager might do this, but there are so many different GUI apps, that I prefer the command line.

Again, the filename is at the top, and the lines to add or change are noted below. Let’s start with the pincodes file:

-- /var/lib/bluetooth/aa:bb:cc:dd:ee:ff/pincodes
-- Add a line that looks like this (put your desired pincode instead of 1234):

nn:xx:tt:mm:aa:cc 1234

Then the trusts file:

-- /var/lib/bluetooth/aa:bb:cc:dd:ee:ff/trusts
-- Add a line that looks like this:

nn:xx:tt:mm:aa:cc [all]

And finally we’ll edit the rfcomm.conf file:

/etc/bluetooth/rfcomm.conf

-- Edit the rfcomm0 section like so:
rfcomm0 {
        # Automatically bind the device at startup
        bind yes;

        # Bluetooth address of the device
        device nn:xx:tt:mm:aa:cc;

        # RFCOMM channel for the connection
        channel 1;

        # Description of the connection
        comment "OGEL";
}

Now, restart the Bluetooth daemon so that it catches the changes we’ve made so far. Watch the hcidump terminal window…

rhempel@debian:~$ sudo /etc/init.d/bluetooth restart

We’re finally ready to try pairing the NXT and our host machine. At this point you’ll want to type in the following command to your NXT. Copy the command so you can paste it in later again. Use your own PIN that you put in the pincodes file. And don’t forget the quotes – it’s a string!

> nxt.BtSetPIN("1234")

-- Optionally, you can run this little snippet of code that will send the
-- PIN every time you press the orange button...

function sendPIN()
  local oldButton = 0
  local newButton = 0

  while( 1 ) do
    newButton = nxt.ButtonRead()

    if 0 == oldButton then
      -- Only check buttons if no buttons were pressed!

      if 8 == newButton then
        nxt.BtSetPIN("1234") 
      end
    end

    oldButton = newButton
  end
end
  
-- And run the function (it's an endless loop - power off the NXT to stop it)
sendPIN()

OK, we’re almost done. And if you do this right, it will go way faster for your other NXTs. The following commands will attempt to connect to the device on rfcomm0, which is our NXT if everything got entered correctly.

rhempel@debian:~$ sudo rfcomm bind 0
rhempel@debian:~$ sudo rfcomm connect 0
Can't create RFCOMM TTY: Address already in use

You have about 10 seconds to enter the PIN on your NXT – that’s why I asked you to cut and paste the command for easy access. Don’t worry, if you miss it, you can attempt to connect again. Or just use the script in the previous code section that sends the PIN every time you press the ornage button.

After a successful pairing, you should see a few extra files in the Bluetooth config directory:

rhempel@debian:~$ ls /var/lib/bluetooth/aa:bb:cc:dd:ee:ff/
classes  features  lastused  manufacturers  pincodes
config   lastseen  linkkeys  names          trusts

That’s it – you’re done! Now you should restart the Bluetooth subsystem so that you don’t lose all the hard work:

rhempel@debian:~$ sudo /etc/init.d/bluetooth restart

-- Verify that rfcomm0 is assigned to OGEL
rhempel@debian:~$ rfcomm
rfcomm0: 00:16:53:04:E8:A3 channel 1 clean

And finally we’ll provide a default minicom config file that you can use to communicate with the NXT over Bluetooth.

-- One last step, create a config for minicom in your home directory
-- called .minirc.OGEL with this as the contents:
# Machine-generated file - use setup menu in minicom to change parameters.
pu port             /dev/rfcomm0
pu minit
pu mreset

You’ll have to reboot the NXT and select the Bluetooth console before trying to connect to the NXT. And it may take a few tries to get minicom to connect properly.

Here’s the final result…

rhempel@debian:~$ minicom OGEL
Welcome to minicom 2.3

OPTIONS: I18n 
Compiled on Oct 24 2008, 06:37:44.
Port /dev/rfcomm0

                 Press CTRL-A Z for help on special keys
>