Shares
Control a Relay From Anywhere Using the Raspberry Pi

Control a Relay From Anywhere Using the Raspberry Pi

Last Update: / by Marco Schwartz

If you found this article after doing a search on Google, welcome! On this website you will find plenty of content around DIY home automation using open-source hardware. Enjoy the article!

I have been asked a lot about writing tutorials using the Raspberry Pi for home automation, as well as how to access your home automation systems from anywhere. And this is exactly what I will show you in this tutorial: you are going to learn how you can control a relay that is attached to your Raspberry Pi, from any device like your computer or smartphone, and from anywhere in the world.

In this project, we will connect the relay to a simple LED, but of course this LED could be replaced by anything like a lamp, some lights on the ceiling, or the motor of your electric curtains. And to control this relay from anywhere, we are going to run a small app based on Node.js to control the relay from any web browser. Sounds exciting ? Let’s dive in !

Hardware & Software Requirements

For this tutorial, of course you will need a Raspberry Pi board. The version of the board or the model (A or B) doesn’t really matter, but keep in mind that you will have to connect it to your local network, so you will need a WiFi dongle if you are using the A model which doesn’t have an Ethernet port. In this tutorial, I used a Raspberry Pi model B with the WiPi dongle.

For the components, you will need a small 5V relay, a P2N2222A transistor (but any similar NPN transistor will do the job), a 1N4001 diode, a 1K ohm resistor, a 220 ohm resistorone LED, and of course a breadboard and some jumper wires. To connect the Raspberry Pi to the breadboard, I also used the Adafruit cobbler kit, but any cobbler kit for the Raspberry Pi will work fine.

This is the list of the components that have been used in this tutorial:

On the software side, you will need a fully usable Raspberry Pi. And by usable I mean already configured with the Raspbian Linux distribution installed on it. There are many tutorials on the web that will guide you through the installation of Raspbian on your Pi, but I recommend this one:

http://www.raspberrypi.org/documentation/installation/installing-images/

You also have to check that your Raspberry Pi is connected to the Internet. Again, this will depend on your configuration (Ethernet or WiFi) and your router, but is usually really easy. If you are using the Ethernet connection, simply connect a cable to your router and it should work automatically. If you’re using a WiFi dongle, the easiest solution is to use the GUI that comes with Raspbian to find your wireless network and enter your WEP/WPA password.

The server part is based on Node.js, so you will need to install it on your Pi with:

wget http://node-arm.herokuapp.com/node_latest_armhf.deb
sudo dpkg -i node_latest_armhf.deb

Now, you need to download the GitHub repository of the project somewhere on your Pi:

git clone https://github.com/openhomeautomation/pi-node-relay

Finally, you need to go into the folder you just downloaded, and install the node.js module to interface directly with the GPIO pins of the Pi:

sudo npm install rpi-gpio express

If it doesn’t work, just restart the Pi and try again the same operation. You are now ready to build the hardware!

Hardware Configuration

There is quite a lot of hardware to connect for this project, so pay attention to this paragraph. First, let’s speak about the relay itself. A relay is an electromagnetic switch. The one I used in this project basically has 2 parts. The first part, the coil, is the low-power part of the circuit, and will be controlled by the Raspberry Pi. The second part of the relay is the switch, which can sustain higher powers. This part is actually mechanical on the relay I used, so you should hear a “click” when the relay is switching to another state. Activating the low-power part by applying 5V on the coil will activate the switch and change the state of the relay. To monitor in which state the relay is, I used one LED on one part of the switch. Of course, the LED can be replaced by any device you want to switch on or off, for example a lamp.

But … there is still a problem. The relay is rated at 5V to switch, and the Raspberry Pi GPIO port can only deliver 3.3V. This is why we need a transistor in between to activate the relay. The transistor is basically a solid-state switch, which will be activated via the digital output of the Raspberry Pi board. When the transistor is on, the 5V pin of the Raspberry Pi board will directly power the relay, thus making the relay switch.

We still need to place one component I haven’t spoken about yet: the diode. The role of this diode will be to protect the low-power circuit when the relay is switching. In case any current is flowing through the input of the relay because of the switching of the high-power part, it will just flow through this diode instead of destroying the transistor or the output of the Raspberry Pi. Just place this diode in parallel of the input part of the relay, with the cathode connected to the positive 5V power supply.

This schematic describes the whole project:

Control a Relay From Anywhere Using the Raspberry Pi

Connecting the Relay to The Cloud

Now it’s time to connect our project to the cloud. Again, make sure that the Raspberry Pi is connected to your local network & to the web, for example by trying to ping a web page from a terminal.

What we have to do now is to build the Node.js script that will connect the Raspberry Pi to the aREST.io cloud platform, which is a tool that I created specifically to control boards like the Raspberry Pi remotely. And from this cloud server, we’ll be able to control the relay from your web browser. Here is the complete code for this part:

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

// Set unique ID
piREST.set_id('p5dgwt');
piREST.set_name('pi_cloud');

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

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

Let’s now see the details of this file. It starts by including all the required libraries:

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

Then, we’ll set a unique ID and a name to your board. Make sure to change at least the ID here, so you are sure that only you can access the device:

piREST.set_id('p5dgwt');
piREST.set_name('pi_cloud');

After that, we connect the Raspberry Pi to the cloud server with:

piREST.connect();

Finally, we start the server on the Pi:

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

It’s now time to test the project. To do so, simply go over to the folder where you downloaded the files from the Github repository for this project and type in a terminal:

sudo npm install pi-arest express

After that, type:

sudo node app.js

You should see the following message in your terminal:

Listening on port 3000
Connected to aREST.io

You can now go to any web browser, and type:

http://cloud.arest.io/p5dgwt/digital/7/1

You should immediately hear the relay turning on. You can of course do the same with a 0 at the end to turn the relay off again. Congratulations, you can now command your relay directly from any web browser!

Access The Interface From Anywhere

You can now control the relay from anywhere, but only by typing commands in your web browser. That’s nice, but it would be much better if you could do the same from a web-based dashboard, right. Well, that’s what I did inside the aREST framework, and I created a way to easily create a dashboard for devices connected to aREST.io.

The first step is to visit the aREST dashboard website:

http://dashboard.arest.io/

Create an account there, and then create a new dashboard, for example called Raspberry Pi:

Control a Relay From Anywhere Using the Raspberry Pi

After that, open this dashboard, and you will be able to create a new element:

Control a Relay From Anywhere Using the Raspberry Pi

Here, give a name to your dashboard element, and enter the ID you assigned to the device in the ‘device ID’ input. Also set the element type to ‘Digital’, on pin 7, and with an On/Off switch. This should be the result:

Control a Relay From Anywhere Using the Raspberry Pi 

As you can see, the application automatically detected that your Raspberry Pi was online. You can now try the buttons: it should automatically turn the relay on or off. This dashboard is of course accessible from anywhere on the planet, so you can now control your relay from anywhere using this nice graphical interface!

Update 9/12/15: Many of you had problems with the initial versions of the project which were using a combination of custom HTML & JavaScript code. That’s why I completely re-written the project using my own aREST framework, which made the project much easier to build. You can now build your own graphical interface without any code, and control your Raspberry Pi from anywhere. Enjoy!

How to Go Further

There are many things you can do to improve this project with what you just learnt. You can connect more relays to the project, and command them all from the same interface. You can also install several Raspberry Pi’s in your home to command them all separately. You might also use what you learned in this project to read data from the GPIO pins (for example from a light or motion sensor), and display this data on the web interface we created in the project. Finally, if you built an exciting project based on this tutorial, please share in the comments!

Want to learn more? Get my free eBook about the Raspberry Pi!
The Raspberry Pi is a powerful board that can be used for all sorts of projects. Download today my free eBook "Build a Simple Thermostat with the Raspberry Pi". Simply click on the button below!

Leave a Comment

Please enter your name.
Please enter your email.
Please enter your comment.
Majlos 7 months ago
Hello Marco, thank you for your manual. I am about to purchase RsPI and make some home automation of blinds with 2/3 relay boards (16+8)/(8+8+8) but one think is very confusing for me. In hardware part you are using transistor because GPOI has 3,3V to energize 5V coil. Can you explain me why in some other manuals their are not using it and can energize coil directly ? For example: https://www.youtube.com/watch?v=oaf_zQcrg7g Thanks a lot
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Jack Sheridan 2 years ago
You wrote that you got rid of the self-made html tutorial because it was causing some problems for some people. Any way you could point me in the right direction to do that?Maybe still using you aRest.io, but with some javascript in my own page?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Ian McDaniel 2 years ago
Newbie here, although the whole concept makes a lot of sense to me.I ran into an issue at 'sudo npm install pi-arest express'It kind of looks like something is outdated based on the logs.There are a lot of errors.. so I've attached the log. Any [email protected]:~/pi-node-relay $ sudo npm install pi-arest expressnpm WARN engine [email protected]: wanted: {"node":">= 4"} (current: {"node":"0.10.29","npm":"1.4.21"})npm WARN engine [email protected]: wanted: {"node":">=4.0.0"} (current: {"node":"0.10.29","npm":"1.4.21"})npm WARN engine [email protected]: wanted: {"node":">=4"} (current: {"node":"0.10.29","npm":"1.4.21"})npm WARN engine [email protected]: wanted: {"node":">=0.10.32"} (current: {"node":"0.10.29","npm":"1.4.21"})npm WARN engine [email protected]: wanted: {"node":">= 0.12"} (current: {"node":"0.10.29","npm":"1.4.21"})npm WARN engine [email protected]: wanted: {"node":">=4"} (current: {"node":"0.10.29","npm":"1.4.21"})npm WARN engine [email protected]: wanted: {"node":">=0.10.40"} (current: {"node":"0.10.29","npm":"1.4.21"})npm WARN engine [email protected]: wanted: {"node":">=0.10.40"} (current: {"node":"0.10.29","npm":"1.4.21"})npm WARN engine [email protected]: wanted: {"node":">=0.10.40"} (current: {"node":"0.10.29","npm":"1.4.21"})> [email protected] install /home/pi/node_modules/pi-arest/node_modules/rpio> node-gyp rebuildmake: Entering directory '/home/pi/node_modules/pi-arest/node_modules/rpio/build' CC(target) Release/obj.target/rpio/src/bcm2835.o CXX(target) Release/obj.target/rpio/src/rpio.oIn file included from ../src/rpio.cc:17:0:../node_modules/nan/nan.h:326:47: error: ‘REPLACE_INVALID_UTF8’ is not a member of ‘v8::String’ static const unsigned kReplaceInvalidUtf8 = v8::String::REPLACE_INVALID_UTF8; ^rpio.target.mk:83: recipe for target 'Release/obj.target/rpio/src/rpio.o' failedmake: *** [Release/obj.target/rpio/src/rpio.o] Error 1make: Leaving directory '/home/pi/node_modules/pi-arest/node_modules/rpio/build'gyp ERR! build errorgyp ERR! stack Error: `make` failed with exit code: 2gyp ERR! stack at ChildProcess.onExit (/usr/share/node-gyp/lib/build.js:267:23)gyp ERR! stack at ChildProcess.emit (events.js:98:17)gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:809:12)gyp ERR! System Linux 4.4.11-v7+gyp ERR! command "nodejs" "/usr/bin/node-gyp" "rebuild"gyp ERR! cwd /home/pi/node_modules/pi-arest/node_modules/rpiogyp ERR! node -v v0.10.29gyp ERR! node-gyp -v v0.12.2gyp ERR! not oknpm WARN This failure might be due to the use of legacy binary "node"npm WARN For further explanations, please read/usr/share/doc/nodejs/README.Debiannpm ERR! Error: ENOENT, lstat '/home/pi/node_modules/pi-arest/node_modules/raspicam/node_modules/lodash/functions.js'npm ERR! If you need help, you may report this *entire* log,npm ERR! including the npm and node versions, at:npm ERR! npm ERR! System Linux 4.4.11-v7+npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "pi-arest" "express"npm ERR! cwd /home/pi/pi-node-relaynpm ERR! node -v v0.10.29npm ERR! npm -v 1.4.21npm ERR! path /home/pi/node_modules/pi-arest/node_modules/raspicam/node_modules/lodash/functions.jsnpm ERR! fstream_path /home/pi/node_modules/pi-arest/node_modules/raspicam/node_modules/lodash/functions.jsnpm ERR! fstream_type Filenpm ERR! fstream_class FileWriternpm ERR! code ENOENTnpm ERR! errno 34npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26npm ERR! fstream_stack Object.oncomplete (evalmachine.:107:15)npm ERR! error rolling back Error: ENOTEMPTY, rmdir '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/core-util-is'npm ERR! error rolling back [email protected] { [Error: ENOTEMPTY, rmdir '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/core-util-is']npm ERR! error rolling back errno: 53,npm ERR! error rolling back code: 'ENOTEMPTY',npm ERR! error rolling back path: '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/core-util-is' }npm ERR! [email protected] install: `node-gyp rebuild`npm ERR! Exit status 1npm ERR!npm ERR! Failed at the [email protected] install script.npm ERR! This is most likely a problem with the rpio package,npm ERR! not with npm itself.npm ERR! Tell the author that this fails on your system:npm ERR! node-gyp rebuildnpm ERR! You can get their info via:npm ERR! npm owner ls rpionpm ERR! There is likely additional logging output above.npm ERR! System Linux 4.4.11-v7+npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "pi-arest" "express"npm ERR! cwd /home/pi/pi-node-relaynpm ERR! node -v v0.10.29npm ERR! npm -v 1.4.21npm ERR! code ELIFECYCLEnpm ERR! EEXIST, mkdir '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/util-deprecate'File exists: /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/util-deprecateMove it away, and try again.npm ERR! System Linux 4.4.11-v7+npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "pi-arest" "express"npm ERR! cwd /home/pi/pi-node-relaynpm ERR! node -v v0.10.29npm ERR! npm -v 1.4.21npm ERR! path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/util-deprecatenpm ERR! fstream_path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/util-deprecate/browser.jsnpm ERR! fstream_type Filenpm ERR! fstream_class FileWriternpm ERR! code EEXISTnpm ERR! errno 47npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:171:23npm ERR! fstream_stack /usr/lib/nodejs/mkdirp/index.js:46:53npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)npm ERR! Error: ENOENT, lstat '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/process-nextick-args/.travis.yml'npm ERR! If you need help, you may report this *entire* log,npm ERR! including the npm and node versions, at:npm ERR! npm ERR! System Linux 4.4.11-v7+npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "pi-arest" "express"npm ERR! cwd /home/pi/pi-node-relaynpm ERR! node -v v0.10.29npm ERR! npm -v 1.4.21npm ERR! path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/process-nextick-args/.travis.ymlnpm ERR! fstream_path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/process-nextick-args/.travis.ymlnpm ERR! fstream_type Filenpm ERR! fstream_class FileWriternpm ERR! code ENOENTnpm ERR! errno 34npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26npm ERR! fstream_stack Object.oncomplete (evalmachine.:107:15)npm ERR! EEXIST, mkdir '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/websocket-stream/node_modules/ws/lib'File exists: /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/websocket-stream/node_modules/ws/libMove it away, and try again.npm ERR! System Linux 4.4.11-v7+npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "pi-arest" "express"npm ERR! cwd /home/pi/pi-node-relaynpm ERR! node -v v0.10.29npm ERR! npm -v 1.4.21npm ERR! path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/websocket-stream/node_modules/ws/libnpm ERR! fstream_path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/websocket-stream/node_modules/ws/lib/PerMessageDeflate.jsnpm ERR! fstream_type Filenpm ERR! fstream_class FileWriternpm ERR! code EEXISTnpm ERR! errno 47npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:171:23npm ERR! fstream_stack /usr/lib/nodejs/mkdirp/index.js:46:53npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)npm ERR! Error: ENOENT, lstat '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/string_decoder/LICENSE'npm ERR! If you need help, you may report this *entire* log,npm ERR! including the npm and node versions, at:npm ERR! npm ERR! System Linux 4.4.11-v7+npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "pi-arest" "express"npm ERR! cwd /home/pi/pi-node-relaynpm ERR! node -v v0.10.29npm ERR! npm -v 1.4.21npm ERR! path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/string_decoder/LICENSEnpm ERR! fstream_path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/string_decoder/LICENSEnpm ERR! fstream_type Filenpm ERR! fstream_class FileWriternpm ERR! code ENOENTnpm ERR! errno 34npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26npm ERR! fstream_stack Object.oncomplete (evalmachine.:107:15)npm ERR! Error: ENOENT, chown '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/mqtt-packet/node_modules/bl'npm ERR! If you need help, you may report this *entire* log,npm ERR! including the npm and node versions, at:npm ERR! npm ERR! System Linux 4.4.11-v7+npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "pi-arest" "express"npm ERR! cwd /home/pi/pi-node-relaynpm ERR! node -v v0.10.29npm ERR! npm -v 1.4.21npm ERR! path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/mqtt-packet/node_modules/blnpm ERR! fstream_path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/mqtt-packet/node_modules/blnpm ERR! fstream_type Directorynpm ERR! fstream_class DirWriternpm ERR! fstream_finish_call chownnpm ERR! code ENOENTnpm ERR! errno 34npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:305:19npm ERR! fstream_stack /usr/lib/nodejs/graceful-fs/polyfills.js:143:7npm ERR! fstream_stack Object.oncomplete (evalmachine.:107:15)npm ERR! Error: ENOENT, lstat '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/help-me/node_modules/glob-stream/node_modules/glob/LICENSE'npm ERR! If you need help, you may report this *entire* log,npm ERR! including the npm and node versions, at:npm ERR! npm ERR! System Linux 4.4.11-v7+npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "pi-arest" "express"npm ERR! cwd /home/pi/pi-node-relaynpm ERR! node -v v0.10.29npm ERR! npm -v 1.4.21npm ERR! path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/help-me/node_modules/glob-stream/node_modules/glob/LICENSEnpm ERR! fstream_path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/help-me/node_modules/glob-stream/node_modules/glob/LICENSEnpm ERR! fstream_type Filenpm ERR! fstream_class FileWriternpm ERR! code ENOENTnpm ERR! errno 34npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26npm ERR! fstream_stack Object.oncomplete (evalmachine.:107:15)npm ERR! EEXIST, mkdir '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/help-me/node_modules/glob-stream/node_modules/extend'File exists: /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/help-me/node_modules/glob-stream/node_modules/extendMove it away, and try again.npm ERR! System Linux 4.4.11-v7+npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "pi-arest" "express"npm ERR! cwd /home/pi/pi-node-relaynpm ERR! node -v v0.10.29npm ERR! npm -v 1.4.21npm ERR! path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/help-me/node_modules/glob-stream/node_modules/extendnpm ERR! fstream_path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/help-me/node_modules/glob-stream/node_modules/extend/component.jsonnpm ERR! fstream_type Filenpm ERR! fstream_class FileWriternpm ERR! code EEXISTnpm ERR! errno 47npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:171:23npm ERR! fstream_stack /usr/lib/nodejs/mkdirp/index.js:46:53npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)npm ERR! error rolling back Error: ENOTEMPTY, rmdir '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/safe-buffer'npm ERR! error rolling back [email protected] { [Error: ENOTEMPTY, rmdir '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/safe-buffer']npm ERR! error rolling back errno: 53,npm ERR! error rolling back code: 'ENOTEMPTY',npm ERR! error rolling back path: '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/safe-buffer' }npm ERR! Error: ENOENT, lstat '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/safe-buffer/index.js'npm ERR! If you need help, you may report this *entire* log,npm ERR! including the npm and node versions, at:npm ERR! npm ERR! System Linux 4.4.11-v7+npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "pi-arest" "express"npm ERR! cwd /home/pi/pi-node-relaynpm ERR! node -v v0.10.29npm ERR! npm -v 1.4.21npm ERR! path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/safe-buffer/index.jsnpm ERR! fstream_path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/readable-stream/node_modules/safe-buffer/index.jsnpm ERR! fstream_type Filenpm ERR! fstream_class FileWriternpm ERR! code ENOENTnpm ERR! errno 34npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26npm ERR! fstream_stack Object.oncomplete (evalmachine.:107:15)npm ERR! error rolling back Error: ENOTEMPTY, rmdir '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/websocket-stream/node_modules/duplexify/node_modules/end-of-stream/node_modules/once'npm ERR! error rolling back [email protected] { [Error: ENOTEMPTY, rmdir '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/websocket-stream/node_modules/duplexify/node_modules/end-of-stream/node_modules/once']npm ERR! error rolling back errno: 53,npm ERR! error rolling back code: 'ENOTEMPTY',npm ERR! error rolling back path: '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/websocket-stream/node_modules/duplexify/node_modules/end-of-stream/node_modules/once' }npm ERR! Error: ENOENT, lstat '/home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/websocket-stream/node_modules/duplexify/node_modules/end-of-stream/node_modules/once/once.js'npm ERR! If you need help, you may report this *entire* log,npm ERR! including the npm and node versions, at:npm ERR! npm ERR! System Linux 4.4.11-v7+npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "pi-arest" "express"npm ERR! cwd /home/pi/pi-node-relaynpm ERR! node -v v0.10.29npm ERR! npm -v 1.4.21npm ERR! path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/websocket-stream/node_modules/duplexify/node_modules/end-of-stream/node_modules/once/once.jsnpm ERR! fstream_path /home/pi/node_modules/pi-arest/node_modules/mqtt/node_modules/websocket-stream/node_modules/duplexify/node_modules/end-of-stream/node_modules/once/once.jsnpm ERR! fstream_type Filenpm ERR! fstream_class FileWriternpm ERR! code ENOENTnpm ERR! errno 34npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26npm ERR! fstream_stack Object.oncomplete (evalmachine.:107:15)npm ERR! Error: ENOENT, lstat '/home/pi/node_modules/pi-arest/node_modules/request/node_modules/har-validator/node_modules/ajv/dist/nodent.min.js'npm ERR! If you need help, you may report this *entire* log,npm ERR! including the npm and node versions, at:npm ERR! npm ERR! System Linux 4.4.11-v7+npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "pi-arest" "express"npm ERR! cwd /home/pi/pi-node-relaynpm ERR! node -v v0.10.29npm ERR! npm -v 1.4.21npm ERR! path /home/pi/node_modules/pi-arest/node_modules/request/node_modules/har-validator/node_modules/ajv/dist/nodent.min.jsnpm ERR! fstream_path /home/pi/node_modules/pi-arest/node_modules/request/node_modules/har-validator/node_modules/ajv/dist/nodent.min.jsnpm ERR! fstream_type Filenpm ERR! fstream_class FileWriternpm ERR! code ENOENTnpm ERR! errno 34npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26npm ERR! fstream_stack Object.oncomplete (evalmachine.:107:15)npm ERR!npm ERR! Additional logging details can be found in:npm ERR! /home/pi/pi-node-relay/npm-debug.lognpm ERR! not ok code 0
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Mike Tierney 2 years ago
Thank you for this thread Marcos it is very well written and I cant wait to set this up and control my espresso machine. I have one question to see if it was possible to also control this on a time schedule so I could turn the machine on 30 minuets before I wake up. Anyways cant wait to try this out it will be my first project with the Raspberry PI.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Paolo 3 years ago
hi!Congratulations for the tutorial!It seems fantatic!I'll try it asap!!I have just one question. Could you explain the meaning of "function" in the dashboard? In other words, can I use "function" to run a custom python code?Thanks a lot.Paolo
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Vtelix 3 years ago
Hi .I have two relays connected to PIN 11 and 12 and the plan is to control one of them over cloud. So i installed android app and all parts for RSP. but funny thing happens when i run the code. Before i explain in detail its important to write that there are other parts hooked up to GPIO. Those other parts are 4x4 keypad and two push buttons and they are working OK until i load JS script with aREST.When the JS code starts it look like that all the GPIO's are blocked and i get message " channel already in use".After that i can not access keypad and button. I added GPIO cleanup function in python keypad and button code and this has solved the problem but this isn't good solution because, relay can be triggered any time and when it does, he will again block the GPIO ports.Btw. each HW part is on different GPIO pin.Why do i get such error message when i run JS aREST code and how can i "limit" arest to controll just two predefined GPIO pins ( 11 / 12 in my case ) ?Please be so kind and reply asap.Best regards !GM
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Juan Lugmania 3 years ago
Hi, Please can you help me with this [email protected]:~ $ node app.jsCamera module offLitening on port 3000Connected to aREST.io[Error: Pin has not been exported for write][Error: Pin has not been exported for write][Error: Pin has not been exported for write]
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz Juan Lugmania 3 years ago
Hi! Just run the Node.js file as sudo :)
Deven Swiergiel 3 years ago
Hey Marco,I loved this tutorial!! It was very well explained and easy to follow. I'm a third year computer science major starting to get into robotics and electronics. Anyway the relay I purchased has inverse logic, that is it has High actually == Low. This means that every time I load the dashboard once the Pi is found, it sets the relays to low or 0, but with my relay that actually turns it on. I tried modifying your code to change it but can't seem to find the right functions to change. Im still new to some of this stuff so I was wondering if you could guide me in the right direction!Thanks a ton!!Deven
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz Deven Swiergiel 3 years ago
Hey Deven, you're welcome! Glad to hear you enjoyed the tutorial :)
Chandler 3 years ago
When I type sudo npm install pi-arest express it says /usr/local/bin/node: /usr/lib/arm-linux-gnueabihf/libstdc++.so.6: version 'GLIBCXX_3.4.20' not found (required by /usr/local/bin/node) and /usr/local/bin/node: /lib/arm-linux-gnueabihf/libc.so.6: version 'GLIBC_2.16' not found (required by /usr/local/bin/node)
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Malik Adeel 3 years ago
Hi marco i was follwing the tutorial but the problem is in the second command when i try to run "sudo dpkg -i node_latest_armhf.deb" it gives error :error processing archive node_latest_armhf.deb codejs-legacy conflicts with node Thankx in advance
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz Malik Adeel 3 years ago
Hi Malik! It seems like you already have Node.js installed :) Check this by typing node -v in a terminal
Miguel Garcia 3 years ago
Hi Marco SchwartzI bought your ebooks "HOME AUTOMATION WITH THE RASPBERRY PI"and "Internet of Things with the Raspberry Pi " on Amazon.So, first of all, congratulations on them as, at a first glance, they seam to bevery interesting.As before I bought them I was about to go ahead with this project but, as I'm a linux novice, I need you help with the following:At a certain point it is said:What we have to do now is to build the Node.js script that will connect the Raspberry Pi to the aREST.io cloud platform, which is a tool that I created specifically to control boards like the Raspberry Pi remotely.Question is:How do I create this script ?Is it to put inside a plain text file (created with Leafpad, for example) ?If yes, in which directory do I put it ?Can you please explain in details.If any other one can help me, please do itThanks in advance
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz Miguel Garcia 3 years ago
Thanks! I already answered to your question by email :)