If you’re wondering how to trigger the GPIO of a Raspberry Pi remotely with another device, then this tutorial is for you. Set your Pi to open an LED or even a door with an SMS from your android device. With IFTTT, the sky is the limit!
IFTTT (If This Then That) is a free web platform connecting devices and services such as Gmail, Dropbox, Fitbit, and even home assistants like Amazon Alexa. With IFTTT, the features and functionalities of a web service become readily available for other web services or internet-connected devices.
IFTTT works just like a standard if statement—if this condition is triggered, do this action. To do that, IFTTT uses Applets. These are tiny programs that use a trigger to carry out events. An Applet can do several things, such as controlling GPIO pins from a Raspberry Pi using text messages just like what we are doing today.
To integrate IFTTT with Raspberry Pi, we need webhooks. Webhooks is a service that triggers events via HTTP requests. HTTP requests, also known as web requests, are messages that a client sends to a server.
There are two types of web requests: HTTP GET and HTTP POST.
HTTP GET is a web request that retrieves data from a web browser. It does not change anything on the server. It just fetches the data from it.
On the other hand, HTTP POST is a web request that transmits data to the server. It adds something new to it.
A typical example of a GET request is browsing a website. You fetch the data needed to display the webpage on your personal computer. On the other hand, POST requests are used to type text into a web page, such as a username and password.
A common webhooks system works like this:
Alternatively, you can use a different trigger to set off an HTTP request with a webhook. Our sample project will trigger an LED connected to the Raspberry Pi using an SMS from an Android device. The Raspberry Pi will act as a simple webserver. We can specify a keyword in IFTTT wherein IFTTT will make an HTTP GET request to the Raspberry Pi server if the SMS matches the keyword given. Depending on the URL of the web request, the LED will turn on or turn off.
Don’t worry if it’s still quite blurry. You’ll understand how it works as we build the project. Let’s begin by setting up the hardware!
Connecting the Components
This project only involves 1 LED and a Raspberry Pi. Connect the long side of the LED to pin 3 and the LED’s shorter side to the ground. After this tutorial, you can replace the LED with any digitally controlled component or module and it should still work.
Setting up IFTTT
Go to the IFTTT website and create an account.
Once logged in, go to the top right toolbar on your homepage and select create. This directs you to a page where you can start creating an Applet.
Then, set a condition by clicking “Add” beside This.
Next, search for “Android SMS”.
In Android SMS, choose “New SMS sent matches search.” This trigger activates every time you send an SMS on your android device that matches a certain keyword you specify later. Note that any phone number should work as long as the keyword is in the message.
Next, specify the keyword you want to use to trigger the LED. I’ll use “LED ON” for mine.
Let’s proceed with the effect. Click “Add” after That.
Search for the webhooks service.
Select make a web request.
Next, specify the type of web request you want to make. How do we know what URL to use for this part? This is where Bottle comes in.
Bottle is a micro web framework in Python. It is designed to be fast, simple and lightweight, and is distributed as a single file module with no dependencies other than the Python Standard Library. First, download Bottle into your raspberry Pi with:
Next, copy the code below into any editor or IDE.
import RPi.GPIO as GPIO import time from bottle import route, run, template GPIO.setmode(GPIO.BOARD) GPIO.setup(3, GPIO.OUT) GPIO.output(3, False) @route('/LED/:ledstate') def ledtrigger(ledstate=0): if ledstate == '0': GPIO.output(3, False) return 'LED OFF' elif ledstate == '1': GPIO.output(3, True) return 'LED ON' run(host='0.0.0.0', port=8081)
The code is pretty straightforward. First, import the required libraries. Then, set the PIN of your LED as an output and set the default signal to LOW or False. Next, set up your web server by handling the requests to specific URLs.
@route('/LED/:ledstate') handles the requests in 192.168.100.24:8081/LED/1 or 0.
Note that the preceding IP address is the IP address of your Raspberry Pi. If you don’t know your Pi’s IP address, enter
ifconfig in the terminal and search for the wlan0 section. The address that comes after inet is your IP address.
We then use if statements to control the status of the LED. The return value is printed to your web browser once it is triggered.
You can test the URLs on any web browser from any device connected to your home network. For instance, if I visit 192.168.100.24/8081/1, the LED turns on, and I get the message “LED ON” in my web browser. If I enter 192.168.100.24/8081/0, the LED turns off.
Now, go back to IFTTT and enter the URL into your webhooks web request. One downside of this configuration is that you will need two applets —one for turning the LED on and another for turning it off. For the turn-off applet, you can use LED OFF as the keyword.