This example is not designed to run on the FunHouse. It is an example of a device you could connect to Adafruit IO and then display the connected feed on your FunHouse IoT hub.

This project uses a Feather and either the Relay FeatherWing or the 4 Outlet Power Relay to control devices that turn on and off through Adafruit IO.

For this project, you will need:

It's what you've been waiting for, the Feather M4 Express featuring ATSAMD51. This Feather is fast like a swift, smart like an owl, strong like a ox-bird (it's half ox,...
$22.95
In Stock
Give your Feather project a lift with the Adafruit AirLift FeatherWing - a FeatherWing that lets you use the powerful ESP32 as a WiFi co-processor. You probably have your...
$12.95
In Stock
This is the FeatherWing Tripler - a prototyping add-on and more for all Feather boards. This is similar to our
$8.50
In Stock
A Feather board without ambition is a Feather board without FeatherWings! This is the Power Relay FeatherWing. It gives you power to control, and control over power....
$9.95
In Stock

OR

Say goodbye to hazardous high voltage wiring and create the Internet of Things with safe, reliable power control....
Out of Stock

Feather Setup

First, click Download Project Bundle below. This zip file will contain everything you need for this example. However, the files are also in the zip file you downloaded for the FunHouse, so you can get them from either one, just make sure you take the files from the relay_peripheral directory.

# SPDX-FileCopyrightText: 2021 Dylan Herrada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
Controlling a relay with Adafruit IO
Uses:
    * Feather board
    * https://www.adafruit.com/product/2890
    * https://www.adafruit.com/product/4264

    * https://www.adafruit.com/product/3191
    OR
    * https://www.adafruit.com/product/2935
"""
import board
import busio
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
import neopixel
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from adafruit_io.adafruit_io import IO_MQTT
from digitalio import DigitalInOut, Direction

### WiFi ###

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

# If you are using a Relay FeatherWing, make sure this pin corresponds to the pin shorted on the
# bottom of the Relay FeatherWing
RELAY = DigitalInOut(board.D10)
RELAY.direction = Direction.OUTPUT

# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.D13)
esp32_ready = DigitalInOut(board.D11)
esp32_reset = DigitalInOut(board.D12)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
"""Use below for Most Boards"""
status_light = neopixel.NeoPixel(
    board.NEOPIXEL, 1, brightness=0.2
)  # Uncomment for Most Boards
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

# Define callback functions which will be called when certain events happen.
# pylint: disable=unused-argument
def connected(client):
    client.subscribe("lamp")


def subscribe(client, userdata, topic, granted_qos):
    # This method is called when the client subscribes to a new feed.
    print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))


def on_lamp(client, topic, message):
    RELAY.value = eval(message)  # pylint: disable=eval-used


# Connect to WiFi
print("Connecting to WiFi...")
wifi.connect()
print("Connected!")

# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)

# Initialize a new MQTT Client object
mqtt_client = MQTT.MQTT(
    broker="io.adafruit.com",
    username=secrets["aio_username"],
    password=secrets["aio_key"],
)


# Initialize an Adafruit IO MQTT Client
io = IO_MQTT(mqtt_client)

io.add_feed_callback("lamp", on_lamp)
# Connect the callback methods defined above to Adafruit IO
io.on_connect = connected
io.on_subscribe = subscribe

# Connect to Adafruit IO
print("Connecting to Adafruit IO...")
io.connect()

io.get("lamp")

while True:
    io.loop()

The first thing you'll need to do is copy all the required libraries over. The required libraries are:

  • adafruit_esp32spi/
  • adafruit_minimqtt/
  • adafruit_io/
  • neopixel.mpy
  • adafruit_requests.mpy

After you've copied all that over, rename relay_daughter.py to code.py and copy it over to the CIRCUITPY drive.

Finally, copy secrets.py to the CIRCUITPY drive.

After you've done all that, this is what your CIRCUITPY drive should look like:

Assembly

Solder all the headers to the devices they came with. Then, put everything on the FeatherWing Tripler.

If you're using the Four Outlet Power Relay, then plug a breadboard wire into pin D10 on the FeatherWing Tripler and another wire into the GND pin. Then pull out the green part that the pins go into in the Relay itself and plug the wire from D10 into the receptacle marked with a + (the one on the left) and the wire from GND into the receptacle with a - (the one on the right). Then, screw in the two screws that hold the wires in place and plug the green part back into the Power Relay.

Now that you've set everything up, this is what yours should look like if you're using the Relay FeatherWing:

And, if you're using the 4-Outlet Power Relay, this is what it should look like:

This guide was first published on May 26, 2021. It was last updated on 2021-05-26 10:50:53 -0400.

This page (Relay Control Example) was last updated on Sep 18, 2021.

Text editor powered by tinymce.