My Cart

Bluetooth Module for Raspberry Pi

Difficulty Level: Intermediate -

Introduction

Ingredients:

Preparation Time: 45 minutes

NOTE: If you don't have a Raspberry Pi you can get one buying a Raspberry Pi Starter Kit.
NOTE: All the code examples in this tutorial use the arduPi library. You can see the documentation and download the library here.

Step 1: The Bluetooth module:

The Bluetooth module can be connected directly in the Raspberry Pi to Arduino shield.

Technical characteristics:

Bluetooth Chip: eUnistone 31308/2

Version: Bluetooth 2.0 + EDR (Configurable BT 1.2)

TX Power: 2.5dBm

RX Sensitivity: -86dBm

Antenna: 2dBi

Outdoor Range: 60 m LOS (Line of sight)

Indoor Range: 40 m NLOS (Non line of sight)

(Range measured between the Bluetooth module for Arduino and the BT Linksys dongle)

Special Options:

  • Minimum and Maximum RSSI thresholds control
  • Adaptive Frequency Hopping
  • Security: Authentication, Pairing and Encryption
  • Store 5 different trusted devices in its internal memory
  • Master or Slave operation supported

Step 2: Device discovery:

For the device discovery example we are going to access the bluetooth module through a graphical serial port communications program (cutecom).

Using cutecom we can send / receive AT commands directly from the Raspberry Pi to the Bluetooth module.

One of the devices we'll find is a Linksys USB-Bluetooth adapter connected to a PC.

Open the serial port terminal (Cutecom) on the Raspberry Pi using 115200 bps, 8 data bits,1 stop bit, no parity and no flow control. The AT command for discovering devices is JDDS, the command to send is AT+JDDS=0. The response to this command will be a list of the bluetooth devices close to my module.

For each device we get its Bluetooth address (12 hexadecimal numbers), Remote name and class.

Step 3: Service discovery:

Service discovering is a tool that we can use to discover services in a bluetooth device.

For example, we add Serial port service to our Bluetooth PC (Linksys Bluetooth Adapter) using the command sdptool add SP.

Now we can connect the bluetooth module in gateway mode to the PC and launch Cutecom.

We discover bluetooth devices (AT+JDDS=0) and then we discover the Serial port service in the Bluetooth address 0016B6EE445D.

The command used is AT+JSDS=0016B6EE445D,1101

0016B6EE445D is the bluetooth address

1101 is the serial port service code

The module answers with the Serial ports detected in the other device.

Step 4: Connect without security:

In this basic example we'll use a Raspberry Pi with bluetooth module and a PC with a bluetooth USB dongle (Linksys Bluetooth Adapter).

The Raspberry Pi will send a message (Hola caracola...) every 2 seconds and we receive it in the PC.

Raspberry Pi code:

Code:
/*
   *  Sending data using Bluetooth module from Libelium for Arduino
   *  Connect without security
   *
   *  Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
   *  http://www.libelium.com
   *
   *  This program is free software: you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation, either version 3 of the License, or
   *  (at your option) any later version.
   *
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program.  If not, see .
   *
   *  Version 0.1
   *  Author: Marcos Yarza
   */

   //Include ArduPi library
   #include "arduPi.h"
  
   
   int led = 8;
   int val = -1;

   void setup(){
   delay(2000);
   Serial.begin(115200);
   delay(2000);
   Serial.print("AT+JSEC=1,1,1,04,1111\r\n");                       // Enable security command
   delay(2000);
   Serial.print("AT+JDIS=3\r\n");                                   // Discorable command
   delay(2000);
   Serial.print("AT+JRLS=1101,11,Serial Port,01,000000\r\n");     // Register local sevice command
   delay(2000);
   Serial.print("AT+JAAC=1\r\n");                       // Auto accept connection requests command
   delay(2000);
   pinMode(led, OUTPUT);
   digitalWrite(led,HIGH);
   Serial.flush();
   val = Serial.read();
   while (val != 'R'){
      val = Serial.read();
   }
   delay(1000);
   Serial.print("AT+JSCR\r\n");                               // Stream Connection Request command
   }

   void loop(){
   Serial.println("Hola caracola...");
   delay(2000);
   }

   int main (){
     setup();
     while(1){
       loop();
     }
     return (0);
   }					
                

Setting up the PC

In this example we use a Debian Linux PC with an USB Linksys Bluetooth Adapter From a terminal we launch these commands:

Command  
hcitool scan Scan the Bluetooth devices. We'll use the MAC addres of the Bluetooth module in Arduino (In this case 00:03:19:05:2B:9A)
sdptool search SP Discover the channel (In this case channel 1)
rfcomm connect 0 00:03:19:05:2B:9A 1 Connect to the Bluetooth module MAC: 00:03:19:05:2B:9A channel:1

Now we have connected the PC bluetooth with the Raspberry Pi, as we can see, the serial port will be /dev/rfcomm0. So we can connect this port using a serial port monitor (Cutecom - 115200 bps, 8 data bits no parity 1 stop bit) and receive data from the Raspberry Pi with bluetooth module:

Step 5: Connect with security PIN:

This example is very similar to the previous one, with the difference that now we use the PIN security for the connection between Raspberry Pi with bluetooth module and PC.

Raspberry Pi code:

Code:
 /*
   *  Sending data using Bluetooth module from Libelium for Arduino
   *  Connect with security PIN
   *
   *  Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
   *  http://www.libelium.com
   *
   *  This program is free software: you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation, either version 3 of the License, or
   *  (at your option) any later version.
   *
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program.  If not, see .
   *
   *  Version 0.1
   *  Author: Marcos Yarza 
   */

   //Include ArduPi library
   #include "arduPi.h"
  
   
   // variables
   int led = 8;
   int val = -1;

   void setup(){
   delay(2000);
   Serial.begin(115200);
   delay(2000);
   Serial.print("AT+JSEC=3,1,1,06,123456r\n");     // Register local sevice security PIN activated
   delay(2000);
   Serial.print("AT+JDIS=3\r\n");                     // Discoverable Inquiry & Page Scan enabled
   delay(2000);
   Serial.print("AT+JRLS=1101,11,Serial Port,01,000000\r\n");  // Register local service
   delay(2000);
   Serial.print("AT+JAAC=1\r\n");                        // Auto accept
   delay(2000);
   Serial.print("AT+JPCR=06,123456\r\n");                // PIN code reply
   delay(2000);
   pinMode(led, OUTPUT);
   digitalWrite(led,HIGH);
   Serial.flush();
   val = Serial.read();                                  // wait for connection
   while (val != 'R'){
      val = Serial.read();
   }
   delay(1000);
   Serial.print("AT+JSCR\r\n");                         // Stream Connection Request
   
   
   }

   void loop(){
   digitalWrite(led, LOW);
   Serial.println("Hola caracola (connected with PIN 123456)...");
   delay(1500);
   digitalWrite(led,HIGH);
   delay(500);
   }

   int main (){
     setup();
     while(1){
       loop();
     }
     return (0);
   }					
                

Setting up the PC

From a terminal we launch these commands:

Command  
hcitool scan Scan the Bluetooth devices. We'll use the MAC addres of the Bluetooth module in Arduino (In this case 00:03:19:05:2B:9A)
rfcomm connect 0 00:03:19:05:2B:9A 1 Connect to the Bluetooth module MAC: 00:03:19:05:2B:9A channel:1

When the PC try to connect to the module, we get a Pin request window.

We type the PIN code "123456" and the connection is established. We can open Cutecom and check the messages sent from the Raspberry Pi.

Step 6: Connecting two Bluetooth modules:

Here we're going to connect two Raspberry Pi with bluetooth module. In one we will use cutecom and on the other we will run a program.

Commands for the Raspberry using cutecom:

AT+JSEC=1,1,1,04,1111

AT+JCCR=0003190d1064,01 ==> 0003190d1064 is the MAC address of the Bluetooth in Arduino

AT+JSCR

After each command you must send

Raspberry Pi code:

Code:
/*
  *  Sending data using Bluetooth module from Libelium for Arduino
  *
  *  Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
  *  http://www.libelium.com
  *
  *  This program is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>
  *
  *  Version 0.2
  *  Author: Marcos Yarza
  */

  //Include ArduPi library
  #include "arduPi.h"

  int led = 8;
  int val = -1;

  void setup(){

  Serial.begin(115200);
     pinMode(led, OUTPUT);
  digitalWrite(led,LOW);
  delay(4000);
  Serial.print("AT+JRES\r\n");
  val = Serial.read();
  while(val != 'R'){
     val = Serial.read();
  }
  delay(500);
  Serial.print("AT+JSEC=1,1,1,04,1111\r\n");           // Register local sevice
  val = Serial.read();
  while(val != 'O'){
     val = Serial.read();
  }
   delay(500);
  Serial.print("AT+JDIS=3\r\n");
  val = Serial.read();
  while(val != 'O'){
     val = Serial.read();
  }
   delay(500);
  Serial.print("AT+JRLS=1101,11,Serial Port,01,000000\r\n");
  val = Serial.read();
  while(val != 'O'){
     val = Serial.read();
  }
   delay(500);
  Serial.print("AT+JAAC=1\r\n");
  val = Serial.read();
  while(val != 'O'){
     val = Serial.read();
  }
  delay(2000);
  pinMode(led, OUTPUT);
  digitalWrite(led,HIGH);
 
  val = Serial.read();
  while (val != '+'){
     val = Serial.read();
  }
 
  digitalWrite(led,LOW);
  delay(1000);
  Serial.print("AT+JSCR\r\n");                         // Stream Connection Request
      }

  int count = 0;

  void loop(){
 
  Serial.print("Hola caracola...");
  Serial.print(" Counter: ");
  Serial.println(count);
  count++;
  digitalWrite(led,HIGH);
  delay(2000);
  digitalWrite(led,LOW);
  delay(500);
  }

  int main (){
    setup();
    while(1){
      loop();
    }
    return (0);
  }