Friday, 7 July 2017

Python Bluetooth RFCOMM Client Server

As part of the Blue Dot project I needed to create a simple Bluetooth client / server library so that the communication could be managed. This library, btcomm, is part of bluedot but its not exclusive and can be used for Bluetooth communication in Python.

It uses a 2 way RFCOMM communication - you can send messages to and from 2 devices, 1 being the server which waits for connections, 1 being the client which makes a connection.

Install the library
sudo apt-get install python3-dbus
sudo pip3 install bluedot


The 2 devices you which want to communicate between will need to be paired, the Blue Dot documentation describes how to pair 2 raspberry pi's which might be useful.

Simple Client / Server Example
Lets create a simple example, a server which waits for connections and when it receives data it echo's it back to the client.

Create a new Python program and save it as

from bluedot.btcomm import BluetoothServer
from signal import pause

def data_received(data):

s = BluetoothServer(data_received)

Create a 2nd program and save it as
from bluedot.btcomm import BluetoothClient
from signal import pause

def data_received(data):

c = BluetoothClient("nameofyourserver", data_received)


Run the server and then run the client, the client should connect and "Hello World" will be sent to the server and displayed on the screen, the server will then send the same "Hello World" message back to the client, which will print it to the screen.


There is also a useful API for accessing the Bluetooth adapter allowing you to get its current status, power it on/off, make it discoverable or find the devices its paired with.
from bluedot.btcomm import BluetoothAdapter

a = BluetoothAdapter()

print("Powered = {}".format(a.powered))


There is comprehensive documentation for the btcomm library, which describes the API and how to use it.

Tuesday, 4 July 2017

Mac OS - Check Java version before running script

I needed to check what version of Java was installed on a Mac before running my program, so with the help of stackoverflow and a few other resources I pulled together the following bash script which checks to see if the version of Java is greater than 1.8 before continuing.
# Work out the JAVA version we are working with:

# Based on:
for token in $(java -version 2>&1 | grep -i version)
    if [[ $token =~ \"([[:digit:]])\.([[:digit:]])\.(.*)\" ]]

#check version is greater than 1.7 (i.e. at least 1.8)
if [ "$JAVA_VER_MAJOR" -gt "1" ]; then
    echo start your program
elif [ "$JAVA_VER_MINOR" -gt "7" ]; then
    echo start your program
    echo ERROR - Java needs to be updated.
    echo Currently installed version is $JAVA_VER_MAJOR.$JAVA_VER_MINOR - 1.8 is required

Friday, 5 May 2017

Raspberry Pi Touchscreen Portrait

I recently wanted to turn my Raspberry Pi Official Touchscreen portrait (i.e. sideways!), which turns out is a bit of pain.

Turning the display is relatively easy but making the touch work is more difficult - there was a set of instructions on the Raspberry Pi forum, but a recent update to Jessie meant they no longer worked, so I pulled this set of instructions together:

Install xinput:
sudo apt-get install xinput
Rotate the display by editing config.txt:
sudo nano /boot/config.txt
 .. add this to the buttom of the file:
Use Ctrl X, Yes to Save
Create a script to rotate the touchscreen:
nano /home/pi/
 .. add the following command
xinput --set-prop 'FT5406 memory based driver' 'Coordinate Transformation Matrix'  0 1 0 -1 0 1 0 0 1
Make the script executable:
chmod +x
Make the script run when the GUI starts by editing autostart:
sudo nano ~/.config/lxsession/LXDE-pi/autostart
 .. add this to the bottom to run your script
sudo reboot

Monday, 24 April 2017

Blue Dot - a bluetooth remote for Raspberry Pi

Blue Dot is a really simple way to add Bluetooth remote control to your Raspberry Pi projects.

I created Blue Dot after being asked many times at Picademy “how can I get rid of all these wires?”. 

Blue dot is an android app (client) and really easy to use Python library which allows you to wirelessly control your Python projects, whether that is a light switch, remote camera, robot or anything else you can think of!

See the getting started guide for more info on 'getting started', or follow the tutorial below.

Installation & Use
These instructions assume your Raspberry Pi is running the latest version of Raspbian with Pixel.

You will need a Raspberry Pi with built-in Bluetooth (such as the Pi 3 or Pi Zero W) or a Raspberry Pi and a USB Bluetooth dongle.

Get the app
Download and install the Blue Dot app from the google play store.

If you are wondering why there is no iOS app? Its because iOS doesn't support Bluetooth serial comms; you can only really talk to 'standard devices' (cars, speakers, fitness trackers, etc).

Python library
Open a terminal (Menu > Accessories > Terminal) and type:
sudo apt-get install python3-dbus
sudo pip3 install bluedot
Or if you need to use Python 2 (please dont tho!):
sudo apt-get install python-dbus
sudo pip install bluedot

In order to communicate over Bluetooth securely you need to pair your phone to your Raspberry Pi.

On your Android phone:
  1. Open Settings
  2. Select Bluetooth
  3. This will make your phone Discoverable
Using your Raspberry Pi
  1. Click the bluetooth icon on the taskbar
  2. Turn on Bluetooth (if its off)
  3. Click Make Discoverable
  4. Click Add Device
  5. Your phone will appear in the list, select it and click Pair
  6. Enter a PIN code
On your Android phone
  1. Enter the same PIN code when prompted
  2. Click Ok
The simplest way to use the Blue Dot is as a button:
  1. Open Python 3 (Menu > Programming > Python 3)
  2. Create a new file (File > New File)
  3. The following code, will start up the Blue Dot, and wait for it to be pressed:
  4. from bluedot import BlueDot
    bd = BlueDot()
    print("You pressed the blue dot!")
  5. Save your program (File > Save) as
  6. Run your program (Run > Run Module)
  7. Open the Blue Dot app
  8. Connect to your Raspberry Pi
  9. Press the Blue Dot
As well as waiting for something to happen you can also call functions when the button is pressed, released or the position its pressed moves.
from bluedot import BlueDot
from signal import pause

def say_hello():
    print("Hello World")

def say_goodbye():

bd = BlueDot()
bd.when_pressed = say_hello
bd.when_released = say_goodbye

By using the position of where the button is pressed you can use the Blue Dot like a joystick:
from bluedot import BlueDot
from signal import pause

def dpad(pos):
    elif pos.bottom:
    elif pos.left:
    elif pos.right:
    elif pos.middle:

bd = BlueDot()
bd.when_pressed = dpad

Add to this gpiozero's Robot functions, you can create a Bluetooth controlled robot with very little code.
from bluedot import BlueDot
from gpiozero import Robot
from signal import pause

bd = BlueDot()
robot = Robot(left=(lfpin, lbpin), right=(rfpin, rbpin))

def move(pos):
    elif pos.bottom:
    elif pos.left:
    elif pos.right:

def stop():

bd.when_pressed = move
bd.when_moved = move
bd.when_released = stop

Check out the Blue Dot documentation for more information and ideas - you really can do a lot with a simple circle :)

Thursday, 5 January 2017

Raspberry Pi - 4 digit 7 Segment display, gpiozero

I recently picked up some 'retro 4 digit LED displays' from pimoroni, noticing there was no support in gpiozero for 7 segment displays (either single or multi digit) I decided to add them and create a pull request.

This builds on the code I created for driving single 7 segment displays.

Hopefully the PR will get added into a gpiozero release soon, but until then add this code to your project and use the following to drive your display.
#setup the pins

#these are the pins the LED are connected too
# (in the order A, B, C, D, E, F, G, decimal point)
LED_PINS = (7, 22, 25, 17, 8, 27, 4, 24)
#these are the pins the digits are connected too
DIGIT_PINS = (23, 18, 15, 14)

#create the multi seven segment display
# use active_high=True when digit pins are cathode (ground)
multi_sev = MultiSevenSegmentDisplay(LED_PINS, DIGIT_PINS,

#display your message

#turn off the display using
The display function works by plexing the display, turning the LEDs on one at a time, so quickly it tricks the eye into thinking the display is showing 1 message.

Thursday, 8 December 2016

Raspberry Pi - bash memory split check

I needed a bash script which would only launch a program on a Raspberry Pi if there was enough memory dedicated to the GPU.

I pulled this script together which checks to see if there is at least 128 MB dedicated to the GPU before starting the program.


#check there is enough gpu memory to start
#get the gpu memory and output to a file called gpu_mem
vcgencmd get_mem gpu > gpu_mem
source gpu_mem
#strip last char from the output (i.e. 64M)
if (( $gpu >= 128 )); then
    echo "The program needs at least 128MB of memory allocated to the GPU"
    echo "Use sudo raspi-config (Advanced Options > Memory Split) to change"
#remove the gpu_mem file
rm gpu_mem

Tuesday, 29 November 2016

Compile Allegro 5.x for Raspberry Pi

I am in the process of porting Mayhem 2 to Allegro 5 (with the help of Jonas Karlsson), and wanted to compile the latest version of Allegro on the Pi, as only an older version is available through apt.

Install the dependencies
sudo apt-get install build-essential git cmake cmake-curses-gui xorg-dev libgl1-mesa-dev libglu-dev libpng-dev libcurl4-nss-dev libfreetype6-dev libjpeg-dev libvorbis-dev libopenal-dev libphysfs-dev libgtk2.0-dev libpulse-dev libflac-dev libdumb1-dev
Get the Code
git clone
cd allegro5
Check out the version you want - see here for a list of versions
git checkout 5.2.1
Build it
mkdir build
cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/Toolchain-raspberrypi.cmake
Install it
sudo make install
export PKG_CONFIG_PATH=/home/pi/allegro5/build/lib/pkgconfig
sudo ldconfig