The purpose of this post is to create a battery charge level indicator in Qt/QML. For the buttery icon we’ll use Font Awesome, which is a free icon font, so that we don’t need to design the icon from scratch.

Here is the list of contents of this post.

Contents
– Steps
1. Download icon font and add to your project
2. Create “BatteryIcon” component
3. Use “BatteryIcon” component
– Summary
– Reference

 

Steps
1. Download icon font and add to your project

1-1. Download Font Awesome from here and extract the zip file.

1-2. Copy .ttf file to your Qt Creator project folder.

1-3. Then, right-click on “qml.qrc” and select “Add Existing Files…” in the left side bar.

1-4. Select .ttf file to add.

 

2. Create “BatteryIcon” component

2-1. In your Qt Creator project, click on “Resources” in the left side bar.

2-2. Then, right-click on “qml.qrc” and select “Add New…”

2-3. Select “Qt” and “QML File (Qt Quick2)”.

2-4. Input “BatteryIcon” as the file name and click on “Finish”.

2-5. Copy below lines to the file.

— BatteryIcon.qml —

2-6. Click on “qml.qrc” and “/” in the left side bar.

2-7. Right-click on the .ttf file and select “Copy URL …”.

2-8. Update URL on Line 31 with the URL copied at Step 2-7.

 

3. Use “BatteryIcon” component

3-1. Use BatteryIcon component like below example. It has three properties, icon color, icon size (in pixel) and battery level from 0 to 4 (0:empty, 1:quarter, 2:half, 3:three quarters and 4:full).

If everything is fine, it will show like below battery icon.

 

Summary

I needed some icons for a project but was hesitant to create because I thought it’s gonna take some time. Then I found this article. It was really helpful for me at the situation, and Font Awesome is, yes, awesome! Using icon fonts for QML is makes sense. It requires less work, and it works perfectly.

 

Reference

– How to make a quick custom Qt/QML checkbox using icon fonts
https://medium.com/@eduard.metzger/how-to-make-a-quick-custom-qml-checkbox-using-icon-fonts-b2ffbd651144

– Font Awesome — The iconic font and CSS toolkit
http://fontawesome.io/icons/

 

 


This post shows how to make a Raspberry Pi a Bluetooth Beacon by using Bluetooth Developer Studio and its Beacon plugin. Bluetooth Developer Studio is a free application development tool from Bluetooth SIG and with Beacon plugin, it generates a script file which makes a Raspberry Pi a Bluetooth beacon.

The steps below basically follow a tutorial (Turning your Raspberry Pi into a Beacon using Bluetooth Developer Studio v1.0) which is included in Beacon plugin for Bluetooth Developer Studio but are simplified for my learning purpose. Please note, this is not very practical but rather kind of “getting started with” Bluetooth Developer Studio and Bluetooth Beacon.

Here are the contents of this post.

Prerequisites

  • Raspberry Pi
    Assuming your Raspberry Pi is up and running. If it’s not already, please set it up first. Raspberry Pi 3 or Raspberry Pi Zero W is recommended since they have a built-in Bluetooth chip. If you want to use other model which doesn’t have Bluetooth chip, you need a Bluetooth Low Energy USB dongle such as this.
  • Windows PC (Windows 7 or higher)
    Bluetooth Developer Studio works on only Windows 7 and higher.

Steps
1. Install Bluetooth Developer Studio and Beacon plugin
1-1. Download Bluetooth Developer Studio from here. As of 9/2/2017, the latest version is 1.1. It requires registration to download.

1-2. Install Bluetooth Developer Studio by following the Setup wizard.

1-3. Download Beacon plugin from here.

1-4. Unzip the downloaded file and go to \beacon-1\tutorial\RaspberryPi\starter-files.

1-5. Then, copy “beacon-plugin” folder into Bluetooth Developer Studio’s plugins folder.
(e.g. “C:\Program Files (x86)\Bluetooth SIG\Bluetooth Developer Studio\Plugins”)

1-6. Go into the newly copied folder (i.e. “beacon-plugin”) and open “plugin.js” with a text editor.

1-7. Search the lines below and change “true” to “false” on the if statement.

* Note: Change the permissions to modify the file if needed.

 

2. Create Beacon Profile
2-1. Launch Bluetooth Developer Studio.

2-2. Create a new project from “FILE” > “NEW”

2-3. Enter “Beacon” in the “NAME” field of “NEW PROJECT” window.

2-4. Enter “org.bds.beacon” in the “NAMESPACE” field, then click “OK” button.

2-5. Click on “New Profile”, change the name to “Beacon”, then click “SAVE”.

2-6. Click on the “Custom Service” button.

2-7. Click on “Beacon new service” and change it to “Beacon”, then, “SAVE”.

2-8. Click on “GAP” button

2-9. Enter beacon data as below and click on “DONE”.
TX Power : “-127″
Manufacturer Specific Data” : “ffeeddccbbaa99887766554433221100facefeed”

2-10. From the menu bar, select “FILE” > “SAVE” to save the profile.

 

3. Generate Beacon script

3-1. From the top menu, select “TOOLS” > “GENERATE CODE”.

3-2. Select “Beacon” plugin and click on “GENERATE”.

 

3-3. After “Code generated successfully”, check on “Open output location when finished”, then click on “FINISH”.

The folder which contains the generated script file will be opened automatically.

 

4. Execute Beacon script on Raspberry Pi

4-1. Copy the generated “Beacon.sh” script to Raspberry Pi’s home directory. (e.g. via USB drive, scp)

4-2. Open terminal on Raspberry Pi and add execute access to the script.

4-3. Run the script.

If it worked you should be able to see output such as:

If you get an error like,

and/or,

that is because the script file has Windows style line endings and bash doesn’t understand it. You can fix the error by changing the line endings from Windows style to Unix style by the following command.

5. Verify
You can check if it’s really broadcasting the beacon data by using a smartphone app such as “BLE Scanner” on Android.

 

 


I’ve been using Raspberry Pi for my projects and have many Raspbian OS image backups. It’s eating up storage space on my PC, so I decided to shrink those images so that I can create more space. I’ve found several sites explain how to do it and writing this post mostly for myself to repeat the process since it’s a little complicated.

If you will follow the steps, please do so at your own risk. Manipulating partition may cause damage to your system.

 

Contents
– Assumptions
– Steps
1. Find out SD card mount points on your PC
2. Create OS image from SD card
3. Shrink root partition
4. Update partition table
5. Trim empty space from the image file
– Summary
– Reference

 

Assumptions

Hardware

  • Linux PC (I used Ubuntu 16.04)
  • Micro-SD card reader like this (in case your PC doesn’t have SD card slot)
  • Micro-SD card which contains Raspbian OS you want to shrink

Software

  • GParted (Partition management tool)
    If it’s not installed on your PC, please do so.

Steps
1. Find device name of SD card on your PC
1-1. Before insert SD card into your PC, check all the mounted devices by typing:

Output should be like this:

1-2. Then, insert the SD card and check it again.

1-3. Compare the outputs. Since Raspbian has two partitions, there should be two new lines added. In the example above, /dev/sdb1 and /dev/sdb2. Also, the device name of the SD card is /dev/sdb. The last character in a mount point represents the partition number.

 

2. Create OS image from SD card
2-1. First, unmount the partitions.

2-2. Copy the image from SD card to PC. In the below example, please replace “/dev/sdb” with device name which you found out in above steps, and “raspbian.img” with anything you want for the file name on your PC.

2-3. Change owner of the newly created image file.

3. Shrink root partition
3-1. Raspbian has two partitions, “boot” and “root”. “boot” is very small and it’s not worth to compress. So, let’s find out the start sector of “root” partition by typing:

The output should be like this. Take a note of value of “Start” of the bigger partition. It’s start sector of “root” partition. In example below, it’s “94208”.

3-2. Create a loopback device with the Raspbian image so that GParted can recognize the image as device. Please replace “94208” with the value you got in previous step.

3-3. Launch GParted.

It will open a new window.

3-4. Click “/dev/loop0” and select “Partition” > “Resize/Move”.

3-5. Then, change “New size” of the image.
Note: This step is a little tricky. It seems it requires some margin but I couldn’t figure out how much margin it exactly requires. Seems it differs based on the “Minimum size”. I needed to do trial-and-error till successfully complete resizing.

3-6. Click on “Resize” button.

3-7. Select “Edit” > “Apply All Operations”.
Note : If you get “resize2fs: New size smaller than minimum” error, go back to step 3-4.

3-8. Click on “Save Details”.

3-9. Open saved “gparted_details.htm” file on browser and search a line starts with “resize2fs”. The value followed by “K” is new size for “root” partition. Take a note of this value. In example below, it’s “4934656K”.

3-10. Click on “Close” button and close GParted.

3-11. Remove the loopback device.

4. Update partition table
4-1. Create a new loopback device for the whole image

4-2. Launch fdisk to update partition table.

4-3. Delete 2nd partition (i.e. root partition).
4-3-1. Type “d” and enter.

4-3-2. Type “2” and enter to select 2nd partition.

4-4. Create new 2nd partition.
4-4-1. Type “n” and enter to create new partition.

4-4-2. Type “p” and enter to make it a primary partition.

4-4-3. Type “2” and enter to select partition number.

4-4-4. Enter the start sector of 2nd partition. (See step 3-1)

4-4-5. Type “+” and the new size for 2nd partition (see step 3-9).

4-5. Apply all the changes by typing “w” and enter.

Then you’ll get a warning message like below but you can ignore it.

5. Trim empty space from the image file
5-1. Find out the end sector of the 2nd partition by using fdisk.

The output should be like this:

Note down the end sector value of 2nd partition. (i.e. “9963519”)

5-2. Remove the loopback device.

5-3. Trim the empty space from the image file. Please replace “9963519” in the example below with your end sector value from the previous step.

That’s it. Now the Raspbian image has been shrinked. In the example below, the size is now 1.8 GB. (originally it’s 30 GB)

Summary
It’s not very easy but it can definitely reduce the size of the SD card image. If you want more free space on your PC, you can zip the image file.

Here is an example (32 GB SD card) of comparison between before, after, and after + zip.

To write Raspbian image (.img or .zip) to SD card, please see this.

 

Reference
– Shrinking Raspberry Pi SD Card Images
http://www.aoakley.com/articles/2015-10-09-resizing-sd-images.php

– How to BackUp and Shrink Your Raspberry Pi Image
http://www.instructables.com/id/How-to-BackUp-and-Shrink-Your-Raspberry-Pi-Image/

– Installing operating system images on Linux
https://www.raspberrypi.org/documentation/installation/installing-images/linux.md

 

 


This is how to write Raspbian OS image to SD card by using Linux command line. Since the steps below use “dd” command which may cause damage to your system if used wrongly, please be careful about the usage.

 

Contents
– Assumptions
– Steps
1. Download Raspbian OS Image
2. Find device name of SD card on Linux
3. Write Raspbian OS image to SD card
– Reference

 

Assumptions

  • Raspberry Pi board
  • Micro SD card
  • Linux PC
  • Micro SD card adapter

Steps
1. Download Raspbian OS Image
1-1. Download Raspbian OS zip archive from the official site to your Linux PC.

 

2. Find device name of SD card on Linux
2-1. Before insert SD card into your PC, check all the mounted devices by typing:

Output should be like this:

2-2. Then, insert the SD card and check it again.

2-3. Compare the outputs. Newly added device is for the SD card. Since the last part (“1”) is the partition number, the device name for whole SD card is /dev/sdb. Take a note of the device name.

2-4. Unmount the SD card partition(s). Type below, replacing /dev/sdb1 with your SD card device name.

 

3. Write Raspbian OS image to SD card
3-1. Type below to unzip the archive and write it to SD card. Please replace “2017-08-16-raspbian-stretch.zip” with the file name of your OS image archive and “/dev/sdb” with your SD card device (see step 2-3).

In case you are writing .img file, type below:

Reference
– INSTALLING OPERATING SYSTEM IMAGES ON LINUX
https://www.raspberrypi.org/documentation/installation/installing-images/linux.md

– RPi Easy SD Card Setup
http://elinux.org/RPi_Easy_SD_Card_Setup

 

 


The purpose of this post is to enable Bluetooth Handsfree Profile (HFP) with PulseAudio on Raspbian Stretch so that Raspberry Pi can act like as a handsfree speaker phone or a handsfree car kit. This is an updated version of this post. When I wrote the previous post, I was using Raspbian Jessie and some steps needed to be updated to apply Raspbian Stretch.

There is one thing needs to be mentioned before start. The original plan was to use Raspberry Pi3’s on-board Bluetooth chip for this project. Unfortunately, it didn’t work. It seems that the problem is related with the chip. So, we have to use a Bluetooth dongle instead.

 

Here is the list of contents of this post.

Contents
– Prerequisites
– Steps
1. Preparations
2. oFono Installation
3. PulseAudio Installation & Settings
4. Connecting Your Phone
5. Making a Call
– Summary
– Reference

 

Prerequisites

Below are the required application versions to enable Bluetooth HFP according to PulseAudio release note.

  • Bluez 5.0 or later (v5.43 is pre-installed in Stretch)
  • PulseAudio 6.0 or later (not pre-installed in Stretch, but it supports v10.0)
  • oFono 1.13 or later (not pre-installed in Stretch, but it supports v1.18)

Steps
1. Preparations
1-1. Connect Bluetooth dongle and USB sound card with Raspberry Pi.
1-2. Connect the microphone and speakers to 3.5 mm audio jacks on the USB sound card.
1-3. Boot up Raspberry Pi Board.
1-4. As described, we don’t use on-board Bluetooth chip. So we’ll disable it. To do that, open “/etc/modprobe.d/raspi-blacklist.conf”.

1-5. Add lines below and save.

1-6. According to a Raspberry Pi Foundation blog post, Bluetooth audio is handled by ALSA through bluez-alsa in Stretch. However, since in this post we are using PulseAudio, let’s uninstall bluez-alsa.

1-7. Then reboot the board.

1-8. After reboot, check dmesg and make sure there is no Bluetooth-related errors.

1-9. Also, make sure hci0 is UP.

The result should be like this:

If it’s DOWN for some reason, then turn it up.

 

2. oFono Installation
2-1. oFono is not installed by default, so let’s install it.

2-2. Make sure if it’s successfully installed.

2-2. Then, start the service.

You can check the service running like below.

3. PulseAudio Installation & Settings
3-1. In Raspbian Stretch, PulseAudio is not pre-installed. Install PulseAudio and its Bluetooth module:

3-2. Check the version. It should be v10.0 (or higher).

3-3. Open “/etc/pulse/default.pa”.

3-4. Add “headset=ofono” on the line of ‘module-bluetooth-discover’.

3-5. Restart Raspberry Pi.

 

4. Connecting Your Phone
4-1. Launch bluetoothctl.

4-2. Then, input below commands.

4-3. Start searching your phone. Make sure your phone is discoverable.

4-4. After detecting your phone, turn scan off.

devices command shows a list of found devices.

4-5. Then, pair, trust and connect to your phone by specifying the Bluetooth device address (in this case it’s “XX:XX:XX:XX:XX:XX”, please replace it wity your device’s address).

If everything is ok, you should see “successful” at the end of the result for each command like below.

5. Making a Call
To make a call, you can use oFono test scripts which is included in the source code.
5-1. First, download the source and extruct.

5-2. Then, you can make a call by:

If you want to end the call, then type:

Summary
Since Raspbian Stretch supports PulseAudio v10.0, the steps are now a little simpler than previous one. I confirmed both sending and receiving audio are fine during a hands free call. I confirmed Bluetooth audio streaming (A2DP) is working too.


Update (May 2, 2018):
The steps to enable equalizer on HFP sending audio is newly posted.


Reference

– Raspbian Stretch has arrived for Raspberry Pi
https://www.raspberrypi.org/blog/raspbian-stretch/

– Connect Bluetooth Headset To Raspberry Pi 3 (A2DP & HSP)
http://youness.net/raspberry-pi/bluetooth-headset-raspberry-pi-3-ad2p-hsp

– PulseAudio 6.0 Release Notes –freedesktop.org
https://www.freedesktop.org/wiki/Software/PulseAudio/Notes/6.0/

– PulseAudio Documentation –freedesktop.org
https://freedesktop.org/wiki/Software/PulseAudio/Documentation/