Skip to content

Commit b15ca89

Browse files
committed
Simplify mqtt demo
1 parent a4e914b commit b15ca89

File tree

2 files changed

+11
-59
lines changed

2 files changed

+11
-59
lines changed

examples/funhouse_adafruit_io_mqtt.py

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,36 @@
55
import time
66
import random
77
import board
8-
from digitalio import DigitalInOut, Direction, Pull
98
import adafruit_dps310
109
import adafruit_ahtx0
1110
from adafruit_funhouse import FunHouse
1211

13-
funhouse = FunHouse(
14-
default_bg=0x0F0F00,
15-
scale=2,
16-
)
17-
1812
i2c = board.I2C()
1913
dps310 = adafruit_dps310.DPS310(i2c)
2014
aht20 = adafruit_ahtx0.AHTx0(i2c)
2115

22-
def random_color():
23-
return random.randrange(0, 7) * 32
16+
funhouse = FunHouse(
17+
default_bg=0x0F0F00,
18+
scale=2,
19+
)
2420

2521
funhouse.peripherals.set_dotstars(0x800000, 0x808000, 0x008000, 0x000080, 0x800080)
2622

27-
# sensor setup
28-
sensors = []
29-
for p in (board.A0, board.A1, board.A2):
30-
sensor = DigitalInOut(p)
31-
sensor.direction = Direction.INPUT
32-
sensor.pull = Pull.DOWN
33-
sensors.append(sensor)
34-
35-
def set_label_color(conditional, index, on_color):
36-
if conditional:
37-
funhouse.set_text_color(on_color, index)
38-
else:
39-
funhouse.set_text_color(0x606060, index)
40-
41-
# Create the labels
42-
funhouse.display.show(None)
43-
slider_label = funhouse.add_text(text="Slider:", text_position=(50, 30), text_color=0x606060)
44-
capright_label = funhouse.add_text(text="Touch", text_position=(85, 10), text_color=0x606060)
45-
pir_label = funhouse.add_text(text="PIR", text_position=(60, 10), text_color=0x606060)
46-
capleft_label = funhouse.add_text(text="Touch", text_position=(25, 10), text_color=0x606060)
47-
onoff_label = funhouse.add_text(text="OFF", text_position=(10, 25), text_color=0x606060)
48-
up_label = funhouse.add_text(text="UP", text_position=(10, 10), text_color=0x606060)
49-
sel_label = funhouse.add_text(text="SEL", text_position=(10, 60), text_color=0x606060)
50-
down_label = funhouse.add_text(text="DOWN", text_position=(10, 100), text_color=0x606060)
51-
jst1_label = funhouse.add_text(text="SENSOR 1", text_position=(40, 80), text_color=0x606060)
52-
jst2_label = funhouse.add_text(text="SENSOR 2", text_position=(40, 95), text_color=0x606060)
53-
jst3_label = funhouse.add_text(text="SENSOR 3", text_position=(40, 110), text_color=0x606060)
54-
temp_label = funhouse.add_text(text="Temp:", text_position=(50, 45), text_color=0xFF00FF)
55-
pres_label = funhouse.add_text(text="Pres:", text_position=(50, 60), text_color=0xFF00FF)
56-
funhouse.display.show(funhouse.splash)
57-
58-
#pylint: disable=unused-argument
23+
# pylint: disable=unused-argument
5924
def connected(client):
6025
print("Connected to Adafruit IO! Subscribing...")
6126
client.subscribe("buzzer")
6227
client.subscribe("neopixels")
6328

29+
6430
def subscribe(client, userdata, topic, granted_qos):
6531
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
6632

33+
6734
def disconnected(client):
6835
print("Disconnected from Adafruit IO!")
6936

37+
7038
def message(client, feed_id, payload):
7139
print("Feed {0} received new value: {1}".format(feed_id, payload))
7240
if feed_id == "buzzer":
@@ -77,7 +45,8 @@ def message(client, feed_id, payload):
7745
color = int(payload[1:], 16)
7846
funhouse.peripherals.dotstars.fill(color)
7947

80-
#pylint: enable=unused-argument
48+
49+
# pylint: enable=unused-argument
8150

8251
# Initialize a new MQTT Client object
8352
funhouse.network.init_io_mqtt()
@@ -110,21 +79,3 @@ def message(client, feed_id, payload):
11079
last_pir = funhouse.peripherals.pir_sensor
11180
funhouse.network.mqtt_publish("pir", "%d" % last_pir)
11281
funhouse.peripherals.led = False
113-
114-
set_label_color(funhouse.peripherals.captouch6, onoff_label, 0x00FF00)
115-
set_label_color(funhouse.peripherals.captouch7, capleft_label, 0x00FF00)
116-
set_label_color(funhouse.peripherals.captouch8, capright_label, 0x00FF00)
117-
118-
slider = funhouse.peripherals.slider
119-
if slider is not None:
120-
funhouse.peripherals.dotstars.brightness=slider
121-
funhouse.set_text("Slider: %1.1f" % slider, slider_label)
122-
set_label_color(slider is not None, slider_label, 0xFFFF00)
123-
124-
set_label_color(funhouse.peripherals.button_up, up_label, 0xFF0000)
125-
set_label_color(funhouse.peripherals.button_sel, sel_label, 0xFFFF00)
126-
set_label_color(funhouse.peripherals.button_down, down_label, 0x00FF00)
127-
set_label_color(funhouse.peripherals.pir_sensor, pir_label, 0xFF0000)
128-
set_label_color(sensors[0].value, jst1_label, 0xFFFFFF)
129-
set_label_color(sensors[1].value, jst2_label, 0xFFFFFF)
130-
set_label_color(sensors[2].value, jst3_label, 0xFFFFFF)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ adafruit-circuitpython-portalbase
99
adafruit-circuitpython-dotstar
1010
adafruit-circuitpython-requests
1111
adafruit-circuitpython-simpleio
12+
adafruit-circuitpython-minimqtt

0 commit comments

Comments
 (0)