[Learning] raspberry pie 4B thirteen, raspberry pie 4B and HC-05 Bluetooth serial communication test

  • 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

Here Insert Picture Description

Furthermore, the search into the Bluetooth interface, wait a moment, you can find the corresponding HC-05 module, click Pair to pair

Here Insert Picture Description
Follow the prompts, enter the Bluetooth pairing password
Here Insert Picture Description

If after an error occurs as shown below, can be simply ignored.

Here Insert Picture Description

Raspberry Pi open the command line, type hcitool scan, scan for Bluetooth (included with the MAC address)

Here Insert Picture Description

Subsequently input sudo rfcomm connect 1 98:D3:32:71:30:7A(which 1can be customized for other digital [] If this is the case, 98:D3:32:71:30:7Athe HC-05 is the MAC address), to connect success.

Here Insert Picture Description

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 /devregistering a name for the directory rfcomm1of the device. When unplugged Bluetooth, the corresponding device number rfcomm1remains.
Next plug in Bluetooth can be used directly in the program rfcomm1the 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 rfcomm1that we just registered HC-05 equipment.
Here Insert Picture Description

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.pycode 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.pythe file, at the same time open the PC serial port aide, and observe the receiver box change
    Here Insert Picture Description
    Here Insert Picture Description

  • 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

Here Insert Picture Description
Here Insert Picture Description

  • 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 rfcomm1device, 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!


reference

Published 693 original articles · won praise 1115 · Views 780,000 +

Guess you like

Origin blog.csdn.net/ReCclay/article/details/104671734