The best tools to make your project dreams come true

Login or Signup

1/21/2019 | By Maker.io Staff

How to Use Triggers in Your Adafruit IO Project

Gathering IoT data is all great and fun, but what if you need to generate a warning when something goes wrong? In this how-to article, we will learn how to take advantage of Adafruit IO triggers and create Python programs that can check messages and respond automatically when specific events occur.

Use for triggers

In a previous article, we learned how to send and receive data using Adafruit IO and Python, and the data that we sent was temperature readings. Now, monitoring temperature is rather pointless if we cannot react or perform an action based on those readings. This is where triggers come in!

When creating triggers on Adafruit IO, there are two main categories of triggers: reactive triggers and scheduled triggers. Reactive triggers fire when they detect that a value has fallen outside of some range, whereas scheduled triggers periodically fire every unit of specified time (such as hourly).

Reactive triggers can be made to email users, send data to feeds, and send messages, whereas schedule triggers can only send emails, so, in this article, we will look at reactive triggers.

To create a trigger, go to the main page on Adafruit IO, and, from the menu on the left, select “Triggers”. From here, select Actions > Create a new trigger and then select “Reactive trigger”.

How to Use Triggers in Your Adafruit IO Project

The popup that opens presents you with a number of options. The first option, “Select Trigger Feed”, is the feed that will be used to check for some event. In our case, we want to know if the temperature of the workshop goes over 25 degrees, so we will set this to “Workshop temperature”.

The next box asks the condition, and the box next to that asks for a feed or comparison value. We will make the first box “greater than” and leave the second box as “comparison value or feed”, and then, in the box below, enter 25. The last box asks us what to do (then), so we will select “email me” and then enter the message “OPEN THE WINDOW!” below. Your form should look similar to the one shown below.

How to Use Triggers in Your Adafruit IO Project

If all goes to plan, then when you click “Create”, your trigger list should show the following.

How to Use Triggers in Your Adafruit IO Project

But receiving an email is kind of boring if the end goal is to have a device automatically open your window so that you don’t have to! Therefore, we will make the trigger do something slightly different…

A Digital Feed

To make a device automatically open your window when it gets too hot and close it when it gets too cold, we will create a second feed, “Window state”, which will hold the current state of your window. This feed will only ever hold one of two values: 1, which represents open, and 0, which represents closed.

How to Use Triggers in Your Adafruit IO Project

We will also delete your previous trigger and create two new triggers: one that will open the window and another that will close it. When the open trigger fires, we will publish the value “1” to the “Window State” feed, and when the close trigger fires, we will publish the value “0” to the “Window State” feed.

How to Use Triggers in Your Adafruit IO Project

How to Use Triggers in Your Adafruit IO Project

But now, we need to figure out how to use this new “Window state” feed to control a window. Well, luckily for us, we have Python. It’s time to copy the code from the previous article!

Copy Code
from Adafruit_IO import Client, Feed, Data
aio = Client("YOUR AIO KEY HERE")
 
aio.send('Workshop temperature', 21)
 
currentTemperature = aio.receive("Workshop temperature").value
print (currentTemperature)
 
print("done")

However, we will get rid of a few lines, so we are left with only the following.

Copy Code
from Adafruit_IO import Client, Feed, Data
aio = Client("YOUR AIO KEY HERE")

And we will now add the following code to give us the entire program.

Copy Code
import time
from Adafruit_IO import Client, Feed, Data
aio = Client("YOUR AIO KEY HERE")
 
 
oldState = False
newState = False
 
while(True):
	newState = bool(int(aio.receive("Window State").value))
	if(oldState != newState):
		if(newState == True):
			print(“Window will open now”)
		else:
			print(“Window will close now”)	
oldState = newState 
time.sleep(1)

How this Program Works

The first few lines of code import our needed libraries, while the third line creates an Adafruit IO object that allows us to send and receive data. Remember that you need to copy and paste your AIO key where it says "YOUR AIO KEY HERE"! Then, we create two Boolean variables, oldState and newState, which are used to prevent multiple actions when there is no change in data. After this, we enter the main loop of the program, which constantly gets the latest value of the “Window State” feed. But before we can use this value, we have to first convert it into an integer (int) then convert it into a Boolean (this is because the value 1 is stored as a string and NOT as a number). Then, depending on if there has been a change of state and what that new state is, we will either open or close the window. If this was written on a Raspberry Pi, then this code could be accessing GPIO to control a solenoid or motor system to open and close the window.