Sunday, April 13, 2014

Connecting to an EV3 running LeJOS using Bluetooth

I had known all along that the EV3 comes equipped with Bluetooth and that LeJOS allowed you to write programs that employed Bluetooth for communication, but the whole truth is way cooler.
I learned only recently that with Bluetooth activated, LeJOS running on the EV3 implements a Bluetooth PAN (Personal Area Network) by becoming a GN (Group ad-hoc Network) Controller.
This means the EV3 is acting as a server on a bluetooth-based network. You can connect your computer to this network using bluetooth. The computer acts as a PANU (a client of the PAN) and is able to ssh in to the EV3 and perform all of the actions we were performing using the WiFi dongle. The immediate advantage is that one does NOT need the WiFi dongle (saves money and you don't have the dongle protruding off of the side of the EV3).
Bluetooth on Linux (my experience is with Ubuntu 12.04 and 13.04) is still buggy but the setup for connecting to the EV3 was surprisingly painless once I figured it out (that part was harder).
The first step is to confirm that bluetooth is running on your computer by running the following command in a terminal:
hciconfig -a
This will spit out a bunch of information:
hci0: Type: BR/EDR  Bus: USB
 BD Address: 00:16:CF:FE:03:A0  ACL MTU: 1017:8  SCO MTU: 64:8
 UP RUNNING PSCAN ISCAN 
 RX bytes:301012 acl:2298 sco:0 events:460 errors:0
 TX bytes:94808 acl:966 sco:0 commands:67 errors:0
 Features: 0xff 0xff 0x8d 0xfe 0x9b 0xfd 0x00 0x80
 Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 
 Link policy: RSWITCH HOLD SNIFF PARK 
 Link mode: SLAVE ACCEPT 
 Name: 'Abid-Laptop'
 Class: 0x6e010c
 Service Classes: Networking, Rendering, Capturing, Audio, Telephony
 Device Class: Computer, Laptop
 HCI Version: 2.0 (0x3)  Revision: 0x20da
 LMP Version: 2.0 (0x3)  Subversion: 0x4182
 Manufacturer: Broadcom Corporation (15)
Look for "Networking" in 'Service Classes' and "Computer" in 'Device Class'. In addition we need a useful 'Name'. To change it issue:
sudo hciconfig hci0 name '<device name>'
To connect to the EV3 (running LeJOS) turn it on and navigate to the "Bluetooth" menu. Make sure that it says "Visibility on". Now on your computer issue:
hcitool scan
The response should look like:
Scanning ...
 00:16:53:40:E9:7D EV3
We will need the Bluetooth address of the EV3 (the 12 hexadecimal number interspersed with colons) and the IP address of the EV3 on the PAN (displayed on the EV3 screen - 10.0.1.1 in my case). To connect to the EV3 as a PANU client one uses the pand utility (from the bluez-compat package):
sudo pand --connect 00:16:53:40:E9:7D
The PAN has been established but we need to start the bnep0 network interface manually on the computer to allow us to access the EV3 over the bluetooth PAN. Since the EV3 has IP address 10.0.1.1 we choose for our computer 10.0.1.2 (the first three numbers (sub-domain) need to match, the last one can be any number from 2 to 255):
sudo ifconfig bnep0 10.0.1.2
To confirm that the bnep0 network interface is active simply run:
ifconfig
and look for:
bnep0     Link encap:Ethernet  HWaddr 84:a6:c8:9b:ce:58  
          inet addr:10.0.1.2  Bcast:10.255.255.255  Mask:255.0.0.0
          inet6 addr: fe80::86a6:c8ff:fe9b:ce58/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:670 errors:0 dropped:0 overruns:0 frame:0
          TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:30820 (30.8 KB)  TX bytes:428 (428.0 B)

To confirm connectivity with the EV3 we ping it:
ping -c3 10.0.1.1
One can ssh in to the EV3 using IP Address 10.0.1.1, username "root" and blank password:
ssh root@10.0.1.1
It helps with automation if you set up an SSH private/public key-pair and store the public key in /home/root/.ssh/authorized_keys allowing password-less access. Finally to disconnect the computer from the EV3 PAN issue:
sudo pand -K