Arduino和PyBluez之间的蓝牙通信

时间:2013-03-18 20:44:12

标签: python bluetooth arduino bluez

我正在尝试使用Python PyBluez在Arduino Uno板(带蓝牙防护板)和我的Linux操作系统之间建立蓝牙通信。

我已成功将笔记本电脑与Uno配对。我能够连接到电路板,但是电路板没有读取正在发送的数据,也无法发送数据。

这是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');
}

我的Python模块:

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()

我有Arduino IDE 1.0.4,我的笔记本电脑正在运行Ubuntu 11.10

2 个答案:

答案 0 :(得分:0)

你的这一行:

 string jsonObject = @"{  
                                   'titles':[  
                                  {  
                                     'lastUnlock':'2016-12-28T16:34:36.0390000Z',
                                     'titleId':566278,
                                     'serviceConfigId':'6ee10100-671e-4fc4-8cf1-91700008a406',
                                     'titleType':'DGame',
                                     'platform':'Durango',
                                     'name':'Game1',
                                     'earnedAchievements':4,
                                     'currentGamerscore':60,
                                     'maxGamerscore':1000
                                  },
                                  {  
                                     'lastUnlock':'2016-08-05T13:02:18.4140000Z',
                                     'titleId':10027721,
                                     'serviceConfigId':'28dd0100-1521-414e-a1d8-f0ba009902c9',
                                     'titleType':'DGame',
                                     'platform':'Durango',
                                     'name':'Game2',
                                     'earnedAchievements':17,
                                     'currentGamerscore':1000,
                                     'maxGamerscore':1000
                                  },
                                  {  
                                     'lastUnlock':'2016-05-02T20:52:40.3705214Z',
                                     'titleId':62572131,
                                     'serviceConfigId':'54240100-7870-4a47-8cec-7cfd03bac663',
                                     'titleType':'DGame',
                                     'platform':'Durango',
                                     'name':'Game3',
                                     'earnedAchievements':35,
                                     'currentGamerscore':1000,
                                     'maxGamerscore':1000
                                  },
                                     ],
                                   'pagingInfo':{  
                                      'continuationToken':null,
                                      'totalRecords':86
                                   }
                                }";


        var games = JsonConvert.DeserializeObject<Games>(jsonObject);

可能没有收到足够的字节,所以试试Tim发布的这个信息: Only receiving one byte from socket

答案 1 :(得分:-1)

使用Itade Studio蓝牙防护罩,您必须在没有连接防护罩的情况下上传代码,然后连接防护罩。我很好奇其他蓝牙设备是否如此。

为了完成,建立您的笔记本电脑和带有BT屏蔽的Arduino Uno之间的通信:

  1. 将笔记本电脑与蓝牙防护罩配对

  2. 使用屏蔽分离上传Arduino代码。

  3. 连接盾牌

  4. 运行python模块。

  5. 在itade盾牌上,当D1指示灯变为稳定的绿色时,您知道何时建立连接,而不是闪烁的绿色。