Page 1 of 1

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

Posted: Tue Jul 30, 2019 10:48 am
by gordon77
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.

Re: Connecting Bluetooth to Arduino and HC-05

Posted: Tue Jul 30, 2019 11:07 am
by PhatFil
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.

Re: Connecting Bluetooth to Arduino and HC-05

Posted: Tue Jul 30, 2019 11:10 am
by gordon77
Thanks I've tried this one, bluez not available?

Re: Connecting Bluetooth to Arduino and HC-05

Posted: Tue Jul 30, 2019 11:28 am
by PhatFil
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..

Re: Connecting Bluetooth to Arduino and HC-05

Posted: Tue Jul 30, 2019 12:24 pm
by gordon77
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

Re: Connecting Bluetooth to Arduino and HC-05

Posted: Tue Jul 30, 2019 1:15 pm
by gordon77
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 !

Re: Connecting Bluetooth to Arduino and HC-05

Posted: Tue Jul 30, 2019 6:08 pm
by gordon77
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)

Re: Connecting Bluetooth to Arduino and HC-05

Posted: Tue Jul 30, 2019 6:48 pm
by Douglas6
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.

Re: Connecting Bluetooth to Arduino and HC-05

Posted: Tue Jul 30, 2019 7:03 pm
by gordon77
Thanks, all useful info. I'm still learning. :)

Re: Connecting Bluetooth to Arduino and HC-05

Posted: Tue Jul 30, 2019 7:17 pm
by scruss
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 …

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

Posted: Tue Jul 30, 2019 10:58 pm
by Douglas6
Thanks for the tip scruss. A situation I haven't come across.