The MRAA library

Galileo IoT Developer Kit image brings a very useful library named MRAA (https://github.com/intel-iot-devkit/mraa). It is a low-level skeleton for the I/O communication, helping you stay away from the GPIOs direct manipulation, which can be harmful if you don't know what you are doing. Like the Arduino methods to control or read from the board pins, this library offers similar methods, comprising the following submodules:

Let's test this library by reading an analog light sensor. Grab your Grove sensors kit and connect the light sensor to the Base Shield V2 A0 connector using the 26AWG Grove cable and attach it to your Galileo board expansion header.

The MRAA library

In the Base Shield, you'll find a switch, allowing you to use the shield in 3.3 V or 5 V mode. For this example, make sure it is set to 5 V.

Coming back to the Galileo SSH terminal, let's now create a new folder named chapter5 to accommodate our test project files. To do so, execute the mkdir chapter5 command. Enter the directory by typing cd chapter5. Since everything in Linux is a file, you'll be able to get a sneak peek into the A0 pin input value by typing cat./sys/bus/iio/devices/iio:device0/in_voltage0_raw. The outputted value should be in the range of 0 to 4095 (12 bit resolution). Although both Intel® Galileo boards provide 12 bit Analog-to-Digital Converter (ADC) resolution on the analog pins, when using the MRAA library, the default ADC resolution will be of 10 bits, allowing us to read values in the range of 0 to 1023.

Let's take a look at how we can use this library to read from the light sensor using different development languages.

Node.js

In your Galileo SSH session, create and edit a new file using the vi editor by typing vi mraaTest.js. Press I to enter the insertion mode and copy and paste the following Node.js script:

// Import the library
var m = require('mraa'),
console.log('MRAA Version: ' + m.getVersion());

// Export pin A0
var analogPin = new m.Aio(0);

// Read the analog pin raw value
function readValue() {
  var value = analogPin.read();
  console.log(value);
}

// Keep reading from the sensor every second (1000 milliseconds)
setInterval(readValue, 1000);

To be able to use the MRAA library in your code, the first thing you need to do is to load it and assign it to a variable by using var m = require('mraa'). After exporting the pin A0 with new Aio(0), we are able to start reading the sensor values. Using the setInterval method, we are able to keep reading from the sensor by calling the readValue method every second. This method reads the input value from the analog pin. You can leave the insertion mode by pressing the Esc key. Save the file and leave the editor by typing :wq, followed by Enter.

Now, let's run the script we just created by executing the node mraaTest.js command in the Galileo SSH session. In your terminal, you'll see the raw read values being printed. To stop the script, press Ctrl + C (or cmd + C if you are using an Apple keyboard).

The documentation for the MRAA Node.js API can be found at http://iotdk.intel.com/docs/master/mraa/node/modules/mraa.html, and taking a look at the Aio section, you'll find the setBit method, which will allow you to change the ADC bit resolution. If you wish to have more sensibility while reading data from your sensor, you can change the ADC resolution to use 12 bits by adding the line analogPin.setBit(12), after exporting the pin A0. If you run the script again with the same light conditions, you'll see a considerable change in the read values.

Python

Now, let's try doing the same using Python. In your shell, type vi mraaTest.py. Type I to enter insert mode and copy and paste the following Python code:

# Import the MRAA and time libraries
import time
import mraa

print (mraa.getVersion())
# Export the A0 pin   
x = mraa.Aio(0)

# Keep reading from sensor every second
while 1:
    print (x.read())
    time.sleep(1)

Using import mraa, we are able to load the library in our code. To export the analog pin A0, we need to create a new Aio(0) object. Having the pin exported, we can read its value by calling the read method. Using the time library, we can keep reading in loop for every 1 second.

Leave the insertion mode by pressing the Esc key. Save the script and leave the editor by typing :wq, followed by Enter. Now, let's run it by executing the python mraaTest.py. command, and you'll have the raw read values printed in the terminal. Changing the incident light will also change the outputted values. To exit the process, press Ctrl + C (or cmd + C).

Note

You can find the MRAA Python API documentation at http://iotdk.intel.com/docs/master/mraa/python/.

C++

If you prefer, you can just use the C++ library. In your shell, type vi mraaTest.cpp. Press I to enter the insert mode and copy and paste the following code (https://github.com/intel-iot-devkit/mraa/blob/master/examples/c%2B%2B/Blink-IO.cpp):

// Import the MRAA library
#include "mraa.hpp"

int main () {
  // Declare vars
  uint16_t adc_value;
  mraa::Aio* a0;

  // Export pin A0
  a0 = new mraa::Aio(0);

  // Keep reading from sensor every second
  while( 1 ) {
    adc_value = a0->read();
    fprintf(stdout, "%d
", adc_value);
    sleep(1);
  }
}

Similar to the previous examples, you'll also have to import the MRAA library to your code and then export the analog pin by creating a new analog pin object on calling new mraa::Aio(0). The read method will allow you to obtain the sensor data and when it is used in a while(1) loop together with the sleep method, it allows us to keep reading the sensor data every second.

Leave the insertion mode by pressing the Esc key. Save the script and leave the editor by typing :wq, followed by Enter. Using C++ , you'll need to compile your code by typing g++ -std=c++0x mraaTest.cpp -o mytest –lmraa in to the Galileo terminal. When it finishes compiling, you can run it by executing ./mytest.

To exit the process, press Ctrl + C (or cmd + C).

Note

As you were able to see, this library provides a great level of abstraction, making it simpler to manipulate the Linux GPIOs. If you wish to have a look at more examples, you can find them at https://github.com/intel-iot-devkit/mraa/blob/master/examples.

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

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