3

I am trying to establish bluetooth communication between an Arduino Uno board (with a bluetooth shield) and my Linux OS, using Python PyBluez.

I've successfully paired my laptop to the Uno. I'm able to connect to the board, however the board is not reading the data being sent nor is it able to send data.

Here is the Arduino Sketch

#include <SoftwareSerial.h>

#define RxD 0    //receive data on digital 0
#define TxD 1 //transmit on digital 1

SoftwareSerial blueToothSerial(RxD, TxD);
int counter = 0;
int incoming;
void setup(void){
  Serial.begin(9600);
  //pinMode(RxD,INPUT);
  //pinMode(TxD,OUTPUT);
  setupBlueToothConnection();
}

void setupBlueToothConnection(){
  blueToothSerial.begin(19200);
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as     "SeeedBTSlave"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  //blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
  delay(2000); // This delay is required.
  blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
  Serial.println("The slave bluetooth is inquirable!");
  delay(2000); // This delay is required.
  blueToothSerial.flush();
} 

void loop(){
  if(blueToothSerial.available())
     Serial.println(blueToothSerial.read());
  blueToothSerial.write('x');
}

And my Python Module:

import bluetooth
import sys
bd_addr = "00:12:10:23:10:18" #itade address

port = 1
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
print 'Connected'
sock.settimeout(1.0)
sock.send("x")
print 'Sent data'

data = sock.recv(1)
print 'received [%s]'%data

sock.close()

I have Arduino IDE 1.0.4, my laptop is running Ubuntu 11.10

mingxiao
  • 1,712
  • 4
  • 21
  • 33
  • Can you send and receive data to/from Arduino board using Serial Monitor inside Arduino IDE? – 4d4c Mar 18 '13 at 21:03
  • Seems to work for me. I uploaded your script on my Arduino Uno. And executed the python script. This is the result: `user@ubuntu:~/$ python w.py Connected Sent data received [2]`. Are you sure that you have working connection to your Arduino and Bluetooth device? – 4d4c Mar 18 '13 at 21:40
  • Which Bth shield are you using: is it [this one](http://www.seeedstudio.com/wiki/Bluetooth_Shield)? – angelatlarge Mar 19 '13 at 04:49
  • No, [RS232](http://www.amazon.co.uk/Serial-Bluetooth-Module-Transceiver-Arduino/dp/B008VOR67C/ref=sr_1_2?ie=UTF8&qid=1363693797&sr=8-2) – 4d4c Mar 19 '13 at 11:46
  • @ton1c yes I can send/receive data through the serial monitor inside the Arduino IDE. – mingxiao Mar 27 '13 at 19:08
  • @angelatlarge No I am not using the Seeed shield, nor am I using the RS232, I am using the itead BT shield [link](http://imall.iteadstudio.com/im120417006.html) – mingxiao Mar 27 '13 at 19:13
  • @ton1c My python module prints 'Sent' however I don't see the sent data on the serial monitor of the IDE. And my module gets stuck on recv() – mingxiao Mar 27 '13 at 19:16
  • Is the SoftwareSerial library even needed? Someone with a similar [problem](http://stackoverflow.com/questions/11048062/sending-ascii-from-c-sharp-to-arduino-using-bluetooth?rq=1) does not use the SoftwareSerial library. – mingxiao Mar 27 '13 at 20:19
  • Wait, like all similar Bth boards this shield uses 9600 baud by default. Why do you have `blueToothSerial.begin(19200);` instead of `blueToothSerial.begin(9600);`? – angelatlarge Mar 27 '13 at 20:34
  • Have you tried using software serial on pins 2 and 3? – Jack Wilson Oct 19 '16 at 06:26

2 Answers2

0

This line of yours:

data = sock.recv(1)

may be not receiving enough bytes so try this info posted by Tim: Only receiving one byte from socket

TomoMiha
  • 1,218
  • 1
  • 14
  • 12
-1

Using the Itade Studio Bluetooth Shield, you have to upload the code without the shield attached, and then attach the shield. I'm curious if this is so with other bluetooth units.

For the sake of completion, to establish communication between your laptop and Arduino Uno w/BT shield:

  1. Pair your laptop with the bluetooth shield

  2. Upload Arduino code with shield detached.

  3. Attach the shield

  4. Run python module.

On the itade shield, you know when connection is established when the D1 led turns solid green, as oppose to flickering green.

mingxiao
  • 1,712
  • 4
  • 21
  • 33