[Raspberry Pi 3B+ Review] Simple Example of Communicating with CC2640R2F[Copy link]
This post was last edited by icefire2012 on 2018-9-29 23:56 In the previous post, the Raspberry Pi and the mobile phone communicated via Bluetooth, using classic Bluetooth. In this post, we will try to use another mode of Bluetooth, Bluetooth Low Energy (BLE). The protagonist is of course the Raspberry Pi 3B+, and the one used for communication testing is a popular BLE SoC, TI's CC2640R2F (hereinafter referred to as R2F). 1. A brief introduction to the program on R2F R2F is an upgraded version of CC2640, with more FLASH space, support for Bluetooth 5 and backward compatibility with Bluetooth 4.x. During the test, the simple_peripheral demo program in the official SDK was run on R2F. The path in the SDK is:
The README.html
README.html(2.69 MB, downloads: 1)
2018-9-29 23:53 上传
点击文件名下载附件
in the simple_peripheral folder is the introduction and demonstration process of this demo program. In short, the simple_peripheral routine provides a custom service, which has five characteristics. Each characteristic can be read, written, notified, or needs to be authenticated before it can be read. The README document shows the process of the BLE scanner App on the mobile phone accessing these five characteristics. This demonstration basically covers the common operations in BLE communication.
The next operation will be to communicate with R2F through Python programming on the Raspberry Pi, access these five characteristics one by one, and repeat the demonstration operations in the README document. The following is a detailed introduction. 2. PYGATT In the previous evaluation, the classic Bluetooth communication between the Raspberry Pi and the mobile phone used the RFCOMM protocol in Bluetooth. The communication between Raspberry Pi and R2F now uses the GATT protocol in BLE. Python has a module called pygatt, which provides an API interface for accessing GATT descriptors when programming in Python. Here is how to install pygatt:
sudo pip install pygatt
复制代码
sudo pip install "pygatt[GATTTOOL]"
复制代码
sudo pip install pexpect
复制代码
Note: pexpect also needs to be installed manually. Otherwise, errors related to pexpect will be reported when running the code below. 3. Python programming code to access R2F First paste the communication code here.
4. Code Description Before communicating with R2F, we need to obtain some parameters of the communication object R2F, which can be obtained by calling pygatt API in python. Here are a few commands to get it in the terminal. First, get the address of R2F, which needs to be scanned first: sudo hcitool lescan
The SimpleBLEPeripheral in the fifth line is R2F, and its address is 54:6C:0E:6D:D7:96 The next step is to get the service and characteristics information of R2F.
What you need to pay attention to here is the handle value in the blue box and the uuid in the green box. The top framed line is used to get the device name, and the five frames at the bottom are the five characteristics mentioned in the first part. The following programming access requires the corresponding char value handle or uuid (the uuid in the screenshot of the first part is 16 bits, and here it should be completed into the standard format uuid, fff1-fff5 is still easy to identify). In addition to the char value handle and uuid, the previous handle seems to be a sequence number, which is not used in the following programming. char properties indicate read, write and notification properties. Read = 0x02 Write = 0x08 Notification = 0x10. Characteristics1 (uuid = 0xfff1) is readable and writable, so its char properties = 0x02 | 0x08 = 0x0a. The following is a brief introduction to the program content. Line 12 is to establish a connection with R2F by specifying the address. Line 13 is to obtain the device name of R2F. The parameter of char_read is uuid, and the return value is the value corresponding to characteristics. Line 17 is to write characteristics1 through handle. The first parameter of char_write_handle is the char value handle in the above picture. Hexadecimal 0x1e is decimal 30, and the second parameter is the value to be written. Line 19 is to read characteristics1 through uuid. I tried to use char_read_handle to read through char value handle, but it always reports an error. I haven't figured out what's going on yet. Line 29 is to enable the notification of characteristics and register the callback function. The first parameter of the subscribe function is the uuid of characteristics, the second parameter is the callback function handle of the notification, and the third parameter is indicate when it is True and notification when it is False. The two parameters of the callback function are the corresponding char value handle and the returned value, which are sent back by the system after receiving the notification. Line 33 is also a read operation. As described in the README of the simple_peripheral routine, there must be an authentication process to read characteristics5. When reading with a BLE scanner, a pairing dialog box will pop up and prompt you to enter the pairing password (this password is given by the R2F end, and the password will be printed out through the serial port in real time). When reading with Python programming here, it will also try to pair with R2F, but it usually fails. I guess this may be because the pairing password is not entered (there will be no prompt to enter the pairing password). In order to bypass this process, I tried to pair the Raspberry Pi with R2F first. I clicked the Bluetooth icon at the top of the graphical interface, searched for SimpleBLEPeripheral, clicked pair, and was prompted to enter the pairing password, and then paired correctly. However, when running the program to read characteristics5, it still prompts that the pairing failed. Later, I tried to establish pairing by calling the bluetoothctl related commands in the terminal. There were some twists and turns in the process, but the pairing can be achieved by following the following operations. And when I run the program to read characteristics5 again, it can be read successfully. The screenshot of running bluetoothctl in the terminal to establish pairing is as follows (the left side is the printed information of R2F, the right side is the operation in the Raspberry Pi terminal, and the red box is the command that needs to be entered). If you pair the device directly, an error will be reported. You need to remove the device first, then trust the device, and finally pair it. This will have a higher success rate. After the pair is successful, the service and other information on R2F will be printed out. Because the content is large, only two lines are captured here (the last two lines in the picture).
Finally, attach the screenshots of the printed information of R2F and the python program when the python program is running. The two lines of notify are the two notification messages received within the ten seconds of sleep(10). The rest are basically the same as the operations in the routine introduced in the first part.
5. Summary In this article, I mainly completed some communication operations with CC2640R2F through programming of Python's pygatt module. In addition, I also used two tools, gatttool and bluetoothctl. After going through the whole process, I still learned a lot of things related to BLE on Linux, and had a preliminary understanding of the communication process of BLE, as well as concepts such as service, characteristics, and notification. Of course, I only have a rough understanding of the content of gatt. As for the ble-related modules under Python, I found, in addition to pygatt, pygattlib and bluepy. Using the other two modules, I also realized the read and write operations with R2F, but I encountered problems in handling notifications. First of all, I don’t know how to enable notifications. The notification of characteristics4 is closed by default in the R2F program, but I didn’t find the API for opening the operation in these two modules. In addition, the implementation of the callback function is not as easy to understand as the pygatt API. If anyone has experience in this regard, you can also communicate. Due to time constraints, the Bluetooth part came to an end, and the communication between 8266 and Raspberry Pi was started later.These are two notification messages received within ten seconds of sleep(10). The rest are basically the same as the operations in the example program introduced in the first part.
5. Summary In this article, some communication operations with CC2640R2F were mainly completed through the pygatt module of python. In addition, the two tools gatttool and bluetoothctl were also used in the middle. After going through the whole process, I still learned a lot about BLE on Linux, and had a preliminary understanding of the communication process of BLE, as well as concepts such as service, characteristics, and notification. Of course, I only know the basics of gatt. As for the ble-related modules under python, I found, in addition to pygatt, pygattlib and bluepy. I also realized the read and write operations with R2F using the other two modules, but encountered problems in handling notifications. First of all, I didn't know how to enable notifications. The notification of characteristics4 is disabled by default in the R2F program, but I didn't find the API to enable it in these two modules. In addition, the implementation of the callback function is not as easy to understand as the pygatt API. If anyone has experience in this regard, you can also communicate with me. Due to time constraints, the Bluetooth part came to an end, and the communication between 8266 and Raspberry Pi was started later.These are two notification messages received within ten seconds of sleep(10). The rest are basically the same as the operations in the example program introduced in the first part.
5. Summary In this article, some communication operations with CC2640R2F were mainly completed through the pygatt module of python. In addition, the two tools gatttool and bluetoothctl were also used in the middle. After going through the whole process, I still learned a lot about BLE on Linux, and had a preliminary understanding of the communication process of BLE, as well as concepts such as service, characteristics, and notification. Of course, I only know the basics of gatt. As for the ble-related modules under python, I found, in addition to pygatt, pygattlib and bluepy. I also realized the read and write operations with R2F using the other two modules, but encountered problems in handling notifications. First of all, I didn't know how to enable notifications. The notification of characteristics4 is disabled by default in the R2F program, but I didn't find the API to enable it in these two modules. In addition, the implementation of the callback function is not as easy to understand as the pygatt API. If anyone has experience in this regard, you can also communicate with me. Due to time constraints, the Bluetooth part came to an end, and the communication between 8266 and Raspberry Pi was started later.These are two notification messages received within ten seconds of sleep(10). The rest are basically the same as the operations in the example program introduced in the first part.
5. Summary In this article, some communication operations with CC2640R2F were mainly completed through the pygatt module of python. In addition, the two tools gatttool and bluetoothctl were also used in the middle. After going through the whole process, I still learned a lot about BLE on Linux, and had a preliminary understanding of the communication process of BLE, as well as concepts such as service, characteristics, and notification. Of course, I only know the basics of gatt. As for the ble-related modules under python, I found, in addition to pygatt, pygattlib and bluepy. I also realized the read and write operations with R2F using the other two modules, but encountered problems in handling notifications. First of all, I didn't know how to enable notifications. The notification of characteristics4 is disabled by default in the R2F program, but I didn't find the API to enable it in these two modules. In addition, the implementation of the callback function is not as easy to understand as the pygatt API. If anyone has experience in this regard, you can also communicate with me. Due to time constraints, the Bluetooth part came to an end, and the communication between 8266 and Raspberry Pi was started later.These are two notification messages received within ten seconds of sleep(10). The rest are basically the same as the operations in the example program introduced in the first part.
5. Summary In this article, some communication operations with CC2640R2F were mainly completed through the pygatt module of python. In addition, the two tools gatttool and bluetoothctl were also used in the middle. After going through the whole process, I still learned a lot about BLE on Linux, and had a preliminary understanding of the communication process of BLE, as well as concepts such as service, characteristics, and notification. Of course, I only know the basics of gatt. As for the ble-related modules under python, I found, in addition to pygatt, pygattlib and bluepy. I also realized the read and write operations with R2F using the other two modules, but encountered problems in handling notifications. First of all, I didn't know how to enable notifications. The notification of characteristics4 is disabled by default in the R2F program, but I didn't find the API to enable it in these two modules. In addition, the implementation of the callback function is not as easy to understand as the pygatt API. If anyone has experience in this regard, you can also communicate with me. Due to time constraints, the Bluetooth part came to an end, and the communication between 8266 and Raspberry Pi was started later.