BlueTooth Link With Auto-detect & Connect

110K8045

Intro: BlueTooth Link With Auto-detect & Connect

This will sow a way to auto-detect and create a link Using BlueTooth, this is a way for a 'master' module to detect a [known] 'slave' module using Arduino and BlueSmirf BlueTooth.

II used the BlueSmirf BlueTooth breakout board in default (slave) mode by connecting with it using an Android phone.  The next logical step was to use the BlueSmirf interface by programming the Arduino. Using a second Bluetooth board, I decided to create a link between two autonomous Arduinos. To make things interesting, I've set a couple of rules for the project. I wanted to heave the same code on both Arduinos and have the whole connection process be automatic. I also wanted the Master device to scan for other devices, retrieve the MAC address, connect and send data.

Features:
- Auto-Detect: The Master module will scan, discover and connect to the first [known] Slave Module. 
- Info Light: The RGB LED will help visualize the states of the modules and the connection.
- Master-Slave Jumper: You can quickly set a module type (master or slave) by using a single jumper.
Optional feature:
- Data logging: You can add Micro-SD data logging to the Master and/or the Slave module

STEP 1: Part List

There is no soldering involve in this project since all connections are done on the breadboards.

Here is the parts list (for two modules -  1 master and 1 slave):
- 2 Arduino UNO boards
- 2 proto shields
- 2 BlueSmirf BlueTooth breakout boards (I use the silver ones)
- 2 RGB LEDs
- 2 sets of jumper (you can also use a simple switch)
- 6 560 Ohm resistors (Green, Blue, Brown)
- 2 10k Ohm resistors (Brown, Black, Red)
Not on the Picture:
- Breadboard wires
- A way to power both modules

Optional parts:
- A Micro-SD breakout board
- A Micro-SD SD card adapter

STEP 2: Assembling

The following instructions are all making reference to the image model.

The BlueTooth board
Connect the the bluetooth rx to the Arduino tx and the bluetooth tx to the Arduino rx.  Finishi by connecting the bluetooth GND to the Arduino GND and the bluetooth VCC to the Arduino 5V.
Note: The simplest way to connect the BlueSmirf breakout board is to connect it directly to the RX (Pin0) and TX (Pin1) of the Arduino.  This way we can directly use the Arduino Serial commands.

The RGB LED
Use 560 Ohm resistors to connect the LED to the Arduino pins.  Connect the Red, Green and Blue LED pins to the Arduino Pin 5, 6 and 7 (in that order). Connect the LED GND pin to the Arduino GND.

Note: Make sure you know the function of each pins of your LED.  The documentation is not always right (voice of experience).  To be on the safe side, the program will cycle the three colors (Blue, Red and then Green) at start-up. If they don't light up in that sequence, fix it by switching theme around. 


The Switch/Jumper
Connect one side (A) of the Jumper to GND.  Connect the other side (B) to 5v using a 10k Ohm resistor. Connect side B to Pin4 of the Arduino.


STEP 3: (Optional) Data-loging

The code that is included in this project is ready to log all the steps to a Micro-SD breakout board.  If you want you can add this to one or both modules.  Here's how:

Using pins 10-13, follow these instructions for the wiring. 

- Connect the 5v pin to the 5V pin on the Arduino
- Connect the GND pin to one of the three GND pin on the Arduino
- Connect CLK to pin 13
- Connect DO to pin 12
- Connect DI to pin 11
- Connect CS to pin 10

STEP 4: Code

The included code (bt2bt.zip) is for both Master & Slave modules.  The code will execute as Master or Slave depending on the Jumper (or switch) state.  On/closed is the master while off/open is the slave.

IMPORTANT: You must edit the code to add you BlueTooth devices MAC addresses into the list of 'trusted' addresses.  To do that set the length of the gBtKnownMAC variable to the total number of devices you want to scan.  Then go in the BtInit() function and add your addresses into the gBtKnownMAC list.  Here is an example where 2 addresses are added:

  gBtKnownMAC[0] = "000666412345";
  gBtKnownMAC[1] = "000666454321";

Upload this code to both modules and move to the next step.

If you want to play further with the code, read the following tips first:
- Read the User Manual (I included a copy)
- Serial speed: By default, BlueSmirf is set to 115200 so you need to call Serial.begin(115200); before sending the first command.
- The “$$$” command is the only one that is not followed by a carriage return. Why? To complicate things of course. Use print() for the “$$$” command and println() for all other commands.
- Read and validate all the responses that are sent back.
- Wait 100ms delay after each command you send and before trying to read the response. This will give Bluesmirf the time to process the command.
- Don't forget to exit the command mode (using “---”). Some commands are not effective until you have exited the command mode (e.g. MS command).

STEP 5: Using It

To create the link power-up both modules at the same time.

- The info lights will flash Blue, Red and Green, 
    - When the power is ON a red light will appear on the BlueTooth boards
- The lights will then turn Red.  Indicating that there's no connection
    - While the master is inquiring, the red light on the BlueTooth boards will be flashing rapidly
    - While the slave is waiting for a connection, the red light on the BlueTooth boards will be flashing
- After the Master is done Inquiring for devices, its light will turn Blue
- When the Master identifies a known Slave device, its light will turn Cyan
    - When the connection is made a Green light will appear on both BlueTooth boards

Note: When the connection is made, the Master's light will be Green and the Slave will be Blue.

After the connection is made, data is sent by the Master and received by the salve.  The lights are flashing to indicate these events.

Go here  to see video of this in action.

44 Comments

Hi, I use hc06 with my mobile Phone and I want the Phone to connect automatically. For example when I get in my car, my Phone connects automatically.

Hi guys, I am using HC05 modul as a slave in one of my
projects. This HC05 is connected to the PC via USB-FTDI interface. That is USB
to FTDI and FTDI to HC05. This is supposed to send data to th buetooth of the
mobile. However I am not able to do it. The mobile pairs with the HC05 module,
but fails to communicate further. HC05 fails to transmit data to mobile. How do
I configure this.

And what does PASSIVE MODE in HC-05 mean (HC05 is passive in
slave mode)

First of all, thanks for sharing!

I have a quick question: how would you go about using what you did above but also determining RSSI once connected? I am trying to make the master device sense if there is a slave device within around 4 ft or so, then the slave sends a simple signal (like turn on an LED on the master module). The tricky part is that I want the slave to be able to pass by quickly and still send that data. Will it be able to connect fast enough and could I implement multiple slave modules?

Thanks!

Help me, I have a problem for connection with 4 arduino and can to connect to others with distance measure ex, 2 mater or others.

hi,i am a beginner to bt modules.. i need to sent alphabets from one arduino to an other say 's' and blink an led......plz help.....

Hello, I wonder if you can help me, I need to configure the Bluetooth so that when he loses the signal (when I turn away more than a meter, bluetooth / arduino he lights a LED., Or would like to get away, realizing signal dropout and regulate it, for example, after I verify that a given signal, a yard, gets a value, I put in the program and when it reached this value, the LED would light, or erase.

thank you

Eduardo castellani

You could check the connection status at regular intervals (lets say, every seconds) using the "GK" command. 1=connected, 0=not connected.

Thank you for your attention, I understand, when the signal goes off, the LED lights, open the proton etc?

I too have tried to use the GK command, with no luck. The problem is, it is impossible to get the RN42 to go into command mode when it is connected in HID mode. I tried it as a master and as a slave, and changed the config timeout to 255 (no timeout), but it still refuses to go into command mode after a connection is made. Instead, it just forwards $$$ as it would any other set of characters. Any advice?

hi, if u having trouble in making into AT mode of HC-05 (ZS-040)(especiall if one having en/wakeup up pin instead of key pin). Follow they bellow procedure

Power off HC-05 module.

Press and hold small button above EN pin.

Power on and keep pressing small button.

Small LED should start to blink slowly about once every 2 seconds.

for more info visit below site:

http://abratukhin.blogspot.in/2015/04/connect-atmega328-with-hc-05-zs-040.html

Is there a sketch that will allow the Arduino/bluetooth to auto pair to iOS device when in range?

Hi Phillipe, (using ProMicro and SMiRF Silver) I started with your post on simple Arduino-Bluetooth-link. That code is so simple yet it did not work for me. I tried setting master to SM,3 and that did nothing. Now I tried this post and I still cannot get my SMiRFs to communicate with each other. I must be missing something really simple. I can connect to either SMiRF using my phone, so I think it is authentication because the phone at least asked for it, and this code does not mention it. How might I add authentication to either code example from you? But you also mention a jumper in the video but not in the online text. What would you suggest I try? Your online posts are amazing and I ordered 2 HC-05s just to try your other posts. Thank you.

Dear Phillipe,

This is karthik, I need an urgent help from you. I need to know, how to make a knot via machine using two same side threads like our fingers. shall we use any stepper motors or anything else. Please give me some ideas. I need your contact mail, Please reply me back. I'm pleasure to waiting for you reply. This is my mail (karthikjs27@gmail.com)

This sounds like a nice problem to solve but I have no spare time right now to add a new project. I would suggest to google on the subject of knot making robots. Don't forget that the mechanism for sewing machine (kind of) makes knot in a very simple way. Good luck with your project.

Dear sir i want your help in our project. me want to connect arduino uno r3 board with different shimmer sensors at a time and uno board auto detect and connect and save the data and when other device want that data they send it .. plz send me logic of programming of this code. i am w8ng for your positive and early response .

Can I use HC-05 as master with this metode?

Regard

Bambangsud

Yes. I have a couple of post on my blog about the HC-05: http://phillipecantin.blogspot.ca/search/label/HC-05

Hi. I have a weird question. I have a bluetooth speaker (JBL) and I'm looking for a way to connect it with a bluetooth microphone, so I could use this system to speak in meetings and conferences. Trouble is : it's two slave items if I've right understood the bluetooth system. Is there any way to link both item ? Maybe with a computer?

David

Thanks for posting this! I have implemented this code but I would like to try a different method.

I am trying to use Autoconnect master mode on one smirf but it keeps disconnecting after a period of time. In this mode, shouldn't it automatically try to connect again?

I would have to revisit this project and build a more serious library that would also do that. For now this simple code doesn't handle error recovery like disconnection.

More Comments