If you haven’t heard of it before, don’t worry, I’ll explain everything in a way that even my grandma could understand.
First things first, what MRAA is and why we need it. Basically, MRAA provides a simple API for communicating with microcontrollers over various protocols like SPI, I2C, UART, etc. This means you can easily read data from sensors or control actuators without having to worry about the low-level details of each specific protocol.
Now that we know what MRAA is and why it’s useful how to install it on our beloved operating system Ubuntu Xenial (16.04) or Bionic (18.04). If you’re using a different version of Ubuntu, don’t worry, the process is pretty much the same.
First, we need to add the MRAA repository to our system by running this command:
# This script adds the MRAA repository to the system, allowing for installation of MRAA on Ubuntu Xenial (16.04) or Bionic (18.04).
# First, we need to import the necessary package management tools.
import apt
# Next, we need to add the MRAA repository to our system.
# This command uses the apt-add-repository tool to add the PPA (Personal Package Archive) for MRAA.
# The PPA is maintained by the user "walac" and contains the necessary packages for MRAA.
# The "sudo" command is used to run the following command with root privileges.
# The "apt-add-repository" command adds the PPA to the list of repositories used by apt.
# The "ppa:walac/mraa" argument specifies the PPA to be added.
sudo apt-add-repository ppa:walac/mraa
This will add a new PPA (Personal Package Archive) that contains all the necessary packages for installing MRAA. If you’re not familiar with PPAs, don’t worry, they’re basically just repositories that contain additional software that isn’t included in Ubuntu by default.
Once we have added the PPA, we can update our package list and then install MRAA using these commands:
# This script updates the package list and installs MRAA, a necessary package for installing MRAA.
# First, we need to add the PPA (Personal Package Archive) to our list of repositories.
sudo add-apt-repository ppa:mraa/mraa
# Then, we update our package list to include the new repository.
sudo apt-get update
# Next, we upgrade any existing packages to their latest versions.
sudo apt-get upgrade
# Finally, we install MRAA using the apt-get command.
sudo apt-get install mraa
This will download all the necessary packages from the repository and install them on your system. If you’re not familiar with these commands, don’t worry, they’re pretty straightforward `update` updates our package list, `upgrade` upgrades any existing packages to their latest versions, and finally, we use `install` to actually install MRAA.
And that’s it! You should now have MRAA installed on your system and ready to go. If you want to test it out, you can run this simple example code:
# Import the mraa library, which allows us to interact with hardware components
import mraa
# Create a new SPI device with the specified chip select pin (CS)
# Replace 9 with your actual CS pin number
spi = mraa.Spi(9)
# Set up the SPI configuration
# In this case, we're using mode 0 and a clock speed of 1 MHz
spi.setMode(mraa.SPI_MODE0) # Set the SPI mode to 0
spi.setBaudrate(1000000) # Set the clock speed to 1 MHz
# Test sending data to the microcontroller
# In this example, we're just sending 'hello world'
data = "hello world".encode() # Convert our string into a byte array for sending over SPI
spi.write(data) # Send the data over SPI using the configured settings
This code creates a new SPI device with CS pin 9 and sets up its configuration to use mode 0 (CPOL=0, CPHA=0) and a clock speed of 1 MHz. Then it sends the string “hello world” over SPI using the `write()` method.
And that’s all there is to it! With MRAA installed on your system, you can easily communicate with microcontrollers without having to worry about the low-level details of each specific protocol. So give it a try I promise it won’t hurt (much).