首页 > 解决方案 > 无法连接树莓派 Pi3B 蓝牙到 HC-06 模块?

问题描述

在我的 pi 崩溃后,我一直在尝试建立与 Hc-06 BT 模块的蓝牙连接。我过去成功地做到了,但由于 pi 崩溃而丢失了原始程序。我相信我已经遵循了每个教程的每一步,但不知何故它不起作用。

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

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。这种连接已经过测试并且很好。草图充当了两者之间的桥梁。

现在我的记忆告诉我,执行这行代码会在过去建立连接:

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() new Serial( this , "/dev/rfcomm0" , 9600 ); 之后,每 10 秒有效地调用以下行 程序的输出:

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...

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

不同的一件事是我的工作方法。事故发生后,我希望 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]#

标签: bluetoothprocessing

解决方案


我按照这些说明设置了我的 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 库,所以我用它来创建蓝牙串行客户端。

我使用了位于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 上,随机生成的数字出现在我的树莓上

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

推荐阅读