Problem sending char array from arduino to python using HC-05 and PyBlueZ

I have been trying to get a communication set-up between my raspberry pi model 3 B+ with PyBlueZ library for python 3 and my arduino uno with a HC-05 module in slave mode.

I have successfully paired and am able to send information both ways, however limited up to a couple of characters.

I could not find any solution on the internet, but I am also very new to networking and the HC-05 module so chances are also I don’t know the right place to look.

My Arduino code is as follows (see void openLock and void closeLock for where the sending part happens):

#include <SoftwareSerial.h>

int dataReceived;
char lockedMsg[] = "Locked it";
char openMsg[] = "Opened it";

SoftwareSerial BTserial(10, 11);

void setup() {
  BTserial.begin(38400);
}

void loop() {
  if (BTserial.available() > 0) {   // Check if data is coming in from the serial port
    dataReceived = BTserial.read(); // Read data from the serial port
  }
  if (dataReceived == '1'){
    BTserial.write(openMsg);
    delay(500);
    dataReceived = 3; // Prevent infinite looping
  }
  else if (dataReceived == '0'){
    BTserial.write(lockedMsg);
    delay(500);
    dataReceived = 3; // Prevent infinite looping
  }
}

In python I have the following:

import bluetooth, subprocess

name = "HC-05"
passkey = "1234"
addr = "98:D3:21:F7:4C:33"

port = 1

try:
    s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
    s.connect((addr, port))
    data = input()
    s.send(data)
    data_received = s.recv(1024)
    print(data_received)
except bluetooth.btcommon.BluetoothError as err:
    print(err)
    pass

The only characters being printed is “Loc” or “Ope”. I don’t know if the error is in python or arduino and at this point I have no clue how to approach troubleshooting this.

To check if the Arduino program works you should make a slightly modified version that sends to data to the Serial Monitor. Or, perhaps use a terminal program on your PC to receive and display the data from the Bluetooth serial port on the PC in place of your Python code.

If the Arduino program is working (as I suspect it is) then you need to get help on a Python forum.

This Python - Arduino demo may be of interest.

...R

Or use an application called 'Blueterm' on an Android tablet of phone to monitor the output of the Arduino Bluetooth device.

Thank you for the suggestion. I have slightly changed my python script to keep receiving until a specific end string/token is found.

while endstring not in received_data:
        rcv = s.recv(512)
        rcv = rcv.decode("utf-8")
        print("Received: "+ rcv)
        received_data = received_data + rcv
    print(received_data)

This returns the following output:

Received: Thi Received: s is a very long string and all of it Received: comes in multiple goes and Received: I have no clue why 7&7

This is a very long string and all of it comes in multiple goes and I have no clue why 7&7

So for some reason it receives it in bits and not in one go

So how have you checked, as suggested, that the Arduino is sending the entire string correctly ?

I would incidently suggest the same trouble shooting method if the receiver was another Arduino, first check that the data is being sent correctly…

Unfortunately I do not have an android device and my iOS device does not find the BT module. So I tried to see if at least it sends the entire string, which it does. Is it possible that the HC-05 does not send it all in one go? As I found that for TCP/IP messages it is normal to be fragmented, is it possible that it is normal for this as well?

https://stackoverflow.com/questions/13229688/only-receiving-one-byte-from-socket

Is it possible that the HC-05 does not send it all in one go?

Who knows, thats why you need a Bluetooth device to check.

The link you quoted is for an issue with TCP\IP and Python ?

The connection (pun intended) with Arduino is ?

Yes I do realise that, the arduino to python connection is RFCOMM, I stumbled upon it through another bluetooth thread. I thought perhaps anyone knew if the HC-05 is supposed to send data fragmented, in which case I will just have to send data packets of the same size or use an end line terminator like I did now.