Josh From BreakoutBros.com
Published © CC BY-NC

Raspberry Pi Bluetooth Terminal

This is a tutorial for getting Terminal access for your smartphone via Bluetooth.

IntermediateProtip1 hour18,498
Raspberry Pi Bluetooth Terminal

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
HC-06 Bluetooth Module
×1

Software apps and online services

BlueTerm

Story

Read more

Schematics

0Bwb6txz85JFCSUJnvkH.png

Code

Bluetooth Terminal for R pi code

Python
This is the Code for connecting your R pi to your smartphone over Bluetooth.
import subprocess
import select
import serial
 
 
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.splitlines()
        else:
            return []
 
    def send_serial(self, text):
        self.port.write(text)
 
 
class ShellWrapper:
    def __init__(self):
        self.ps = subprocess.Popen(['bash'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
 
    def execute_command(self, command):
        self.ps.stdin.write(command + "\n")
 
    def get_output(self):
        timeout = False
        time_limit = .5
        lines = []
        while not timeout:
            poll_result = select.select([self.ps.stdout, self.ps.stderr], [], [], time_limit)[0]
            if len(poll_result):
                for p in poll_result:
                    lines.append(p.readline())
            else:
                timeout = True
        return lines
 
 
def main():
    shell = ShellWrapper()
    ble_comm = SerialComm()
    while True:
        out = ble_comm.read_serial()
        for ble_line in out:
            print(out)
            shell.execute_command(ble_line)
            shell_out = shell.get_output()
            for l in shell_out:
                print(l)
                ble_comm.send_serial(l)
 
 
if __name__ == "__main__":
    main()

Credits

Josh From BreakoutBros.com

Josh From BreakoutBros.com

14 projects • 89 followers
At BreakoutBros we try to be a link between electronics designers and hobbyists. We make electronics tinkering easy for everyone but teach professional methods.

Comments