Control a LED from anywhere in the world

For the first project of this chapter, we are simply going to learn how to control a simple LED from a cloud dashboard.

For this project, you will need a LED and a 330 Ohm resistor. For the connection of the components to the Pi, you can refer to Chapter 4, Control Appliances from the Raspberry Pi Zero of this book, in which you will learn how to connect those components. You need to connect the LED to GPIO14 of the Pi.

This is the final result:

Control a LED from anywhere in the world

Let's now see how to configure the board, so we can control it from the cloud. To do so, we'll use the aREST framework that we have already used several times in this book. The following is the complete code for this part:

// Required modules
var express = require('express');
var app = express();
var piREST = require('pi-arest')(app);

// Thing name
piREST.set_id('98t52d');
piREST.set_name('pi_zero_cloud');
piREST.set_mode('bcm');

// Connect to cloud.aREST.io
piREST.connect();

// Start server
var server = app.listen(80, function() {
    console.log('Listening on port %d', server.address().port);
});

Of course, make sure that you modify the ID of the board inside the code, as it will identify your board on the aREST cloud server.

Then, either put this code inside a file or get the whole code from the GitHub repository of the book.

Then, inside a terminal, type:

sudo npm install pi-arest express

This will install the required modules for this section. Then, start the software using:

sudo node arest_control.js

You can then go to the following website and register:

http://dashboard.arest.io/

We'll basically use this site to build a cloud dashboard to control our LED. Once you create an account, create a new dashboard:

Control a LED from anywhere in the world

Inside this dashboard, create a new element by giving the ID of your Raspberry Pi that you set in the code earlier. I chose a 'Push' button as the type of the element and 14 as the pin to control.

Once you have created the element, this is what you should be able to see on the dashboard:

Control a LED from anywhere in the world

You can now test the push button of the dashboard: when you keep the button pressed ON, you should immediately be able to see the LED turning ON on your Pi. Note that this is purely done in the cloud, so you can now control your LED from anywhere in the world!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset