Arduino, Linux and bluetooth

– This project was a little tricky, basically to pair the bluetooth board, and the raspberry I used.

Bluetooth shield

Bluetooth shield

– I used a bluetooth shield on a standard Uno Arduino. There is also a small bluetooth module, as I also have one of this. Bluetooth HC-05 module
I will migrate my Meteorological station for a small Arduino nano, so I can reuse my Arduino Uno again.

Bluetooh shield It can be found at Dx.com and ebay.

Well, let´s go to Arduino bluetooth. First, for my shield I found an excellent description article in this link : Bluetooth Shield

It came correctly configured by default, but before I put it to work, I changed my configurations, so it is very useful to know the correct meanings of
every switch and configuration.

Here is the pinout:

Bluetooth shield pinout

Bluetooth shield pinout

There is a complete description of At commands, specific for the Bt module in the shield, it is the HC-05 bluetooth, At commands for HC-05 , the same one you can buy without the shield.

The Arduino shield will only need the Serial standard library. The bluetooth works like old modems, with the AT interface. The real trick is to find the correct commands and pair both sides correcly.

In my case I used the arduino bluetooth shield as server, as this is the default, no need to send ‘at role’ command,

I had only to use at+init and at+inq, these two commands do a correct configuration on default mode.

The real trick was on the raspberry ( or any linux ) .
I used rfcomm, part of bluez package. Just install bluez package. Then you can use hcitool to check out your bluetooth, in response to hcitool
command every bluetooth device will pop up, just figure out which one is yours and take note.

– install bluez

– hcitool scan

rfcomm bind

the rfcomm command will use the address to bind your bluetooth. after that connect and pair it, using the commands:

binding
rfcomm bind 0 00:12:08:xx:xx:xx

connecting
rfcomm connect hci0 00:12:08:xx:xx:xx

Pairing
– sudo bluetooth-agent ( the default should be 0000 or 1234 ) you can define your giving at commands

You only need to pair once, after that you just need to bind, the trusted device will pair connect autamatically.

on your arduino programm if you print on your bluetooth serial and at linux you type a cat < /dev/rfcomm0 you will see your sting. In my case my bluetooth was part of a meteorolical station, so I sent values separated by semicollons ( ; ) Some useful at commands: AT+NAME=HC-05\r\n ( renames your device ) AT+ ROLE=(0/1) ( set role as slave=0/master=1 ) AT+ROLE? ( check role mode ) AT+ PSWD? ( check pin ) AT+ PSWD="1234" ( set pin as 1234 ) Here is a sample program to connect your arduino using bluetooth. In future article I will show how I did to integrate this to my meteorologial station at cosm.com I was able to see the string on android, after pairing it, using some bluetooth terminals such as 'Android Blueterm' Now, you can integrate bluetooth in Arduino, you can do many things! Remote control, sensors, and lots of stuff!
// Example to show Bluetooth usage – Ricardo Pecanha
// Eletrofun – public domain

// include the library code:
#include
#include
#include
#include

/* RTC – Set the appropriate digital I/O pin connections */
uint8_t CE_PIN = 5;
uint8_t IO_PIN = 6;
uint8_t SCLK_PIN = 7;

char bufhora[20];
char bufdia[20];
char bufbt[40];

char day[10];

// This is the Bluetooth serial, if you have a different configuration on your shield, check it!
// ( this should be the default… )
#define rxPin 2
#define txPin 3

SoftwareSerial mySerial(rxPin, txPin);

void setup() {

// default serial, only for monitor debugging
Serial.begin(9600);

// define pin modes for tx, rx pins for bluetooth:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
mySerial.begin(38400);
Serial.begin(38400);

EnviaBT(“at+init”);
EnviaBT(“at+inq”);
}

void EnviaBT(char * comando)
{
unsigned long timeinit,timeend;
Serial.println(“Recebi comando”);
Serial.println(comando);
timeinit=millis();
timeend=timeinit;
mySerial.println(comando);
char lido,lido0=0,ok=0;
delay(5);
while ( (ok == 0 ) && ( ( timeend – timeinit ) < 900 ) ){ if ( mySerial.available() ) lido=(char)mySerial.read(); else { delay(10); continue; } if ( lido=='O') { lido0=1; } // if the module is powered off, it will giver ok, otherwise it will print ERR, // that can be ignored on init command, this check must be improved if you dont want to // turn off your arduino everytime..., as i have an lcd, I print ok, when everything goes right.. // anyway you must wait for ok or error, to respect the at (ok/err) response... if ( ( lido=='K') && ( lido0 == 1 ) ) { Serial.println("Recebi OK "); // lcd.setCursor(0, 1); // lcd.print("ok"); Serial.println(comando); ok=1; break; } delay(5); Serial.print(lido); timeend=millis(); } } void loop() { snprintf(bufbt,sizeof(bufbt),"test string"); mySerial.println(bufbt); delay(500); }

4 thoughts on “Arduino, Linux and bluetooth

  1. Hi,
    I am using HC-05 for my final year project.I am unable to configure the module using AT commands.I followed the steps mentioned in the datasheet to configure the module,also tried using TTL to RS232 convertor to configure it but dint get any response at the hyperterminal.Please help.

    • Hi Ankita! I assume you are using windows to communicate with your bt module, is this correct?
      Were you able to have any communication at all, with your module?
      Are you connected directly to an arduino, or a windows machine?

      In my case, i used hc05 on my arduino, and i gave the commands through it first, please send me more detailed information about your configuration.

      Greetings,
      Ricardo

  2. Pingback: Controle remoto Bluetooh para Arduino | EletroFun