My Cart

Bluetooth module PRO for Arduino and Raspberry Pi

Difficulty Level: Intermediate -


Introduction

This tutorial describes some features of Bluetooth module PRO for Arduino / Raspberry Pi which has been mainly designed to discover high amount of bluetooth devices in a variable area. Besides that, connection processes between two bluetooth modules are shown.
It has to be mentioned that inquiry processes of bluetooth module are anonymous due to only MAC address is obtained from the bluetooth remote device. No account or phone numbers are obtained. These facts allows saving privacy of bluetooth users.

Ingredients

Preparation Time: 20 minutes

Step 1: Bluetooth module PRO

Bluetooth module can be connected in a Xbee shield for Arduino or in the Raspberry Pi to Arduino Shields connection bridge.

Technical characteristics:

  • Chip: Bluegiga WT12
  • Bluetooth v2.1 + EDR. Class 2
  • TX Power: 7 Power levels [-27dBm, +3dBm]
  • RX sensitivity: -90 dBm
  • Antenna: 2dBi
  • Range: from 10 to 50 meters depending tx power and environment conditions.
  • Received Strength Signal Indicator (RSSI) for each scanned device
  • Class of Device (CoD) for each scanned device

Special features:
Bluetooth module integrates a firmware called Iwrap, developed by manufacturer which offers many possibilities. Refer to link & documentation section for more information.
Moreover, Libelium has developed a specific API to allow using this module with Waspmote. There are many useful functions which can be used as a guideline for other designs. Refer to Libelium web site for more information.

Power consumption
Bluetooth module has different power consumption according its operating state. Next table shows information about it.

Estate Power consumption
OFF 0
Sleep < 0.5 mA
ON (IDLE state) 2 mA
Inquiry at minimum power 33.5 mA
Inquiry at maximum power 36.5 mA

Step 2 : Device discovery

NOTE: Pay attention when using configuration commands and always be sure of what you are doing. Otherwise bluetooth module can become unresponsive and blocked.

Device discovery with Arduino

For a device discovery example connect bluetooth module PRO in Gateway mode. An Arduino and a Xbee shield will be needed. Now you have two options: you can upload a "BareMinimum" to Arduino or remove the Microcontroller (Atmega328) from Arduino to use it in gateway mode. Jumpers must be set to USB position in both options.

Using gateway mode, commands can be sent from our PC to bluetooth module. The module has and integrated firmware called IWRAP which allows several possibilities. See links & documentation section to see where you can find a description of all supported commands of bluetooth module and IWRAP firmware. Commands are not case sensitive.
Once gateway is ready let's open a serial monitor (Cutecom in this case) and open serial port with parameters 38400 bps, 8 data bits,parity none and 1 stop bit. To test communication you can simply type “AT” command and “OK” will be returned.
The command used to carry out a device discovery is “INQUIRY {timeout}” . Timeout can be a number between 1 and 48 and it will determine time spent searching for devices. The module will answer with a list of discovered devices as in shown in picture below.

In this case, module has discovered three devices, showing each one in a different line with next data fields:

  • MAC address, which is a unique identification number of a bluetooth device. It has 12 hexadecimal digits separated by “:”. One example could be “12:34:56:aa:bb”.
  • CoD (Class of Device): Bluetooth devices are classified according to the device which they are integrated. Therefore a vehicle hands free device will belong to a different class than a pedestrian mobile phone. This parameter has 6 hexadecimal digit and it allows distinguish if detected bluetooth device is a vehicle, a pedestrian, and so on.
  • RSSI (Received Signal Strength Indicator): This parameters shows quality of the radio link. It can be used to know distance between bluetooth module and inquired device. It is shown as a negative value rounding -40 dBm (close devices) and -90 dBm (far devices)

By default, bluetooth module PRO is in visible mode, so you are able to discover it with other bluetooth modules. If you want to change to NOT visible mode use command “SET BT PAGEMODE 0” and if you want to make it visible again just use “SET BT PAGEMODE 4” .
If the user wants to know also “friendly name” of remote devices, the inquiry command must include “NAME” flag following timeout parameter, for instance “INQUIRY 5 NAME” , and name will be shown at the end of module response. Next image shows an example of it.

It could be that a bluetooth device has friendly name property disabled, in this case bluetooth module will answer with an event like “NAME ERROR 0x104 {MAC} HCI_ERROR_PAGE_TIMEOUT” .
If the user wants to stop inquiry processes in a certain moment, IC should be used (Inquiry Cancel). It can be useful for example to limit number of discovered devices. However, this command has to be used before summary is given by module, and name resolution processes can not be canceled.
Furthermore, “SLEEP” command can be used to reduce power consumption of bluetooth module. It has to be taken into account that awaking module by UART will produce the lost of first byte sent, which is used to wake up the module. Other useful commands are “INFO” and “SET” which show information about module.
There is another command which uses the internal temperature sensor of bluetooth module. It has no relevance in bluetooth actions but it can be used as a reference of the approximated temperature surrounded module. Just type “TEMP” command and temperature will be answered in Celsius degrees. Take into account that refresh rate of temperature sensor is not very high.

Device discovery with Raspberry Pi:

For a device discovery example we will connect bluetooth module PRO in Gateway mode. Using gateway mode, commands can be sent from our Raspberry Pi to bluetooth module. The module has and integrated firmware called IWRAP which allows several possibilities. See links & documentation section to see where you can find a description of all supported commands of bluetooth module and IWRAP firmware. Commands are not case sensitive. To stablish UART connection with the module you can use several programs as cutecom in Raspbian graphical environment or minicom in a terminal window in order to use it via SSH. For using minicom follow these steps:

  1. Install minicom (only if you haven't done it before):
    sudo apt-get install minicom

  2. Open comunication with the module UART, 115200bps is the default baud rate:
    minicom -b 115200 -o -D /dev/ttyAMA0

  3. To test communication you can simply type “AT” command , press Enter and “OK” will be returned. The first response of the module may be “SYNTAX ERROR”, it's a normal behavior due to unwanted bytes at the beginning of the communication.

  4. Now type the INQUIRY command followed with a timeout. Timeout can be a number between 1 and 48 and it will determine time spent searching for devices.
    INQUIRY 10

    The module will answer with a list of discovered devices as in shown in picture below.

  5. To exit minicom press CTRL+A, then press X and Yes.

In this case, module has discovered two devices, showing each one in a different line with next data fields:

  • MAC address, which is a unique identification number of a bluetooth device. It has 12 hexadecimal digits separated by “:”. One example could be “12:34:56:aa:bb”.
  • CoD (Class of Device): Bluetooth devices are classified according to the device which they are integrated. Therefore a vehicle hands free device will belong to a different class than a pedestrian mobile phone. This parameter has 6 hexadecimal digit and it allows distinguish if detected bluetooth device is a vehicle, a pedestrian, and so on.
  • RSSI (Received Signal Strength Indicator): This parameters shows quality of the radio link. It can be used to know distance between bluetooth module and inquired device. It is shown as a negative value rounding -40 dBm (close devices) and -90 dBm (far devices)

By default, bluetooth module PRO is in visible mode, so you are able to discover it with other bluetooth modules. If you want to change to NOT visible mode use command “SET BT PAGEMODE 0” and if you want to make it visible again just use “SET BT PAGEMODE 4” .
If the user wants to know also “friendly name” of remote devices, the inquiry command must include “NAME” flag following timeout parameter, for instance “INQUIRY 5 NAME” , and name will be shown at the end of module response. Next image shows an example of it.

It could be that a bluetooth device has friendly name property disabled, in this case bluetooth module will answer with an event like “NAME ERROR 0x104 {MAC} HCI_ERROR_PAGE_TIMEOUT” .
If the user wants to stop inquiry processes in a certain moment, IC should be used (Inquiry Cancel). It can be useful for example to limit number of discovered devices. However, this command has to be used before summary is given by module, and name resolution processes can not be canceled.
Furthermore, “SLEEP” command can be used to reduce power consumption of bluetooth module. It has to be taken into account that awaking module by UART will produce the lost of first byte sent, which is used to wake up the module. Other useful commands are “INFO” and “SET” which show information about module.
There is another command which uses the internal temperature sensor of bluetooth module. It has no relevance in bluetooth actions but it can be used as a reference of the approximated temperature surrounded module. Just type “TEMP” command and temperature will be answered in Celsius degrees. Take into account that refresh rate of temperature sensor is not very high.

Step 3 : Connecting devices without security

Connecting Arduinos without security

If the user wants to make a basic transparent link between two bluetooth modules, CALL command should be used. Destination address must be also included. Just connect the two bluetooth modules in gateway mode and open both serial monitors. Type on module 1 next command, with address of module 2:

CALL 00:07:80:4b:2b:c4 1101 RFCOMM

After sending this command you will see “CALL” event followed to link_ID of the call and “CONNECT” event on module 1 side and “RING” event on module 2 side. Next picture shows all this process carried out, whit module 1 serial monitor on the right and module 2 serial monitor on the left.
After this events all data sent through each serial monitor will be sent to the other one. If the user want to send a command to module, escape sequence of “+++” must be sent (without end of line characters) and then module will be listening for commands. At the end, the call can be ended using “CLOSE” command followed with link_ID, and events NO CARRIER 0 and ERROR 0 will be received if everything is OK.

(example code of an Arduino connecting with a bluetooth module in gateway mode and sending “hello word”)

Connecting two Rasperry Pi without security

If the user wants to make a basic transparent link between two bluetooth modules, follow these steps:

  1. Install minicom (only if you haven't done it before):
    sudo apt-get install minicom

  2. Open comunication with the module UART, 115200bps is the default baud rate:
    minicom -b 115200 -o -D /dev/ttyAMA0

  3. To test communication you can simply type “AT” command , press Enter and “OK” will be returned. The first response of the module may be “SYNTAX ERROR”, it's a normal behavior due to unwanted bytes at the beginning of the communication.

  4. Now type the CALL command in module 1 with the number 2 module destination adress number:
    CALL 00:07:80:49:58:b5 1101 RFCOMM

    You will see in module 1:
    CALL 0
    CONNECT 0 RFCOMM 1

    And in module 2:
    RING 0 00:07:80:0a:a7:de 1 RFCOMM

  5. After this events all data sent through each serial monitor will be directly sent to the other one.

  6. To close connection enter the escape sequence in one of the modules (without end of line characters so don't press Enter button) and wait a second:
    +++

    Yoy will see:
    READY.

  7. Then module will be listening for commands, the call can be ended using “CLOSE” command followed with link_ID:
    CLOSE 0

  8. Finally you will see in both terminals:
    NO CARRIER 0 ERROR 0

  9. To exit minicom press CTRL+A, then press X and Yes.

Step 4 : Connecting devices with security

Connecting two Arduinos with security

Connecting with security means that a PIN code will be necessary to open a connection. If the user wants to connect two Arduino with bluetooth module PRO, next steps should be followed.
First of all, same PIN code must be set in each module (in this example “1234”) using the command “SET BT AUTH * 1234” . This command saves PIN code into non-volatile memory of bluetooth module and enables security for all connections. PIN code length is configurable from 0 to 16 digit. If PIN code is set OK, same command will be returned. Also PIN code can be read using “SET” command. If the user wants to delete PIN code, it can be done with “SET BT AUTH * ” (with no pin code).
After setting same PIN code on both sides, process is equal to previous section “connecting without security” except for one detail. Devices will be paired automatically if their connection is successful. This can be checked also using ”SET” command and reading line “SET BT PAIR ...” .
However, pairing process can be done manually using “PAIR {MAC ADDR}” and the paired device will become into a trusted device (if bluetooth devices has same PIN code, or both have no pin code set). Next image shows how to set PIN code, enabling security and also how to pair a trusted device.
After setting PIN code and pairing devices, just use “CALL” command as in previous section to open a transparent connection.

Connecting two Raspberry Pi with security

If the user wants to make a basic transparent link between two bluetooth modules, follow these steps:

  1. Install minicom (only if you haven't done it before):
    sudo apt-get install minicom

  2. Open comunication with the module UART, 115200bps is the default baud rate:
    minicom -b 115200 -o -D /dev/ttyAMA0

  3. To test communication you can simply type “AT” command , press Enter and “OK” will be returned. The first response of the module may be “SYNTAX ERROR”, it's a normal behavior due to unwanted bytes at the beginning of the communication.

  4. Same PIN code must be set in both modules (in this example “1234”). The next command saves PIN code into non-volatile memory of bluetooth module and enables security for all connections. PIN code length is configurable from 0 to 16 digit.
    SET BT AUTH * 1234

    To check if PIN is correctly set you can type “SET” command. If the user wants to delete PIN code, it can be done with “SET BT AUTH * ”(with no pin code).

  5. After setting same PIN code on both sides, process is equal to previous section “connecting without security” except for one detail. Devices will be paired automatically if their connection is successful. This can be checked also using ”SET” command and reading line “SET BT PAIR ...”. Now type the CALL command in module 1 with the number 2 module destination adress number:
    CALL 00:07:80:49:58:b5 1101 RFCOMM

  6. You will see in module 1:
    CALL 0
    CONNECT 0 RFCOMM 1

    And in module 2:
    RING 0 00:07:80:0a:a7:de 1 RFCOMM

  7. Pairing process can be also done manually using “PAIR {MAC ADDR}” and the paired device will become into a trusted device (if bluetooth devices has same PIN code, or both have no pin code set).

  8. After this events all data sent through each serial monitor will be directly sent to the other one.

  9. To close connection enter the escape sequence in one of the modules (without end of line characters so don't press Enter button) and wait a second:
    +++

    Yoy will see:
    READY.

  10. Then module will be listening for commands, the call can be ended using “CLOSE” command followed with link_ID:
    CLOSE 0

  11. Finally you will see in both terminals:
    NO CARRIER 0 ERROR 0

  12. To exit minicom press CTRL+A, then press X and Yes.

Step 5 : Connecting devices to a PC without security

In this case, an standard bluetooth USB dongle will be connected with bluetooth module PRO inserted in Arduino or Raspberry Pi (gateway mode). The PC runs under Linux.
First of all, connect USB dongle to PC, open a terminal window and use “hcitool” to scan for bluetooth module PRO typing “hcitool scan” . All discovered devices will be listed with MAC address and friendly name. In this point can be seen if USB dongle is able to detect Arduino with bluetooth PRO module.
After that, use “sdptool” to discover remote device services and channel used for RFCOMM, typing “sdptool search sp” . Search lines corresponding to Bluetooth PRO module and see rfcomm channel like in next picture.

Then, open a connection with Bluetooth module typing command “rfcomm connect 0 {MAC_ADDR} {Channel}” and answer will show rfcomm port where USB is connected (rfcomm0 in this case).
Finally, just go to a serial monitor and open port “\dev\rfcomm0” . Now there is a transparent connection between two bluetooth modules, as is shown below.

Other useful hcitool commands are:

  • hcitool dev : shows hci devices connected to PC.
  • hcitool name {mac_addr} : shows name of given mac address.

Moreover, “hciconfig” is also useful to configure some features of bluetooth modules. Some useful commands are:

  • hciconfig : show each hci device with its main features.
  • hciconfig hciX name Bluetooth_PRO : Set local device “X” name as bluetooth_PRO.
  • hciconfig hciX piscan : Makes device “X” visible for inquiries.

Instead of using Arduino in gateway mode, you can upload a code like next one and see how to make a transparent connection.

Step 6 : Connecting a device to a PC with security

This step can be carried out in same way as step 5 processes. The only difference is that bluetooth module Pro should have set a PIN code (using SET BT AUTH * {PIN_CODE} ), and this code will be prompted when “sdptool search sd” is typed into a terminal. Just enter same PIN code and both devices will be paired automatically and you can continue as in step 5 to open a transparent connection.

Device Compatibility

This shield is tested and fully compatible with the following boards:

Arduino Boards

  • Arduino Uno

Raspberry Pi Boards

  • Raspberry Pi
  • Raspberry Pi (Model B+)
  • Raspberry Pi 2
  • Raspberry Pi 3

Fritzing Libraries

Download Fritzing Bluetooth module PRO for Arduino

Bluetooth Module PRO for Arduino has been mainly designed to discover high amount of bluetooth devices in a variable area. It can be connected in a Xbee shield for Arduino to work.

You can download our Fritzing libraries from this area .

Links and documentation