gordon77
Posts: 4220
Joined: Sun Aug 05, 2012 3:12 pm

[solved] Connecting Pi bluetooth to Arduino and HC-05

Tue Jul 30, 2019 10:48 am

Can someone advise me or point me to a website to achieve this. I want to send / receive data between them, using python.
All websites I can find are out of date ?

I can pair..but after that it says no connection as no services.
Last edited by gordon77 on Tue Jul 30, 2019 8:21 pm, edited 3 times in total.

PhatFil
Posts: 1415
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Connecting Bluetooth to Arduino and HC-05

Tue Jul 30, 2019 11:07 am

Bluetooth provides a couple of communication methods, probably the easiest to use is serial over bluetooth connection.

This came up searching for the keywords 'arduino bluetooth serial pi'
http://www.uugear.com/portfolio/bluetoo ... d-arduino/
check the arduino lib for your h/w for a serial coms example sketch.

gordon77
Posts: 4220
Joined: Sun Aug 05, 2012 3:12 pm

Re: Connecting Bluetooth to Arduino and HC-05

Tue Jul 30, 2019 11:10 am

Thanks I've tried this one, bluez not available?

PhatFil
Posts: 1415
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Connecting Bluetooth to Arduino and HC-05

Tue Jul 30, 2019 11:28 am

gordon77 wrote:
Tue Jul 30, 2019 11:10 am
Thanks I've tried this one, bluez not available?
then install it..
sudo apt-get install bluetooth bluez
entered at the cli should do it..

gordon77
Posts: 4220
Joined: Sun Aug 05, 2012 3:12 pm

Re: Connecting Bluetooth to Arduino and HC-05

Tue Jul 30, 2019 12:24 pm

thanks, but sudo bluez-simple-agent hci# xx:xx:xx:xx:xx:xx

gives sudo: bluez-simple-agent: command not found

and

sudo rfcomm bind all

gives Missing dev parameter
Last edited by gordon77 on Tue Jul 30, 2019 8:22 pm, edited 1 time in total.

gordon77
Posts: 4220
Joined: Sun Aug 05, 2012 3:12 pm

Re: Connecting Bluetooth to Arduino and HC-05

Tue Jul 30, 2019 1:15 pm

I've managed to stumble through this and if I now reboot and do a sudo rfcomm bind 0 xx:xx:xx:xx:xx:xx I can communicate :D

It's all as clear as mud !

gordon77
Posts: 4220
Joined: Sun Aug 05, 2012 3:12 pm

Re: Connecting Bluetooth to Arduino and HC-05

Tue Jul 30, 2019 6:08 pm

To anyone interested here's my quickest way to get it working..

I used a Pi4, Arduino Uno R3, and a DSD HC-05 module.

I made a new SD card with FULL Raspbian (BUSTER dated 10/7/2019)

Installed the HC-05 on the Arduino Uno R3, note the Uno R3 is all 5v so no resistors required.

UNO >> HC-05
5v >> VCC
GND >> GND
RX >> TX
TX >> RX

Connect an LED, and suitable resistor, between I/O 13 and GND on the Uno.

Install the following into the Uno for testing. NOTE best to do this with a USB cable BEFORE you install the HC-05.

Code: Select all

int led = 13;

// the setup routine runs once when you press reset:
void setup() { 
  Serial.begin( 9600 );  
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  // listen for the data
  if ( Serial.available() > 0 ) {
    // read a numbers from serial port
    int count = Serial.parseInt();
    
     // print out the received number
    if (count > 0) {
        Serial.print("You have input: ");
        Serial.println(String(count));
        // blink the LED
        for (int lup = 0; lup < count; lup++) { 
        // turn the pin on:
        digitalWrite(led, HIGH);   
        delay(500);                  
        // turn the pin off:
        digitalWrite(led, LOW);
        delay(500);    
  }
    }
  }               // wait for a second
}

On the Pi...

Enter hcitool scan in a terminal window, this will show you the HC-05 address

Code: Select all

pi@raspberrypi:~ $ hcitool scan
Scanning ...
	00:15:02:09:14:E5	DSD TECH HC-05
	

Click on the Bluetooth symbol on the PI Taskbar, click on Add Device, It should search and find DSD TECH HC-05.
Click on PAIR, enter the PIN CODE 1234 when requested.
It should now pair but say no services which can be used on the Raspberry Pi.

Modify the following python script on your Pi with the address found above, and run it.
It will ask you to input a number, and the LED on the UNO will flash that many times if working OK, and print a response like ...You have input: 5...on the Pi.

Hopefully now you can progress and write your own scripts..


Code: Select all

#!/usr/bin/python3
 
import serial
import os
import time

if os.path.exists('/dev/rfcomm0') == False:
    path = 'sudo rfcomm bind 0 00:15:02:09:14:E5'
    os.system (path)
    time.sleep(1)

bluetoothSerial = serial.Serial( "/dev/rfcomm0", baudrate=9600 )
 
count = None
while count == None:
    count = int(input( "Please enter the number of times to blink the LED : "))

j = str(count) 
b = j.encode() 
bluetoothSerial.write(b)
RXData = (bluetoothSerial.readline()).strip().decode("utf-8")
print (RXData)
Last edited by gordon77 on Tue Jul 30, 2019 8:23 pm, edited 2 times in total.

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

Re: Connecting Bluetooth to Arduino and HC-05

Tue Jul 30, 2019 6:48 pm

My experience, may not be helpful:
1. I always use SoftwareSerial for the HC-05 on the Arduino. It works great and frees up the hardware serial for programming and debugging.
2. I like to put the the rfcomm command in a systemd service unit on the Pi. That way it's always available.

Is the Serial Port Profile available by default in Buster? I'll need to look into that.

gordon77
Posts: 4220
Joined: Sun Aug 05, 2012 3:12 pm

Re: Connecting Bluetooth to Arduino and HC-05

Tue Jul 30, 2019 7:03 pm

Thanks, all useful info. I'm still learning. :)

User avatar
scruss
Posts: 2546
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Connecting Bluetooth to Arduino and HC-05

Tue Jul 30, 2019 7:17 pm

Thanks for the writeup, Gordon: remembering all the stages is the difficult bit.

SoftwareSerial on the Arduino, though great, can really mess up timing accuracy. If you're using SoftwareSerial and millis(), prepare for unusual numbers …
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

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

Re: [solved] Connecting Pi bluetooth to Arduino and HC-05

Tue Jul 30, 2019 10:58 pm

Thanks for the tip scruss. A situation I haven't come across.

Return to “Troubleshooting”