Control a Relay From Anywhere Using the Raspberry Pi

Control a Relay From Anywhere Using the Raspberry Pi

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

Jack Sheridan a year 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
Ian McDaniel a year 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! <http: github.com="" npm="" npm="" issues="">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.<anonymous>: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! <http: github.com="" npm="" npm="" issues="">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.<anonymous>: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! <http: github.com="" npm="" npm="" issues="">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.<anonymous>: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! <http: github.com="" npm="" npm="" issues="">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.<anonymous>: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! <http: github.com="" npm="" npm="" issues="">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.<anonymous>: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! <http: github.com="" npm="" npm="" issues="">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.<anonymous>: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! <http: github.com="" npm="" npm="" issues="">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.<anonymous>: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! <http: github.com="" npm="" npm="" issues="">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.<anonymous>: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
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
Paolo 2 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
Vtelix 2 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
Juan Lugmania 2 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
Marco Schwartz Juan Lugmania 2 years ago
Hi! Just run the Node.js file as sudo :)
Deven Swiergiel 2 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
Marco Schwartz Deven Swiergiel 2 years ago
Hey Deven, you're welcome! Glad to hear you enjoyed the tutorial :)
Chandler 2 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
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
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
Marco Schwartz Miguel Garcia 3 years ago
Thanks! I already answered to your question by email :)
Edward Morra 3 years ago
My project is to make a button in my iphone app Simple Remote that issues an http command like http://192.168.1.72:3000/digital/12/1 and turn on a relay to lower my projector screen.I have installed Raspian Jesse Lite on a Pi B,SSH into it and followed the guide for installing aRest for Pi https://arest.io/pi/. I also installed rpi.gpio and wiringPi so I could test the relay from the Pi B command line with 'gpio -1 mode 12 out' and 'gpio -1 write 12 1' to turn the relay on, which works well.I then issue 'node app.js &' at the Pi B command line and point a browser on my laptop to http://192.168.1.72:3000. it returns the expected default result {"id":"34f5eQ","name":"my_new_Pi","hardware":"rpi","variables":{"temperature":24,"humidity":40},"connected":true}When I try http://192.168.1.72:3000/digital/12/1 i get the expected {"id":"34f5eQ","name":"my_new_Pi","hardware":"rpi","connected":true,"message":"Pin 12 set to 1"}. However the Pi B command line spits out '[Error: Pin has not been exported for write] Written to pin' and the relay does not turn on. I have tried multiple different pins such as http://192.168.1.72:3000/digital/13/1, all with the same error.How do I declare that pin twelve is to be written to (ie., is an Out pin) to make this work?code versions are node -v 4.2.1, gpio -v 2.32.Thank you in advance for any helpful comments.
Reply
Marco Schwartz Edward Morra 3 years ago
Did you start the node.js command with sudo ? That's usually the main problem
kd4hls 3 years ago
hi, great instructions..eg. got it working!im using this to open some gates, but i need the relay to stay on for a second or two.thanksrodger
Reply
malini 3 years ago
I'm using a raspberry pi 2 model B. I've followed all the steps and when i type the url https://cloud.arest.io/digi... i get the message "requested device not found". Please help.
Reply
Marco Schwartz malini 3 years ago
You need to insert the name of your device first :) For example http://cloud.arest.io/3g83d...
Ryangodammit lᴉɐɯƎ 3 years ago
I seem to be having the same problem some else here was having. When I push the on button, the relay very quickly switches on then off, but doesn't stay on. How do I fix this?
Reply
Lynda Barry 3 years ago
Okay, I finally got past the node.js installation problem. Had to uninstall nodejs-legacy and nodered before the latest node.js would install. I've got all the relevant packages installed correctly now, but I must be doing something wrong with renaming and changing id for my board in app.js. After changing from the default values listed in the tutorial all I get when trying to operate the relay is {"message": "Requested device not found".} In the cloud.arest.io dashboard that I created it shows the device as "offline".Any suggestions of how to fix this issue will be greatly appreciated.
Reply
Lynda Barry 3 years ago
So, I decided to start over again to try and resolve some issues I've been having. I followed steps above and installed node, but when I run this command sudo dpkg -i node_latest_armhf.deb I get the following error message.dpkg: regarding node_latest_armhf.deb containing node: nodejs-legacy conflicts with node node (version 4.2.1-1) is to be installed. node provides node and is to be installed.dpkg: error processing archive node_latest_armhf.deb (--install) : conflicting packages - not installing nodeErrors were encountered while processing: node_latest_armhf.debI have no idea why this has happened (I've tried starting from scratch twice now) - does anyone have any ideas what is going on and how I can fix it?
Reply
Claudio Lynda Barry 3 years ago
you solved?i have the same error :(
Lynda Barry 3 years ago
I just reinstalled the packages for the relay operation from github following the listed procedure. I can launch app.js and get the expected response but when I try to access interface.html I get the following error message:Error: ENOENT: no such file or directory, stat '/piRelay/views/interface.html' at Error (native)I'm not sure how to resolve this problem.
Reply
Marco Schwartz Lynda Barry 3 years ago
Strange, the file is definitely inside the folder. Make sure that it is really there on the folder on your Pi.
Rob McGeown 3 years ago
I'm struggling with the "failed to change group ownership" error. I've changed the path in gpio-admin.c as suggested in the listed solutions but no luck...
Reply
TJ Gilbert 3 years ago
I saw another user post this same question but it did not get a response. Could someone help me out?State on receivedError when trying to open pin 7gpio-admin: failed to change group ownership of /sys/devices/virtual/gpio/gpio 4direction: no such file or directoryCan anyone offer any suggestions to fix this error?
Reply
Ronald Golec 3 years ago
Hi Marco,I've installed version 3.18.5 RPi model B+ and modified the path in gpio-admin.c.I receive the following proper status in Terminal:"Listening on port 3700" "State on received"However, the led on GPIO pin 36 flashes once and quickly turns OFF when pressing the 'ON' button in the web server.For troubleshooting purposes I commented gpio.close(36); in app.js, the LED remains ON but am prompted with an error in Terminal.All LED's functioned and responded properly prior to upgrading to 3.18.5.Any suggestions? Thank you!
Reply
Marco Schwartz Ronald Golec 3 years ago
I'll modify this tutorial soon with a simpler GPIO module, stay tuned :)
Lynda Barry 4 years ago
I just finished installing the revised software (I've previously used the HTML/JS/PHP version) and connected to a relay board that was working fine with the earlier program. When trying to operate the relay I get the following output in the terminal window:State on receivedError when trying to open pin 7gpio-admin: failed to change group ownership of /sys/devices/virtual/gpio/gpio 4direction: no such file or directoryCan anyone offer any suggestions to fix this error?
Reply
igm4ever 4 years ago
Hi Marco,I'd like to do the same thing but with variable voltages, so as to control dimmable lights, how hard would it be to expand this? I have very basic coding knowledge
Reply
Marco Schwartz igm4ever 4 years ago
With the right hardware that would be easy I guess, you would need to look at PWM output on the Raspberry Pi to control the intensity of the lights.
Geert Van Moer 4 years ago
Can anyone help me with this problem on and off the same [email protected] ~/quick2wire-gpio-admin/pi-node-relay $ node app.jsListening on port 3700express deprecated res.sendfile: Use res.sendFile instead app.js:18:12State on received.Error when trying to open pin 7gpio-admin: failed to change group ownership of /sys/devices/virtual/gpio/gpio4/direction: No such file or directoryI have tried most things I could find in goole and forums and after installing and reinstalling no succes.Raspberry model PI 2
Reply
Ratmir Geert Van Moer 4 years ago
Got the same problem, on a model B+. It means the quick2wire gpio-admin library isn't working, and the pi's pins can not be configured and/or set.However, on the original raspberry pi, we got the above working like a charm... So I'm guessing that the models with more pins (2, B+, others?) are not supported by quick2wire gpio-admin.Perhaps somebody can confirm or deny this?
Jack Hughes 4 years ago
Hi, Great write up. I would like to achieve the remote control of an output gpio pin, but as well as this, control a pcd8544 LCD screen.In otherwords, while GPIO is 1 (high) the LCD displays one message. While the GPIO is 0 (low) then the LCD would display another message. Any thanks would be greatly appreciated!
Reply
Marco Schwartz Jack Hughes 4 years ago
Thanks! That should be easy as well, I know there are libraries out there to control an LCD screen from Node.js, for example: https://github.com/fivdi/lcd
Rohan 4 years ago
Your tutorial's are really amazing...I have a doubt regarding my project suppose i want to implement an irrigation system,an led on raspberry pi and access them through a web server but with a different web page to make it more innovative, how will i do that and i want to display a graph using an ultrasonic sensor on a web page and get a notification on an android application i design.Please help me out sir asap.Regards.
Reply
Marco Schwartz Rohan 4 years ago
Thanks Rohan! Your project sounds exciting, I really recommend you to check all the tutorials on this website and you will sure find how to make it!
etype 4 years ago
how can i switch on and off a 12v circuit if relay is 12v and pi is 5v
Reply
Clint etype 4 years ago
You would let the 12V flow through the 30 & 87 pins on the relay, the 5V is just to let the 12V flow between them.
Marco Schwartz etype 4 years ago
The Pi is 3.3V actually :) But for your project you need to use a transistor to drive the 12V circuit. See: http://elinux.org/RPi_GPIO_...
nathan 4 years ago
GET / 404 Not Found anyhelp
Reply
Clint nathan 4 years ago
when you plug in the link on your browser, make sure to do "/interface" to the end of the ngrok link
Mark 4 years ago
Hi,This is a great project which I have successfully managed to get to work on a Pi B, Now I am trying to do the same with a Pi 2 and am running to the following issue = Error when trying to open pin 7 gpio-admin: failed to change group ownership of /sys/devices/virtual/gpio/gpio4/direction: No such file or directoryPlease could someone advise me what to do. I have read that the directory may have changed location but am unable to locate it.RegardsMark
Reply
Kiran Gore Mark 4 years ago
To access GPIO you need administrative permission try by using sudo command
Swapneel Golapkar 4 years ago
Hi,I am having 100 switches in my home across 5 rooms, how can I connect them through 1 rasberry pi ?Plz suggest I am a naive developer for this project, I will start with small 2 to three switches, but would like to automate my entire home, thanks for your valuable guidance.RegardsNeel
Reply
Kiran Gore Swapneel Golapkar 4 years ago
you can use digital decoder circuit. use PI to generate codes and decoder circuit will decode it and will turn on respective switch http://www.instructables.co...http://johnlimbo.blogspot.i...study this you need to use multiple 4 to 16 decoder circuit
Kiran Gore 4 years ago
any one known how to start app.js on system start every time
Reply
Marco Schwartz Kiran Gore 4 years ago
You can use the forever module for that: http://blog.nodejitsu.com/k...
Darshan Bagul 4 years ago
Hello, can the same project be done using Django and python instead of Node.js?? If yes, what all tools will I need for the same?
Reply
Marco Schwartz Darshan Bagul 4 years ago
I don't personally know Django, but it should be possible as well :)
Jessie 4 years ago
Hi Marco, I have follow the steps you show us as above until I have faced a problem where the terminal didn't shows "Listening on port 3700" after I key-in "node app.js" in terminal. The below picture shows the result in the terminal.Another question I would like to ask is about the code: "<button class="btn btn-block btn-lg btn-primary" type="button" id="1" onclick="buttonClick(this.id)">On</button>", is this coding has to be key-in in app.js file?Lastly, is that "Node.js" file is equivalent to "app.js" file? Because I can't find any file name "Node.js" file in my downloaded pi-node-relay folder.I am apologize for my weak English and I am a new user to pi and networking. Sorry, if I have ask any basic knowledge or silly question here.Hope sir or anyone can help me. Thank you.
Reply
Clint Jessie 4 years ago
You're not in the "pi-node-relay" folder to run the applicationtry "cd pi-node-relay" before you type in "node app.js"I could be wrong but I think it may be that simple.
Marco Schwartz Jessie 4 years ago
Hello Jessie, it seems you are just not in the right folder :) Make sure to download all the code from the GitHub repository and then run node app.js in the folder where the app.js file is located and it should work.
Kiran Gore 4 years ago
Sir I complied every thing, you told and also updated web interface for all input output and working grate with local network except NGROK getting error "404 not found" error image attached herePlease Helpif any one want source code for new web page I will upload for youThank you
Reply
Clint Kiran Gore 4 years ago
I'm having the exact same issue. Seems to be happening to a few of us in the last couple weeks.
Shares
Control a Relay From Anywhere Using the Raspberry Pi Control a Relay From Anywhere Using the Raspberry Pi Control a Relay From Anywhere Using the Raspberry Pi Control a Relay From Anywhere Using the Raspberry Pi Control a Relay From Anywhere Using the Raspberry Pi Control a Relay From Anywhere Using the Raspberry Pi
Someone from the United States
Recently signed up
2 months ago
Someone from Philippines
Recently signed up
2 months ago
Someone from Philippines
Recently signed up
3 months ago
Someone from United Kingdom
Recently signed up
3 months ago
Someone from France
Recently signed up
4 months ago
Someone from Italy
Recently signed up
4 months ago
Someone from Sweden
Recently signed up
4 months ago
Someone from the United States
Recently signed up
6 months ago
Someone from India
Recently signed up
6 months ago
Someone from Australia
Recently signed up
6 months ago
41 people recently viewed this page