16034 views|1 replies

15

Posts

0

Resources
The OP
 

[Raspberry Pi 3B+ Review] Try Bluetooth with pybluez examples [Copy link]

This post was last edited by icefire2012 on 2018-9-21 23:31 This week I learned about the Bluetooth module on 3B+. 1. Understanding the Bluetooth hardware part I found the schematic diagram of 3B+ on the official website of Raspberry Pi (see reference link 1), but only part of it, the Bluetooth chip and the connection interface with the CPU are not shown. I found on another website (see reference link 2) that the Bluetooth of 3B+ uses Cypress's CYW43455. CYW43455 integrates high-performance 802.11ac, low-power Bluetooth 4.2 and classic Bluetooth. The structure diagram of the manual of CYW43455 (see reference link 3) is as follows:
In terms of hardware, the external interface of the Bluetooth part, in addition to a few control signals, is UART, I2S and PWM. I have seen on other websites (I don't remember the source) that the CPU is connected to the UART on the Bluetooth chip and the UART on the IO expansion port (see GPIO14 and GPIO15 in the figure below) are shared, and the two cannot be used at the same time. Later, I will try the UART on the IO expansion port to see if this is the case.
2. Preliminary and simple trial of Bluetooth The trial here is to establish a connection between the Raspberry Pi and the mobile phone through Bluetooth and transmit some characters to each other. I have only made Bluetooth hardware before and don't know the protocol stack. In order to establish a connection between the Raspberry Pi and the mobile phone, I referred to a more mature solution and used the python pybluez library to operate the Bluetooth on the Raspberry Pi. pybluez also provides some simple test routines on github, which can be used directly. 3. Test steps 1. Install pybluez pybluez depends on the software protocol stack bluez under linux and the library file libbluetooth-dev.
  1. sudo apt-get install bluez
复制代码
  1. sudo apt-get install libbluetooth-dev
复制代码
  1. sudo pip install pybluez
复制代码
For php3, it is
  1. sudo pip3 install pybluez
复制代码
The following shows the versions of the various packages I currently have installed.
On my version of the system, bluez comes with the system, and libbluetooth-dev is not installed. When I installed pybluez directly, the following error was reported. Installing libbluetooth-dev first solved it.
2. Here, the Raspberry Pi and the mobile phone communicate using the RFCOMM protocol in Bluetooth. pybluez has provided a simple test routine rfcomm-server.py (see reference link 4). This routine establishes a simple BluetoothSocket Server and then starts listening for connections from other devices. At this time, you can use an Android phone to connect to the Raspberry Pi. If everything goes well, a connection will be established. Next, the Raspberry Pi can receive the data sent by the phone and print it out. Finally, if the connection is disconnected on the phone, the Raspberry Pi server will handle the exception, close the socket, and end the program. Before running the test routine, you need to modify the bluetooth.service file:
  1. sudo vi /lib/systemd/system/bluetooth.service
复制代码
The -E -C in the first line in the red box needs to be added. If not, an error bluetooth.btcommon.BluetoothError: (2, 'No such file or directory') will be reported when running the routine. The second line in the red box needs to be added manually. Without this sentence, it will always fail when connecting to the Raspberry Pi on the phone.
  1. ExecStartPost=/usr/bin/sdptool add SP
复制代码
In addition, add a line of code to line 33 of the original rfcomm-server.py file:
  1. client_sock.send(data.encode('utf-8'))
复制代码
Send the received characters back, so that the characters are transmitted to each other. To run the test routine in the terminal, you need to add sudo, otherwise an error will be reported if there is insufficient authority.
  1. sudo python rfcomm-server.py
复制代码
Now you can connect the Raspberry Pi on your mobile phone.
3. Operation on the mobile phone I operated on an Android phone. Because the main protocol used is RFCOMM, I first need to install a Bluetooth serial port software. I found a few on App Store and felt that the following one is more useful. I will paste an icon here.
After opening, click Connect on the interface and search. I have done this once before, so I just recorded it under "Paired".
After connecting, the MAC address of the mobile phone's Bluetooth will be printed out in the Raspberry Pi terminal:
The screenshots of the "chat" records on the mobile phone and the Raspberry Pi are as follows:
4. Summary Through the study and understanding of the past few days, a simple communication experiment was carried out between the Raspberry Pi and the Android phone through Bluetooth, and a preliminary understanding of the RFCOMM protocol was obtained. The communication here should still be based on the classic Bluetooth protocol. If there is enough time, I will try to make the Raspberry Pi communicate with a BLE device. 5. Reference link 1.3B+ schematic. https://www.raspberrypi.org/docu ...lus_1p0_reduced.pdf 2. Introduction to Raspberry Pi. http://shumeipai.nxez.com/2018/0 ... bplus-sale-now.html 3. Manual of CYW43455. http://www.cypress.com/file/358916/download 4. Download address of rfcomm-server.py.
链接已隐藏,如需查看请登录或者注册



7.png (20.22 KB, downloads: 0)

7.png
This post is from MCU

Latest reply

The previous steps were successful, but the device created by pybluez cannot be searched using the Bluetooth SPP tool. What should I do? You can use nrfconnect to see the device created by pybluez. The version of SPP tool is 7.4.5   Details Published on 2021-1-3 21:34
 

25

Posts

0

Resources
2
 

The previous steps were successful, but the device created by pybluez cannot be searched using the Bluetooth SPP tool. What should I do?

You can use nrfconnect to see the device created by pybluez.

The version of SPP tool is 7.4.5

This post is from MCU
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list