Using Libraries in Your IoT Project

Updated on 23-Nov-2016

Functions and Libraries

Functions are blocks of code that focus on specific tasks. There are two main types of functions: those packaged in a library and those you define in your code. Programming languages such as C++, Python*, Java*, and Node.js* provide built-in functions that you can call at any time. Functions go by different names in different programming languages, such as method, subroutine, and procedure.

Libraries are collections of functions. For example, sqrt(x) is a mathematical C++ built-in function within the math.h C++ library; it facilitates the computation of the square root of a given number. Such a library includes multiple functions, such as logarithm (log10) and exponentials (exp). For Internet of Things (IoT)–related projects, two main libraries are particularly useful: Libmraa* (MRAA) and Useful Packages & Modules (UPM). When working with hardware, you need tools to communicate with the various parts of your board and its connected sensors. These libraries come with definitions for the various sensors and are cross compatible with multiple boards.

Libmraa*

If you want to use Libmraa*, you must first install it on your Intel® Galileo or Intel® Edison board. The library enables you to interact with the board through the functions the library provides. Libmraa has defined configurations for each board and its components. It provides an application programming interface (API) for interfacing with low-level peripherals, such as general-purpose input/output (GPIO), pulse width modulation (PWM), Bluetooth* low energy, interintegrated circuit (I2C), serial peripheral interface (SPI), and Universal Asynchronous Receiver/Transmitter (UART). The physical pins of chips and sensors map to Libmraa. You don’t need to know the details of how communication between the various components happens: The library takes care of which board and breakout pins are connected. Figure 1 illustrates the pins located on the Edison board, and Table 1 reflects a small subset of pins. You can see that the I2C pin is physical pin J17-pin 8, which is mapped in MRAA to 7; also, PWM is shown as physical pins J18-pin 7, with MRAA mapping to pin 20. This way, when you’re programming an MRAA pin, it will communicate with its mapped physical pin on the board. You can see the full breakout board and pins in the paper Intel® Edison Breakout Board.

Figure 1. The Intel® Edison board, with a subset of its pins

Table 1. Subset of pins on the Intel® Edison board

Within Libmraa are multiple API classes, each with numerous functions that you can use for your IoT projects. Here is a small list of those functions:

GPIO class. GPIO interface to Libmraa; functions include:

  • mraa_gpio_init_raw function
  • mraa_gpio_context
  • mraa_gpio_dir

I2C class. I2C to Libmraa; functions include:

  • mraa_i2c_init
  • mraa_i2c_read

AIO class. Analog input/output (AIO) interface to Libmraa; functions include:

  • mraa_aio_init
  • mraa_aio_set_bit

PWM class. PWM interface to Libmraa; functions include:

  • mraa_pwm_init_raw
  • mraa_pwm_period_ms

SPI class. SPI to Libmraa; functions include:

  • mraa_spi_write
  • mraa_spi_transfer_buf
  • mraa_spi_bit_per_word

UART class. UART to Libmraa; functions include:

  • mraa_uart_set_baudrate
  • mraa_uart_set_flowcontrol
  • mraa_uart_set_timeout

COMMON class. Defines the basic shared values for Libmraa; functions include:

  • mraa_adc_raw_bits
  • mraa_get_i2c_bus_count
  • mraa_get_platform_type

For a full list and description of functions that each class supports, see mraa documentation.

To use all the functionality that MRAA provides, you must include mraa.h in your code. Figure 2 shows a C example for connecting an LED to D5 on your board and using GPIO functions. The outcome is a blinking LED; the sleep function controls how often the LED will be in the On and Off states.

Figure 2. C example using MRAA and general-purpose input/output

Useful Packages & Modules

UPM provides software drivers for a variety of commonly used sensors and actuator. Drivers enable the hardware to communicate with the operating system. UPM is a level above MRAA and assists with object control of elements such as RGB LCDs and temperature sensors. The list of supported sensors is vast and includes accelerometers, buttons, color sensors, gas sensor, global positioning system (GPS), radio-frequency identification, and servos. See the full list.

Figure 3 illustrates how you can use the UPM library to read the input of a button. First, you need to include the library grove.hpp, which allows you to use the upm::GroveButton function to create an object for the button and later use the name () and value () functions to obtain the desired values from the sensors. See additional UPM examples.

Figure 3. Useful Packages & Modules example, with definitions for functions and libraries

Integrated development environments (IDEs) such as Eclipse* and Intel® XDK IoT Edition come with MRAA and UPM as integrated libraries. The supported IDEs are available for download from Intel® IoT Developer Kit Integrated Development Environments. When using Eclipse, you can choose to create a new IoT project that will include a couple of tools not available in other types of projects (Figure 4).

Figure 4. Creating a new Internet of Things project in Eclipse*

The IoT Sensor Support tool, shown in Figure 5, contains the list of sensors and actuators, with a description and picture of the hardware. Simply select all the hardware you will include in your project, and the tool automatically adds the related libraries to your code. For example, if you will be using the MMA7455 accelerometer, the tool will append to your current .ccp code with #include <mma7455.h>.

Figure 5. Internet of Things sensors and libraries included in Eclipse*

Libmraa and UPM work hand-in-hand to provide the tools you need to interact easily with the elements in your project. They remove the hassle of knowing the pins and requirements of each piece of hardware, packaging all the specifics into friendly, easy-to-use libraries. From prototyping an automatic pet feeder to creating a wearable that uses GPS, an accelerometer, and an RGB LCD to track your movements, you can develop your projects with the help of MRAA and UPM.
Both libraries are open source projects, with dedicated GitHub* repositories (see the upm and mraarepositories).

Developers are welcome to assist with enhancing and writing new APIs and functions, and reviewing documentation.

Contributors are required to follow a coding and documentation style. Thanks to the open source community’s efforts, new functions and libraries are added frequently for various coding languages.

For more such intel IoT resources and tools from Intel, please visit the Intel® Developer Zone

Source:https://software.intel.com/en-us/articles/using-libraries-in-your-iot-project

Connect On :