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.show(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.show(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# pylint: disable=unused-argument
12def connected(client):
13    print("Connected to Adafruit IO! Subscribing...")
14    client.subscribe("buzzer")
15    client.subscribe("neopixels")
16
17
18def subscribe(client, userdata, topic, granted_qos):
19    print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
20
21
22def disconnected(client):
23    print("Disconnected from Adafruit IO!")
24
25
26def message(client, feed_id, payload):
27    print("Feed {0} received new value: {1}".format(feed_id, payload))
28    if feed_id == "buzzer":
29        if int(payload) == 1:
30            funhouse.peripherals.play_tone(2000, 0.25)
31    if feed_id == "neopixels":
32        print(payload)
33        color = int(payload[1:], 16)
34        funhouse.peripherals.dotstars.fill(color)
35
36
37# pylint: enable=unused-argument
38
39# Initialize a new MQTT Client object
40funhouse.network.init_io_mqtt()
41funhouse.network.on_mqtt_connect = connected
42funhouse.network.on_mqtt_disconnect = disconnected
43funhouse.network.on_mqtt_subscribe = subscribe
44funhouse.network.on_mqtt_message = message
45
46print("Connecting to Adafruit IO...")
47funhouse.network.mqtt_connect()
48sensorwrite_timestamp = time.monotonic()
49last_pir = None
50
51while True:
52    funhouse.network.mqtt_loop()
53
54    print("Temp %0.1F" % funhouse.peripherals.temperature)
55    print("Pres %d" % funhouse.peripherals.pressure)
56
57    # every 10 seconds, write temp/hum/press
58    if (time.monotonic() - sensorwrite_timestamp) > 10:
59        funhouse.peripherals.led = True
60        print("Sending data to adafruit IO!")
61        funhouse.network.mqtt_publish("temperature", funhouse.peripherals.temperature)
62        funhouse.network.mqtt_publish(
63            "humidity", int(funhouse.peripherals.relative_humidity)
64        )
65        funhouse.network.mqtt_publish("pressure", int(funhouse.peripherals.pressure))
66        sensorwrite_timestamp = time.monotonic()
67        # Send PIR only if changed!
68        if last_pir is None or last_pir != funhouse.peripherals.pir_sensor:
69            last_pir = funhouse.peripherals.pir_sensor
70            funhouse.network.mqtt_publish("pir", "%d" % last_pir)
71        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)