Arduino, Bluetooth, Linux
Back to work on the Ardbot. I've decided to build my control panel competely in java/JavaFX on Linux. One challenge was getting my Bluetooth connectivity going on Linux with this configuration:
- Kensington USB Bluetooth dongle on Fedora 11 PC
- Sparkfun Bluetooth modem on robot
My goal will be to control the robot from a gamepad controller attached to the PC, wirelessly over the Bluetooth link.
To test the connectivity, I came up with a simple Arduino sketch that reads data from the Sharp IR range sensor and sends the distance, in centimeters, out over the wireless link, to be read by whatever program on the PC. Here is the Arduino code:
#include <NewSoftSerial.h>
NewSoftSerial blueSerial(11,8);
int irpin = 0;
int distance = 0;
void setup() {
blueSerial.begin(9600);
}
void loop() {
distance = analogRead(irpin);
blueSerial.println(read_gp2d12_range(distance));
delay(1000);
}
float read_gp2d12_range(int distance) {
if (distance < 3)
return -1; // invalid value
return (6787.0 /((float)distance - 3.0)) - 4.0;
}
The robot is wired like this (i'm leaving out the power, gnd, etc wiring):
Arduino analog pin 0 ---> Sharp IR sensor data pin (yellow wire)
Arduino digital pin 11 ---> BT modem TX pin
Arduino digital pin 8 ---> BT modem RX pin
With the Arduino powered up, if everything is wired correctly, the red power light on the BT modem should come on.
After inserting the USB Bluetooth dongle on my PC, I ran the following commands from the linux command line to get connected:
hcitool scan
this will give me the hardware address of the robot's BT modem - my results came back like this
Scanning ...
01:1E:09:0F:0A:15 SparkFun-BT
To connect, I used the following command, using the hardware address from running hcitool scan:
sudo rfcomm connect 1 01:1E:09:0F:0A:15
If all is well, I should see:
Connected /dev/rfcomm1 to 01:1E:09:0F:0A:15 on channel 1
Press CTRL-C for hangup
There are a couple of ways to see the data coming across the wireless connection. The easiest way that i could think of was to run the cat command in a separate terminal window:
sudo cat /dev/rfcomm1
what do you know, i'm reading the sensor data wirelessly!
68.98
68.20
65.97
68.98
68.20
67.44
every second, the bot is sending the distance to an object in centimeters from the front mounted IR sensor.
To disconnect, from a separate terminal window run this command:
sudo rfcomm release 1
In the original window, you'll see:
Disconnected
More to follow!



@ Sat, 2009-09-19 22:11
There were a couple of us
There were a couple of us that played around with using Processing to make an interface.
I did this with one of the bluesmirf modules and a small BT usb key on my comp.
I wasn't able to get a stable connection or at least I wasn't in the Processing app. I'd have an issue with the timing of starting the bluesmirf then the app at around the same time to get reliable connectivity. I however had no issues when i connected to the bt device via the windows term app no special timing was required.
http://letsmakerobots.com/node/6980
Also a curiosity on the cat command, was that reading continusously(not aware that it could do this) or was that buffered data?
Wouldn't tail -f /dev/rfcomm1 be more ideal for continuous reading?
@ Sun, 2009-09-20 20:48
cat...
You are correct regarding the cat command - it was just reading data in the serial port buffer, not reading continuously - tail might work better for this kind of thing from the command line as you mentioned....
I've also noticed some weirdness in getting the RXTX lib to read from the serial port - it seems to hang every once in awhile, flushing the buffer with cat seemed to help for some reason...anyway, i'll keep posting what works and what doesn't...
@ Sat, 2009-09-19 22:20
cat command
Hi good question - i didn't try tail but maybe that would be better...cat was scrolling data down the screen and it seemed like real-time, that is i could move something in front of the sensor and the data would change...
My plan is to use the RXTX java library to hand the COM port stuff...it seems to work pretty well on Windows but haven't tried on Linux yet....
see http://www.rxtx.org/
@ Sat, 2009-09-19 22:32
Yep. thats what Processing
Yep. thats what Processing uses for it's serial com lib.
I highly recommend checking it out. You can also integrate java into your code.
@ Sat, 2009-09-19 23:29
Thanks for the tip...
@ Sun, 2009-09-20 00:19
I'd be interested to see how
I'd be interested to see how things work out with the rxtx lib and if it works better for you if you end up using it out of processing.
I will be keeping an eye on your updates here!