无法将树莓派Pi3B蓝牙连接到HC-06模块?

时间:2020-07-07 18:38:30

标签: bluetooth processing

我的pi崩溃后,我一直在尝试建立与Hc-06 BT模块的蓝牙连接。我过去成功完成了此操作,但由于pi崩溃而丢失了原始程序。我相信我已经按照每个教程的每个步骤进行了操作,但是不知何故。

到目前为止,我做了什么: 我做到了:

  • 使用我的手机和BT串行应用程序连接到HC-06,以验证它是否正常工作!
  • 让pi与HC-06成功配对
  • 让pi信任hc-06。
  • 每次启动应用程序时,我都会运行如下的shell脚本

sudo killall Xvfb

sudo killall java

sudo rfcomm bind rfcomm0 20:14:04:15:23:75 //是,这里的地址是正确的

sudo xvfb-run /home/pi/Documents/application.linux-armv6hf/piTrain

我正在使用的应用程序是一个处理草图。我开发了一个功能以连接到蓝牙模块。 BT背后的设备是arduino nano。整体是一个手动控制器(<-代码中串行对象的名称)。处理草图还通过USB连接到另一个arduino nano。此连接已经过测试,性能良好。草图充当2之间的桥梁。

现在我的记忆告诉我,执行以下代码行可以建立连接:

handController = new Serial( this , "/dev/rfcomm0" , 9600 );

它不再这样做了,所以显然我忘记了要做的事情。

握手功能:

void handShaking() {
    int b;

    if( handController.available() > 0 ) {
        if( handControllerConnected == false ) {
            handControllerConnected = true;
            println("connection with handcontroller established!");
        }

        b = handController.read();
        dccCentral.write( b );
        println("controller: " + (char) b );

        handControllerTimeout = millis() + 5000 ;
    }

    if(dccCentral.available() > 0 ) {
        b = dccCentral.read();
        handController.write( b );
        println("central: " + (char) b );
    }

    if( millis() > handControllerTimeout ) {

        if( handControllerConnected == true ) {
            handControllerConnected = false;
            println("connection with hand controller lost!");
            handControllerTimeout = millis() + 5000 ;
        }
        else {
            try {
                handController.stop();
            }
            catch ( NullPointerException  e) {
                println(" error terminating connection ");
            }
            try {
                handController = new Serial( this , "/dev/rfcomm0" , 9600 );
                handController.write('$');
                handController.write('$');
                handController.write('9');
                println("attempting to connect with hand controller...");
            } 
            catch(Exception e){
                println("error cannot set up hand controller connection");
            }
            handControllerTimeout = millis() + 10000 ;
        }
    }
}

在我调用handController.stop()之后,每隔10秒就会有效地调用下一行 新的Serial(this,“ / dev / rfcomm0”,9600); 程序的输出:

pi@raspberrypi:~ $ ./boot.sh 
Can't create device: Device or resource busy
0 /dev/rfcomm0
1 /dev/serial1
2 /dev/ttyAMA0
3 /dev/ttyUSB0
setting up connecton with DCC central
12
respons = hello
respons confirmed, connection with DCC central established
setting up connecton with RS485 bus
connecton set up!
rs485 bus cleared
central:
central: B
central: a
central: s
central:
central: 

central: 
connection with hand controller lost!
attempting to connect with hand controller...
attempting to connect with hand controller...

只要Wi-Fi不会崩溃,这种情况就会持续下去。

与众不同的一件事是我的工作方法。飞机坠毁后,我希望Rpi尽可能“哑巴”。我在Windows PC上以VS代码对处理草图进行编码。我使用processing-java命令行工具导出草图。使用scp,我将整个文件夹传输到rpi,然后运行启动脚本。

也许这确实意味着一些有用的东西:

pi@raspberrypi:~ $ sudo rfcomm connect hci0 20:14:04:15:23:75
Can't connect RFCOMM socket: Connection reset by peer

pi@raspberrypi:~ $ sudo rfcomm connect rfcomm0 20:14:04:15:23:75
Can't connect RFCOMM socket: Connection refused

上次蓝牙连接是在公园散步,它只是...起作用。

有人可以告诉我我忘记或做错了什么吗?

编辑: 额外信息。在蓝牙程序中,我得到了这个输出。连接后我快速运行信息,仍然会自动断开连接

[bluetooth]# connect 20:14:04:15:23:75
Attempting to connect to 20:14:04:15:23:75
[CHG] Device 20:14:04:15:23:75 Connected: yes
[CHG] Device 20:14:04:15:23:75 ServicesResolved: yes
Failed to connect: org.bluez.Error.NotAvailable
[Hand Controller]# info 20:14:04:15:23:75
Device 20:14:04:15:23:75 (public)
        Name: Hand Controller
        Alias: Hand Controller
        Class: 0x00001f00
        Paired: yes
        Trusted: yes
        Blocked: no
        Connected: yes
        LegacyPairing: yes
        UUID: Serial Port               (00001101-0000-1000-8000-00805f9b34fb)
[CHG] Device 20:14:04:15:23:75 ServicesResolved: no
[CHG] Device 20:14:04:15:23:75 Connected: no
[bluetooth]# info 20:14:04:15:23:75
Device 20:14:04:15:23:75 (public)
        Name: Hand Controller
        Alias: Hand Controller
        Class: 0x00001f00
        Paired: yes
        Trusted: yes
        Blocked: no
        Connected: no
        LegacyPairing: yes
        UUID: Serial Port               (00001101-0000-1000-8000-00805f9b34fb)
[bluetooth]#

1 个答案:

答案 0 :(得分:0)

我按照以下说明安装了arduino和HC-06模块: https://www.aranacorp.com/en/arduino-and-bluetooth-module-hc-06/

使用以下草图将其连接起来

#include <SoftwareSerial.h>

SoftwareSerial hc06(2,3);

String cmd="";
float sensor_val=0;

void setup(){
  //Initialize Serial Monitor
  Serial.begin(9600);
  //Initialize Bluetooth Serial Port
  hc06.begin(9600);
}

void loop(){
  //Read data from HC06
  while(hc06.available()>0){
    cmd+=(char)hc06.read();
  }

  //Select function with cmd
  if(cmd!=""){
    Serial.print("Command recieved : ");
    Serial.println(cmd);
    // We expect ON or OFF from bluetooth
    if(cmd=="ON"){
        Serial.println("Function is on");
    }else if(cmd=="OFF"){
        Serial.println("Function is off");
    }else{
        Serial.println("Function is off by default");
    }
    cmd=""; //reset cmd
  }
  // Simulate sensor measurement
  sensor_val=(float)random(256); // random number between 0 and 255
    
  //Write sensor data to HC06
  hc06.print(sensor_val);
  delay(100);
}

然后在我的Raspberry Pi上执行以下操作以将Raspberry Pi与HC-06配对:

$ bluetoothctl 
Agent registered
[bluetooth]# scan on
Discovery started
[CHG] Device 00:00:12:06:53:92 LegacyPairing: no
[CHG] Device 00:00:12:06:53:92 Name: HC-06
[CHG] Device 00:00:12:06:53:92 Alias: HC-06
[bluetooth]# scan off
[bluetooth]# pair 00:00:12:06:53:92 
Attempting to pair with 00:00:12:06:53:92
[CHG] Device 00:00:12:06:53:92 Connected: yes
Request PIN code
[agent] Enter PIN code: 1234
[CHG] Device 00:00:12:06:53:92 UUIDs: 00001101-0000-1000-8000-00805f9b34fb
[CHG] Device 00:00:12:06:53:92 ServicesResolved: yes
[CHG] Device 00:00:12:06:53:92 Paired: yes
Pairing successful
[CHG] Device 00:00:12:06:53:92 ServicesResolved: no
[CHG] Device 00:00:12:06:53:92 Connected: no
[bluetooth]# 

我已经安装了Blue Dot Python库,所以我用它来创建Bluetooth串行客户端。

我使用了位于https://bluedot.readthedocs.io/en/latest/btcommapi.html#bluetoothclient的文档来创建以下内容:

from bluedot.btcomm import BluetoothClient
from signal import pause

def data_received(data):
    print(data)

c = BluetoothClient("00:00:12:06:53:92", data_received)
c.send("ON")

pause()

“ ON”命令出现在Arduino上,随机生成的数字出现在我的Raspberry上

$ python hc06.py 
09.00
8
.00
83.00
79.00
81.00
2
48.00