Last Updated: December 23, 2019
·
7.061K
· chase_chou

Node-GaragePi, the garage door opener using Node.js

GaragePI Update with Camera and Node.js

It's been over a year since I first made the GaragePi, a garage door opener made with a Raspberry Pi, and since then, it's come in handy in quite a few situations. Like when going on vacations or even a bike ride, I can leave my keys at home. I've also made the following enhancements:

  • rewrote the server using Node.js
  • added a camera module to take a snapshot of the garage door
  • removed the battery in the garage remote and wired it directly to the Pi

Node-GaragePi Installation Instructions

The reason for the Node.js rewrite was just a personal preference and not anything against Python. Since I also have other node apps running on the same Pi, it just makes things a bit easier to manage.

Download and install Node.js and NPM:

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

Check the install worked correctly by typing the command below:

node -v

The next steps are to set it up so the GPIO pins can be used by your logged in account instead of root as it normally requires:

git clone git://github.com/quick2wire/quick2wire-gpio-admin.git
cd quick2wire-gpio-admin
make
sudo make install
sudo adduser $USER gpio

After this, you need to simply log out and then log back in.

Next, we'll download and setup Node-GaragePi:

cd ~
git clone git://github.com/chasechou/node-garagepi.git
cd node-garagepi
npm install

That should install the required modules and you can test it all out by running:

node garagepi.js

Load up http://192.168.1.60:8000 in a browser to see if it's working. Change the IP to that of your machine if it's different.

[update 8/2/15] If you see an error like this:

[[23:45:47]] [ERROR] gpio-admin: failed to change group ownership of /sys/devices/virtual/gpio/gpio4/direction: No such file or directory

Then you'll need to also patch gpio-admin because at some point, Raspbian changed the path to something gpio-admin is expecting. The patch is described here and we can easily make the necessary edit manually:

https://github.com/quick2wire/quick2wire-gpio-admin/pull/6/files

cd quick2wire-gpio-admin/src
vi gpio-admin.c

Change this line:

int size = snprintf(path, PATH_MAX, "/sys/devices/virtual/gpio/gpio%u/%s", pin, filename);

To this instead:

int size = snprintf(path, PATH_MAX, "/sys/class/gpio/gpio%u/%s", pin, filename);

Then simply run the following commands back in the quick2wire-gpio-admin root dir just as we originally did above:

sudo make uninstall
sudo make install
sudo adduser $USER gpio

Logout then back in and re-run the node app and the error should not happen again.

If that all looks correct, then the last step is to set it up so it runs on boot up. I chose to use PM2 as the process manager as I've had some good experience using that in other projects.

sudo npm install -g pm2
sudo env PATH=$PATH:/usr/local/bin pm2 startup raspberry -u pi
cd ~/node-garagepi
pm2 start garagepi.js -x --name "node-garagepi"
pm2 dump

Then restart your Pi again to test that it automatically started the app by loading it up in your browser.

Adding the Pi Camera Module

The camera module was for added so I can visually check the state of the garage door. I highly suggest adding a camera if you plan on using GaragePi remotely. I bought this camera:

Raspberry PI 5MP Camera Board Module - $9

The setup was pretty straight-forward and the directions I followed was this below:

camera module installation instructions

Node-GaragePi is already setup to take a snapshot every 30 seconds and uses that as the background image of the page. The image is saved at ~/node-garagepi/public/images/garage.jpg.

Replace the Garage Door Remote Battery

And, lastly, the battery hack was the most important one of all. In the past year of running GaragePi, I found that I had to change the battery in the remote 2 or 3 times. Even with light usage I found the battery was drained to the point where it couldn't actually open the garage door after only a few months. This modification makes it so the battery is no longer needed.

The idea is simple. My garage door remote uses a CR-2032 coin shaped battery that is 3.3V. The Pi just so happens to have a 3V terminal that should be free to use. So simply remove the battery from the remote and wire the positive terminal on the remote to the 3V pin on the Pi and the negative to a GND on the Pi.

That should be it. No more worrying about the battery as long as your Pi is powered up!

Let me know how it goes... good luck and have fun!

5 Responses
Add your response

I am having trouble getting this to launch at startup using the command. I've received a few errors when running
sudo npm install -g pm2
npm WARN Optional dep failed, continuing fsevents@1.0.12
npm WARN decprecated minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue

over 1 year ago ·

I'm also having an issue if I hit the button to fast twice (probably before the relay cycles) the program throws an error and crashes. The webpage completely goes down and I have to restart the program.

over 1 year ago ·

Getting this error when I try and do the npm install step after downloading from git - any ideas?

make: Entering directory '/home/pi/node-garagepi/nodemodules/onoff/nodemodules/epoll/build'
CXX(target) Release/obj.target/epoll/src/epoll.o
In file included from ../src/epoll.cc:15:0:
../nodemodules/nan/nan.h:324:47: error: ‘REPLACEINVALIDUTF8’ is not a member of ‘v8::String’
static const unsigned kReplaceInvalidUtf8 = v8::String::REPLACE
INVALIDUTF8;
^
epoll.target.mk:84: recipe for target 'Release/obj.target/epoll/src/epoll.o' failed
make: *** [Release/obj.target/epoll/src/epoll.o] Error 1
make: Leaving directory '/home/pi/node-garagepi/node
modules/onoff/nodemodules/epoll/build'
gyp ERR! build error
gyp ERR! stack Error: make failed with exit code: 2
gyp 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 (childprocess.js:809:12)
gyp ERR! System Linux 4.9.24-v7+
gyp ERR! command "nodejs" "/usr/bin/node-gyp" "rebuild"
gyp ERR! cwd /home/pi/node-garagepi/node
modules/onoff/node_modules/epoll
gyp ERR! node -v v0.10.29
gyp ERR! node-gyp -v v0.12.2
gyp ERR! not ok
npm 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.Debian

npm ERR! epoll@0.1.21 install: node-gyp rebuild
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the epoll@0.1.21 install script.
npm ERR! This is most likely a problem with the epoll package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-gyp rebuild
npm ERR! You can get their info via:
npm ERR! npm owner ls epoll
npm ERR! There is likely additional logging output above.

npm ERR! System Linux 4.9.24-v7+
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /home/pi/node-garagepi
npm ERR! node -v v0.10.29
npm ERR! npm -v 1.4.21
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/pi/node-garagepi/npm-debug.log
npm ERR! not ok code 0

over 1 year ago ·

First of all, great article!
I currently have this issue every time I press the button:
GET /api/clickbutton 200 26.035 ms - -
[[15:14:00]] [LOG] gpioWrite, value: 1 seq: 0
fs.js:663
return binding.writeBuffer(fd, buffer, offset, length, position);
^

Error: EPERM: operation not permitted, write
at Error (native)
at Object.fs.writeSync (fs.js:663:20)
at Gpio.writeSync (/home/pi/devel/node-garagepi/nodemodules/onoff/onoff.js:200:6)
at null.
onTimeout (/home/pi/devel/node-garagepi/garagepi.js:59:10)
at Timer.listOnTimeout (timers.js:92:15)

Any possible tips?

Thanks!

over 1 year ago ·

I had trouble with that too @OatStraw. I spent hours fiddling with PM2 and then just removed it.

I read that I could put this in /etc/rc.local to start it at boot:
su pi -c 'node /home/pi/node-garagepi/garagepi.js < /dev/null &'

With a reboot it worked! That seems much simpler.

How do I strip away the camera stuff?

over 1 year ago ·