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:
First, we define the relay pin object:
var relay_pin = new mraa.Gpio(7); relay_pin.dir(mraa.DIR_OUT);
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); });
In this code, we determine the state of the relay from the query. Then, we will write...