Simple test

Ensure your device works with this simple test.

examples/funhouse_simpletest.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2021 Melissa LeBlanc-Williams for Adafruit Industries
 3#
 4# SPDX-License-Identifier: Unlicense
 5import board
 6from digitalio import DigitalInOut, Direction, Pull
 7from adafruit_funhouse import FunHouse
 8
 9funhouse = FunHouse(
10    default_bg=0x0F0F00,
11    scale=2,
12)
13
14funhouse.peripherals.set_dotstars(0x800000, 0x808000, 0x008000, 0x000080, 0x800080)
15
16# sensor setup
17sensors = []
18for p in (board.A0, board.A1, board.A2):
19    sensor = DigitalInOut(p)
20    sensor.direction = Direction.INPUT
21    sensor.pull = Pull.DOWN
22    sensors.append(sensor)
23
24
25def set_label_color(conditional, index, on_color):
26    if conditional:
27        funhouse.set_text_color(on_color, index)
28    else:
29        funhouse.set_text_color(0x606060, index)
30
31
32# Create the labels
33funhouse.display.root_group = None
34slider_label = funhouse.add_text(
35    text="Slider:", text_position=(50, 30), text_color=0x606060
36)
37capright_label = funhouse.add_text(
38    text="Touch", text_position=(85, 10), text_color=0x606060
39)
40pir_label = funhouse.add_text(text="PIR", text_position=(60, 10), text_color=0x606060)
41capleft_label = funhouse.add_text(
42    text="Touch", text_position=(25, 10), text_color=0x606060
43)
44onoff_label = funhouse.add_text(text="OFF", text_position=(10, 25), text_color=0x606060)
45up_label = funhouse.add_text(text="UP", text_position=(10, 10), text_color=0x606060)
46sel_label = funhouse.add_text(text="SEL", text_position=(10, 60), text_color=0x606060)
47down_label = funhouse.add_text(
48    text="DOWN", text_position=(10, 100), text_color=0x606060
49)
50jst1_label = funhouse.add_text(
51    text="SENSOR 1", text_position=(40, 80), text_color=0x606060
52)
53jst2_label = funhouse.add_text(
54    text="SENSOR 2", text_position=(40, 95), text_color=0x606060
55)
56jst3_label = funhouse.add_text(
57    text="SENSOR 3", text_position=(40, 110), text_color=0x606060
58)
59temp_label = funhouse.add_text(
60    text="Temp:", text_position=(50, 45), text_color=0xFF00FF
61)
62pres_label = funhouse.add_text(
63    text="Pres:", text_position=(50, 60), text_color=0xFF00FF
64)
65funhouse.display.root_group = funhouse.splash
66
67while True:
68    funhouse.set_text("Temp %0.1F" % funhouse.peripherals.temperature, temp_label)
69    funhouse.set_text("Pres %d" % funhouse.peripherals.pressure, pres_label)
70
71    print(funhouse.peripherals.temperature, funhouse.peripherals.relative_humidity)
72    set_label_color(funhouse.peripherals.captouch6, onoff_label, 0x00FF00)
73    set_label_color(funhouse.peripherals.captouch7, capleft_label, 0x00FF00)
74    set_label_color(funhouse.peripherals.captouch8, capright_label, 0x00FF00)
75
76    slider = funhouse.peripherals.slider
77    if slider is not None:
78        funhouse.peripherals.dotstars.brightness = slider
79        funhouse.set_text("Slider: %1.1f" % slider, slider_label)
80    set_label_color(slider is not None, slider_label, 0xFFFF00)
81
82    set_label_color(funhouse.peripherals.button_up, up_label, 0xFF0000)
83    set_label_color(funhouse.peripherals.button_sel, sel_label, 0xFFFF00)
84    set_label_color(funhouse.peripherals.button_down, down_label, 0x00FF00)
85
86    set_label_color(funhouse.peripherals.pir_sensor, pir_label, 0xFF0000)
87    set_label_color(sensors[0].value, jst1_label, 0xFFFFFF)
88    set_label_color(sensors[1].value, jst2_label, 0xFFFFFF)
89    set_label_color(sensors[2].value, jst3_label, 0xFFFFFF)

MQTT Example

examples/funhouse_adafruit_io_mqtt.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2021 Melissa LeBlanc-Williams for Adafruit Industries
 3#
 4# SPDX-License-Identifier: MIT
 5import time
 6from adafruit_funhouse import FunHouse
 7
 8funhouse = FunHouse(default_bg=None)
 9funhouse.peripherals.set_dotstars(0x800000, 0x808000, 0x008000, 0x000080, 0x800080)
10
11
12# pylint: disable=unused-argument
13def connected(client):
14    print("Connected to Adafruit IO! Subscribing...")
15    client.subscribe("buzzer")
16    client.subscribe("neopixels")
17
18
19def subscribe(client, userdata, topic, granted_qos):
20    print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
21
22
23def disconnected(client):
24    print("Disconnected from Adafruit IO!")
25
26
27def message(client, feed_id, payload):
28    print("Feed {0} received new value: {1}".format(feed_id, payload))
29    if feed_id == "buzzer":
30        if int(payload) == 1:
31            funhouse.peripherals.play_tone(2000, 0.25)
32    if feed_id == "neopixels":
33        print(payload)
34        color = int(payload[1:], 16)
35        funhouse.peripherals.dotstars.fill(color)
36
37
38# pylint: enable=unused-argument
39
40# Initialize a new MQTT Client object
41funhouse.network.init_io_mqtt()
42funhouse.network.on_mqtt_connect = connected
43funhouse.network.on_mqtt_disconnect = disconnected
44funhouse.network.on_mqtt_subscribe = subscribe
45funhouse.network.on_mqtt_message = message
46
47print("Connecting to Adafruit IO...")
48funhouse.network.mqtt_connect()
49sensorwrite_timestamp = time.monotonic()
50last_pir = None
51
52while True:
53    funhouse.network.mqtt_loop()
54
55    print("Temp %0.1F" % funhouse.peripherals.temperature)
56    print("Pres %d" % funhouse.peripherals.pressure)
57
58    # every 10 seconds, write temp/hum/press
59    if (time.monotonic() - sensorwrite_timestamp) > 10:
60        funhouse.peripherals.led = True
61        print("Sending data to adafruit IO!")
62        funhouse.network.mqtt_publish("temperature", funhouse.peripherals.temperature)
63        funhouse.network.mqtt_publish(
64            "humidity", int(funhouse.peripherals.relative_humidity)
65        )
66        funhouse.network.mqtt_publish("pressure", int(funhouse.peripherals.pressure))
67        sensorwrite_timestamp = time.monotonic()
68        # Send PIR only if changed!
69        if last_pir is None or last_pir != funhouse.peripherals.pir_sensor:
70            last_pir = funhouse.peripherals.pir_sensor
71            funhouse.network.mqtt_publish("pir", "%d" % last_pir)
72        funhouse.peripherals.led = False

Temperature Logger Example

examples/funhouse_temperature_logger.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2021 Melissa LeBlanc-Williams for Adafruit Industries
 3#
 4# SPDX-License-Identifier: MIT
 5"""
 6This example demonstrates how to log temperature on the FunHouse. Due to the sensors being near the
 7power supply, usage of peripherals generates extra heat. By turning off unused peripherals and back
 8on only during usage, it can lower the heat. Using light sleep in between readings will also help.
 9By using an offset, we can improve the accuracy even more. Improving airflow near the FunHouse will
10also help.
11"""
12
13from adafruit_funhouse import FunHouse
14
15funhouse = FunHouse(default_bg=None)
16
17DELAY = 180
18FEED = "temperature"
19TEMPERATURE_OFFSET = (
20    3  # Degrees C to adjust the temperature to compensate for board produced heat
21)
22
23# Turn things off
24funhouse.peripherals.dotstars.fill(0)
25funhouse.display.brightness = 0
26funhouse.network.enabled = False
27
28
29def log_data():
30    print("Logging Temperature")
31    print("Temperature %0.1F" % (funhouse.peripherals.temperature - TEMPERATURE_OFFSET))
32    # Turn on WiFi
33    funhouse.network.enabled = True
34    # Connect to WiFi
35    funhouse.network.connect()
36    # Push to IO using REST
37    funhouse.push_to_io(FEED, funhouse.peripherals.temperature - TEMPERATURE_OFFSET)
38    # Turn off WiFi
39    funhouse.network.enabled = False
40
41
42while True:
43    log_data()
44    print("Sleeping for {} seconds...".format(DELAY))
45    funhouse.enter_light_sleep(DELAY)