Serialization Raspberry Pi 3/Zero W和Arduino/HM-10之间的蓝牙串行

Serialization Raspberry Pi 3/Zero W和Arduino/HM-10之间的蓝牙串行,serialization,bluetooth,bluetooth-lowenergy,raspberry-pi3,bluez,Serialization,Bluetooth,Bluetooth Lowenergy,Raspberry Pi3,Bluez,我正在尝试在运行Raspbian Jessie[03-07-2017]的Raspberry Pi Zero W和Arduino(UNO)之间建立蓝牙串行通信链路。 我目前能够使用bluetoothctl将数据写入Arduino 应用程序要求我们能够将数据写入特定的BLE从机。有多个[HM-10]从机可供切换,需要在程序执行期间选择从机 没有波特率偏好。目前,我们普遍使用9600 创建了自动连接并将数据写入“属性”的函数,该属性在Arduino的串行监视器上显示为数据 Python代码-使用Blu

我正在尝试在运行Raspbian Jessie[03-07-2017]的Raspberry Pi Zero W和Arduino(UNO)之间建立蓝牙串行通信链路。 我目前能够使用
bluetoothctl
将数据写入Arduino

应用程序要求我们能够将数据写入特定的BLE从机。有多个[HM-10]从机可供切换,需要在程序执行期间选择从机

没有波特率偏好。目前,我们普遍使用9600

创建了自动连接并将数据写入“属性”的函数,该属性在Arduino的串行监视器上显示为数据

Python代码-使用BlueZ 5.44(手动安装):

这种方法适用于小数据集,但我的需求要求我非常快速地将数据流传输到Arduino

当前设置为:

  • Pi通过USB串行接收传感器数据(加速计、EEG)
  • Pi处理数据
  • 然后通过Pi Zero W的内置蓝牙将命令发送到Arduino
  • 但是,使用此方法时,当传感器数据发生变化时,蓝牙数据传输将延迟(暂时冻结)


    当使用两个预先配对的HM-10模块时,数据传输是完美的,Pi的GPIO串行端口是使用PySerial配置的

    还尝试了以下方法:

    • 使用WiringPi在/dev/ttyam0上设置蓝牙串行端口
    • 使用Python套接字和
      rfcomm
    尝试使用这两种方法时。然而,Python代码可以编译,一旦串口打开,数据似乎就不会被写入,也不会显示在Arduino的串行监视器上

    这将削弱以前的功能。即使手动使用
    bluetoothctl
    ,模块也不能取消配对/断开连接。写入适当的属性也不起作用

    需要重新启动才能恢复正常功能


    这种方法正确吗? 有没有更好的方法通过BLE发送数据


    更新日期:2017年7月5日 我不再从事这个项目了。但故障排除使我相信代码中的“竞争条件”可能导致程序无法正常运行。
    这一点在测试阶段得到了验证,测试阶段创建了一个功能非常好的基本代码

    import subprocess
    from subprocess import Popen, PIPE
    
    # Replaces the ':' with '_' to allow the MacAddress to be in the form
    # of a "Path" when "selecting an attribute"
    def changeMacAddr(word):
        return ''.join(c if c != ':' else '_' for c in word)
    
    # Connects to a given MacAddress and then selects the attribute to write to
    def connBT(BTsubProcess, stringMacAddr):
        BTsubProcess.stdin.write(bytes("".join("connect "+stringMacAddr +"\n"), "utf-8"))
        BTsubProcess.stdin.flush()
        time.sleep(2)
        stringFormat = changeMacAddr(stringMacAddr)
        BTsubProcess.stdin.write(bytes("".join("select-attribute /org/bluez/hci0/dev_"
                                  + stringFormat +
                                  "/service0010/char0011" + "\n"), "utf-8"))
        BTsubProcess.stdin.flush()
    
    # Can only be run once connBT has run - writes the data in a list [must have numbers 0 - 255 ]
    def writeBT(BTsubProcess, listOfData):
        stringList = [str('{0} ').format(elem) for elem in listOfData]
        BTsubProcess.stdin.write(bytes("".join("write " + "".join(stringList) + "\n"), "utf-8"))
        BTsubProcess.stdin.flush()
    
    # Disconnects
    def clostBT(BTsubProcess):
        BTsubProcess.communicate(bytes("disconnect\n", "utf-8"))
    
    # To use the functions a subprocess "instance" of bluetoothctl must be made
    blt = subprocess.Popen(["bluetoothctl"], stdin=subprocess.PIPE, shell=True)
    # blt with then be passed into the function for BTsubProcess
    
    # Note: the MacAddresses of the Bluetooth modules were pre-connected and trusted manually via bluetoothctl