|
| 1 | +# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2021 Melissa LeBlanc-Williams & John Park for Adafruit |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | + |
| 6 | +import board |
| 7 | +from digitalio import DigitalInOut, Direction, Pull |
| 8 | +from adafruit_funhouse import FunHouse |
| 9 | + |
| 10 | +i2c = board.I2C() |
| 11 | + |
| 12 | +RED = 0x200000 |
| 13 | +GREEN = 0x002000 |
| 14 | + |
| 15 | +funhouse = FunHouse(default_bg=None) |
| 16 | +funhouse.peripherals.set_dotstars(RED, RED, RED, RED, RED) |
| 17 | + |
| 18 | +switch = DigitalInOut(board.A1) |
| 19 | +switch.direction = Direction.INPUT |
| 20 | +switch.pull = Pull.UP |
| 21 | + |
| 22 | +# Initialize a new MQTT Client object |
| 23 | +funhouse.network.init_io_mqtt() |
| 24 | + |
| 25 | +print("Connecting to Adafruit IO...") |
| 26 | +funhouse.network.mqtt_connect() |
| 27 | + |
| 28 | +last_door = 1 |
| 29 | + |
| 30 | + |
| 31 | +def send_io_data(door_value): |
| 32 | + funhouse.peripherals.led = True |
| 33 | + print("Sending data to adafruit IO!") |
| 34 | + funhouse.network.mqtt_publish("door", door_value) |
| 35 | + funhouse.peripherals.led = False |
| 36 | + |
| 37 | + |
| 38 | +send_io_data(0) |
| 39 | + |
| 40 | +while True: |
| 41 | + |
| 42 | + if switch.value and last_door is 0: |
| 43 | + print("Door is open") |
| 44 | + funhouse.peripherals.play_tone(2000, 0.25) |
| 45 | + funhouse.peripherals.dotstars.fill(RED) |
| 46 | + last_door = 1 |
| 47 | + send_io_data(0) |
| 48 | + |
| 49 | + elif not switch.value and last_door is 1: |
| 50 | + print("Door is closed") |
| 51 | + funhouse.peripherals.play_tone(800, 0.25) |
| 52 | + funhouse.peripherals.dotstars.fill(GREEN) |
| 53 | + last_door = 0 |
| 54 | + send_io_data(1) |
0 commit comments