Talking to a Bluetooth Serial Module with a Raspberry Pi




Bluetooth is a very low cost and flexible way to add wireless communication to your projects. However, it can also be a bit tricky to set up. In this post we show you how to set up a Raspberry Pi with a USB Bluetooth dongle so that it can communicate with an Arduino using a Bluetooth serial module.

Once the set up is complete, we’ll have a new serial port on the Raspberry Pi that can be used to communicate with the serial Bluetooth module, either using a program such as Cutecom, or using one of the many serial programming libraries such as pySerial.

This tutorial is aimed at a Raspberry Pi running Raspbian, but it should work on other popular Linux distributions such as Ubuntu. To get a USB serial port set up on Windows, a good tutorial can be found here.

Talking to a Bluetooth Serial Module with a Raspberry Pi

Required Materials

  • Raspberry Pi with Raspbian installed (this tutorial may also work with other distributions)
  • USB Bluetooth dongle
  • Arduino, or an Arduino compatible device such as a Seeeduino
  • A serial Bluetooth module

Preparing the Arduino/Seeeduino for Connection

To test out the Bluetooth serial connection we’ll use a Seeeduino attached to a Bluetooth serial module. First upload the following sketch to the Seeeduino. This program listens on the serial connection for 2 numbers, and then adds them together before sending them back over the serial connection.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void setup()
{
    Serial.begin( 9600 );    // 9600 is the default baud rate for the
                              // serial Bluetooth module
}
void loop()
{
    // Listen for data on the serial connection
    if ( Serial.available() > 1 )
    {
        // Read in 2 numbers
        float a = Serial.parseFloat();
        float b = Serial.parseFloat();
        // Add them together and return the result
        Serial.println( a + b );
    }
}

Connect the two devices up as shown in the diagram below. These connections will power the Bluetooth serial module from the 5V line of the Seeeduino, and connect RX => TX and TX => RX. Whilst the Bluetooth serial module is connected to the UART lines of the Seeeduino, you won’t be able to program the Seeeduino, but you can still power it with a USB cable.

Setting up the USB Bluetooth Dongle and Pairing it with the Bluetooth Serial Module

Plug the USB Bluetooth dongle into the Raspberry Pi. Then open up a command line terminal and run the following commands

sudo apt-get update
sudo apt-get install bluetooth bluez-utils blueman

Get the name of your USB Bluetooth dongle by running

hciconfig

It should be something like ‘hci0′

Now, ensuring that the Seeeduino is powered on, run the following command to find out the address of the serial Bluetooth module.

citool scan

After a short delay, this should return the addresses of nearby Bluetooth devices. The Bluetooth serial module that we sell should be called something like ‘linvor’.

Before Bluetooth devices can communicate, they need to be paired. This can be done by running the following command

sudo bluez-simple-agent hci# xx:xx:xx:xx:xx:xx

where # is the number of your device (probably hci0) and xx:xx:xx:xx:xx:xx is the address of the serial Bluetooth module. After a pause, this program should ask you for the pin code of the Bluetooth module. By default, the pin for the module we sell is 1234.

At this point, we have 2 Bluetooth devices that can communicate with each other, but we need to set up a protocol called RFCOMM so that they can communicate over a serial connection.

 

For more detail: Talking to a Bluetooth Serial Module with a Raspberry Pi


© 2015 Powered By Engineering Projects Team, Raspberry Pi Projects

Scroll to top