-
Notifications
You must be signed in to change notification settings - Fork 21
Bonus 2. Starting the Script from Boot
The script that you have running on your home computer or Pi is the key to our silent doorbell. If something happens like a power outage, we want to make sure that our script gets back up and running.
##On a Mac We're going to use a service called crontab and the nano text editor:
env EDITOR=nano crontab -e
Inside of the file, add @reboot nohup sudo node /Users/UserName/node_modules/node-dash-button/doorbell.js &
. Be sure to replace "UserName" with your own. If you named your script something else or put it in a different directory, replace /Users/UserName/node_modules/node-dash-button/doorbell.js with the correct path. The path in my example is the main user directory followed by the node_modules/node-dash-button directory. You can easily copy a file's pathname by following these instructions.
Save the file with Ctl-x, y. You can test if it works by rebooting your computer.
##On a Windows Follow the instructions here to start your node script on reboot. Be sure to specify the script's entire path.
##On a Raspberry Pi/Linux Machine Running a script from boot on the Pi is pretty straightforward. We're going to use a service called crontab:
sudo crontab -e
Pick your favorite text editor (I like nano) and at the bottom of the file (under all of the comments), add @reboot nohup sudo node /home/pi/node_modules/node-dash-button/doorbell.js &
. If you named your script something else or put it in a different directory, replace /home/pi/node_modules/node-dash-button/doorbell.js with the correct path. The path in my example is the main Pi directory followed by the node_modules/node-dash-button directory.
Save the file! You need to reboot for it to take effect, but if you also want to reboot if the internet connection goes down, we are going to add another task to our crontab in this next step.
To handle network drops, I decided to just implement a way for the Pi to detect a network connection and reboot if it's not there.
First we need to create a script to check the WiFi and then trigger shutdown:
cd
sudo nano /usr/local/bin/checkwifi.sh
Place the following inside of the file, being sure to replace the IP address with the IP address of your router:
ping -c4 IP_ADDRESS > /dev/null
if [ $? != 0 ]
then
sudo /sbin/shutdown -r now
fi
The ping checks for a connection. If it returns with a non-zero exit code, the script sends the shutdown command. Save and exit out of the script. Now make sure its permissions are in order:
sudo chmod 775 /usr/local/bin/checkwifi.sh
Just like our doorbell.js script, we are going to add this script to crontab:
sudo crontab -e
Place */5 * * * * /usr/bin/sudo -H /usr/local/bin/checkwifi.sh >> /dev/null 2>&1
underneath the line we added earlier. This will run our checkwifi script every 5 minutes.
Now exit crontab and reboot the Pi:
sudo reboot
Everything should be setup and working! You can also setup a way to monitor running processes on a Pi by following this tutorial.
Initial State (https://www.initialstate.com)
(c) 2018 Initial State Technologies, Inc.