Reader small image

You're reading from  Android Sensor Programming By Example

Product typeBook
Published inApr 2016
Reading LevelBeginner
PublisherPackt
ISBN-139781785285509
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Varun Nagpal
Varun Nagpal
author image
Varun Nagpal

Varun Nagpal has been developing mobile apps since 2005 and has developed and contributed to more than 100 professional apps and games on various platforms, such as Android, iOS, Blackberry, and J2ME. Android app development has been his main area of expertise, and he has developed apps for a wide variety of Android devices, such as Android phones, tablets, watches, smart TVs, Android Auto, and Google Glass. He moved to Chicago in late 2013, and since then, he has become a seasoned mobile architect. He has worked in different roles (mobile architect, technical lead, senior developer, and technical consultant) for a variety of various global clients (Allstate, Verizon, AT&T, Sydbank Denmark, SiS Taiwan, Chams PLC Nigeria, and Nandos South Africa) in order to implement their mobile solutions. He has SCJP (Core Java) and SCWD (JSP and Servlets) certifications from Sun Microsystems and MCP (C#) and MCTS (ASP.NET) certifications from Microsoft. You can find his blogs on mobile technology and white papers written by him on his website at http://www.varunnagpal.com/. When he's not working, Varun can be found meditating or playing the flute. He also loves to develop meditation apps and fun games in his free time. He has developed about 40 meditation apps and games available on Google Play (https://play.google.com/store/apps/developer?id=Creative.Software.Studio) and the Apple App Store (https://itunes.apple.com/us/artist/creative-software-studio/id574745824) under the name of Creative Software Studio, his part-time start-up company (http://creativesoftwarestudio.com/).
Read more about Varun Nagpal

Right arrow

Chapter 3. The Environmental Sensors – The Weather Utility App

In this chapter, we will learn how to make use of different types of sensors in our weather utility application. We will also look at how the temperature, relative humidity, and pressure sensor values can be extended to get more useful data such as the altitude, dew point, and absolute humidity. We will explore an alternate source for getting the temperature, relative humidity, and pressure values to compensate for the unavailability of any environment sensor on the phone.

The things you will learn in the this chapter are as follows:

  • Understanding the requirements for the weather utility app
  • Understanding environmental sensors (temperature, humidity, and pressure)
  • How to use the temperature sensor of the phone
  • Getting pressure sensor values and calculating the altitude using them
  • Getting the relative humidity from the phone sensor and calculating the dew point and absolute humidity using the relative humidity and temperature sensor...

The weather utility app's requirements


Today's Android phones come with environment sensors in them. We will be using the same environment sensors to develop a weather utility app as a part of this chapter. Our app will make use of some additional inputs from external web services combined with our environment sensor's data to drive some more meaningful information, such as dew point, absolute humidity, and altitude. We will also compare weather data obtained from environment sensors to weather data obtained from an external web service. The following are high-level requirements for weather utility apps. The source code of this weather utility app can be found in GitHub under the author's account name and on the support page for the book:

  1. Get the outside temperature using the phone's temperature sensor.
  2. Get the air pressure using the phone's pressure sensor.
  3. Calculate the altitude using the phone's pressure sensor.
  4. Get the relative humidity using the phone's humidity sensor.
  5. Calculate the dew...

Understanding environmental sensors


Environment-based sensors measure environmental conditions (temperature, humidity, and pressure) around the phone and report values only in one dimension (only one value in the values[] array). Temperature and humidity sensors have an on-change reporting mode, that is, they will report values whenever there is a change in values from the last reported values, whereas the pressure sensor has a continuous reporting mode; that is, it will report values continuously as per the suggested time interval. Environmental sensors are hardware-based and are only available if a device manufacturer has built them into a device. Because of this, it's particularly important that you verify, at runtime, that an environment sensor exists before you attempt to acquire data from it. Also, unlike motion sensors and position sensors, which often require high-pass or low-pass filtering, environment sensors do not typically require any data filtering or data processing:

  • The temperature...

Time for action – using the temperature sensor


In this section, we will get the ambient temperature from the phone temperature sensor and will show it in the foreground activity. Before accessing the value for ambient temperature, we will also check the availability of the temperature sensor on the device.

  1. We created a TemperatureActivity and implemented it with the SensorEventListener interface so that it receives the ambient temperature values. We are also checking if the ambient temperature is available on the device and are maintaining the availability state using the isSensorPresent Boolean variable. If the temperature sensor is not present, then we show the relevant message using  TextView. The following code snippet shows the necessary steps:

            public class TemperatureActivity extends Activity 
            implements SensorEventListener{ 
     
              private SensorManager mSensorManager; 
              private Sensor mSensor; 
              private boolean isSensorPresent...

Getting air pressure from the phone's pressure sensor


The procedure to get values from the phone's pressure sensor is exactly the same as the previous example showing getting values from temperature sensors. The only difference is the sensor type. To get values from the pressure sensor, we have to specify the sensor type as TYPE_PRESSURE. All other best practices (initiating SensorManager, the Sensor object, and registering and unregistering the listener and sensor callback) remain the same as they were in the previous temperature sensor example.

Time for action – calculating the altitude using the pressure sensor

Once we have atmospheric pressure from the phone's pressure sensor, we can calculate the altitude of the phone using the getAltitude(float p0, float p1) method of the SensorManager class. The first parameter of the altitude API is the atmospheric pressure at sea level, and the second parameter is the atmospheric pressure of the current location, which can be obtained from the phone...

Getting relative humidity from the phone's humidity sensor


Getting values from the phone's relative humidity sensor is a very similar process to getting values from the temperature or pressure sensors. The only difference is in specifying the sensor type. To get the values from the relative humidity sensor, we have to specify the sensor type as TYPE_RELATIVE_HUMIDITY. All the other standard practices  (initiating SensorManager, the Sensor object, and registering and unregistering the listener and the sensor callback) remain the same. Another point to note is that humidity and relative humidity are used interchangeably; they both refer to the same value.

Time for action - calculating the dew point and absolute humidity

We can use the phone's relative humidity and temperature sensor to calculate the dew point and absolute humidity. Before calculating, let's look at the definition and formula of the dew point and absolute humidity.

  • Dew Point: The dew point is the temperature at which a given volume...

Summary


In this chapter, we learned how to use various environment sensors. We also derived of altitude, absolute humidity, and dew point values using the temperature, pressure, and relative humidity sensors. We also looked at alternate sources (the openweathermap web service) to obtain the temperature, relative humidity, and pressure values to compensate for the non-availability of any environment sensor on the phone.

In the next chapter, we will learn how to use light and proximity sensors, and we will look at how they can be used in real-world applications.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Sensor Programming By Example
Published in: Apr 2016Publisher: PacktISBN-13: 9781785285509
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 €14.99/month. Cancel anytime

Author (1)

author image
Varun Nagpal

Varun Nagpal has been developing mobile apps since 2005 and has developed and contributed to more than 100 professional apps and games on various platforms, such as Android, iOS, Blackberry, and J2ME. Android app development has been his main area of expertise, and he has developed apps for a wide variety of Android devices, such as Android phones, tablets, watches, smart TVs, Android Auto, and Google Glass. He moved to Chicago in late 2013, and since then, he has become a seasoned mobile architect. He has worked in different roles (mobile architect, technical lead, senior developer, and technical consultant) for a variety of various global clients (Allstate, Verizon, AT&T, Sydbank Denmark, SiS Taiwan, Chams PLC Nigeria, and Nandos South Africa) in order to implement their mobile solutions. He has SCJP (Core Java) and SCWD (JSP and Servlets) certifications from Sun Microsystems and MCP (C#) and MCTS (ASP.NET) certifications from Microsoft. You can find his blogs on mobile technology and white papers written by him on his website at http://www.varunnagpal.com/. When he's not working, Varun can be found meditating or playing the flute. He also loves to develop meditation apps and fun games in his free time. He has developed about 40 meditation apps and games available on Google Play (https://play.google.com/store/apps/developer?id=Creative.Software.Studio) and the Apple App Store (https://itunes.apple.com/us/artist/creative-software-studio/id574745824) under the name of Creative Software Studio, his part-time start-up company (http://creativesoftwarestudio.com/).
Read more about Varun Nagpal