Reader small image

You're reading from  Raspberry Pi Android Projects

Product typeBook
Published inSep 2015
Reading LevelBeginner
Publisher
ISBN-139781785887024
Edition1st Edition
Languages
Right arrow
Author (1)
Gökhan Kurt
Gökhan Kurt
author image
Gökhan Kurt

Gkhan Kurt has been trying to keep up with the the latest developments in technology and IT in his 15-year-long development career. For the past 4 years, he has been working at IFS Labs, one of the top innovation departments of the Swedish software industry. Currently, he is involved in the Internet of Things and has been developing prototype IoT implementations using Raspberry Pi. He has a master's of science degree from Chalmers University of Technology and a bachelors degree from the Middle East Technical University. You can connect with him on Twitter and on LinkedIn.
Read more about Gökhan Kurt

Right arrow

Chapter 5. Missed Calls with Pi

In this chapter, we will implement a much more programming-oriented project and dive into Bluetooth Smart or Bluetooth Low Energy (BLE) programming. We will make the Pi and Android phones communicate through Bluetooth, and control the Pi using this channel. We will cover the following topics in this chapter:

  • Installing the necessary components

  • Adding a sensor service to Bluetooth Low Energy

  • Connecting from an Android app

  • Sending the reboot command from your Android phone to the Pi

  • Sending more commands from your Android phone to the Pi

Installing the necessary components


The hardware component needed for this project is a BLE-enabled Bluetooth USB dongle. It is important that this hardware supports BLE as we will specifically make use of this part of the Bluetooth stack. We will use one by Plugable, which is available on Amazon.

The Bluetooth dongle by Plugable

The Raspbian distribution that we have downloaded already contains support for Bluetooth, but we need to update Bluetooth packages for better LE support. You can build and install a more modern of the Bluetooth package version using the following commands:

sudo apt-get install libdbus-1-dev libdbus-glib-1-dev libglib2.0-dev libical-dev libreadline-dev libudev-dev libusb-dev make
mkdir -p work/bluepy
cd work/bluepy
wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.33.tar.xz
tar xvf bluez-5.33.tar.xz
cd bluez-5.33
./configure --disable-systemd
make
sudo make install

The make step will compile the necessary packages needed for the Pi and will take about 15 minutes...

Adding a sensor service to Bluetooth Low Energy


We will add a new service to the already existing example from Gatt. This new service will publish two new characteristics to begin with: one for humidity and the other for temperature measurements. We will read the measurements the same way using the techniques we've discussed in Chapter 2, Server Management with Pi. To read these measurements, we will create two new files with content similar to the sense.py file that we discussed Chapter 2, Server Management with Pi. Let's create two files under the home directory, and name them humidity.py and temperature.py. The temperature.py file has the following content:

#!/usr/bin/python

import sys
import Adafruit_DHT

humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, 4)
print str(temperature)

The humidity.py file has similar content. The only difference is that it prints out the humidity part of the measurement instead of the temperature:

#!/usr/bin/python

import sys
import Adafruit_DHT...

Connecting from an Android app


We have used an existing app to connect to the BLE service that we implemented on Raspberry Pi. This app, called BLE Scanner, is very general purpose and would work for any kind of BLE device. However, we need a more specialized app that only reads measurements and abstracts away details of the BLE protocol, such as device scan, services, and service characteristics. In this section, we will implement an Android app to connect to the Raspberry Pi BLE. We need to install the Android Studio for this purpose. Android studio is specifically designed for Android app development by Google. You can read more about it by visiting http://developer.android.com/tools/studio/. You can find instructions for installation at http://developer.android.com/sdk/. We will use a real device to test our app and not the built-in emulator. For this purpose, you may need to install device drivers specific to your Android phone and make configuration changes to the Android Studio installation...

Sending the reboot command from your Android phone to the Pi


Until now, we have been receiving data from the Pi through BLE. Now, we will send commands to it using the same channel. We will implement a new write characteristic in the same service as our temperature and humidity read characteristics are, which were defined on the Pi. Using these new characteristics, we will send the reboot command to the Pi. Let's begin by editing the sensor.go file again and put the following code at the end of it:

s.AddCharacteristic(gatt.MustParseUUID("41fac9e0-c111-11e3-9246- 0002a5d5c51b")).HandleWriteFunc(
  func(r gatt.Request, data []byte) (status byte) {
   log.Println("Command received")
   exec.Command("sh", "-c", "sudo reboot").Output()
   return gatt.StatusSuccess
 })

Build and restart the BLE server using the following commands:

cd /home/pi/gopath/src/github.com/paypal/gatt
go build examples/server.go
sudo ./server

Now, test the characteristics mentioned previously using the BLE Scanner app. Whenever...

Sending more commands from your Android phone to the Pi


In the previous section, we have sent the reboot command from Android to the Pi. In this section, we will send two new commands. One to light up a LED that we will connect to the Pi, and another to play sound on the Pi. These commands will be reused in the forthcoming sections.

Lighting the LEDs

We'll begin by connecting a LED light to the GPIO ports of the Pi. The LEDs usually come with a short and long leg. Connect a resistor to the short leg of the LED, and connect a female/female jumper to the other side of the resistor. This jumper should then be connected to one of the ground pins of the Pi. Take a look at the schema in Chapter 2, Server Management with Pi, to identify the pins. Note that we already used one of the ground pins when we connected our temperature-humidity sensor to the Pi. However, there are plenty of ground pins available. The long leg of the LED should be connected to one of the GPIO pins. We will choose number 17...

Summary


In this chapter, we covered a lot of content, ranging from BLE implementations on the Pi to details of the Android BLE code. We had great fun with the Pi and came up with a useful project that can be developed further.

In the next chapter, we will learn more ways to make use of BLE equipment on the Pi and use our phones not just as Android devices, but also as access points for the Pi.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi Android Projects
Published in: Sep 2015Publisher: ISBN-13: 9781785887024
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Gökhan Kurt

Gkhan Kurt has been trying to keep up with the the latest developments in technology and IT in his 15-year-long development career. For the past 4 years, he has been working at IFS Labs, one of the top innovation departments of the Swedish software industry. Currently, he is involved in the Internet of Things and has been developing prototype IoT implementations using Raspberry Pi. He has a master's of science degree from Chalmers University of Technology and a bachelors degree from the Middle East Technical University. You can connect with him on Twitter and on LinkedIn.
Read more about Gökhan Kurt