- Bluetooth module uses HC-05, 9600, inserted into the PC, assistant send and receive messages using the serial port
- Raspberry Pi uses its own Bluetooth pairing with the HC05.
HC-05 configuration is omitted here, reference is here
A Bluetooth raspberry pie paired with HC-05
Open the VNC desktop raspberry pie, click the Bluetooth icon in the upper right corner, select Add Device
Furthermore, the search into the Bluetooth interface, wait a moment, you can find the corresponding HC-05 module, click Pair to pair
Follow the prompts, enter the Bluetooth pairing password
If after an error occurs as shown below, can be simply ignored.
Raspberry Pi open the command line, type hcitool scan
, scan for Bluetooth (included with the MAC address)
Subsequently input sudo rfcomm connect 1 98:D3:32:71:30:7A
(which 1
can be customized for other digital [] If this is the case, 98:D3:32:71:30:7A
the HC-05 is the MAC address), to connect success.
This connection, the current terminal can not be closed.
And when pulled Bluetooth, need to re-enter the connection instruction. Is there a way to bind its own it?
Of course there is, at the input terminal
sudo rfcomm bind 1 98:D3:32:71:30:7A
, equivalent to/dev
registering a name for the directoryrfcomm1
of the device. When unplugged Bluetooth, the corresponding device numberrfcomm1
remains.
Next plug in Bluetooth can be used directly in the programrfcomm1
the device without the need to register again.
Of course, there unbundling corresponding to the command, is not repeated here, reference may be interested here
Then open a terminal and type sudo ls /dev -l
, you will find much rfcomm1
that we just registered HC-05 equipment.
Second, install python-serial library
Raspberry Pi can use Bluetooth to transfer data of pyserial python module
sudo apt-get install python-serial
Third, write python program
1, PC and send data back to the PC significantly
Finally, the most exciting part - the test.
- USB-TTL connector HC-05
- Open the PC serial port aide, set the baud rate to 9600.
Raspberry come, write 4.py
code as shown below.
# -*- coding: utf-8 -*
import serial
import time
def main():
while True:
# 获得接收缓冲区字符
count = ser.inWaiting()
if count != 0:
# 读取内容并显示
recv = ser.read(count)
print recv
# 清空接收缓冲区
ser.flushInput()
# 必要的软件延时
time.sleep(0.1)
if __name__ == '__main__':
try:
# 打开串口
ser = serial.Serial('/dev/rfcomm1', 9600)
if ser.isOpen == False:
ser.open() # 打开串口
ser.write(b"Raspberry pi is ready")
main()
except KeyboardInterrupt:
if ser != None:
ser.close()
-
Raspberry Pi running
4.py
the file, at the same time open the PC serial port aide, and observe the receiver box change
-
Above the PC serial port aide successful print
Raspberry pi is ready
, distribute instructions to the PC Raspberry end did not have a problem. -
Then test Raspberry Pi PC receiving end of the message
- Raspberry Pi reception did not have a problem! ! !
Note: When the ctrl + C off the end of the program, in which case HC-05 Bluetooth disconnected. No need to panic, no need to re-pair, because we bind to the Bluetooth
rfcomm1
device, so a direct run the program again, the change will automatically connect. Even unplugged Bluetooth, plug it in again, still automatically connects matching!
So far, the Raspberry Pi 4B and HC-05 Bluetooth serial communication test, it successfully!