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.

Text Editor

Adafruit recommends using the Mu editor for using your CircuitPython code with the Funhouse. You can get more info in this guide.

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.

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.

# SPDX-FileCopyrightText: Copyright (c) 2021 John Park for Adafruit
#
# SPDX-License-Identifier: MIT
# FunHouse Mail Slot Detector

import board
from adafruit_debouncer import Debouncer
from digitalio import DigitalInOut, Pull
from adafruit_funhouse import FunHouse

beam_sense_pin = DigitalInOut(board.A0)  # defaults to input
beam_sense_pin.pull = Pull.UP  # turn on internal pull-up resistor
beam_sensor = Debouncer(beam_sense_pin)

AMBER = 0xF0D000
BLUE = 0x00D0F0
RED = 0xFF0000
WHITE = 0xFFFFFF
GRAY = 0x606060

funhouse = FunHouse(default_bg=None, scale=3)
funhouse.peripherals.dotstars.brightness = 0.05
funhouse.peripherals.dotstars.fill(AMBER)

# Create the labels
funhouse.display.show(None)
mail_label = funhouse.add_text(
    text="No Mail yet", text_position=(4, 14), text_color=AMBER
)
reset_label = funhouse.add_text(text="reset", text_position=(3, 70), text_color=GRAY)

funhouse.display.show(funhouse.splash)


def send_io_data(mail_value):
    funhouse.peripherals.led = True
    funhouse.network.push_to_io("mail", mail_value)
    funhouse.peripherals.led = False


send_io_data(1)

while True:
    beam_sensor.update()

    if beam_sensor.fell:
        funhouse.peripherals.set_dotstars(RED, WHITE, BLUE, WHITE, RED)
        funhouse.peripherals.play_tone(2000, 0.25)
        funhouse.set_text("Mail is here!", mail_label)
        funhouse.set_text_color(BLUE, mail_label)
        send_io_data(0)

    if funhouse.peripherals.button_down:
        funhouse.peripherals.dotstars.fill(AMBER)
        funhouse.set_text("No Mail yet", mail_label)
        funhouse.set_text_color(AMBER, mail_label)
        send_io_data(1)

Next, drag the contents of the uncompressed bundle directory onto you microcontroller board CIRCUITPY drive, replacing any existing files or directories with the same names, and adding any new ones that are necessary.

How it Works

The Funhouse Mail Detector acts a bit like a simple switch that sends data to Adafruit IO when it detects a change. To do this, you first import some libraries.

Libraries

import board
from adafruit_debouncer import Debouncer
from digitalio import DigitalInOut, Pull
from adafruit_funhouse import FunHouse

The board library provides pin definitions. digitalio is used to check the switch.

adafruit_debouncer make it easy to check for switch open and switch close events.

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 messages to Adafruit IO

Setup

Next you'll do some setup including setting up the break beam sensor on the A0 pin, creating the debouncer object on that pin, setting LED color variables, and creating the funhouse object.

beam_sense_pin = DigitalInOut(board.A0)  # defaults to input
beam_sense_pin.pull = Pull.UP  # turn on internal pull-up resistor
beam_sensor = Debouncer(beam_sense_pin)
 
AMBER = 0xF0D000
BLUE = 0x00D0F0
RED = 0xFF0000
WHITE = 0xFFFFFF
GRAY = 0x606060
 
funhouse = FunHouse(default_bg=None, scale=3)
funhouse.peripherals.dotstars.brightness = 0.05
funhouse.peripherals.dotstars.fill(AMBER)

FunHouse Display

You'll set up the FunHouse display with text labels to show the state of the mail slot, and to label the reset button.

# Create the labels
funhouse.display.show(None)
mail_label = funhouse.add_text(
    text="No Mail yet", text_position=(4, 14), text_color=AMBER
)
reset_label = funhouse.add_text(text="reset", text_position=(3, 70), text_color=GRAY)
 
funhouse.display.show(funhouse.splash)

Send IO Data Function

You'll create a function to send either a 0 or a 1 to the AIO door feed when the beam is broken or reset.

def send_io_data(mail_value):
    funhouse.peripherals.led = True
    funhouse.network.push_to_io("mail", mail_value)
    funhouse.peripherals.led = False

Main Loop

The main loop of the program checks the switch pin with the debouncer's beam_sensor.update() function.

Two if statement do the rest. If the beam sensor state falls, the mail has arrived and the feed is sent a message with the data value 0.

If the reset button is pressed, 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!

This guide was first published on May 11, 2021. It was last updated on 2021-05-11 18:41:20 -0400.

This page (Code the FunHouse Mail Detector) was last updated on Sep 15, 2021.

Text editor powered by tinymce.