Raspberry Pi Bluetooth Terminal

This is another Breakoutbros.com Tutorial. See the full version here and Subscribe for more.

The first thing you do when you buy a Raspberry Pi is to access the terminal using SSH, with PuTTY for instance. This is a great when you have network connectivity, but what if you want to use your Pi without network access? You could connect using the micro USB port as a serial port, but then you can’t use that port for anything else. In this article I’ll walk you through a simple project to access a Raspberry Pi terminal over Bluetooth using your Android device or PC. You can create files, install packages, run scripts or do any of the things you normally do over SSH.

 

raspberry-pi-bluetooth-terminal

 

You can use the concepts in this tutorial to add Bluetooth connectivity for lots of different projects. Note that the Raspberry Pi 3 has built in Bluetooth functionality, but I only have version 2 so I’ll be using an external module

First here is a video showing the final result:

Hardware

I’ll be using an HC-06 module as a Bluetooth transceiver. You can find it for less than $10 shipped on Amazon. This little guy will act as a Bluetooth slave device that sends and receives serial data. There are only 4 pins to hook up, your standard ground and Vcc (3.3V) and the Tx/Rx Serial pins. The HC-06 Tx pin goes the Rpi Rx pin and the same for the Rx pin as shown below. The Key and State pins are used on the HC-05, but you can leave them unconnected.

As an alternative to the HC-06 you could also use an HC-05 Bluetooth module. This lets you send a variety of serial commands to, for instance, change the UART baud rate. These features are nice but they do increase the complexity and aren’t really needed for what we are doing here. Whichever module you use the major steps in this tutorial should be pretty much the same.

Software

The hardware was pretty simple,  now let’s tackle software. I’ll go over the main parts and then post the entire script at the end.

We are going to be controlling the serial port using a Python script. If you haven’t already, install the Python Serial library just run sudo apt-get install python-serial to install it.

Our Python script will have 2 main jobs:

  • Read any serial data coming in from the Bluetooth module and send it to a terminal process
  • Send any data coming out of the terminal to the Bluetooth module

Let’s make a small class to handle each of these tasks and we can tie them together in our main function. First the class to read serial data, we will call it SerialComm .

class SerialComm:
    def __init__(self):
        self.port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=1)
 
    def read_serial(self):
        res = self.port.read(50)
        if len(res):
            return res
        else:
            return None
 
    def send_serial(self, text):
        self.port.write(text)

The HC-06 only supports 9600 baud unfortunately, so we initialize the baud rate to a constant. The read() function will return either after receiving the given number of characters or when the read function times out.

Read More:  Raspberry Pi Bluetooth Terminal


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *