Getting mobile notifications from Raspberry Pi GPIO

Published by Jorge on

raspberrypi

In first place we need to setup the GPIO monitoring code. This code moraspberry-pi-logonitors every 5 seconds if the state of the GPIO pin 25 has changed. If so, it makes a post to the Maker IFTTT channel.

It’s important to replace <maker-key> with your maker channel key and <eventname> with your recipe event name.

#!/usr/bin/python
#gpio code
import os, sys
import RPi.GPIO as GPIO
import time
import requests 
from datetime import datetime

def post(value):
	payload={"value1":str(value),"value2":str(datetime.now())}
	req = requests.post("http://maker.ifttt.com/trigger/<eventname>/with/key/<maker-key>", data=payload)
	print (req.url)

GPIO.setmode(GPIO.BCM)
GPIO.setup(25,GPIO.IN)
start=1
past=0
while True:
	now=GPIO.input(25)
	if(now!=past and start==0):
		post(now)
	start=0
	time.sleep(5)
	past=now
GPIO.cleanup()

If we’re executing the code through SSH we need to execute the code in background. To do that you can use screen. Remeber giving execution access to the file.

chmod +x program.py
screen gpio
python program.py &

Then we need to set up the IFTTT recipe:

IF Maker channel post then Notification.

With IFTTT we could use other recipes such as making a log with the Google Spreadsheet channel or a Evernote Log.

With this setup we could attach to the GPIO pin a digital sensor and monitor it in our smartphone. If we attach a switch we are doing a physical DO recipe.

 


If you want to read more like this, subscribe to my newsletter or follow me on Twitter