|
| 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 | + |
| 6 | +import time |
| 7 | +import board |
| 8 | +import digitalio |
| 9 | +from adafruit_display_shapes.circle import Circle |
| 10 | +from adafruit_funhouse import FunHouse |
| 11 | + |
| 12 | +OUTLET_STATE_TOPIC = "funhouse/outlet/state" |
| 13 | +OUTLET_COMMAND_TOPIC = "funhouse/outlet/set" |
| 14 | +MOTION_TIMEOUT = 300 # Timeout in seconds |
| 15 | + |
| 16 | +try: |
| 17 | + from secrets import secrets |
| 18 | +except ImportError: |
| 19 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 20 | + raise |
| 21 | + |
| 22 | +def set_outlet_state(value): |
| 23 | + global last_pir_timestamp |
| 24 | + if value: |
| 25 | + funhouse.peripherals.dotstars.fill(0x00FF00) |
| 26 | + last_pir_timestamp = time.monotonic() |
| 27 | + else: |
| 28 | + funhouse.peripherals.dotstars.fill(0xFF0000) |
| 29 | + last_pir_timestamp = time.monotonic() - MOTION_TIMEOUT |
| 30 | + |
| 31 | + outlet.value = value |
| 32 | + publish_outlet_state() |
| 33 | + |
| 34 | +def publish_outlet_state(): |
| 35 | + if OUTLET_STATE_TOPIC and OUTLET_COMMAND_TOPIC: |
| 36 | + funhouse.peripherals.led = True |
| 37 | + output = "on" if outlet.value else "off" |
| 38 | + # Publish the Dotstar State |
| 39 | + print("Publishing to {}".format(OUTLET_STATE_TOPIC)) |
| 40 | + funhouse.network.mqtt_publish(OUTLET_STATE_TOPIC, output) |
| 41 | + funhouse.peripherals.led = False |
| 42 | + |
| 43 | +def connected(client, userdata, result, payload): |
| 44 | + status.fill = 0x00FF00 |
| 45 | + status.outline = 0x008800 |
| 46 | + print("Connected to MQTT! Subscribing...") |
| 47 | + client.subscribe(OUTLET_COMMAND_TOPIC) |
| 48 | + |
| 49 | +def disconnected(client): |
| 50 | + status.fill = 0xFF0000 |
| 51 | + status.outline = 0x880000 |
| 52 | + |
| 53 | +def message(client, topic, payload): |
| 54 | + print("Topic {0} received new value: {1}".format(topic, payload)) |
| 55 | + if topic == OUTLET_COMMAND_TOPIC: |
| 56 | + set_outlet_state(payload == "on") |
| 57 | + |
| 58 | +def timeleft(): |
| 59 | + seconds = int(last_pir_timestamp + MOTION_TIMEOUT - time.monotonic()) |
| 60 | + if outlet.value and seconds >= 0: |
| 61 | + minutes = seconds // 60 |
| 62 | + seconds -= minutes * 60 |
| 63 | + return "{:01}:{:02}".format(minutes, seconds) |
| 64 | + return "Off" |
| 65 | + |
| 66 | +# Set Initial States |
| 67 | +funhouse = FunHouse(default_bg=0x0F0F00) |
| 68 | +funhouse.peripherals.dotstars.fill(0) |
| 69 | +outlet = digitalio.DigitalInOut(board.A0) |
| 70 | +outlet.direction = digitalio.Direction.OUTPUT |
| 71 | +last_pir_timestamp = None |
| 72 | +funhouse.display.show(None) |
| 73 | +funhouse.add_text( |
| 74 | + text="Timeout Left:", |
| 75 | + text_position=(20, 60), |
| 76 | + text_color=0xFF0000, |
| 77 | + text_font="fonts/Arial-Bold-24.pcf", |
| 78 | +) |
| 79 | +countdown_label = funhouse.add_text( |
| 80 | + text_position=(120, 100), |
| 81 | + text_anchor_point=(0.5, 0.5), |
| 82 | + text_color=0xFFFF00, |
| 83 | + text_font="fonts/Arial-Bold-24.pcf", |
| 84 | +) |
| 85 | +funhouse.display.show(funhouse.splash) |
| 86 | + |
| 87 | +status = Circle(229, 10, 10, fill=0xFF0000, outline=0x880000) |
| 88 | +funhouse.splash.append(status) |
| 89 | + |
| 90 | +# Initialize a new MQTT Client object |
| 91 | +if OUTLET_STATE_TOPIC and OUTLET_COMMAND_TOPIC: |
| 92 | + funhouse.network.init_mqtt( |
| 93 | + secrets["mqtt_broker"], |
| 94 | + secrets["mqtt_port"], |
| 95 | + secrets["mqtt_username"], |
| 96 | + secrets["mqtt_password"], |
| 97 | + ) |
| 98 | + funhouse.network.on_mqtt_connect = connected |
| 99 | + funhouse.network.on_mqtt_disconnect = disconnected |
| 100 | + funhouse.network.on_mqtt_message = message |
| 101 | + |
| 102 | + print("Attempting to connect to {}".format(secrets["mqtt_broker"])) |
| 103 | + funhouse.network.mqtt_connect() |
| 104 | +set_outlet_state(False) |
| 105 | + |
| 106 | +while True: |
| 107 | + if funhouse.peripherals.pir_sensor: |
| 108 | + last_pir_timestamp = time.monotonic() |
| 109 | + if not outlet.value: |
| 110 | + set_outlet_state(True) |
| 111 | + if outlet.value and time.monotonic() >= last_pir_timestamp + MOTION_TIMEOUT: |
| 112 | + set_outlet_state(False) |
| 113 | + funhouse.set_text(timeleft(), countdown_label) |
| 114 | + # Check any topics we are subscribed to |
| 115 | + funhouse.network.mqtt_loop(0.5) |
0 commit comments