Raspberry Pi + IFTTT: Is There Power at Home?

Raspberry Pi, IFTTT, Dropbox, and a Cron Job will answer the question.

Photo by Louis Reed on Unsplash

Considerations

Communication

IFTTT Setup

Sender Applet

Listener Applet

Dropbox Setup

Time to Script

Yup, that’s part of our script
$ git clone https://github.com/MaxMonteil/isTherePower.git
$ pipenv install
$ sudo apt-get install python3-pip git# if you want to use pipenv
$ pip3 install --user pipenv
$ pip3 install requests dropbox

The Script

import os
import dropbox
import requests

def main():
folderPath = '/IFTTT/SMS/'
fileName = 'is_there_power.txt'
print('Connecting to Dropbox...') # Instantiate dropbox object
dbx = dropbox.Dropbox(os.environ['DBX_ACCESS_TOKEN'])
print('Searching for message...')
searchResult = dbx.files_search(folderPath, fileName, max_results=1)
if len(searchResult.matches) == 1:
print('Message found! Sending reply...')
requests.post('https://maker.ifttt.com/trigger/there_is_power/with/key/'
+ os.environ['WEBHOOK_KEY'],
data={'value1': 'Looks like there is power!'})
# cleanup
print('Reply sent, cleaning up...')
dbx.files_delete(folderPath + fileName)
print('Done!')
else:
print('No message received. Exiting.')

if __name__ == '__main__':
main()

Checking for messages on boot

Wait to Connect to Router

pi ~$ sudo systemctl edit --force --full isTherePower.service
[Unit]
Description=If asked, tell the user there is power
After=network-online.target
[Service]
Type=idle
User=pi
WorkingDirectory=/home/pi/isTherePower
# If using pipenv
ExecStart=/home/pi/.local/bin/pipenv run python /home/pi/isTherePower/isTherePower.py
# Otherwise
ExecStart=/usr/bin/python3 /home/pi/isTherePower/isTherePower.py
[Install]
WantedBy=multi-user.target
pi ~$ systemctl status isTherePower.service
pi ~$ sudo systemctl enable isTherePower.service
pi ~$ sudo systemctl start isTherePower.service

Wait for WiFi

def wait_for_internet_connection(url='http://www.example.com', timeout=3):
print('Waiting for an internet connection...')
while True:
try:
requests.head(url, timeout=timeout)
print('Connection OK')
return
except requests.ConnectionError:
pass
if __name__ == '__main__':
wait_for_internet_connection()
main()

Checking for messages while powered on

Creating a Cron job

* * * * * command to execute
- - - - -
| | | | |
| | | | ----- Day (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of the month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
pi ~$ crontab -e
# if using pipenv
*/30 * * * * cd /home/pi/isTherePower; /home/pi/.local/bin/pipenv run python isTherePower.py 2>&1
# otherwise
*/30 * * * * python3 /home/pi/isTherePower.py 2>&1
>> /path/to/file/log.txt

Conclusion

Improvements

My goal is to solve problems people face with beautiful, practical, and useful solutions. I’m driven to apply and share what I learn.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store