0

I'm having trouble receiving data from arduino and the bluetooth module HC-05. I tried to receive data with PySerial and Pybluez, but none have worked for me. I appreciate if someone could review what I'm doing wrong.

I found this in StackOverflow, but it did not work. Bluetooth communication between Arduino and PyBluez

This is my arduino code:

  #include <SoftwareSerial.h>

  #define RxD 10
  #define TxD 11

  SoftwareSerial BTSerial(RxD, TxD);

  void setup()
  {
    BTSerial.flush();
    delay(500);

    BTSerial.begin(9600);
    BTSerial.println("The controller has successfuly connected to the PC");

    Serial.begin(9600);
    delay(100);
  }

  void loop()
  {
    BTSerial.write("{Dato1: 545}");
  }

This's how I tested it with pyserial:

import serial

device_handler = serial.Serial('COM6', 9600, timeout=1)
count = 0
while (count < 5):
    print device_handler.readline()
    count += 1

device_handler.close()

And this's how I tried with pybluez, as explained in this link: https://people.csail.mit.edu/albert/bluez-intro/x232.html

import bluetooth
import sys
bd_addr = "20:15:03:19:27:02"

port = 1
sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
print 'Connected'
sock.settimeout(1.0)

count = 0;
while (count < 10):
    data = sock.recv(12)
    print 'received: %s'%data

    count += 1


sock.close()

None of the two forms worked for me. Pyserial doesn't throw an error and performs the five readings. Apparently it is getting nothing. On the other hand, pybluez throws this exception:

IOError: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

Thaks for your help and sorry for my English so bad.

Community
  • 1
  • 1
cluisalvarado
  • 13
  • 1
  • 5

1 Answers1

0

In you PyBluez code you have this line of code

sock.settimeout(1.0)

This is the line that is causing your error because the Arduino is not responding to your Bluetooth connection within that 1 (second?) time period. To fix this either remove that line of code or math the timeout longer.

TurkeyDev
  • 1
  • 1