Reader small image

You're reading from  Android Things Projects

Product typeBook
Published inJun 2017
Reading LevelBeginner
PublisherPackt
ISBN-139781787289246
Edition1st Edition
Languages
Right arrow
Author (1)
Francesco Azzola
Francesco Azzola
author image
Francesco Azzola

Francesco Azzola is an electronic engineer with over 15 years of experience in computer programming and JEE architecture. He is SCEA certified (Sun Certified Enterprise Architect), SCWCD, and SCJP. He is an Android and IoT enthusiast. He loves creating IoT projects using Arduino, Raspberry Pi, Android, and other platforms. He is interested in the convergence between IoT and mobile applications. Previously, he worked in the mobile development field for several years. He has created a blog called survivingwithandroid,where he shares posts about coding in Android and IoT projects.
Read more about Francesco Azzola

Right arrow

Chapter 2. Creating an Alarm System Using Android Things

In this chapter, we will build an alarm system using Android Things. The target of this project is creating a system that detects movements using PIR sensors and when this event happens the Android Things app will send a notification to the user smartphone. The principles of this project are commonly used in real alarm systems we have in our homes, but we will build it with a totally new operating system. This is an interesting project because it uses, at the same time, sensors and cloud platforms. Through this project, we will explore how to use GPIO pins in Android Things and how to interact with two states sensors.

The main topics covered in this chapter are:

  • How to use build an alarm system
  • How to use GPIO pins and PIR sensors
  • How to handle events from a GPIO pin
  • How to build an app that is independent of the board
  • How to notify events from Android Things to Android smartphones

This project demonstrates how powerful Android Things SDK...

Alarm system project description


An alarm system is a complex system that uses several sensors to keep our home safe. At the heart of these types of systems, there are sensors that are able to detect motion. In other words, these sensors can detect if an object is moving in their detection area. When this happens, they notify this event. In this chapter, we will create a real-life project that uses these sensors to detect motion and notifies the event to the smartphones. At the end of this project, we will be able to detect if someone is entering our home without our authorization. Once you have built this project, you can expand it, adding more sensors so that you can monitor several rooms. Moreover, this project can be used as a starting point and can be expanded, adding new features as we will see in the following sections. The following figure describes how this Android Things IoT project will work:

The following are the main steps:

  1. The PIR sensor scans the detection area looking for movement...

How to close the connection with a GPIO pin


In this last step, we will learn how to close the connection with a GPIO pin. This is an important step because in this way, we free the resources and remove all the listeners we added to the GPIO pins.

An Android Things app has a life cycle very similar to an Android app. The place where we implement these actions is the Activity onDestroy method. In this method we have to:

  • Remove all the listeners attached to the GPIO pins
  • Close the connection to the GPIO pins

So, open MainActivity.java again and look for the onDestroy method and modify it:

@Override
protected void onDestroy()
  { super.onDestroy(); Log.d(TAG, "onDestroy");
  if (gpioPin != null) {
    gpioPin.unregisterGpioCallback(sensorCallback);
      try {
    gpioPin.close(); gpioPin = null;
      }
    catch(Exception e) {}
  }
}

Handle different boards in Android Things


There are two important aspects that we have not covered:

  • How to select the pin to connect the peripherals to
  • How to identify the pin name

Regarding the first aspect, in Raspberry Pi 3 and Intel Edison, but in general for all the boards, the pins do not provide the same features. In other words, we cannot connect the peripherals to a pin by choosing it randomly. We have to select the pin according to the peripheral specifications. In this context it is important to know the pinout of the boards so that we can identify the right pins for our peripherals.

The second aspect is relevant when we want to develop an Android Things app that will run on different boards. As we said in the previous chapter, from the code point of view, this is not a problem because the Java language at the base of Android Things SDK guarantees us that we can run it on all compatible boards. Until now, when we had to identify a pin, we used a double version, one for Raspberry Pi...

How to implement the notification system


Now we are ready to implement the last part of this project: the notification system. In the next paragraphs, we will describe how to send a notification to the user smartphone when motion is detected. As the messaging system, this IoT project uses Google Firebase. This is a cloud platform developed by Google providing several interesting services. We will use the Notification service.

There are several ways we can send a notification from the Android Things app to a user smartphone. To keep things simple, we will use topic. You can imagine a topic like a channel. After a device subscribes to a topic, it will receive all the messages published to this channel. In our project, the user smartphone behaves like a subscriber receiving messages from the channel, while the Android Things app behaves like a publisher publishing the messages.

Now it is clear the roles these two apps play in this project.

Before implementing it, we have to configure the Firebase...

Android companion app


In order to receive the notifications, we have to install an Android companion app on our smartphone. Just to simplify the system, this Android app will:

  1. Subscribe to the channel used by the Android Things app to send notifications.
  2. Implement a service to listen to the incoming notification.
  3. Show the notification to the user.

If you do not know how to receive notifications in Android you can visit https://firebase.google.com/docs/android/setup to know more. The source code of the Android companion app is provided with this book's source code. The app interface is very simple because we simply have to subscribe to the topic and wait for the incoming notifications.

To install the app, just open the project using Android Studio and connect your smartphone to your PC/Mac. Add the google-services.json file to your app module. This file is the same one that you downloaded in the previous steps. Run and install the app on your smartphone. That's all.

The following screenshot shows...

Summary


At the end of this chapter, we implemented an alarm system using Android Things SDK. Moreover, you now know how to use a two-state sensor using GPIO pins. In the last part of the chapter, you learned how to integrate the Android Things app with Google Cloud services such as Firebase to send notifications.

You can use this knowledge to develop this project, adding new features such as more PIR sensors to monitor several rooms at the same time. Moreover, you can use a Firebase real- time database to log the time when the sensor detects movements.

In the next chapter, we will learn how to use more complex sensors that measure physical properties. We will experiment how to use I2C sensors and how we can integrate them with the Android Things app.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Things Projects
Published in: Jun 2017Publisher: PacktISBN-13: 9781787289246
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
Francesco Azzola

Francesco Azzola is an electronic engineer with over 15 years of experience in computer programming and JEE architecture. He is SCEA certified (Sun Certified Enterprise Architect), SCWCD, and SCJP. He is an Android and IoT enthusiast. He loves creating IoT projects using Arduino, Raspberry Pi, Android, and other platforms. He is interested in the convergence between IoT and mobile applications. Previously, he worked in the mobile development field for several years. He has created a blog called survivingwithandroid,where he shares posts about coding in Android and IoT projects.
Read more about Francesco Azzola