Book Image

Intel Galileo Blueprints

By : Marco Schwartz
Book Image

Intel Galileo Blueprints

By: Marco Schwartz

Overview of this book

Table of Contents (19 chapters)
Intel Galileo Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Setting Up the Galileo Board and the Development Environment
Index

Building an interface to control the relay


The next thing that we need to do is to build an interface to control the relay.

The On and Off buttons will serve as our trigger controls.

Just a note: the code that we will use here is similar to the code that we used in Chapter 5, Interacting with Web APIs. Nevertheless, we will still go over the code that we will be using:

  1. First, we define the relay pin object:

    var relay_pin = new mraa.Gpio(7);
    relay_pin.dir(mraa.DIR_OUT);
  2. Then, we will use the same API we built in the previous chapter, and add functionalities to it. Create a new API call for the relay using the following code:

    app.get('/api/relay', function(req,res){
        
      // Get desired state    
      var state = req.query.state;
      console.log(state);
        
      // Apply state
      relay_pin.write(parseInt(state));       
        
      // Send answer
      json_answer = {};
      json_answer.message = "OK";
      res.json(json_answer);
    });
  3. In this code, we determine the state of the relay from the query. Then, we will write...