Enabling Python Autocomplete in VIM on Raspberry Pi


This post shows steps to enable Python autocomplete in vim editor [1] on Raspberry Pi.

In this post, jedi-vim [2] and Vundle.vim [3] will be used for autocomplete plugin and plugin manager respectively. Also, even Raspbian Stretch has vim preinstalled, it’s a minimal version which is called “vim.tiny” and to use the plugin, we need to install another version of vim.

 

Prerequisites (parentheses indicate my environment)

  • Raspberry Pi board (Raspberry Pi3) running Raspbian Stretch (2019-04-08)

 

Steps
1. Install VIM
As mentioned above, another version of vim than the preinstalled one needs to be installed.

1-1. Update the package list.

1-2. Install vim-nox.

 

2. Setup VIM
2-1. Clone VundleVim plugin.

2-2. Create a vim configuration file with vim.

2-3. Activate the paste mode by the command below.

2-4. Type “i” to go insert mode. Make sure that the paste mode is on as below.

2-5. Copy and paste the lines from line 1 to line 10 below.
Note: The lines after line 13 is my preferred settings and just for my reference. 

2-6. Exit the insert mode by pressing “Ctrl” + “[“.

2-7. Save the file by typing “:w” and enter.

2-8. Execute the file by the command below.

2-9. Install the plugins by entering the command below.

It will show “Processing” in the status bar. It takes a while.

2-10. When the status bar shows “Done!”, close the file by entering “:q”.

 

3. Verify Autocomplete

 

References
[1] Vim – the ubiquitous text editor
[2] jedi-vim – awesome Python autocompletion with VIM – GitHub
[3] Vundle.vim – GitHub
[4] jedi-vim – Vim Awesome
[5] VIM and Python – A Match Made in Heaven

 

 

Setting Up Bluetooth Serial Port Profile on Raspberry Pi using D-Bus API

This post shows steps to set up Bluetooth Serial Port Profile[1] (or SPP) on Raspberry Pi.

The goal is to establish an SPP connection between Raspberry Pi 3 and Android phone, then send and receive texts using serial terminal applications on both ends.

SPP is not available by default. There are two options to enable it in BlueZ (which is the default Bluetooth stack in Linux). This post shows steps for the 2nd option (D-Bus API).

  1. sdptool (please see this post for this option)
  2. D-bus API

 

Prerequisites (parentheses indicate my environment)

 

Steps
1. Installation
1-1. Install a serial terminal application on Raspberry Pi. In this post, I’ll use minicom[2].

 

2. Enable SPP on Raspberry Pi
2-1. Create a python script (e.g. “spp.py”)[3][4].

2-2. Run the script.

2-3. Suspend the script by pressing Ctrl+Z.

 

3. Pairing
To establish a connection, Raspberry Pi and the phone need to be paired.

3-1. Launch bluetoothctl.

3-2. Enter below in order to be discovered from the phone.

3-3. On the phone, scan for Raspberry Pi and pair. You should be able to see something like below.

3-4. Press Ctrl+D to quit.

 

4. Establishing Connection from Phone

4-1. Listen for incoming connection on Raspberry Pi.

4-2. Install and launch “Serial Bluetooth Terminal” app[5] on the phone.

4-3. In the app, go to “Device” menu and select Raspberry Pi. If everything goes well and the connection is established, you should be able to see like this:

 

5. Connecting Serial Terminal on Raspberry Pi
5-1. Open another terminal and launch the serial terminal.

 

6. Test
6-1. Input some text on the phone.

You should be able to see the text on Raspberry Pi’s serial terminal.

6-2. Input some text back to the phone on Raspberry Pi.

You should be able to see the text on the phone.

 

References
[1] Serial_Port_Profile_(SPP) – Wikipedia
[2] minicom(1) – Linux man page
[3] Re: Bluetooth Python script. – Raspberry Pi Forum
[4] Tutorial: Creating a Bluetooth service
[5] Serial Bluetooth Terminal – Google Play

 

 

Setting Up Bluetooth Serial Port Profile on Raspberry Pi using sdptool

This post shows steps to set up Bluetooth Serial Port Profile (or SPP)[1] on Raspberry Pi.

The goal is to establish SPP connection between Raspberry Pi 3 and Android phone, then send/receive text using serial terminal applications on both ends.

SPP is not available by default. There are two options to enable it in BlueZ (which is the default Bluetooth stack in Linux). This post shows steps for the 1st option (sdptool).

  1. sdptool
  2. D-bus API (please see this post for this option)

 

Prerequisites (parentheses indicate my environment)

 

Steps
1. Installation
1-1. Install a serial terminal application on Raspberry Pi. In this post, I’ll use minicom[2].

 

2. Enable SPP on Raspberry Pi
In order to use SPP, Bluetooth service needs to be restarted with ‘compatibility’ flag[3].

2-1. Open Bluetooth service configuration file.

2-2. Look for a line starts with “ExecStart” and add compatibility flag ‘-C’ at the end of the line.

2-3. Add a line below immediately after “ExecStart” line, then save and close the file.

2-4. Reload the configuration file.

2-5. Restart the service.

 

3. Pairing
To establish a connection, Raspberry Pi and the phone need to be paired.

3-1. Launch bluetoothctl.

3-2. Enter below in order to be discovered from the phone.

3-3. On the phone, scan for Raspberry Pi and pair. You should be able to see something like below.

3-4. Press Ctrl+D to quit.

 

4. Establishing Connection from Phone

4-1. Listen for incoming connection on Raspberry Pi.

4-2. Install and launch “Serial Bluetooth Terminal” app[4] on the phone.

4-3. In the app, go to “Device” menu and select Raspberry Pi. If everything goes well and the connection is established, you should be able to see like this:

 

5. Connecting Serial Terminal on Raspberry Pi
5-1. Open another terminal and launch the serial terminal.

 

6. Test
6-1. Input some text on the phone.

You should be able to see the text on Raspberry Pi’s serial terminal.

6-2. Input some text back to the phone on Raspberry Pi.

You should be able to see the text on the phone.

 

References
[1] Serial_Port_Profile_(SPP) – Wikipedia
[2] minicom(1) – Linux man page
[3] sdptool is broken in Bluez 5 – Arch Linux
[4] Serial Bluetooth Terminal – Google Play
[5] Bluetooth issues – Raspberry Pi Forum

 

 

Creating BLE GATT Server (UART Service) on Raspberry Pi


In this post, I will create BLE GATT server on Raspberry Pi 3 using BlueZ dbus interface with Python. I will reuse BlueZ example code as much as possible. As an example of GATT service, I’ll create UART service (a.k.a. Nordic UART Service/NUS [1] ), so that I can test it with Nordic’s smartphone app [2].

 

Assumptions

 

Steps
1. BlueZ Update
Raspbian Stretch comes with BlueZ 5.43. BlueZ’s Advertising Manager has been officially supported from 5.48. So, let’s update BlueZ by following this post.

 

2. UART Service Implementation
I’ll use “example-advertisement” and “example-gatt-server” from the downloaded source code in Step 1. Some of the classes and functions will be reused.

2-1. First, let’s create a working directory.

 

2-2. Copy the example code with new names so that they can be imported as modules.

 

2-3. Create a new file (e.g. “uart_peripheral.py”). I’ll reuse ‘Advertisement’ class, ‘Service’ class, ‘Characteristic’ class, and callback functions from the example code. Also, combine the both main() functions from the example code into one and modify the sub-classes of Advertisement, Application, Service, and Characteristic to realize the UART service.

 

 

3. Test
3-1. Run the UART service on Raspberry Pi.

If all goes well, the output should be like this. Now, the service is running on Raspberry Pi and it’s broadcasting BLE advertising message.

3-2. Lauch nRF Toolbox app on the smartphone and tap on “UART”.

3-3. Tap on “CONNECT” button. Then the app will start scanning for nearby BLE devices.

3-4. Select your Raspberry Pi from the detected device list. It triggers the connection between the Raspberry Pi and the app.


 

 

 

 

 

* In case of iPhone, Raspberry Pi’s host name may be displayed instead of LOCAL_NAME in the code.

3-5. Tap on “Show Log”, enter some strings and tap on “Send”.

 

 

 

 

 

 

 

 

 

 

 

3-7. Check the console on Raspberry Pi. You should be able to see what you sent from the app like below.

3-8. Let’s send back something from Raspberry Pi by entering below on Raspberry Pi’s console.

3-9. Check the app screen.

 

 

 

 

 

 

 

 

 

 

 

 

References
[1] UART/Serial Port Emulation over BLE – Nordic Semiconductor 
[2] nRF Toolbox App – Nordic Semiconductor
[3] Turning a Raspberry Pi 3 into a Bluetooth Low Energy peripheral

 

 

Updating BlueZ on Raspberry Pi (from 5.43 to 5.50)

This post shows how to update BlueZ on Raspberry Pi from 5.43 (the default version comes with Raspbian Stretch) to 5.50 (released notes [1]). In this post, I assume that you already have a Raspberry Pi 3, Pi 3+, or Raspberry Pi Zero W running Raspbian Stretch.

 

Steps [2]
1. Check Current BlueZ Version
1-1. Before starting, let’s check the current BlueZ version.

In case of Raspbian Stretch, the BlueZ version should be 5.43.

 

2. Install Dependencies
2-1. Update the package list.

2-1. Install the dependencies.

 

3. Install BlueZ
3-1. Download BlueZ source code.

3-2. Uncompress the downloaded file.

3-3. Configure.

3-4. Compile the source code.

3-5. Install.

3-6. Reboot Raspberry Pi 3.

 

4. Verify Update[3] [4]
4-1. Verify the BlueZ version by issuing the command below.

The result should be like this:

 

References
[1] BlueZ Release Notes
[2] Installing Bluez 5.44 onto Raspbian? – Stack Exchange
[3] Bluetooth LE upgrade not working – Raspberry Pi Forum
[4] Why does bluetoothd show version 5.37 after installing Bluez 5.45? – Stack Exchange