Jan 15, 2012

Arduino Bluetooth Link


In a previous Arduino project, I used the BlueSmirf Bluetooth Breakout Board to communicate with my Android phone. There was absolutely no special code on the Arduino to handle Bluetooth since, by default, the BlueSmirf is set as Slave and will accept any connection call. The phone app was doing all the work.

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.  Here's the video of the final result:



My biggest issue started with the Sparkfun proto shield for Arduino. It has a built-in socket for their Bluesmirf device. Nice marketing move! This socket is hardwired to use the Arduino pins 0 and 1 for communications. It all looks good until you need to use it in the real world. Here are the pros and cons:

Pros:

  • No wiring needed to connect the Bluesmirf board.
  • No extra library needed to do serial communications.

Cons:

  • You must remove the Bluesmirf breakout board every time you need to plug the Arduino board in the computer. Why? Because of the hardwired Bluesmirf socket the Arduino board communications are mixed with the Bluesmirf interface.
  • Another consequence of the previous problem: You can't used the USB serial output to the computer to send debug info. That is really annoying when debugging your project.
  • The power is always ON for the Bluesmirf.

I'll fix this by using different pins for the Bluetooth communication using the NewSoftSerial library. I will also modify the Sparkfun Bluesmirf socket to use any communication pins and to have control on the power usage.

For more details continue reading after the break. (Warning! Geeky stuff about code and electronics)




Tips about BlueSmirf interface
  1. Read the User Manual
  2. Serial speed: By default, BlueSmirf is set to 115200 so you need to call Serial.begin(115200); before sending the first command.
  3. 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.
  4. Read and validate all the responses that are sent back.
  5. 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.
  6. Don't forget to exit the command mode (using “---”). Some commands are not effective until you have exited the command mode (e.g. SM command).

A simple test
A quick way to get two Arduino+Bluesmirf connected is by hard-coding the Slave's MAC address into the Master connection command. With this there's no need for code in the loop() function. In the following example, the MAC address of the Slave Bluesmirf is 000666123ABC. The example also assumes that the RX and TX are connected to pins 0 and 1. Here's the code for both Master ans Slave:

To test, start the Slave 5 seconds before the Master. The 'Connect' lights on both Bluesmirfs will turn green when the connection is open.

Instructable
After many questions about this project, I finally decided to make an Instructables about it.


119 comments:

  1. I hope I understand this corectly, and it would help me greatly if I am, so tell me if I'm right.

    by running the master and slave code on two separate BlueSmirf Gold modules I can then just replace a wired serial connection with those modules without editing the rest of my code?

    Sincerely,
    Anton Stamhuis.
    (Anton[at]fullformfreedom[dot]cc

    ReplyDelete
    Replies
    1. I just went ahead and tested it, and it seems I was right. :)

      one thing though, the "MS" that you have in your code there should be "SM"

      greetings,
      Anton Stamhuis

      Delete
    2. Thanks for catching this mistake! I'm glad this could help.

      Delete
    3. no problem, and yeah, I mostly just didn't know where to start. but I got it figured out now, so thank you for that. ^^

      one more thing I ran into, I was getting a few errors after I copied the code from your blog, figured out it was a problem with the quotation marks. just had to take them out and replace them.

      Tomorrow I shall do some wiring and get my project back up and running. :3

      thanks again.

      ~anton

      Delete
  2. Cool. I'll follow that on Twitter. If you have any other problems, I can probably help since I have 2-3 projects using Bluetooth.

    I'm not sure about the quotation marks. They might have been modified during the copy-paste.

    This is an ultra simplified version of the code I'm running. If you need more help I can send you my whole project code to make things easier.

    ReplyDelete
  3. Very cool! But what exactly do those commands mean? Serial.print(“$$$”) // Command Mode?
    Serial.println(“SM,1”); // Maybe setting it to receive?
    Serial.println(“C,000666123ABC”); //Connect to that Address?
    Serial.println(“---”); //Just for kicks?

    Thanks For the help!

    ReplyDelete
  4. For those specific commands:
    $$$ : is to set the device in command mode
    SM, : is to set the the device mode
    C,[address] : is to connect to a specific address
    --- : is to exit the command mode

    I explain some in the post but, for the best reference, I have a link to the User Manual where all the commands are defined.

    ReplyDelete
  5. could u show us how to make that?

    ReplyDelete
    Replies
    1. I have made an Instrucable about it. http://www.instructables.com/id/BlueTooth-Link-with-auto-detect-connect/

      Delete
  6. After a short battle with the Blogger "WYSIWYG" editor I finally have fixed my bad quotes characters in the print statements.

    ReplyDelete
  7. Is it possible to connect more than one bluetooth device to the arduino and log data? Lets say I had 4 arduinos and 3 of them want to connect to the 4th arduino, will there be a problem?

    ReplyDelete
    Replies
    1. Yes you can connect more than one Bluetooth device on a single Arduino by using ‘SoftwareSerial Library’ and opening a different serial connection for each device. The down side of this approach is that your 4th Arduino would need 3 bluetooth devices ($$$).
      On the other hand, it looks like a single Bluetooth device could manage multiple connections but I haven’t tried that yet.

      Delete
    2. Thank you, I appreciate your quick response. I will let you know how it goes.

      Delete
    3. Dear Phillipe, thanks for your useful response which solved my question as well. Is there any other solution rather than what you said?

      Delete
    4. I'm happy to be helping. Some bluetooth device can support a multiple connection called piconet where 1 master can connect to up to 7 slave devices. I have not try this yet.

      Delete
  8. About multiple connections: So far I could only find one Bluetooth SMD Module that can support multiple connection (Piconet <7>). It's the Rayson BTM-182. I'll try to get one and start working on a Bluetooth swarm network.

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete
    Replies
    1. @Dwin I had to remove the comment since it contained a link to a .rar file. This is unsafe so please use standard URL links pointing to safe HTML content.

      Delete
    2. I'm sorry about it sir my bad.
      I just like to ask if this product can be use as an alternative to Blue Smirf that you use for interfacing your Arduino RObots. We will interface it to mindwave mobile controller of neurosky

      btw sir here is the link of the product :
      http://www.e-gizmo.com/KIT/BLUETOOTH%20SHIELD.html

      Delete
    3. With a quick read through the manuals (link at the end) it is clear that all the necessary functions are present to make a similar-type of link.
      I say 'similar' because if you use some 'Slave Only' modules (EGBT-04S), you won't be able to run the same code on those. So, I would suggest using only the Master-Slave modules (EGBT-045MS) which will resemble the BlueSmirf behavior.

      Link to the manual (link is in on the left): http://www.e-gizmo.com/KIT/egbt-04.htm

      Delete
    4. Thank you for your quick reply sir!
      your blog is really a great help :)
      Godbless!

      Delete
  10. Hi Phillipe,

    I owuld like to create multiple game devices with 1 joystick + 4 buttons + 1 rumble.

    In first, can I establish a communication in both sides : gamepad <-> PC with 2 BT devices ?

    Moreover, have you test the "Rayson BTM-182" to use only one BT device and listen all gamepad devices ?

    Thx, Gilles

    ReplyDelete
    Replies
    1. Sadly I haven't tested it yet but I'm planning to get one soon. It supports up to 7 connections (with piconet) and I'd like to build an easy to use networking API for it.

      Are you having trouble using this device?

      Delete
    2. I'm still waiting for it. I should receive it in one or two weeks :/
      (it seams that it's no more produced) ... do you know another kind of this device ; that could connect with multiple slaves ?

      Delete
    3. There's the "BT44-191S Bluetooth Class 2" that you can find at zbaus.com. You can find more by google'ing "bluetooth module supporting piconet". Be careful because there's a lot of Chinese knockoffs on the market.

      Delete
  11. Hi Phillipe,

    I'm starting a new project and I want to connect two arduinos via bluetooth to control some relays(the relays are located at the slave).
    The bt modules I buy are: http://i.ebayimg.com/t/Wireless-Serial-4-Pin-Bluetooth-RF-Transceiver-Module-with-Backplane-RS232-New-/00/s/ODAwWDgwMA==/$%28KGrHqF,!oMFC3OZgs2LBQzq4jts7Q~~60_12.JPG
    The connections I made are ok, Vcc->arduino3.3V GND->arduinoGND TXD->RXD RXD->TXD
    Do I need switches to define master and slave or the software does it?

    Thank you in advance
    Steve

    ReplyDelete
    Replies
    1. This module can be interfaced using AT commands. If by default they are in Slave mode (like bluesmirf) then you will have to set one to be the Master, using AT commands (AT+ROLE=1).
      Google this "jy mcu bt board commands" and/or this "bluetooth at commands" and you'll find many documentation and examples of how to use basic AT commands for this device.
      Let me know how it turns out.

      Delete
  12. Thank you for your quick answer.
    I check it on google but I can't understand if I need a serial connection between bt module and my pc to define master and slave or I can do this with arduino and his software?
    Sorry for my ignorance this is the first time I get involved with bt and arduino.
    Steve

    ReplyDelete
    Replies
    1. Connect the Arduino pin0 (Rx) to BTmodule Tx and Arduino pin1 (Tx) to BTmodule Rx. Connect power and ground wires to the BTmodule. Then try something like this in your Arduino sketch:

      //Set the serial with the right baud rate for JY-MCU
      Serial.begin(38400);

      //Test the communications
      Serial.println("AT");
      //Here you can add a loop/function to read the output that should be "OK"

      //Set as master
      Serial.println("AT+ROLE=1");

      Delete
    2. Follow up: This my last answer help? Keep me updated, I'd like to know how it turned out.

      Delete
    3. Hello again Phillipe,

      I took 'SoftwareSerialExample' from arduino 1.0.1 examples and changed a little.I don't know if the code is right..

      #include
      SoftwareSerial mySerial(2, 3);
      void setup(){Serial.begin(38400);Serial.println("Goodnight moon!");mySerial.println("Hello, world?");}
      void loop(){Serial.println("AT+ROLE=1");}

      The compiler says,done uploading but I have an error:
      avrdude: stk500_getsync(): not in sync: resp=0x00

      Steve

      Delete
    4. It looks like you can't upload to your Arduino. Do you have wire(s) connected to pin0 and/or pin1? If so you must disconnect them while your are uploading a sketch. Then, in the code, make sure that you are sending the AT commands to the right serial interface. In your example you are using(mixing) 2 interfaces.

      Delete
    5. I disconnected the TX and RX wires and everything seems to be OK! I made some corrections to the code.

      #include
      SoftwareSerial mySerial(2, 3);
      void setup(){Serial.begin(38400);Serial.println("Goodnight moon!");Serial.println("Hello, world?");}
      void loop(){Serial.println("AT+ROLE=1");}

      I run 'putty' and at the serial section I choose my com port and 38400 speed.Then 'open' and on console screen AT+ROLE=1 was running again and again.
      Am I ok?

      Thanks you again!

      Delete
    6. Are you trying to communicate with the BT device and sending debug info on 2 separate serial interfaces? If so, you have to tweak your code. Currently mySerial(pins 2&3) is not used while arduino serial default (pins 0&1) is use to do both. Maybe we could continue this conversation on Google+ (email, chat or hangout) it would go faster.

      Delete
  13. Hello Phillipe,

    I have two RN42XV Bluetooth modules each on my two Arduino Uno boards. I'm trying to connect the two together, so on the serial monitor I enter into command mode, "$$$", and then "c,<>" to connection with each other. It says that it is "TRYING" but then just freezes, but the LEDs on the Bluetooth modules indicate they are connected. However it doesn't say that the connection is successful. Any ideas?

    Earl

    ReplyDelete
    Replies
    1. Try "SM,1" before calling "c,<>". Don't forget to close the command mode with "---" after the "c,<>".

      Delete
  14. HI Phillipe
    I copy your code and compiled it successfully to my Arduino devises ,sadly I never got connection between the two BT
    I am using standard Bluetooth BC417 module and two separate Arduino devises (Arduino Nano & Arduino Uno).
    Do you have any idea why ?
    Thanks,
    Yaniv

    ReplyDelete
    Replies
    1. The code in this post in intended to work with the BlueSmirf device. The device you mention seems to us the AT commands. Other comments in this page are talking about the AT commands.

      Delete
    2. O.K Thanks
      I will try looking at the AT commends how to do this

      Delete
    3. HI
      Well I have investigated this thing for the last few hours.
      I have tried to enter the AT command and I got "O.K" at the siral port that means that I can send AT command
      But when I have tried to send " BluetoothSerial.write("AT+ROLE=1");"
      I got nothing.
      I think that my devise can't be Master, could it be ?
      I am working with Linovor HC-06
      DO you now if St Sak succeed to connect the tow Arduino's over BT

      This is my code :
      #include

      /* DIO used to communicate with the Bluetooth module's TXD pin */
      #define BT_SERIAL_TX_DIO 10
      /* DIO used to communicate with the Bluetooth module's RXD pin */
      #define BT_SERIAL_RX_DIO 11

      /* Initialise the software serial port */
      SoftwareSerial BluetoothSerial(BT_SERIAL_TX_DIO, BT_SERIAL_RX_DIO);

      void setup()
      {
      /* Set the baud rate for the hardware serial port */
      Serial.begin(9600);
      /* Set the baud rate for the software serial port */
      BluetoothSerial.begin(9600);
      BluetoothSerial.write("AT+VERSION");

      BluetoothSerial.write("AT+ROLE=1");
      }

      /* Main loop that will pass any data to and from the Bluetooth mode to the
      host PC */
      void loop()
      {
      /* If data is available from the Bluetooth module then pass it on to the
      hardware serial port. */
      if (BluetoothSerial.available())
      Serial.write(BluetoothSerial.read());

      /* If data is available from the hardware serial port then pass it on
      to the Bluetooth module. */
      if (Serial.available()){
      BluetoothSerial.write(Serial.read());
      }
      }

      Thanks,
      I am truly grateful yaniv,

      Delete
    4. I wish I had a pair of AT devices to test this. I don't know if @St Sak got his project to work. I haven't heard anything since my last comment in January. You should check the specs of your device to know if it can be set to Master Mode. Some device are Salve only while other device can be either Master or Slave.

      Delete
    5. O.K thanks the BT can be slave only :)
      I need to order HC-05

      Delete
  15. Hi Phillipe
    I am working on a project where I am building two portable gaming consoles with their separate lcds, switches..... and i wanted them to communicate over bluetooth:
    Like your project the two arduinos(in my case Due) will read the same code from the sd card and all i want to communicate from 1(kinda slave ) console to oder(master) console...
    Could you help me with the code for this to work plzzzzz.....

    ReplyDelete
    Replies
    1. If you will be using BlueSmirf BT devices and only those two 'console' will connect to each other then you can hardcode the connection using the code from this post. Of course you will have to use your slave device MAC instead of 000666123ABC. Once the connection is done you can use the BtInit, BtReceive and BtSend functions that are included in my Instructables to transmit data between the two console.

      Delete
    2. My device is not a BlueSmirfBT device but it also uses the RN 42 module....Should that be fine????????N Thnxxx for your help

      Delete
    3. It should work. Try sending "$$$" to it and see if it sends "CMD" back.

      Delete
    4. Perfect! You're all set to go then. Keep us posted on your progress

      Delete
    5. Hello Phillipe,
      Sorry to trouble you this way but I am new to arduino and I am finding it hard to find how exactly to do the connection between the two arduinos..... Can you explain me in steps from the starting and send me a code of two arduinos communicating like 1 sending some character and other on receiving should respond by glowing a led or something...Plzzzzz

      Delete
    6. I tried to do few things like changing baud rate and name of device and device mode but now i am stuck in the part of sending stuff from 1 arduino to other......Means like if the two arduinos are now connected now what commands to use to send data from one to another....

      Delete
    7. For code sample please use the link to the Instructables I've made (the link is on the very last line of this post). As for the communication after the link is establish, it works exactly like a normal serial port. You can use the basic Arduino serial functions to Serial.read() and Serial.println(). The sample code in the Instructable does all that since the Master sends (println) stuff the the slave who's listening (read).

      Delete
    8. Thanx Phillipe,
      I think this should get me going....I am really thankful for your help...

      Delete
    9. I have tried with codes with similar bluetooth modules on an Arduino and 1 Mega but for some reason it did not work out.....I am adding both the codes.....Can you plzzz help with the problem the code might be having......Also I have changed the baud rate to 9600 for the devices so in code also I am using 9600.....

      For master
      void setup()
      {
      Serial.begin(9600);
      Serial.print("$$$");
      delay(100);
      Serial.println("SM,1"); delay(1000);
      while(!Serial.println("C,000666495767"));
      delay(1000);
      Serial.println("---");
      pinMode(13,OUTPUT);

      }

      void loop() {
      if(Serial.print('H'))
      digitalWrite(13,HIGH);
      delay(5000);
      if(Serial.print('L'))
      digitalWrite(13,LOW);
      delay(5000);

      // put your main code here, to run repeatedly:

      }



      For Slave
      void setup()
      {
      Serial.begin(9600);
      Serial.print("$$$");
      delay(1000);
      Serial.println("SM,0");
      delay(1000);
      Serial.println("---");
      pinMode(13,OUTPUT);

      }

      void loop() {
      while(Serial.available()==0);
      char ch=Serial.read();
      if(ch=='H')
      digitalWrite(13,HIGH);
      if(ch=='L')
      digitalWrite(13,LOW);

      // put your main code here, to run repeatedly:

      }

      Delete
    10. I ran your exact code here. It fails with the 9600 baud but works right away with 115200. Note that I'm using 2 BlueSmirf silver. You should check the specs of your devices and make sure that they can work at 9600 baud (my guess is that they can't).
      One other tweak is to add a pause of a few seconds at the end of the setup functions to let some time for the connection to be made.
      you are sooooo close to your goal. Don't give up.

      Delete
  16. Hello Phillipe,

    I just want to say thank you for posting such useful tutorial on the bluetooth linkage between arduinos. It was very helpful for our "smart shoes" project for an embedded design courses at our university.

    Thank you!

    ReplyDelete
    Replies
    1. Glad it could help. Do you have a link to your project?

      Delete
  17. would anyone happen to know how to communicate bluetooth through matlab? more specifically coding? We are using a bluetooth mate silver(found from sparkfun). My group is trying to connect wirelessly to matlab but yet still can not.

    ReplyDelete
  18. Hi Phillipe,

    May I know how many connections can RN42XV support? Thank you!

    ReplyDelete
    Replies
    1. I have not experimented with RN42XV but some bluetooth devices are able to support a piconet of one master and up to 7 slaves. Like I mentioned before, I have not tried that yet but I wish I could.

      Delete
  19. The RN42XV can communicate with an android phone? Basically Im asking whether it is possible to connect a single bluetooth module to e.g. 3 phones (NOT the other way around). Hope you can help. Im using the RN-41-sm bluetooth module but can only connect with one phone; I need to connect with more

    ReplyDelete
    Replies
    1. I have connected RN42 modules with my Android phones in many projects but only one at a time. I never tried with 2 or more phones. I'll see if I can try to make this work.

      Delete
  20. Keep me posted. I need to know if this is even possible with bluetooth:) And if so, if i can send data simultaneously (or within a second after each other)
    Appreciate your time!

    ReplyDelete
  21. Tried anything yet? Additionally how fast can a multipoint bluetooth module switch between its connected devices. 1 seconds, 15 seconds or a matter of milliseconds?
    Im really interested to know

    ReplyDelete
  22. I'm trying to make two RN42-I/RM bluetooth talk. They are mounted in bluetooth modules from parallax inc. and then connected to arduino mini pro boards. I uploaded the code you have here but it is still not working. Do you know why?

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. If you send "$$$" to the module do you get an "AOK" back?

      Delete
    3. This comment has been removed by the author.

      Delete
    4. I got it to work! Now I'm trying to learn how to send a character from the Master to the Slave and I created a loop that if the slave reads the character H then an LED I connected to pin 8 should light up but it's not working. Do you have any similar code?

      I added this to my master code:
      void loop ()
      {
      Serial.print('H');
      }

      and then I added this to my Slave

      pinMode(8,OUTPUT);


      void loop ()
      {
      while(Serial.available()==0);
      char ch=Serial.read();
      if(ch=='H'){
      digitalWrite(8,HIGH);}
      if(ch=='L'){
      digitalWrite(8,LOW);}
      }

      THank you!!

      Delete
    5. Hello Marietta...Looks like you need to add a Serial.print('L');
      } in the Master Code

      Delete
    6. Marietta,

      If you have connected your LED anode (long lead) to pin 8, it should turn on when you send 'H'. If you have connected the cathode (short lead) it will turn on whit the 'L'.

      Delete
  23. Phillipe thank you very much for sharing your knowledge. I think that where some people are failing as I am is that we are just "taking for granted" that this code will in fact work for various BlueTooth modules. I am using an HC-05 which utilizes the AT command set, and please correct me if I am wrong but the code that you wrote as is will not function for an HC-05. properly unless you replace all of the BlueSmirf commands with the AT equivalent. Thank you very much.

    ReplyDelete
    Replies
    1. As mentioned in the BlueSmirf documentation "Roving Networks Bluetooth devices are programmed with a simple ASCI command language which is
      similar to the industry standard Hayes AT protocol." so the code that I'm sharing will only work on RN devices. Sadly I don't have bluetooth modules that uses AT commands so that I can code and test an 'AT' version of my project.

      I'll add this comment in the main post to help clarify things.

      Delete
  24. Thanks Phillipe..I'm am definitely a novice but that which you have completed has definitely sparked my curiosity. So I have a "few" HC-05/HC-06's laying around and I'm willing to send you a pair to "tool" around with. I've recently ordered a few HC-11's which are basically 1000 meter repeaters that connect to the HC series. I'm thinking that range extension should open doors for other applications.

    ReplyDelete
    Replies
    1. I really like the idea of a repeater. Can they handle multiple messages simultaneously? I'll take you up on that offer for the 2 HC modules. You can contact me through g+ or at phillipe (a) cantin dot com.

      Delete
    2. Not sure why I can't send email to that address...If you send me your address to kneeyhow @ Yahoo.com I'll get the boards out in the morning.

      Delete
  25. Here is one of the companies that I deal with...See anything interesting? You should be able to translate the page with Google Chrome.. http://item.taobao.com/item.htm?spm=a1z10.5.w4002-2724859378.15.psWx5z&id=20265137304

    ReplyDelete
    Replies
    1. I'll see if I can find them in the Canadian (or US) sites I normally buy from. I try to buy local as much as possible.

      Delete
  26. I'll get the HC-05's to you by next week...I have some HC-11 samples that I should be receiving in the next two weeks of which I can share one or two...

    ReplyDelete
  27. Hee Philippe, I'm back again.
    I need to get some insight on how fast piconet mode actually works.
    Perhaps you can help.

    ReplyDelete
    Replies
    1. I never tried Piconet and don't have bt modules that supports it. Which modules are you using?

      Delete
  28. Hi, i have read your article with a lot of intereset.
    I have a module with an IMU and this module streams data from the IMU by a bluetooth (RN-42) slave module. Questions...

    Which is the best suitable module can i buy to make this RN-42 talks with an arduino board?

    ReplyDelete
    Replies
    1. Personally, I would use the RN-42 BlueSMiRF Silver.

      Delete
  29. sir is it possible to transmit data with the input of sensor in the master bluetooth ? and receive by the slave bluetooth then has the output of LED screen ?

    ReplyDelete
    Replies
    1. Short answer is 'yes'. You could start with my project code and modify it to do exactly what you want. A crude way to do it would be to continuously send the sensor value.

      Delete
  30. Hello sir, may I ask if the modulation of EGBT-045MS is the same as of the bluesmirf? Because I am also making a project using neurosky and I don't have Bluesmirf but I have EGBT-045MS

    ReplyDelete
  31. Because the EGBT-045MS is using the AT commands, you should look up my other post were I'm using an AT-type module: (HC-05 Bluetooth link of 2 Arduino) http://phillipecantin.blogspot.ca/2014/01/hc-05-bluetooth-link-of-2-arduino.html

    ReplyDelete
  32. Hello sr,
    This is my situation. I am having a problem connecting two bluesmirf. I have an arduino UNO with one of them and a Teensy 3.1 with the other. I try with the code that you use for the configuration of them, but them never connect between each other. Also, I test each one of them, using the bluetooth of a laptop and it works fine. but when i try to connect with the other bluesmirf it doesnt work, the master can find the slave, but when i try to connect them, the master doesnt connect with the slave because the green led never blinks.

    This is code to setup both of them:
    Serial.begin(9600); // Begin the serial monitor at 9600bps

    bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
    bluetooth.print("$"); // Print three times individually
    bluetooth.print("$");
    bluetooth.print("$"); // Enter command mode
    delay(100); // Short delay, wait for the Mate to send back CMD*/
    bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
    // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
    bluetooth.begin(9600); // Start bluetooth serial at 9600

    I configure the master and the slave using the serial port in arduino for both of them. I use teraterm to configure the bluesmirf. Firstly, i configure one in slave mode using SM,0 and no more. And for the master i'm using SM,1 then it search the other address and store it using SR. Finally i put "C" to make the connection.

    Could you please help me to solve my situation, i'm stuck!!!

    ReplyDelete
    Replies
    1. Do you have a github (github.com) repo where I can see your code? That would be the best way for me to help you.

      Delete
  33. Hi Philipe,

    I would like to transmit an array of 3*150 integers every 250ms using bluesmirf silver.

    I am having trouble getting this to work, is it a realistic, achievable data rate?

    Thank you in advance!

    Joe

    ReplyDelete
    Replies
    1. You could try to profile the communication speed by sending characters for 250ms and logging the total. You should repeat this test 10 time, make an average and that should give your answer.

      Delete
  34. Replies
    1. Yes you can but follow this post: http://phillipecantin.blogspot.ca/2014/01/hc-05-bluetooth-link-of-2-arduino.html

      Delete
  35. hello sir plz help me i am from india an engg. student and i am doing project " humanoid"
    i want to sent flex sensor readings and commands to another arduino using two HC05 so you plz help me with your're that great programming brain . i don't have any not that great experience in programming

    ReplyDelete
    Replies
    1. Please follow this link (http://phillipecantin.blogspot.ca/2014/01/hc-05-bluetooth-link-of-2-arduino.html) to a post where I connect two Arduino using HC-05 and send data between them

      Delete
  36. Hello Phillipe, Thanks for the tutorial! I want to use blueterm app to send data to a bluesmirf and this should transfer the data automatically to another bluesmirf. I am using your tutorial.The issue is, when I uploaded the code for the master and slave on the two bluesmirfs, they are not getting connected.The master found the slave as known mac address but is not connecting with it. The numVal is coming as 0 everytime. Could you please help me?

    ReplyDelete
    Replies
    1. To do that you will have to modify the code. If you want to connect with one arduino (let's call it A1 and the other one would be A2) using your phone, this arduino must be set as a slave. Then, once the A1 device receives the data you want to send, it should reset itself as a master and initiate the connection with the A2 device. Then A1 can send the data to A2

      Delete
    2. Thanks for your reply Phillipe.
      I think I am on right track. Whatever you wrote is my exact plan.But,the major issue is the connection between the two BTs. Let's forget the android app for now and just try to make the connection as Master and Slave (by simply providing slave's mac address to master). The connect light is not On. The simple test which you gave above is not working for my modules. This might be the root cause of the issue.
      I have checked with the commands like D , GK, GF for the Master BT. It is showing the mac of the slave as the remote device. The red stat light of the two BTs blinks like twice/second. But not the connect .

      And yes, I have modified your code. But first I need the basic connection. I want to see the Master-slave working. So, any advice would be of great help to me. Loads of thanks!

      Delete
    3. Did you tried the simple test code?(from this post) It should connect the two devices right away. If that is not working, there might be a bigger problem with one of your two BT modules or Arduino boards.

      Delete
    4. Yes I tried the simple code from this post. It's not working. I am afraid if my modules are faulty. I also reset these to factory default and tried back. By SM,1 command, it sets the module in Master mode and shows the slave as the remote device(though not connecting). But, my modules beautifully work as slaves and communicate successfully with blueterm app.
      Anyway, I will keep trying and will update you.Desperately trying to find the solution.

      Delete
    5. Hello Phillipe,
      FYI,the master module can detect and pair with my android and pc , but no connect light yet.This clears that the modules are not faulty.

      I have a question. Does the connection depends on the mode(data/command) of the modules? Example, If one module is in command mode and trying to connect (C,address), and other bluesmirf also gets into it's command mode for checking the connection ("GK"). What is the trick of the connection ? Really, is it that challenging?

      Delete
    6. If you don't get a green Connect light by using the Simple Test code, something is not right. Which Arduino pins are you using to connect to the modules TX and RX pins? Are you closing the command mode by calling the "---" command? When you call the "SM,1" on the master, are you getting an "AOK" message back?

      Delete
    7. Which Arduino pins are you using to connect to the modules TX and RX pins?-> 0 and 1.TX/RX of bluetooth with RX/TX of arduino, respectively.
      Are you closing the command mode by calling the "---" command?-> Yes
      When you call the "SM,1" on the master, are you getting an "AOK" message back?-> Yes

      I wanted to share my code with you.There might be some delay() issue.

      Master Code->
      int ledPin = 13;


      int incomingByte = 0;
      char msgArray[256];
      int numVal;
      boolean flagDone = false;

      void checkCommandOutputAtSerial(void){
      bool keepReading = true;
      int index = 0;

      msgArray[0] = '\0';

      while(keepReading){
      keepReading = false;

      if (Serial.available() > 0) {
      // read the incoming byte:
      incomingByte = Serial.read();
      if(incomingByte != 13){
      msgArray[index++] = incomingByte;
      keepReading = true;
      }
      }
      }
      msgArray[index] = '\0';
      }


      //Master code
      void setup() {

      pinMode(ledPin, OUTPUT);
      Serial.begin(115200);

      Serial.print("$$$");
      delay(1000);
      Serial.println("SM,1");
      delay(100);
      Serial.println("C,00066666351B"); //C,address - Connect to address
      delay(70000);

      while(!flagDone){

      Serial.println("---");
      delay(1000);

      Serial.print("$$$");
      delay(1000);

      Serial.println("SM,1");
      delay(100);
      Serial.println("C,00066666351B");
      delay(70000);

      Serial.println("GK");//check the status
      delay(10000);

      checkCommandOutputAtSerial(); //read the command output in msgArray
      delay(1000);

      if(strlen(msgArray) > 0) {
      //Serial.println(msgArray);
      numVal= atoi(msgArray);
      //Serial.println("msgArray numVal is "+numVal);
      if(numVal == 1){
      Serial.println("Success! Connection Made!!");
      flagDone = true;
      digitalWrite(ledPin, HIGH);
      }
      else{
      //Serial.println("Not connected");
      }

      }
      }
      delay(10000);
      Serial.println("---");
      }

      void loop(){}

      SLAVE->
      //Slave code
      int ledPin = 13;
      int incomingByte = 0;
      char msgArray[256];
      int numVal;
      boolean flagDone = false;

      void checkCommandOutputAtSerial(void){
      bool keepReading = true;
      int index = 0;

      msgArray[0] = '\0';

      while(keepReading){
      keepReading = false;

      if (Serial.available() > 0) {
      // read the incoming byte:
      incomingByte = Serial.read();
      if(incomingByte != 13){
      msgArray[index++] = incomingByte;
      keepReading = true;
      }
      }
      }
      msgArray[index] = '\0';
      }


      void setup() {

      pinMode(ledPin, OUTPUT);
      Serial.begin(115200);

      Serial.print("$$$");
      delay(1000);
      Serial.println("SM,0");
      delay(1000);

      while(!flagDone){

      Serial.println("---");
      delay(4000); //wait for connection

      Serial.print("$$$");
      delay(1000);
      Serial.println("GK");//check the status
      delay(4000);

      checkCommandOutputAtSerial(); //read the command output in msgArray
      delay(1000);

      if(strlen(msgArray) > 0) {
      //Serial.println(msgArray);
      numVal= atoi(msgArray);
      //Serial.println("msgArray numVal is "+numVal);
      if(numVal == 1){
      Serial.println("Success! Connection Made!!");
      flagDone = true;
      digitalWrite(ledPin, HIGH);
      }
      else{
      //Serial.println("Not connected");
      }

      }
      }
      delay(10000);
      Serial.println("---");


      }

      void loop(){}

      Let me know if these work on your module.Even if after changing some delay parameters or anything else.Thanks a lot phillipe.

      Delete
    8. As a quick test I have removed a the code after the connection (“GK” and if validation) on both master and slave. It connected right away so ... we got that going for us.

      As for the rest of the code (which is not working for me either) I can one main problem. When opening a new set of command instruction, you don't always close the series of command by calling the “---” command. And example of this is in your master code after the "C,00066666351B" command you delay of 70 seconds before calling the “---” command and let the module try to connect.
      Think of this as a state machine where you can set the module in a certain state by calling some commands and [always] finish with the “---”. Then you can delay to let the module in this 'state' for a while.

      Almost there.

      Delete
    9. I removed the while loop where I was doing status check.Also, I am now closing the commands before using delay().
      //Master code
      void setup() {

      pinMode(ledPin, OUTPUT);
      Serial.begin(115200);

      Serial.print("$$$");
      delay(100);
      Serial.println("SM,1");
      delay(100);
      Serial.println("C,00066666351B"); //Connect to address
      delay(100);
      Serial.println("---");
      delay(70000);
      }
      Similarly, I changed for slave. But, didn't work for me. Do you think this issue is because I am not using 'Sparkfun proto shield ' ?? If not, could you please send me the modified code which worked for you.

      Delete
    10. This code should work and is the same as what I've successfully tested yesterday. The shield has no impact on the test. I have used these modules directly on breadboards with only the Atmega328 chip (no arduino board and no shield) and it works fine.

      It is possible that your BT modules are slave only.

      Delete
    11. You might be right. I uploaded master code and still blueterm app could connect it. Using the app, I gave D command. the code sets the mode to master and also sets the slave as remote device.But, actually it's not behaving like a master.It should not be able to connect with other bluetooth devices.
      I factory reset my bluesmirf and tried again .No change. I will replace my master with another bluetooth and will try again.Thanks for all your help. I am really obliged. I will update you if I somehow succeed.

      Delete
    12. Hello Phillipe,
      I am back to inform you that finally I got the Master-Slave connection.It was not working because of authentication.I added BT.println("SA,0"); to the Master and slave modules and it started working. Invested so much time for such a small thing. But I learned a lot from your and other sources.

      Thanks & Regards.

      Delete
    13. I'm happy to hear that. Keep on hacking!

      Delete
  37. Hey! I cant enter the command mode since two days on my bluesmirf silver! I am running the same code from sparkfun website and hitting $$$ but no response from it! I have a arduino mega adk and i am using pins 14 15 as rx tx ..

    ReplyDelete
    Replies
    1. I would need to see the code to be able to help. Can you give the link?

      Delete
  38. Sorry Phillipe for a late reply here is the link https://learn.sparkfun.com/tutorials/using-the-bluesmirf/all

    ReplyDelete
    Replies
    1. Using this code you don't have to enter "$$$" since the setup function is doing that for you. You can use all the other command and I suggest you send a "---" when you're done, to close the command mode.

      Delete
  39. Dear Phillipe,
    This is karthik, i would like to ask you a Question, i am doing my project in Human emotion sensors. i have using (Affectiva Q sensor ) Device. This device is used to find the human skin temperature and this device have an inbuilt bluetooth. i already connect this device to my mobile and get the data. But now i am using arduino and bluetooth. is there any possibility to collect this device data using arduino.

    ReplyDelete
    Replies
    1. It shouldn't be a problem. When you connect using your phone take note of the MAC address of the sensor. You can then set your Arduino/bluetooth as a Master ("SM,1") and connect to this MAC address using the "C,000666123ABC" command (of course here you have to replace the 000666123ABC fake address with the address of your sensor).

      Delete
    2. But Phillipe, i have some error compilation problem in this masters code. could you please tell me, what is the problem.when i uploaded this coded it shows error compilation.

      Delete
    3. To help, I will need more info like the code and the error message (send it to: phillipe at cantin dot com)

      Delete
  40. Hi.... Could you help me with something? I want to pair a master RN-42 module with a slave HC-05 module? How can it be done?

    ReplyDelete
    Replies
    1. According to the user manual (https://www.sparkfun.com/datasheets/Wireless/Bluetooth/rn-bluetooth-um.pdf) it is suported. I have not tried it though. Yet another thing to try. bluetooth never ends

      Delete
  41. Hello Philipe, i have spent several hours trying to figure out whats wrong with my code, because my two RN42-XV bluetooth modules would not connect to each other, could you please help, i've set the authentication on both bluetooth modules to SA,2. could that be the problem ??? here's the code for both master and slave. Thank you.

    //Master
    #include

    String Slave1 = "0006666931BA"; //Mac Address of Slave1

    void setup() {
    Serial.begin(9600); //baudrate
    Serial.print("$$$"); // Enter command mode
    delay(100);
    Serial.println("SM,1"); // Act as Master
    delay(100);
    Serial.println("c,"+ Slave1 +"\r"); //connect to device
    delay(100);
    Serial.println("---,"+ Slave1 + "\r"); //exit command mode and enter data mode
    }

    void loop() {
    Serial.println("L"); //Send L when in data mode
    delay(100);
    Serial.println("H"); //Send H in data mode
    delay(100);
    }


    //slave
    #include

    String address = "00066669386C"; //address of master bluetooth

    void setup() {
    Serial.begin(9600); //baudrate
    Serial.print("$$$");
    delay(100);
    Serial.println("c,"+ address +"\r"); //connect to master
    delay(100);
    Serial.println("SM,0"); //slave mode
    delay(100);
    Serial.println("---,"+ address +"\r"); //exit command mode
    delay(100);
    }

    void loop() {
    if(Serial.available()>0) { //check if data is avialable
    char signal = Serial.read(); //read incoming data
    if(signal=='L') {
    digitalWrite(13,HIGH);
    delay(100);
    }
    else if (signal=='H') {
    digitalWrite(13,LOW);
    delay(100);
    }
    }
    }

    ReplyDelete