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
 7
 8from adafruit_funhouse import FunHouse
 9
10funhouse = FunHouse(
11    default_bg=0x0F0F00,
12    scale=2,
13)
14
15funhouse.peripherals.set_dotstars(0x800000, 0x808000, 0x008000, 0x000080, 0x800080)
16
17# sensor setup
18sensors = []
19for p in (board.A0, board.A1, board.A2):
20    sensor = DigitalInOut(p)
21    sensor.direction = Direction.INPUT
22    sensor.pull = Pull.DOWN
23    sensors.append(sensor)
24
25
26def set_label_color(conditional, index, on_color):
27    if conditional:
28        funhouse.set_text_color(on_color, index)
29    else:
30        funhouse.set_text_color(0x606060, index)
31
32
33# Create the labels
34funhouse.display.root_group = None
35slider_label = funhouse.add_text(text="Slider:", text_position=(50, 30), text_color=0x606060)
36capright_label = funhouse.add_text(text="Touch", text_position=(85, 10), text_color=0x606060)
37pir_label = funhouse.add_text(text="PIR", text_position=(60, 10), text_color=0x606060)
38capleft_label = funhouse.add_text(text="Touch", text_position=(25, 10), text_color=0x606060)
39onoff_label = funhouse.add_text(text="OFF", text_position=(10, 25), text_color=0x606060)
40up_label = funhouse.add_text(text="UP", text_position=(10, 10), text_color=0x606060)
41sel_label = funhouse.add_text(text="SEL", text_position=(10, 60), text_color=0x606060)
42down_label = funhouse.add_text(text="DOWN", text_position=(10, 100), text_color=0x606060)
43jst1_label = funhouse.add_text(text="SENSOR 1", text_position=(40, 80), text_color=0x606060)
44jst2_label = funhouse.add_text(text="SENSOR 2", text_position=(40, 95), text_color=0x606060)
45jst3_label = funhouse.add_text(text="SENSOR 3", text_position=(40, 110), text_color=0x606060)
46temp_label = funhouse.add_text(text="Temp:", text_position=(50, 45), text_color=0xFF00FF)
47pres_label = funhouse.add_text(text="Pres:", text_position=(50, 60), text_color=0xFF00FF)
48funhouse.display.root_group = funhouse.root_group
49
50while True:
51    funhouse.set_text(f"Temp {funhouse.peripherals.temperature:0.1F}", temp_label)
52    funhouse.set_text(f"Pres {funhouse.peripherals.pressure:d}", pres_label)
53
54    print(funhouse.peripherals.temperature, funhouse.peripherals.relative_humidity)
55    set_label_color(funhouse.peripherals.captouch6, onoff_label, 0x00FF00)
56    set_label_color(funhouse.peripherals.captouch7, capleft_label, 0x00FF00)
57    set_label_color(funhouse.peripherals.captouch8, capright_label, 0x00FF00)
58
59    slider = funhouse.peripherals.slider
60    if slider is not None:
61        funhouse.peripherals.dotstars.brightness = slider
62        funhouse.set_text(f"Slider: {slider:1.1f}", slider_label)
63    set_label_color(slider is not None, slider_label, 0xFFFF00)
64
65    set_label_color(funhouse.peripherals.button_up, up_label, 0xFF0000)
66    set_label_color(funhouse.peripherals.button_sel, sel_label, 0xFFFF00)
67    set_label_color(funhouse.peripherals.button_down, down_label, 0x00FF00)
68
69    set_label_color(funhouse.peripherals.pir_sensor, pir_label, 0xFF0000)
70    set_label_color(sensors[0].value, jst1_label, 0xFFFFFF)
71    set_label_color(sensors[1].value, jst2_label, 0xFFFFFF)
72    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
 6
 7from adafruit_funhouse import FunHouse
 8
 9funhouse = FunHouse(default_bg=None)
10funhouse.peripherals.set_dotstars(0x800000, 0x808000, 0x008000, 0x000080, 0x800080)
11
12
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(f"Subscribed to {topic} with QOS level {granted_qos}")
21
22
23def disconnected(client):
24    print("Disconnected from Adafruit IO!")
25
26
27def message(client, feed_id, payload):
28    print(f"Feed {feed_id} received new value: {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# Initialize a new MQTT Client object
39funhouse.network.init_io_mqtt()
40funhouse.network.on_mqtt_connect = connected
41funhouse.network.on_mqtt_disconnect = disconnected
42funhouse.network.on_mqtt_subscribe = subscribe
43funhouse.network.on_mqtt_message = message
44
45print("Connecting to Adafruit IO...")
46funhouse.network.mqtt_connect()
47sensorwrite_timestamp = time.monotonic()
48last_pir = None
49
50while True:
51    funhouse.network.mqtt_loop()
52
53    print(f"Temp {funhouse.peripherals.temperature:0.1F}")
54    print(f"Pres {funhouse.peripherals.pressure:d}")
55
56    # every 10 seconds, write temp/hum/press
57    if (time.monotonic() - sensorwrite_timestamp) > 10:
58        funhouse.peripherals.led = True
59        print("Sending data to adafruit IO!")
60        funhouse.network.mqtt_publish("temperature", funhouse.peripherals.temperature)
61        funhouse.network.mqtt_publish("humidity", int(funhouse.peripherals.relative_humidity))
62        funhouse.network.mqtt_publish("pressure", int(funhouse.peripherals.pressure))
63        sensorwrite_timestamp = time.monotonic()
64        # Send PIR only if changed!
65        if last_pir is None or last_pir != funhouse.peripherals.pir_sensor:
66            last_pir = funhouse.peripherals.pir_sensor
67            funhouse.network.mqtt_publish("pir", f"{last_pir:d}")
68        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 = 3  # Degrees C to adjust the temperature to compensate for board produced heat
20
21# Turn things off
22funhouse.peripherals.dotstars.fill(0)
23funhouse.display.brightness = 0
24funhouse.network.enabled = False
25
26
27def log_data():
28    print("Logging Temperature")
29    print("Temperature %0.1F" % (funhouse.peripherals.temperature - TEMPERATURE_OFFSET))
30    # Turn on WiFi
31    funhouse.network.enabled = True
32    # Connect to WiFi
33    funhouse.network.connect()
34    # Push to IO using REST
35    funhouse.push_to_io(FEED, funhouse.peripherals.temperature - TEMPERATURE_OFFSET)
36    # Turn off WiFi
37    funhouse.network.enabled = False
38
39
40while True:
41    log_data()
42    print(f"Sleeping for {DELAY} seconds...")
43    funhouse.enter_light_sleep(DELAY)