Funhouse Door Alert with Email Notification
2021-08-03 | By Adafruit Industries
License: See Original Project
Courtesy of Adafruit
Guide by John Park
Overview
Create a simple door alert system with the Funhouse and Adafruit IO. It'll update your dashboard and send an email whenever a certain door is opened or closed.
Parts
- Adafruit FunHouse – Wi-Fi Home Automation Development Board
- Magnetic contact switch (door sensor)
- STEMMA JST PH 3-Pin to Male Header Cable - 200mm
- 2-Pin Wire Joints (3-pack)
- USB Type A to Type C Cable - approx 1 meter / 3 ft long
- 5V 2A Switching Power Supply w/ USB-A Connector
Adafruit IO Setup
New to Adafruit IO? You can read all about it in this guide. Go ahead and get your account set up and come back to this page when you're ready.
You'll need to obtain our Adafruit IO Key and Username. Visit your Adafruit IO Profile page and click the VIEW AIO KEY button on the left-sidebar.
A window will pop up with your Adafruit IO key and username. Keep a copy of them in a safe place, you'll need them later.
Setting up Adafruit IO Feeds
You'll create a feed called door to receive the door switch status data sent by the Funhouse.
To create the feed for the door, navigate to the Adafruit IO Feeds Page and click Actions->Create a New Feed. Name the new feed door and then click Create.
- If you do not know how to create feeds, head over to the Adafruit IO Basics: Feeds for a quick overview of this process.
Creating the Adafruit IO Dashboard
Next, you’ll create an Adafruit IO Dashboard to display and control our feeds. Navigate to the Adafruit IO Dashboard page and click Actions -> Create a New Dashboard.
Name this dashboard Funhouse Door Alert and click Create. You'll be re-directed to the new Dashboard.
Add a Block
You'll create an Indicator Block to monitor the status of the door sensor.
From the IO Home dashboard, click the gear icon to see the Dashboard Settings and then click on Create New Block to add a new block to the dashboard.
Click the Indicator block. This will take you to the Connect a Feed window.
Pick Block Feed
Select the door feed and then click Next step.
Configure Block
Configure its settings like so:
- Set the Block Title to Door Status
- On Color is green
- Off Color is red
- Conditions is set to = 1
This means the block will be on (green) when the Funhouse sends a 1 to the door feed and off (red) when the Funhouse sends a 0 to the door feed.
When you're done, click Create block.
Email Alerts
Using Adafruit IO, you can create automated email alerts, so you receive an email whenever a feed condition is met.
Here are two different ways to do it, one uses Adafruit IO and IFTTT, the other uses Adafruit IO+ (this is a paid service) with its built-in Triggers.
Email alerts can take up to 15 minutes to be sent, so don't use this for time critical applications!
Email Alert with AIO and IFTTT
The IFTTT (If This Then That) service can be integrated with AIO quite easily. You'll create an IFTTT applet that watches one of your AIO feeds, and when a certain value appears in the feed, IFTTT will send an email.
First, head to https://ifttt.com/ and create an account or log in if you already have one.
Create Applet
On the IFTTT My Applets page, click the + Create button.
Add a Condition
In the If This box, click the Add button.
Choose Adafruit Service
From the Choose a service page, pick the Adafruit service.
Choose Feed Trigger
On the Choose a trigger page, click on the Monitor a feed on Adafruit IO option. This option allows you to filter the data so only some conditions will trigger the applet.
Configure the Trigger
Fill out the fields of the Monitor a feed on Adafruit IO trigger.
In the example shown here, the Feed being monitored is named door, the Relationship is equal to, and the Value is 0.
This means the trigger will run whenever the door feed has a new piece of data with a value of 0.
Once you've filled this out, click the Create Trigger button.
Add an Action
Now that you have a trigger read, you will pick what the resulting action will be. Click on the Then That Box’s Add button.
Choose Gmail Service
Filter the services of the Choose a service page by typing "gmail" into the search bar.
Click on the Gmail icon to select it.
Choose Gmail Action
In the Choose an action page choose to either Send an email.
Email Fields
Here you can fill in the details of the email that'll be sent when the applet runs.
You can send the email to any address you like (just please don't spam people!)
You can even get fancy and have the email sent as a text message! For many carriers you can use your cellular number at txt . carrier. net pattern, e.g. 2128675309@txt.att.net and you'll receive a text message when the email is triggered.
Once filled in, click the Create action button.
Applet Summary
This page shows a summary of the IFTTT applet flow. You can add elements here if needed, but in this case, click Continue.
Review and Finish
Here you'll see a review of the Applet. If everything looks good, click Finish.
If not, you can click the < Back button in the IFTTT window (not the browser back button).
Email Alert with AIO+
Alert Received
Here's what a typical alert email looks like when the feed triggers the IFTTT applet.
Adafruit IO+ Triggers
With a paid Adafruit IO+ subscription, you can use the Triggers to send an email to your account email address.
Triggers
On Adafruit IO, click the Triggers header, and then click the View all link.
New Trigger
Create a new trigger by clicking the + New Trigger button.
Reactive Trigger
Click on Reactive Trigger.
Here, you'll fill out the conditions and action to take. In this example below:
If door Is equal to 0 Then email me door value and time.
Click create and you're done! The next time your feed value equals the conditional number, you'll get an email.
Code the Funhouse Door Alert
Install CircuitPython
The first think to do is install CircuitPython on your Funhouse. Follow this guide to get it set up with the latest release version.
Shhhh... Secrets
In order for the Funhouse to connect to the internet, you'll need to include a secrets.py file on the board that contains your WiFi access point ssid and password, as well as your AIO key.
Follow this guide page to get your secrets.py file set up.
Text Editor
Adafruit recommends using the Mu editor for using your CircuitPython code with the Funhouse. You can get more info in this guide.
Download the Project Bundle
Your project will use a specific set of CircuitPython libraries and the code.py file. In order to get the libraries, you need, click on the Download Project Bundle link below, and uncompress the .zip file.
Next, drag the contents of the uncompressed bundle directory onto your microcontroller board's CIRCUITPY drive, replacing any existing files or directories with the same names, and adding any new ones that are necessary.
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2021 Melissa LeBlanc-Williams & John Park for Adafruit
#
# SPDX-License-Identifier: MIT
import board
from digitalio import DigitalInOut, Direction, Pull
from adafruit_funhouse import FunHouse
from adafruit_debouncer import Debouncer
RED = 0x200000
GREEN = 0x002000
funhouse = FunHouse(default_bg=None)
funhouse.peripherals.dotstars.fill(RED)
switch_pin = DigitalInOut(board.A1)
switch_pin.direction = Direction.INPUT
switch_pin.pull = Pull.UP
switch = Debouncer(switch_pin)
def send_io_data(door_value):
funhouse.peripherals.led = True
print("Sending data to adafruit IO!")
funhouse.network.push_to_io("door", door_value)
funhouse.peripherals.led = False
send_io_data(0)
while True:
switch.update()
if switch.rose:
print("Door is open")
funhouse.peripherals.play_tone(2000, 0.25)
funhouse.peripherals.dotstars.fill(RED)
send_io_data(0)
if switch.fell:
print("Door is closed")
funhouse.peripherals.play_tone(800, 0.25)
funhouse.peripherals.dotstars.fill(GREEN)
send_io_data(1)
How it Works
The Funhouse Door Alert acts a bit like a simple switch that sends out a REST message when its position is changed. To do this, you first import some libraries.
Libraries
import board
from digitalio import DigitalInOut, Direction, Pull
from adafruit_funhouse import FunHouse
from adafruit_debouncer import Debouncer
The board library provides pin definitions. digitalio is used to check the switch.
adafruit_funhouse allows simple access to the Dotstar LEDs, via the peripherals layer, as well as the network layer used to connect to the internet and send REST or MQTT messages (in the case of this project you'll use REST, which is simple and robust).
adafruit_debouncer make it easy to check for switch open and switch close events.
Setup
Next, you'll do some setup including setting LED color variables, creating the funhouse object, setting up the door sensor switch on the A1 pin, and creating the debouncer object on that pin.
RED = 0x200000
GREEN = 0x002000
funhouse = FunHouse(default_bg=None)
funhouse.peripherals.dotstars.fill(RED)
switch_pin = DigitalInOut(board.A1)
switch_pin.direction = Direction.INPUT
switch_pin.pull = Pull.UP
switch = Debouncer(switch_pin)
Send IO Data Function
You'll create a function to send either a 0 or a 1 to the AIO door feed when the switch is opened or closed.
def send_io_data(door_value):
funhouse.peripherals.led = True
print("Sending data to adafruit IO!")
funhouse.network.push_to_io("door", door_value)
funhouse.peripherals.led = False
Main Loop
The main loop of the program checks the switch pin with the debouncer's switch.update() function.
Two if statement do the rest. If the switch state rises, the door has just opened, and the feed is sent a message with the data value 0.
If the switch state falls, the door has just closed, and the feed is sent a message with the data value 1.
This configuration of triggering only on a state change is helpful, because it means the board won't be constantly spamming the AIO feed with data!
switch.update()
if switch.rose:
print("Door is open")
funhouse.peripherals.play_tone(2000, 0.25)
funhouse.peripherals.dotstars.fill(RED)
send_io_data(0)
if switch.fell:
print("Door is closed")
funhouse.peripherals.play_tone(800, 0.25)
funhouse.peripherals.dotstars.fill(GREEN)
send_io_data(1)
Build the Funhouse Door Alert
Door Sensor
The door sensor is a type of switch called a reed switch. It consists of a wired switch and a separate magnet. When the magnet is within about 1/2" of the sensor, the internal switch mechanism is pulled open. When the magnet is further away the switch closes.
Wire the Sensor
If your door sensor has a pair of bare wires, you'll need to join it to a JST-PH 3-pin cable.
Plug each wire of the door switch into the wire joint connector as shown. It doesn't matter which door switch wire is in which side.
Then, plug the black (ground) and white (signal) wires into the other side of the joint connector as shown. You can leave the red wire alone or snip it off, it won't be needed. If you leave it connected, you can secure it to the wiring with a knot to avoid accidental shorts.
Plug the STEMMA connector into the A1 port of the Funhouse.
Here's a design file for a mounting bracket -- you can use it as a template for cutting your own by hand, with a laser cutter or mill, or as a jumping off point for modeling one for 3D printing.
Mount the Funhouse
Use the 3mm screw inserts and brackets to mount the Funhouse on the wall near the door, close enough for the door sensor wiring.
Mount the Sensor and Magnet
Use the included screws or double-stick foam tape to mount the sensor on the door frame and the magnet to the door.
Plug the USB power into a wall outlet and power up the Funhouse.
Now, when the door is opened, you'll get an alert!
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum