[EDIT]The standard RFCOMM/SPP implementation is available in rev.2.0 of the library.[/EDIT]
I am pleased to announce addition of Bluetooth dongles to the family of USB devices supported by USB Host Shield. Bluetooth dongles are affordable, interface is well documented, and USB transport layer is very simple. Even though full Bluetooth protocol is heavy and takes a lot of program space, it can be stripped down quite a bit for a particular application. One good example of such approach is Richard Ibbotson’s HID over Bluetooth implementation – see Wiimote Game Controller Interfacing article for details. I wanted to have simple communication transport over Bluetooth which would fit into Arduino and the code I’m presenting in this article is doing just that – it’s lightweight terminal program allowing two Arduinos talk to each other over Bluetooth. The code has been developed by George Lgotkin.
The source is hosted in GitHub repository. Standard Arduino library format of the code has not been tested yet, for now it’s easier to just copy all the files in a directory and open btclass_h.pde
in Arduino IDE. To use the program, you need two Arduinos, two USB Host Shields, and two Bluetooth dongles. You need to compile and load the sketch into both Arduinos and then open two terminal windows, one to a serial port of each Arduino – this can be done from a single computer. Don’t use Arduino IDE built-in terminal. Also, SPI library shipped with recent versions of Arduino IDE is not compatible with USB Host library, if you have compiler errors related to SPI, replace your library with one from Arduino-0018 or the Playground. Serial port speed is set to 115200 in the sketch, it can be changed in setup()
, if necessary.
Sketch starts by printing short help (see screenshot below) and then waits for the command. In order for two Bluetooth devices to talk to each other, one has to be the server and another one client. In one terminal, type ‘S’ and press Enter. This starts the server. If you are curious, type ‘M’, Enter – the address of the module will be printed. Note that if you send address request before starting the server, address containing all zeroes will be returned.
To establish connection, type ‘C’, Enter in other terminal. The sketch will print “Connecting…”. After some time ( 30 seconds or less ) both terminals will print “Connected”. Now type something in one terminal and press Enter – the string you just typed will appear in other terminal window. Easy.
This code is just a small example, error handling is minimal. It works better if no other Bluetooth radios are transmitting nearby. If client Arduino reports “Connected” but server doesn’t, it means that client has connected to some other server, most likely your laptop or phone. This can also be used to your advantage for testing – start Bluetooth on a PC and then see if both Arduinos are able to connect to it in client mode.
It is possible to instruct client Arduino to connect to a particular host. Look into Serial.cpp
file. InbufAnalyse
function contains addrA.bytes
array initialization. You can initialize it with address of the Bluetooth dongle connected to other Arduino (note that address bytes are loaded backwards), recompile, load, and then use ‘A’ instead of ‘C’ for client connection.
The code has been tested with different Bluetooth dongles from different manufacturers and they all seem to work fine. Sometimes one dongle refuses to work as a client so alternating server and client roles on Arduino may help. If you see any other errors, leave a comment here and I’ll try to help.
In this sketch, communication occurs over plain HCI so it can’t be used to talk to a PC or a phone. On the other hand, the code takes less than 11K of program space and can be used on Atmega168-based Arduinos. The code which implements SDP, L2CAPS and RFCOMM is in the works and will be released soon along with the documentation. For the time being, play with this code and tell me what you think.
Enjoy,
Oleg.
Hi, great job!!!
I need to connect 2 arduinos.
One with a usb bluetooth dongle (through a USB host shield).
The other has no usb host shiel but a bluetooth module connected to RX/TX (a JY-MCU)
Could I use that code to transmit data form one to the other????
THANKSSSSS
See my reply to your email 😉
This will depend on the Bluetooth module. Arduino SPP client is slave only, so it can only connect to SPP master. Many Bluetooth dongles I have seen were also slave only devices.
mi arduino no tiene un puerto usb para conectar ese adaptador Bluetooth…como lo conecto si no tiene?
Are the files linked in this post still up to date?
When compiling the project I get the error
In file included from btclass_h.ino:17:
bt_h.h:104: error: 'EP_RECORD' does not name a type
I had to comment out the lines
#include "Max3421e.h"
because they were triggering errors about calling “Max3421e.h” directly.How can I get my two arduinos communicating with each other over bluetooth dongles connected to usb host shields? I thought this post was my best bet to get them talking.
This code is very old, it is unlikely to work with current rev. of the library. Current BT implementation only supports client side so you can’t make 2 Arduinos talk to each other with it. To make it work you either need to make this code work with current library or add server support to current BT code. Pick your poison.
Would it at least be possible to send commands to a slave bluetooth dongle running SPP to turn on pins and such? Since I cannot use two arduinos, would it be possible to run a script on my computer that pairs with one arduino over bluetooth and checks the state of pins on the Arduino? I’ve downloaded Putty. Could I run a script on that? Would I need a bluetooth transmitter like the BlueSmirf to do these functions?
Once a data link is established anything can be sent over it.
Yes that is exactly what the SPP library is for: https://github.com/felis/USB_Host_Shield_2.0/tree/master#spp-library.
Salut j’ai un arduino adk le code sa ne march pas avec car je veux travaille avec le port usb antegrer sur la carte
I have tried to translate your comment using Google translate, but it can’t make sense of it. Please try to post your question in english.
hello!
I am trying to interface a ps3 controller via bluetooth to arduino mega 2560 adk.BUt didn’t change the data coming from the ps3 controller.
after uploading the code i get result on serial monitor as given below.
PS3 Bluetooth Library Started
Bluetooth Dongle Initialized
HCI Reset complete
Write class of device
Local Bluetooth Address: 00:1B:10:00:2A:EC
Wait For Incoming Connection Request
Dualshock 3 Controller Connected
Bluetooth Address was set to: 00:1B:10:00:2A:EC
Bluetooth Dongle Initialized
HCI Reset complete
Write class of device
Local Bluetooth Address: 00:1B:10:00:2A:EC
Wait For Incoming Connection Request
Keyboard is connecting
Incoming Connection Request
Remote Name: PLAYSTA
Connected to Device: 00:26:5C:D1:B8:D6
HID Control Incoming Connection Request
HID Control Successfully Configured
HID Interrupt Incoming Connection Request
HID Interrupt Successfully Configured
Wait For Incoming Connection Request
Dualshock 3 Controller Enabled
LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 1
LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 1
LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 1
LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 1
LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 1
.
.
.
NEED HELP!
This is because your dongle does not support 2.0+EDR, so you will need another dongle. Please refer to: https://github.com/felis/USB_Host_Shield_2.0/wiki/Bluetooth-dongles.
sir i have tried the “TRENDnet TBW-107UB” bluetooth dongle which is listed in https://github.com/felis/USB_Host_Shield_2.0/wiki/Bluetooth-dongles.
but still i am getting the same result as previous.
the link from where i purchase the dongle is- http://www.amazon.in/TRENDnet-Micro-Bluetooth-Adapter-TBW-107UB/dp/B003KU4DOG/ref=sr_1_1?ie=UTF8&qid=1418892604&sr=8-1&keywords=tbw-107ub
Thank to every one for support …the problem was that my ps3 controller was faulty…now i changed the ps3 controller and everything is going well..
thanks.
@dhanraj choudhary : brother please help me, i am stuck at exactly same problem as yours. i have PS3 dual shock 3 from china and CSR 4.0 2.1+ edr dongle. the controller works fine on android device via sixaxispair app wirelessly. i got contineous printing same as below:
LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 1
LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 1
LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 1
LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 1
LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 1
.
i have mega 2560, ps3 controller from china(but works perfactly wirelessy with android device),CSR 4.0 2.1+edr dongle
I did all the connection and tried uploading code into arduino UNO r3
But I found on serial monitor that “PS3 Bluetooth Library Started”.
It dose not show me any further change even I reset USB Host shield
Hi Im looking to connect a set of scales with an serial/usb connection to an arduino mega, the scales are a ACM device outputing ascii data @ upto 9600bd with 8N1,
is there a model i can follow to set this up
TIA
OK, i found your scales page.. new at this and only just found out that i needed a usb master to connect to my device.. lots to read up on cheers..
Love the code, the minor issue I’m having is that I have to physically press the ‘reset’ button on the Arduino to initialize the bt adapter. I bought the GMYLE from the list. Any suggestions?
Help me !!! please
PS3 Bluetooth Library Started
Bluetooth Dongle Initialized
HCI Reset complete
Write class of device
Local Bluetooth Address: 00:15:83:0C:BF:EB
Wait For Incoming Connection Request
Dualshock 3 Controller Connected
Bluetooth Address was set to: 00:15:83:0C:BF:EB
getConf
BTD Init Failed, error code: 0D
Bluetooth Dongle Initialized
HCI Reset complete
Write class of device
Local Bluetooth Address: 00:15:83:0C:BF:EB
Wait For Incoming Connection Request
Keyboard is connecting
Incoming Connection Request
Remote Name: PLAYSTATION(R)3 Controller
Connected to Device: 00:26:5C:05:48:BF
HID Control Incoming Connection Request
HID Control Successfully Configured
HID Interrupt Incoming Connection Request
HID Interrupt Successfully Configured
Wait For Incoming Connection Request
Dualshock 3 Controller Enabled
LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 1
LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 1
LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 1
LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 1
….