The UPM library

The UPM (https://github.com/intel-iot-devkit/upm) library also comes with the IoT Developer Kit image and acts as a repository for sensors using the MRAA library. With the MRAA library, we were able to read data from the sensor connected to the Galileo analog pin, but the extracted data by itself isn't useful for us. UPM brings a module for Grove sensors, making it possible for us to extract the values that can make sense to us, such as lux values.

While MRAA provides us with low-level methods to read and control the I/O pins, this library makes the development using sensors easier. It supports a list of sensors (http://iotdk.intel.com/docs/master/upm/modules.html), making many useful methods available to facilitate controlling or extracting data from them.

Using the circuit that we just used to test MRAA and the Grove sensors UPM library (libupm-grove), let's see how we can use the UPM library to obtain lux units from the Light sensors. Inside the chapter5 folder, create a new file and edit it by typing viupmTest.js in the Galileo SSH terminal. Copy and paste the following example (https://github.com/intel-iot-devkit/upm/blob/master/examples/javascript/grovelight.js) and let's take a look at it:

// Load JavaScript UPM Grove module
var groveSensor = require('jsupm_grove'),

// Create the light sensor object using Analog IO pin 0
var light = new groveSensor.GroveLight(0);
	
// Read the input and print the raw value and a rough lux value
function readLightSensorValue() {
    console.log(light.name() + " raw value is " + light.raw_value() +
            ", which is roughly " + light.value() + " lux");
}
// Repeat the readLightSensorValue method every second
setInterval(readLightSensorValue, 1000);

To have a list of all the sensors available and find the right library to use, we'll have to check the Node.js UPM library documentation, which is available at http://iotdk.intel.com/docs/master/upm/node/. In the APIs section, you'll find listed all the supported sensors. Expanding the other tab, you'll find listed the grove module. This module contains the Grove sensors APIs documentation. Clicking on GroveLight will display the documentation for the sensor we are using.

To be able to use this library, we need to load the grove module first by requiring it with require('jsupm_grove'). With the module loaded, we need to create a new Grove sensor that has the subtype GroveLight and using the pin A0. Now, we can use the sensor methods:

  • name: This returns the sensor name
  • raw_value: This returns the pin's raw read value
  • value: This returns the read value converted in to lux units

Creating a read interval of 1 second, we'll be printing the name, the raw read value, and the correspondent lux value in loop.

Leave the insertion mode by pressing the Esc key. Save the script and leave the editor by typing :wq, followed by Enter. Run the script on Galileo by typing node upmTest.js and you should now see something similar to this printed in the terminal:

Light Sensor raw value is 209, which is roughly 2 lux

Stop the process by pressing Ctrl + C (or cmd + C).

Like MRAA, you can also use this library in other programming languages:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.181.52