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 3. How to Make an Environmental Monitoring System

This chapter describes how to build an environmental monitoring system. We want to build a complex IoT system, using Android Things, that measures some physical environment properties. Furthermore, in this Android Things project, we will use RGB LED, introduced in Chapter 1, Getting Started with Android Things, and a single color LED to visually represent the environment conditions. To do it, we will use a different class of sensors. While in the previous chapter we learned how to use two-states sensors, in this chapter we will use more complex sensors that require different connections and pins. In more detail, this chapter focuses on learning how to use I2C with Android Things.

Moreover, the main topics covered in this chapter are:

  • How to use I2C sensors with Android Things
  • How to read data from sensors using Sensor Manager
  • How to visualize the data acquired using LEDs
  • Overview of I2C protocol
  • Custom I2c driver

At the end of this chapter...

Environmental monitoring system project overview


Before digging into the code details and implementing the project using Android Things, it is useful to have an overview of the project. The target of this project is building an environmental monitoring system that detects:

  • Temperature
  • Pressure

The interesting aspect of this project, other than the data acquired using sensors, is that the Android Things app will visualize this information using LEDs. In other words, we will implement an app that somehow reacts to the environment properties implementing a custom logic and it is able to control other peripherals.

The following figure shows how the project will work:

This project uses an RGB LED to represent the current pressure condition. The RGB LED will have three different colors:

  • Yellow: Stable condition. The pressure is over 1022 millibar.
  • Green: Cloudy. The pressure is between around 1000 millibar and 1021 millibar.
  • Blue: Chance of rain. The pressure is under 1000 millibar.

There is another red...

How to read data from sensors


Now we are ready to start acquiring data from I2C sensors. Usually, in order to use an I2C peripheral, we need a driver. A driver is a set of classes that handle the communication between the Android Things board and the peripheral. Moreover, these classes handle the specific protocols implemented by the peripheral. We will describe how to implement a low-level protocol in the next sections. By now, we can use a pre-built driver that is a library we have to include in our project. All the drivers officially supported by Android Things are available at GitHub under the folder contrib-drivers at https://github.com/androidthings/contrib-drivers.

Let us start:

  1. Create a new Android Things project by cloning the repository as described in the first chapter.
  2. Open build.gradle and add the following line under the dependencies:
compile 'com.google.android.things.contrib:driver-bmx280:xx'

Where xx is the version of the driver.

Now you are ready to use the BMP280/BME280 sensor...

Handling sensors using the Android sensor framework


The approach described in the previous chapter works if we want to read the pressure and the temperature one shot. In the project we are developing in this chapter, the app has to read the temperature and the pressure continuously. Therefore, it is convenient to use another approach. This approach is the same strategy we implement in Android when the app needs to monitor the smartphone sensors. As you know, nowadays a smartphone has several built-in sensors and to read their values we use the sensor framework provided by Android SDK.

Just to recap briefly how sensor framework works in Android SDK, we can remember that there are key elements that play an important role in these frameworks. These elements are:

  • SensorManager
  • Sensor
  • SensorEvent
  • SensorEventlistener

Luckily, these classes and interfaces are also present in Android Things SDK, and they help us to develop smart Android Things apps easily. The following is a brief description of the most...

Putting it all together - acquiring data


It is time to put everything together and start acquiring data. By now, we have implemented:

  • Two sensor listeners to listen to the new values
  • The listener to know when the sensor is connected to the board

Let us glue all the pieces and make our app work. Open MainActivity.java again and in the onCreate method add the following lines:

callback = new BMX280Callback(); sensorManager.registerDynamicSensorCallback(callback); try {
  mySensorDriver =
  new Bmx280SensorDriver(BoardPins.getSDAPin());
  mySensorDriver.registerTemperatureSensor();
  mySensorDriver.registerPressureSensor();
}
catch(Throwable t) { t.printStackTrace();
}

Where mySensorDriver is an instance of Bmx280SensorDriver that handles the communication details to the BMP280/BME280. Notice that as we described in the previous chapter, to make the app independent from the board we did not directly use the SDA pin identification, but we have used a method to retrieve the pin name according to the...

How to close the sensor connection


When the Android Things app is destroyed, it is important to release all the connections we have used to exchange data with the sensor and remove all listeners. In this way, we free all the resources used by the app. In more details, we have to release the SDA pin used to communicate to the sensor so that other apps can reuse it. In order to destroy the app gracefully, we have to execute the following steps:

  1. Unregister the sensor listener we have used to listen to value changes.
  2. Unregister the sensor listener we have used to know when the sensor is connected to the Android Things board.
  3. Close the sensor connection.

The best approach is executing the previous steps in the onDestroy method of our monitoring app. The following code describes how to implement these steps:

@Override
protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy");
sensorManager.unregisterListener(tempCallback); sensorManager.unregisterListener(pressCallback); mySensorDriver...

How to control GPIO pins


Now that we know how to read the environment parameters, we can implement the application logic to control other peripherals according to the values acquired. As described in the previous sections, the Android Things monitoring app uses the temperature and pressure to controls two devices:

  • An RGB LED that shows the current pressure state
  • A RED LED that shows if the temperature is lower than a threshold

To make the app work, we have to fix the pressure threshold values. To simplify the development process we can suppose that there are two thresholds:

  • Threshold one, that we will call LEVEL_1, is 1022.9 mb
  • Threshold two, that we will call LEVEL_2, is 1009.14 mb

The app logic that we will implement works in this way:

  • If the current pressure is over the LEVEL_1 then the RGB LED will have the green and red color turned on (yellow)
  • If the current pressure is between LEVEL_1 and LEVEL_2 the RGB LED will have only the green color turned on
  • If the current pressure is below LEVEL_2...

Diving into I2C protocol


Until now, we have used I2C sensors and we have implemented a full working project without knowing much about I2C protocol and sensor-specific protocol. This is the power of Android Things: it abstracts the protocol details and we can use I2C sensors developing a normal Android app. This is also possible because we have used an external library in our project. If you remember at the beginning of this project we have included in build.gradle the library to manage the BMP280/BME280 sensor:

compile 'com.google.android.things.contrib:driver-bmx280:xx'

As long as we use peripherals that have a library to handle them we do not have to worry about the protocol details. When we use a peripheral that is not directly supported or there is not a library, we have to implement the specific peripheral protocol. In this context, it is important to know how I2C works.

I2C protocol overview

I2C stands for Inter Integrated Circuit. This is a serial communication protocol that uses two...

How to implement a custom sensor driver


Now that we know how I2C protocol works, we can start developing our custom driver. A powerful feature of Android Things is the capability to add new peripherals developing specific drivers. In other words, it is possible to extend the sensor framework including new sensors. In this way, Android Things does not make a difference between a built-in sensor and the new sensors. It is possible to handle them in the same way we handle built-in sensors. It is important to notice that the driver that handles the sensor depends on the sensor protocol built on top of I2C bus.

In order to implement a new sensor driver in Android Things we have to follow these steps:

  1. Implement a class that extends the UserSensorDriver.
  2. Describe the sensor specifications and capabilities.
  3. Register the sensor driver.

To better understand how these classes are used and the role they play, it is useful to analyze how the BMP280/BME280 sensor driver is implemented. This can help us to...

Summary


At the end of this chapter, you learned how to use I2C sensors and how to connect them to an Android Things board. Moreover, we implemented a full-working Android Things IoT app that monitors environmental parameters. We have learned how to control GPIO pins using the information retrieved from sensors. This project can be extended by adding new features. An interesting feature that you could add is considering if the pressure is rising or lowering to have a more detailed weather forecast. Using this environmental monitoring system, you gained knowledge about how to implement custom drivers. In this way, you have infinite possibilities to use your Android Things board with several I2C sensors.

In the next chapter, we will cover an important aspect of the IoT ecosystem: IoT cloud platforms. We will learn how to use IoT platforms and how to integrate them with Android Things and stream data to the cloud.

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