Rfcomm bluetooth raspberry pi

Headless Raspberry Pi configuration over Bluetooth

With the advent of the newer Raspberry Pi 3 (RPi) with built-in Bluetooth, there are now new options for getting connected to the console of the RPi, without the headache of having to dig up a monitor and keyboard (much less a serial cable with pinouts).

This is especially advantageous when running workshops and hackathons, where connectivity can become problematic. In fact, this specific hack came about from a recent Mozilla hackathon in Berlin, where we realized that we had 10-15 RPi for participants, but not a single monitor, keyboard, serial cable, or other means to connect to the RPis. So it raised the question – why couldn’t we use Bluetooth (BT) as a serial connection to get to the console?

Unlike other tutorials on the web, which assume you already have some console access established, I’ve provided steps which let you do this without any previous connection! To accomplish this, we’ll modify a base image of Raspian-Jesse to allow the RPi to boot into a mode where you can connect via Bluetooth and establish a “virtual” serial connection on startup.

Why is this awesome?

  • No cables, monitors, or keyboards required.
  • It lets you work directly on the RPi when resources like wireless access points or wired switches are not available.
  • It gives you an easy way to get a second screen for viewing logs when testing the primary networking interfaces.
  • Or how about this common problem — you set up wifi for DHCP, but can’t find the machine on your network? Rather than using a tool like Pi Finder after the fact, you can just use your Bluetooth terminal session to enable wifi and then right after, run an ifconfig to get the IP.
  • Last but not least, it is great for multi-RPi hackathons — you’ll come across a number of problems in such circumstances, like too many devices with the same hostname, or ssh ports blocked on event space wifi.

It’s brilliant, fast and simple. It is a little insecure, but we’ll also provide some instructions on how to solve this problem near the end of the article.

There are several useful internet resources that helped us in putting this together, but in particular, kudos to Dr. Rowland for this helpful script.

Preparing an RPi image for editing

Note: We are after the EXT4 partition, which contains the files we want to edit for startup.

Читайте также:  Ваг ком через блютуз

Mounting on Mac

This proved challenging, as OSX doesn’t support EXT4 natively. This limitation left two options; we felt more compelled to do the second, but the first is probably quicker.

1. Install native Ext4 drivers

This option involves installing EXT4 drivers into OSX (see https://osxfuse.github.io/) and read the .img file natively. These are not recommended for writing to EXT4 partitions, so use with caution.

2. Use a virtual machine

Here we will install Virtual Box, and then create a virtual machine of Linux. We then operate in the VM to natively mount the .img of Raspian-Jessie through a shared folder.

  1. First, download and install VirtualBox.
  2. Next, download and install Mint Linux in a virtual machine under Virtual Box:
  3. In the Linux virtual machine, put the image in a shared folder (visible to both the VM and your desktop) and then follow the instructions in the link in the “Mounting on Linux” section below to mount the .img file under Linux and edit the image.

Mounting on Linux

Mounting EXT4 on Linux is a simple, well-documented process: see How can I mount a Raspberry Pi Linux distro image?

Mounting on Windows

This is a fairly simple process, as EXT4 is supported natively by Windows. For a simple guide on how to do this, see Can I view/copy the contents of an img file from Windows?

Editing the image

Once you have the image mounted, and you are at the command prompt, you can start editing the configuration files necessary to get us going.

Let’s start by creating the main script that will set up and establish the default Bluetooth services and serial port you will connect to on startup.

You’ll create this file in the /home/pi directory, like so:

$ sudo nano /home/pi/btserial.sh

Add the following lines to the script:

#!/bin/bash -e #Edit the display name of the RaspberryPi so you can distinguish #your unit from others in the Bluetooth console #(very useful in a class setting) echo PRETTY_HOSTNAME=raspberrypi > /etc/machine-info # Edit /lib/systemd/system/bluetooth.service to enable BT services sudo sed -i: 's|^Exec.*toothd$| \ ExecStart=/usr/lib/bluetooth/bluetoothd -C \ ExecStartPost=/usr/bin/sdptool add SP \ ExecStartPost=/bin/hciconfig hci0 piscan \ |g' /lib/systemd/system/bluetooth.service # create /etc/systemd/system/rfcomm.service to enable # the Bluetooth serial port from systemctl sudo cat /dev/null [Unit] Description=RFCOMM service After=bluetooth.service Requires=bluetooth.service [Service] ExecStart=/usr/bin/rfcomm watch hci0 1 getty rfcomm0 115200 vt100 -a pi [Install] WantedBy=multi-user.target EOF # enable the new rfcomm service sudo systemctl enable rfcomm # start the rfcomm service sudo systemctl restart rfcomm

Save the file, and then make it executable by updating its permissions like so:

$ chmod 755 /home/pi/btserial.sh

Now you have the basics of the script required to turn on the Bluetooth service and configure it. But to do this 100% headless, you’ll need to run this new script on startup. Let’s edit /etc/rc.local to launch this script automatically.

Add the following lines after the initial comments:

#Launch bluetooth service startup script /home/pi/btserial.sh sudo /home/pi/btserial.sh &

Save the rc.local script, unmount the image, and write it to an SD Card using your favorite tool (mine is ApplePiBaker).

Читайте также:  Сони мп3 плеер блютуз

Now you are ready to go. Plug in power to the Rpi and give it 30 seconds or so to startup. Then unplug it, and plug it in again and let it boot up a second time. Restarting the Bluetooth service doesn’t work correctly, so we need to reboot.

Connecting to your RPi via Bluetooth

On your desktop/laptop, open up your Bluetooth preferences and ensure Bluetooth is enabled.

Select “raspberrypi” (or whatever you have used for PRETTY_HOSTNAME in the btserial.sh script) when it appears and pair with it. It should pair automatically (remember what we said earlier about security issues?)

Open a terminal window on your local machine, and start a screen session to connect via the new Bluetooth serial port created from the RPi connection. First let’s check the name of the serial connection:

This should produce a list of available serial ports, one of which should now be named after your pi. Then we can connect.

$ screen /dev/cu.raspberrypi-SerialPort 115200

Give it a second, and you should be at the prompt of the RPi console! Congrats!

Further tips

Don’t stop reading yet — this section contains some useful tips about using the RPi terminal, including making it more secure.

Interactive terminal [optional]

After your initial amazement has subsided, you’ll realize your terminal window size isn’t reflected in the output from the serial session. You can adjust this in one of two ways.

Manual resizing

Simply note the size of your current terminal window , and enter the command:

replacing XX and YY with the size of your local terminal window

Automatic (dynamic) resizing

If you want your terminal session to be responsive when you change your local terminal window size, you’ll need to do the following:

Start up sshd, which is off by default. From the command prompt, run:

  1. Select “Advanced Config”
  2. Select “SSH”
  3. Select “Activate”
  4. Exit raspi config

With SSH active, you can now enter the following commands:

Your terminal window will now resize dynamically.

Security [optional, but not really…]

If you are on a deserted island, far away from humanity, you can stop now. If not, you should be concerned that this connection is wide open for others to connect to and use. To make this a little more secure, I would recommend turning off Bluetooth discoverability and pairing once you have connected to your RPi.

In the console on the RPi, run the following commands:

$sudo bluetoothctl [bluetooth]#

At this point you should be good to go with at least some small security measures in place. If the machine restarts, you’ll need to follow these security steps again when you reconnect.

Summary

So there you have it — you should now have set up your own working RPi console over Bluetooth, configured entirely headlessly. You should now be able to go forth and work on your own RPi projects, alongside others, with the minimum of connectivity issues.

If you run into problems getting this to work, or have any tips of your own to share, please do so in the comments.

Читайте также:  Haier телевизор подключить блютуз наушники

Special thanks to Florian Merz for his technical review of this post.

About Patrick Hundal

Business Development, Strategic Partnerships at Mozilla.

Thanks! Please check your inbox to confirm your subscription.

If you haven’t previously confirmed a subscription to a Mozilla-related newsletter you may have to do so. Please check your inbox or your spam filter for an email from us.

One comment

Cool stuff! Love this blog. Keep writing interesting content. If only Bluetooth wasn’t such a black box.

Comments are closed for this article.

Except where otherwise noted, content on this site is licensed under the Creative Commons Attribution Share-Alike License v3.0 or any later version.

Источник

Raspberry Pi Resources

Our resources for other geeks, designers and engineers.

Serial over Bluetooth

Once you have your RPi paired with another device you have a Bluetooth link, but you still need to set up another protocol over the Bluetooth called «RFCOMM» to provide an emulated serial connection between the two devices.

Setting The RPi to create a serial connection to a Bluetooth device at startup

 sudo nano /etc/bluetooth/rfcomm.conf 

Add the connection details:

Follow the RPI steps to setup the rfcom link at its end.

Open the Windows Control Panel. In search box, type «Bluetooth», and then click «Change Bluetooth settings».

In the Bluetooth Settings dialog box, click the COM Ports tab, select a port, and then click OK. If no COM port is listed, then setup Windows to accept a connection by pressing «Add», and then select «Incoming». OK and exit. Windows should then see the RPi serial connection after a few seconds and create a serial port automatically for it (you see a taskbar message its happened and the new COM port appears in device manager).

Adding The RPi From Windows

In Devices and printers use ‘Add Device’ and if the RPi is in pairing mode it should find the RPi.

Once the device is added and windows has finished adding drivers (can take quite a a while – wait for the taskbar device added message), right click the RPi in device and select properties. In the ‘Services’ tab there should be a servicee for serial, e.g. ‘serial port SSP’. Ensure it is enabled. If there isn’t then there is likely a problem.

Send serial from the RPi command line

We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.

Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Источник

Оцените статью
Adblock
detector