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 you 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)

This guide was first published on Apr 21, 2021. It was last updated on Apr 21, 2021.

This page (Code the Funhouse Door Alert) was last updated on May 10, 2021.

Text editor powered by tinymce.