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)
– Listens for requests (from e.g. mobile app) then sets its own state to either:
– Needs to check for authenticated user
– Requests the setting of the state of the intermediary to either:
– 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 LOW signal). Note that this is a LOW signal because the relay is ground side switched.
– 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)
– 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]:443/ <– Old mobile PWA hosted here (Vue.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:
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
# Gets the list of pm2 processes