Brathi1797
Posts: 4
Joined: Sat Jan 12, 2019 1:18 pm

Connecting to multiple bluetooth devices

Wed Jan 16, 2019 5:23 pm

Hi. I'm working on a project which involves transferring data from sensors connected to Arduino Uno boards. There are two such boards placed in different locations and I wish to transmit the sensor values using the HC-05 Bluetooth module to a Raspberry Pi 3 B+. I'll be using the onboard Bluetooth capabilities of the Pi. I need to do this programmatically using python. I cannot manually pair the Bluetooth devices.

After spending a lot of time, I found out how to connect one BT device and make Pi read data. I used the Bluetooth library and this is the code I wrote to connect two devices:

Code: Select all

import bluetooth, subprocess
nearby_devices = bluetooth.discover_devices(duration=4, lookup_names=True, flush_cache=True, lookup_class=False)
print nearby_devices

addr1 = "XX:XX:XX:XX"
port1 = 1
passkey = "1234"

addr2 = "XX:XX:XX:XX"
port2 = 2
passkey = "1234"

subprocess.call("kill -9 'pidof bluetooth-agent'", shell=True)

status = subprocess.call("bluetooth-agent" + passkey + "&", shell=True)

try:
    s1 = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
    s1.connect((addr1,port1))
    data = s1.recv(1024)
    print data
    subprocess.call("kill -9 'pidof bluetooth-agent'", shell=True)
    s1.close()
except bluetooth.btcommon.BluetoothError as err:
        print "Error"

try:
    s2 = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
    s2.connect((addr2,port2))
    data = s2.recv(1024)
    print data
    s2.close()
except bluetooth.btcommon.BluetoothError as err:
        print "Error"

The code works fine when I'm connecting to a single device instead of two, however, I face issues when I need to connect to two HC-05. I don't know how to go about it. I simply tried switching over to a different port with the address of the target HC-05 (as in the code). This is how I configured the "rfcomm.conf" file:

Code: Select all

rfcomm0 {
	bind yes;
	device XX:XX:XX:XX;
	channel  1;
	comment "Module 1";
}

rfcomm1 {
	bind yes;
	device XX:XX:XX:XX;
	channel 2;
	comment "Module 2";
}
I don't need to read data simultaneously from the two BT modules. What I'm looking for is reading the devices sequentially. i.e., connect to one of the HC-05 modules, receive data and then connect to the second module, receive data and repeat the whole process.

This is the Arduino code:

Code: Select all


#include <SoftwareSerial.h>

SoftwareSerial btSerial(10, 11); // TX, RX

void setup() {
    
  //Setup and flush the serials to begin
  btSerial.begin(9600);
  Serial.begin(9600);
  btSerial.flush();
  Serial.flush();
}

void loop() {

    float data = 150.25; //Just a constant for now. To be replaced with sensor data
    
    String sendData = "<" + String(data, 2) + ">";
    
    //Convert to byte array
    char charArray[sendData.length() + 1];
    sendData.toCharArray(charArray, sendData.length()+1);
    
    btSerial.write(charArray);
}

I tried reading the documentation, but resources for this specific application is sparse and I couldn't understand clearly what's required. Please help me out with this. Thanks in advance.

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Connecting to multiple bluetooth devices

Wed Jan 16, 2019 8:58 pm

Should work. What (exact) issues are you facing?

A couple of comments: blue-agent is not needed and is achieving nothing. And I don't believe rfcomm.conf is being used in later versions of Raspbian, so you'll need another way to bind the addresses, unless you are using an older OS.

Brathi1797
Posts: 4
Joined: Sat Jan 12, 2019 1:18 pm

Re: Connecting to multiple bluetooth devices

Thu Jan 17, 2019 4:17 am

Thanks for the quick reply.
My RPi runs the latest version of Raspbian Jessie. In the case that rfcomm.conf isn't used, and considering the fact that RPi pairs with the devices, I'm guessing I don't need to do anything to make RPi bind to the ports?

Update:
I tried connecting the HC-05 modules one at a time (without powering the other) and running the code to connect to the one that's ON. One module works fine and the other didn't!! I didn't get any error message. i.e., the exception block wasn't executed but, no data was received!! And then, I noticed something odd on my HC-05 modules. One blinks rapidly (I'm guessing it's the 5 times/sec pairing mode). The other blinks more slowly (around two times a sec). Not sure about the number but, it's visibly slower than the other module which works fine. And no, it's not in AT command mode. I checked that too using the serial monitor.

This is the output I'm getting with this module (just a blank line):
>>>
[('00:21:13:00:A3:41', 'HC-05\n')]
Received Data:

>>>

P.S: I added "print 'Received Data: ' before printing the data from the adapter to know that it indeed executes the try block.

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Connecting to multiple bluetooth devices

Thu Jan 17, 2019 4:46 am

The quickly blinking HC-05 indicates that it is not paired with another device. Pairing is a one-time operation that must be performed before the devices will connect. You can pair (and trust) the HC-05 from the Pi using bluetoothctl. Search the internet for exact instructions.

The HC-05's LED will go solid on when it is connected to the Pi.

Brathi1797
Posts: 4
Joined: Sat Jan 12, 2019 1:18 pm

Re: Connecting to multiple bluetooth devices

Thu Jan 17, 2019 5:17 am

I forgot to mention. I'm receiving data from the module that's blinking rapidly. So, that works as I want it to. The one with slow blinking rate doesn't output any data.

And I tried pairing but, it says
"Failed to connect: org.bluez.Error.Failed"

I tried pairing the devices using the Bluetooth icon on the taskbar and I got the following message:

"Connection Failed: No usable services on this device".

Even though not paired in the conventional sense, I'm able to receive data from one of the modules (the one blinking rapidly). The other one is what's causing problems.

Brathi1797
Posts: 4
Joined: Sat Jan 12, 2019 1:18 pm

Re: Connecting to multiple bluetooth devices

Thu Jan 17, 2019 10:33 am

Update:

I went into the AT mode of the HC05 modules to check a few things.

These are my findings:

The module which works fine -
Baud rate = 9600
Firmware = v2.0 20100601
State = Initialised

The module which doesn't work -
Baud rate = 38400
Firmware = V2.0 20161226
State = Pairable

I've been struggling with this for the past three days now. This is for a project in my college and the deadline for presentation is approaching... I'm desparate to make this work. I can proceed only if I figure this out. Any help is much appreciated. Thanks.

Return to “Networking and servers”