Skip to content

Commit 153f886

Browse files
committed
Adding FunHouse Motion Outlet firmware
1 parent 1059e45 commit 153f886

2 files changed

Lines changed: 114 additions & 0 deletions

File tree

FunHouse_Motion_Outlet/code.py

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

0 commit comments

Comments
 (0)