22# SPDX-FileCopyrightText: Copyright (c) 2021 Melissa LeBlanc-Williams for Adafruit Industries
33#
44# SPDX-License-Identifier: MIT
5+ import os
56import time
67import json
78from adafruit_display_shapes .circle import Circle
1617INITIAL_LIGHT_COLOR = 0x008000
1718USE_FAHRENHEIT = True
1819
19- try :
20- from secrets import secrets
21- except ImportError :
22- print ("WiFi secrets are kept in secrets.py, please add them there!" )
23- raise
24-
2520funhouse = FunHouse (default_bg = 0x0F0F00 )
2621funhouse .peripherals .dotstars .fill (INITIAL_LIGHT_COLOR )
2722
7368status = Circle (229 , 10 , 10 , fill = 0xFF0000 , outline = 0x880000 )
7469funhouse .splash .append (status )
7570
76- def update_enviro ():
77- global environment
78-
79- temp = funhouse .peripherals .temperature
80- unit = "C"
81- if USE_FAHRENHEIT :
82- temp = temp * (9 / 5 ) + 32
83- unit = "F"
84-
85- environment ["temperature" ] = temp
86- environment ["pressure" ] = funhouse .peripherals .pressure
87- environment ["humidity" ] = funhouse .peripherals .relative_humidity
88- environment ["light" ] = funhouse .peripherals .light
89-
90- funhouse .set_text ("{:.1f}{}" .format (environment ["temperature" ], unit ), temp_label )
91- funhouse .set_text ("{:.1f}%" .format (environment ["humidity" ]), hum_label )
92- funhouse .set_text ("{}hPa" .format (environment ["pressure" ]), pres_label )
93-
94-
95- def connected (client , userdata , result , payload ):
71+ def connected (client , _userdata , _result , _payload ):
9672 status .fill = 0x00FF00
9773 status .outline = 0x008800
9874 print ("Connected to MQTT! Subscribing..." )
9975 client .subscribe (LIGHT_COMMAND_TOPIC )
10076
10177
102- def disconnected (client ):
78+ def disconnected (_client ):
10379 status .fill = 0xFF0000
10480 status .outline = 0x880000
10581
10682
107- def message (client , topic , payload ):
83+ def message (_client , topic , payload ):
10884 print ("Topic {0} received new value: {1}" .format (topic , payload ))
10985 if topic == LIGHT_COMMAND_TOPIC :
11086 settings = json .loads (payload )
@@ -122,29 +98,28 @@ def message(client, topic, payload):
12298
12399def publish_light_state ():
124100 funhouse .peripherals .led = True
125- output = {
101+ publish_output = {
126102 "brightness" : round (funhouse .peripherals .dotstars .brightness * 255 ),
127103 "state" : "on" if funhouse .peripherals .dotstars .brightness > 0 else "off" ,
128104 "color" : funhouse .peripherals .dotstars [0 ],
129105 }
130106 # Publish the Dotstar State
131107 print ("Publishing to {}" .format (LIGHT_STATE_TOPIC ))
132- funhouse .network .mqtt_publish (LIGHT_STATE_TOPIC , json .dumps (output ))
108+ funhouse .network .mqtt_publish (LIGHT_STATE_TOPIC , json .dumps (publish_output ))
133109 funhouse .peripherals .led = False
134110
135-
136111# Initialize a new MQTT Client object
137112funhouse .network .init_mqtt (
138- secrets [ "mqtt_broker" ] ,
139- secrets [ "mqtt_port" ] ,
140- secrets [ "mqtt_username" ] ,
141- secrets [ "mqtt_password" ] ,
113+ os . getenv ( "MQTT_BROKER" ) ,
114+ os . getenv ( "MQTT_PORT" ) ,
115+ os . getenv ( "MQTT_USERNAME" ) ,
116+ os . getenv ( "MQTT_PASSWORD" ) ,
142117)
143118funhouse .network .on_mqtt_connect = connected
144119funhouse .network .on_mqtt_disconnect = disconnected
145120funhouse .network .on_mqtt_message = message
146121
147- print ("Attempting to connect to {}" .format (secrets [ "mqtt_broker" ] ))
122+ print ("Attempting to connect to {}" .format (os . getenv ( "MQTT_BROKER" ) ))
148123funhouse .network .mqtt_connect ()
149124
150125last_publish_timestamp = None
@@ -162,7 +137,6 @@ def publish_light_state():
162137 last_peripheral_state ["pir_sensor" ] = funhouse .peripherals .pir_sensor
163138
164139environment = {}
165- update_enviro ()
166140last_environment_timestamp = time .monotonic ()
167141
168142# Provide Initial light state
@@ -172,7 +146,20 @@ def publish_light_state():
172146 if not environment or (
173147 time .monotonic () - last_environment_timestamp > ENVIRONMENT_CHECK_DELAY
174148 ):
175- update_enviro ()
149+ temp = funhouse .peripherals .temperature
150+ unit = "C"
151+ if USE_FAHRENHEIT :
152+ temp = temp * (9 / 5 ) + 32
153+ unit = "F"
154+
155+ environment ["temperature" ] = temp
156+ environment ["pressure" ] = funhouse .peripherals .pressure
157+ environment ["humidity" ] = funhouse .peripherals .relative_humidity
158+ environment ["light" ] = funhouse .peripherals .light
159+
160+ funhouse .set_text ("{:.1f}{}" .format (environment ["temperature" ], unit ), temp_label )
161+ funhouse .set_text ("{:.1f}%" .format (environment ["humidity" ]), hum_label )
162+ funhouse .set_text ("{}hPa" .format (environment ["pressure" ]), pres_label )
176163 last_environment_timestamp = time .monotonic ()
177164 output = environment
178165
0 commit comments