Getting Started with MRAA on the Raspberry Pi
The Raspberry Pi (1) is one of the more popular low-cost computers on the market. It has an ARM CPU, hardware video acceleration, RAM, SD card mass storage, and almost everything that a regular desktop computer has to offer at a fraction of the size and cost.
It also has a set of GPIO (General Purpose Input-Output) pins which permit you to connect sensors to it to perform tasks such as measuring the temperature or checking whether plants need to be given more water.
MRAA (2) is a library from Intel that simplifies the logic for connecting to different sensor pins. It allows you to program in C++, Python, JavaScript or Java. It’s also portable: you can run the same code on a BeagleBone Black, Intel Edison or Intel Galileo.
This is a HOW TO guide for building MRAA on the Raspberry Pi.
Getting Started
Hardware
You’ll need a Raspberry Pi 2 or 3. The original Raspberry Pi or even the $5 Raspberry Pi Zero should work, but may be a bit slow. I used a Raspberry Pi 2 Model B.
You’ll need a micro SD card with at least 8 GB of space:
Since I was also installing a Raspberry Pi image, and my computer has a regular SD slot, I also needed a micro SD to SD adapter:
A breadboard:
An LED:
A resistor, preferably between 270 and 330 ohms. I only had a 390 ohm resistor on hand, but the example worked perfectly fine with it:
2 male/female jumper cables:
Downloads
If you haven’t installed an operating system on your Raspberry Pi, you’ll need to download and install one. We assume that you’ll be running Raspbian Linux, the preferred operating system for the Raspberry Pi. You can use other platforms but the instructions will be slightly different.
Download Raspbian from the Internet at https://www.raspberrypi.org/software/(3). I downloaded the full Raspbian Jessie desktop image.
Installing the Operating System
Install the Raspberry Pi image onto the micro SD card
If you’ve already downloaded and installed a Raspberry Pi operating system, you can skip this section.
I did the following on an Ubuntu Linux 14.04 desktop. First, I ran df to show which drives are mounted:
Filesystem 1K-blocks Used Available Use% Mounted on udev 2983428 4 2983424 1% /dev tmpfs 598824 1568 597256 1% /run /dev/sda5 94587748 57331672 32428172 64% / none 4 0 4 0% /sys/fs/cgroup none 5120 0 5120 0% /run/lock none 2994100 80 2994020 1% /run/shm none 102400 56 102344 1% /run/user /dev/sda3 137726972 130667304 7059668 95% /media/zion/90002F7D002F6A04
I inserted the micro SD card. I ran df again and saw which partitions were mounted from the micro SD card. The following line was added to the results above:
/dev/mmcblk0p1 61384 20312 41072 34% /media/zion/boot
I then unmounted that partition:
umount /dev/mmcblk0p1
Note that if your SD card has any other partitions, you’ll need to unmount them also. On my machine, had that partition existed, it would have probably been called /dev/mmcblk0p2. Then I wrote the downloaded image to the SD card:
sudo dd bs=4M if=2016-03-18-raspbian-jessie.img of=/dev/mmcblk0
Note how any suffixes p1, p2, etc. are removed where running dd. We want to write to the whole SD card, not just a partition. This will probably take at least five minutes. You won’t have any progress indication while it’s running. When it’s done, you’ll see a result that looks like the following:
961+1 records in 961+1 records out 4033871872 bytes (4.0 GB) copied, 535.432 s, 7.5 MB/s
You can now eject the SD card from the computer.
If you are using an operating system other than Linux, you can find instructions on how to write to your micro SD card here: https://www.raspberrypi.org/documentation/installation/installing-image…;(4).
Take the micro SD card you just wrote and put it in the slot on the back of your Raspberry Pi. Connect a keyboard and mouse. Connect an Ethernet cable to your network and connect an HDMI cable to your monitor. Now connect the micro USB cable from your power supply and your Raspberry Pi will boot up!
If this is the first time running your Raspberry Pi, please go to the Menu and choose Preferences->Raspberry Pi Configuration and go to the localization tab. Otherwise, you might not have the correct keyboard or date/time settings.
Setting up the Raspberry Pi for MRAA
Make sure that you have enough free space on your Raspberry Pi
Open the Raspberry Pi terminal. Run df. If you see that your /dev/root drive (mounted on /) has used more than 90% of its available space, you might not have enough hard drive space left to install what you need. Unfortunately, the image by default fills only 4 GB of your card. There’s a super easy tool built to fix this.
Run sudo raspi-config. (Read more about this utility here: http://elinux.org/RPi_raspi-config (5) or here https://www.raspberrypi.org/documentation/configuration/raspi-config.md (6)). The first option "1 Expand Filesystem" should be selected by default. Hit return. After it’s done, use the arrow keys to navigate to Finish. It should then ask you to reboot -- which you should do. When the Raspberry Pi reboots, run df again and verify that the hard drive now has much more available space.
Setting Up Libraries and Settings
Now let’s edit the global profile file so that the system will be able to find the JavaScript and Python bindings to MRAA. Open /etc/profile using your favorite text editor (I use nano):
sudo nano /etc/profile
Add the following code right above “export path”:
NODE_PATH="/usr/local/lib/node_modules" PYTHONPATH="/usr/local/lib/python2.7/site-packages" JAVA_HOME="/usr/lib/jvm/default-java" export NODE_PATH export PYTHONPATH export JAVA_HOME
Now we need to update the Raspberry Pi’s libraries. Otherwise, we won’t be able to install nodejs-dev, a module we use for Node JS programming with MRAA.
sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade
Reboot again. We can now install some packages needed by MRAA:
sudo apt-get install git build-essential nodejs-dev python-dev cmake automake Libpcre3 libpcre3-dev Byacc flex
Now let’s install Node JS. I used the version for the Raspberry Pi 2 ARM 7 CPU. I installed it in ~/Documents/IoT:
mkdir ~/Documents/IoT cd ~/Documents/IoT wget https://nodejs.org/dist/latest-v4.x/node-v4.4.1-linux-armv7l.tar.gz tar -xvf node-v4.4.1-linux-armv7l.tar.gz cd node-v4.4.1-linux-armv7l/ sudo cp -R * /usr/local/
If the NodeJS link above changed, just go to https://nodejs.org/dist/latest-v4.x (7) and download the latest Node JS hosted binary.
Feel free to delete the node tar.gz file now.
For those who want to program MRAA examples in Java, the Raspbian image I used came with Java 8 installed, but unfortunately it didn’t work. I installed Java 7 in order to get it to work:
sudo apt-get install default-jre default-jdk
Installing MRAA
First we need to install the SWIG project. SWIG lets us program in Node JS, Python, or Java and call C++ sensor libraries. Go to the folder where you keep all your code. I did this also in Documents/IoT.
cd ~/Documents/IoT
Then I downloaded the SWIG code (8):
git clone https://github.com/swig/swig.git
Note that MRAA requires a minimum SWIG version of 3.0.5. An older version of swig (3.0.2) exists in the Linux package tool library called swig3.0. Don’t be fooled by it! You should install SWIG either from git (as I did) or by downloading a newer version.
Go to your swig directory and run the following:
cd swig ./autogen.sh (this creates the configure script) ./configure make sudo make install
Next we need to install MRAA (9):
cd ~/Documents/IoT git clone https://github.com/intel-iot-devkit/mraa.git cd mraa mkdir build cd build
To run cmake without Java bindings:
cmake ..
To include Java in the resulting Makefile when running cmake:
cmake -DBUILDSWIGJAVA=ON ..
For more information on MRAA build options, see http://iotdk.intel.com/docs/master/mraa/building.html (10). Finally, we build and install it:
make sudo make install
Now MRAA is installed! We can now go on to using the MRAA libraries on the Raspberry Pi to connect sensors to it.
Running an Example
We’ve come so far! Let’s run an example which will show how integrating these various bindings into other languages can make life easier. We’ll do this with an example in JavaScript using Node JS and the MRAA JavaScript bindings.
First of all, turn off your Raspberry Pi before connecting it to the breadboard. Then, let’s connect an LED to the Raspberry Pi using a breadboard and some male/female jumper wires. I’ve based this example off of this blog post: https://thepihut.com/blogs/raspberry-pi-tutorials/27968772-turning-on-a… (11).
In our example, we’ll call the physical pin number directly.
Note that the example uses the Broadcom numbering for accessing the pins. Here’s a chart showing the Broadcom numbering if you want to know more:
https://learn.sparkfun.com/tutorials/raspberry-gpio/gpio-pinout (12).
The chart also lists a WiringPi numbering scheme. If you want to know more about WiringPi numbering: "WiringPi is a GPIO access library written in C for the BCM2835 used in the Raspberry Pi" (13), according to WiringPi.com. If you want to use it, you should check out MRAA’s built-in wiringPi enum mraa_raspberry_wiring_t (14). The WiringPi numbering scheme has changed over time. You should adapt types.h to match your Pi. Look for “RPi B V2” in your api/mraa/types.h and delete the lines that don’t apply. For me, I deleted all the lines marked "RPi B V2."
Back to our example. We’ll need a resistor. Thepihut.com recommended a 330 ohm resistor, while other sites recommended a resistor between 270 and 330 ohms. If you use a resistor with too high a value, the LED may be too dim. I used a 390 ohm resistor, and the LED lit up fine. Note, that if you use no resistor or one with too small a resistance value, you could burn out the LED or your Raspberry Pi. If you want to know more, thePiHut does a great job explaining (11) why that happens.
Connect a ground pin on the Raspberry Pi (we did pin #6) to the negative strip on your breadboard. That’s the red wire in the photo. Connect 1 end of the resistor to the negative strip as well, and then the other end to 3c on the breadboard. Connect the short end of the LED to 3e, and the long end to 4e. Finally connect the green jumper cable from 4a on the breadboard to pin 12 (GPIO) on the Raspberry Pi.
Now your circuit is built and the LED is ready to run! Turn on your Raspberry Pi. In the MRAA directory, there’s a file examples/javascript/GPIO_DigitalWrite.js (15). Let’s modify it to turn on the LED we just attached.
cd ~/Documents/IoT/mraa nano examples/javascript/GPIO_DigitalWrite.js
Ignore the comments on the top of the file.
var m = require('mraa'); //require mraa console.log('MRAA Version: ' + m.getVersion()); //write the mraa version to the console var myDigitalPin = new m.Gpio(12); //setup digital read on pin 12 myDigitalPin.dir(m.DIR_OUT); //set the gpio direction to output myDigitalPin.write(1); //set the digital pin to high (1) setTimeout(function() { myDigitalPin.write(0); //set the digital pin to low (0) process.exit(0); }, 1000);
Note the changes in bold. We set pin 12 to high, then set it to low after 1 second. If you don’t set GPIO pin 12 to low before quitting, the LED will just stay on. Hit Control-X to save, then run it!
node examples/javascript/GPIO_DigitalWrite.js
You might need to run it twice the first time to get it to work. Now you have a programmable LED!
Summary
Our goal was to get MRAA up and running on a Raspberry Pi.
We started by installing an operating system on a Raspberry Pi. We then made sure that the Pi had sufficient hard drive space to handle more code. Then we made it ready for Node JS, Python, and even Java bindings for MRAA. We installed the latest version of the SWIG project -- which creates the bindings between the MRAA code and Node JS, Python, and Java. Then we installed MRAA. Finally we connected an LED to one of the Pi’s GPIO pins and turned it on using MRAA.
Possible Next Steps
There are also a whole lot of sensors on the market that are compatible with MRAA. Maybe you can get one and try a test drive with the Raspberry Pi!
You could also buy a bridge for the Raspberry Pi to use Grove sensors (see http://www.dexterindustries.com/shop/grovepi-board (16) for example). UPM (17), Intel’s repository for sensor drivers built on top of MRAA, has drivers for a boatload of sensors that are built for the Grove connector. You could compile UPM and connect a sensor with a UPM driver (18) to the Raspberry Pi to Grove connector bridge.
References
- Raspberry Pi Project, website, last accessed 28 Mar 2016, http://raspberrypi.org
- MRAA Doxygen documentation, Intel IoT development kit website, last accessed 28 Mar 2016, http://iotdk.intel.com/docs/master/mraa/index.html
- Raspbian download page, Raspberry Pi Foundation website, last accessed 28 Mar 2016, www.raspberrypi.org/software/
- Installing operating system images, Raspberry Pi Foundation website, last accessed 28 Mar 2016, https://www.raspberrypi.org/documentation/installation/installing-images/
- RPi raspi-config page, Embedded Linux Wiki, last accessed 28 Mar 2016, http://elinux.org/RPi_raspi-config
- RPi raspi-config page, Raspberry Pi Foundation website, last accessed 28 Mar 2016, https://www.raspberrypi.org/documentation/configuration/raspi-config.md
- Node JS hosted Linux binaries, Node.js Foundation, last accessed 28 Mar 2016, https://nodejs.org/dist/latest-v4.x
- Github repository for SWIG (Simplified Wrapper and Interface Generator), by the SWIG Project, last accessed 28 Mar 2016, https://github.com/swig/swig.git
- Github repository for MRAA, by the Intel Corporation, last accessed 28 Mar 2016, https://github.com/intel-iot-devkit/mraa.git
- Building libmraa, Intel IoT development kit website, last accessed 28 Mar 2016, http://iotdk.intel.com/docs/master/mraa/building.html
- Turning on an LED with your Raspberry Pi's GPIO Pins, by The Pi Hut, last accessed 28 Mar 2016, https://thepihut.com/blogs/raspberry-pi-tutorials/27968772-turning-on-an-led-with-your-raspberry-pis-gpio-pins
- Raspberry gPIo, by Sparkfun users Jimb0 and MTaylor, last accessed 28 Mar 2016, https://learn.sparkfun.com/tutorials/raspberry-gpio/gpio-pinout
- WiringPi home page, by Gordon Henderson, http://wiringpi.com
- Types.h, source code from MRAA github repository, Intel Corporation’s Intel iot-devkit project, last accessed 28 Mar 2016, https://github.com/intel-iot-devkit/mraa/blob/master/api/mraa/types.h
- GPIO_DigitalWrite.js, example code from MRAA github repository, Intel Corporation’s Intel iot-devkit project, last accessed 28 Mar 2016, https://github.com/intel-iot-devkit/mraa/blob/master/examples/javascript/GPIO_DigitalWrite.js
- GrovePi+ Board page, Dexter Industries, last accessed 28 Mar 2016, http://www.dexterindustries.com/shop/grovepi-board
- UPM Doxygen documentation, Intel IoT development kit website, last accessed 30 Mar 2016, http://iotdk.intel.com/docs/master/upm/index.html
- Github repository for UPM, by the Intel Corporation, last accessed 30 Mar 2016, https://github.com/intel-iot-devkit/upm