Raspberry Pi Garage Door Opener Project

 

High level view of design

The Original Design – The polling method (terrible )

Rasperry Pi Unit
– Long polls for update to state (queries datastore on intermediary for open/hold)
– Trigger close relay script (and immediately set intermediary state to hold)

Intermediary
– Listens for requests (from e.g. mobile app) then sets its own state to either:
– Open
– Hold
– Needs to check for authenticated user

Mobile App
– Requests the setting of the state of the intermediary to either:
– Open
– Hold
– Requires authenticated user

Using Web Sockets (the better solution)

Rasperry Pi Unit
socket.io (client side) listens for event emissions from Intermediary
– trigger close relay script (1 or 2 sec GPIO high signal)

Intermediary
– Has an HTTPs web server that listens to button presses from the Client App.
– Needs to check for authenticated user and validate that the person is authenticated.
– Emits open events for RPI unit to consume (it’s listening, always)

Mobile App
– HTTPs call to Intermediary when button is pressed.
– Requires authenticated user to log into the app.

When using sockets, we can ignore the whole state construct of open or hold.

The RPI unit is always listening to the intermediary on the designated port.

The mobile app sends message to HTTPs listening server on Intermediary. The Intermediary, to which RPI unit is always listening, uses socket.io lib to emit message…if the RPI unit listener it captures a 1 in the data stream…then close the relay and door should open.

https://[domain]:[port]/api/private <– RPI listens here for socket messages
https://[domain]:[port]<– Intemediary listens here for open request (HTTPS call)

https://[domain]:443/ <– Old mobile PWA hosted here (Vuew.js app). This is no longer used, in favour of native Ionic app.

NodeJS process runs as service with pm2. see docs for more information on how to view running services and stop/restart etc:
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-18-04

Setting up PM2 on a Raspberry PI

# Install PM2
sudo npm install -g pm2

# Setup PM2 Startup Script
# sudo pm2 startup # To Start PM2 as root
pm2 startup # To start PM2 as pi / current user
#[PM2] You have to run this command as root. Execute the following command:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u pi –hp /home/pi

# Make currently running apps to start at boot
pm2 save

# Gets the list of pm2 processes
pm2 list

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.