How To Add a RESTful API to an Intel Edison and Work With It

This is the third part of a three part series that explores the Intel Edison compute module as the centerpiece of an IoT prototype (or even as part of a production product given the Edison’s size as a potential production component). The first article discussed why and how to use the Intel Edison for an IoT prototype. In the second article, we listed seven public APIs to consider using when developing your first Edison prototype.

In this third and last segment, you will learn how to control the Intel Edison from a smartphone by developing your own REST API. You can set the general purpose I/O pins (GPIO) and read all the inputs you want too. The Edison is very powerful and it can easily become an HTTP server because it’s Linux and fully customizable.

This article assumes that you have followed Intel’s directions for connecting your computer to an Intel Edison compute module and have updated the Edison with the latest drivers and firmware. Additionally, since you’ll be connecting an LED to the Edison using the GPIOs, it assumes that your Edison is configured with one of the extension boards discussed in the first part of this series.

For this project, we’ll need to need to update the version of Node.js that comes pre-installed in the Yocto-based distribution of Linux that’s already on your Edison. To do this, you must access the Edison’s console as the root user and, at the Linux prompt, enter the command “opkg update Node.js”. Then, within the Edison’s Linux-based directory structure, create a project folder (“mkdir /myapi”) and select it as the active directory (“cd /myapi”). For this project, we need two libraries: one for the GPIO control and the other for the REST API. The first one is mraa and it’s already included by default on the Edison. The second is restify and you should install it by typing the command “npm install restify” at the Linux prompt.

Now everything is installed, let’s begin the code. This section assumes that you already know how to create and save source code files. This can be done with the vi editor that’s built into Yocto Linux, or you can edit the source code files using a code editor on your computer and FTP them into the Edison.

All of our source code will be written in Javascript and will run on the installation of Node.js that you just updated. The first source code file to create is app.js: Here’s the source:

//Load the libraries

var mraa = require('mraa');
var restify = require('restify');

// create a http server
var server = restify.createServer();

// use a body parser, needed for post request with body
server.use(restify.bodyParser());

// Set the route with callback function method GET and POST
server.get('/gpio/:id', getValue);
server.post('/gpio/:id', setValue);

function getValue(req, res, next)
{
// check if all parameters are present
    if(req.params.id === undefined || req.params.value === undefined){
        res.json({error: "missing value"});
    }
    else{
        // connect the mraa lib to the GPIO needed
        var currentPin = new mraa.Gpio(req.params.id);
       
        // set it in input to read data
        currentPin.dir(mraa.DIR_IN);

        // send response with reading the GPIO
        res.json({value: currentPin.read(),id: req.params.id, dir: mraa.DIR_IN});
    }
    next();
}

function setValue(req, res, next)
{
// check if all parameters are present

    if(req.params.id === undefined || req.params.value === undefined){
        res.json({error: "missing value"});
    }
    else{

        // connect the mraa lib to the GPIO needed
        var currentPin = new mraa.Gpio(req.params.id);
       
        // set it in output to write data
        currentPin.dir(mraa.DIR_OUT);
       
        // write the output needed
        currentPin.write(req.params.value);
       
        // send response to client
        res.json({value: req.params.value,id: req.params.id, dir: mraa.DIR_OUT});
    }
    next();
}

Now you have a basic RESTful API to set and get GPIO values from the Intel Edison. To start the API so that it’s listening for instructions, go to the Edison’s Linux prompt and type “node app.js”. For testing, follow the following steps:

  1. Plug an LED between the Edison’s GPIO 13 and the GND (with an appropriate resistor). Intel has published a pretty good tutorial for properly wiring an LED to a GPIO and how to involve the correct resistor.
  2. Next, from another computer on your network, you are going to manually engage the Edison’s RESTful API using the cURL command. This command can be entered at the system prompt from an OS X or Linux based system. For Windows, you must install a cURL utility first. Go to a computer on the same network as the Edison and type the command “ curl –X POST –d “value=1” http://EdisonIP/gpio/13” where “EdisonIP” is replaced with the IP address of the Edison compute module. If you don’t know what that IP address is, enter the command “ip addr show” at the Linux command prompt for the Edison compute module If the LED switch is on it’s perfect. Otherwise check your wire. To switch off, type: “ curl –d “value=0” http://EdisonIP/gpio/13” (again replacing “EdisonIP” with the IP address of the Edison compute module.

For the next step, you need to add these two routes and the associated callback to the app.js source code file:

uart = new mraa.Uart(0)

// initialize the uart
uart.setBaudRate(115200);
uart.setMode(8, 0, 1);
uart.setFlowcontrol(false, false);

server.get('/uart', readUart);
server.post('/uart', writeUart);

function readUart(req, res, next)
{   
    res.json({value: uart.readStr(10), type: "uart"});   
    next();
}

function writeUart(req, res, next)
{
// check if all parameters are present
    if(req.params.value == undifined){
        res.json({error: "missing value"});
    }
    else{
        uart.writeStr(req.param.value);
        res.json({value: req.params.value, type: "uart"});
    }
    next();
}

With this additional code, you can read and write to the Edison’s UART port. For example, type “curl –X POST –d “value=hello” http://EdisonIP/uart ”. If you have something connected to the UART port that can accept text input, you will see the string “Hello”. And, if you have something connected to the UART port that can send information to the Edison (like and RFID reader) you can read on the UART port with the command “curl –X GET http://EdisonIP/uart ”.

Easy! Now it’s your turn to implement something else on your Edison and integrate it in your REST API. Here are some more pointers: :
 

  1. Create the route with a good name, correct HTTP method and the name of your callback.
  2. Create the function named previously in the route and return a result.
  3. Complete your function with the thing it needs to do.
  4. Restart the server and test your new implementation.

For more information and resources about Edison and IoT, check out the Intel IoT Developer Zone and the Intel IoT Dev Kit. Also get support for your IoT projects on the Edison Get-Started Area.

Resources:
Intel Developer Zone
Intel Edison
Intel IoT Dev Kit

Thomas Thous I'm a french engineering student majoring in Embedded Systems who likes to explore and hack.

Comments