2
2
# SPDX-FileCopyrightText: Copyright (c) 2021 Melissa LeBlanc-Williams for Adafruit Industries
3
3
#
4
4
# SPDX-License-Identifier: MIT
5
+ import os
5
6
import time
6
7
import json
7
8
from adafruit_display_shapes .circle import Circle
16
17
INITIAL_LIGHT_COLOR = 0x008000
17
18
USE_FAHRENHEIT = True
18
19
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
-
25
20
funhouse = FunHouse (default_bg = 0x0F0F00 )
26
21
funhouse .peripherals .dotstars .fill (INITIAL_LIGHT_COLOR )
27
22
73
68
status = Circle (229 , 10 , 10 , fill = 0xFF0000 , outline = 0x880000 )
74
69
funhouse .splash .append (status )
75
70
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 ):
96
72
status .fill = 0x00FF00
97
73
status .outline = 0x008800
98
74
print ("Connected to MQTT! Subscribing..." )
99
75
client .subscribe (LIGHT_COMMAND_TOPIC )
100
76
101
77
102
- def disconnected (client ):
78
+ def disconnected (_client ):
103
79
status .fill = 0xFF0000
104
80
status .outline = 0x880000
105
81
106
82
107
- def message (client , topic , payload ):
83
+ def message (_client , topic , payload ):
108
84
print ("Topic {0} received new value: {1}" .format (topic , payload ))
109
85
if topic == LIGHT_COMMAND_TOPIC :
110
86
settings = json .loads (payload )
@@ -122,29 +98,28 @@ def message(client, topic, payload):
122
98
123
99
def publish_light_state ():
124
100
funhouse .peripherals .led = True
125
- output = {
101
+ publish_output = {
126
102
"brightness" : round (funhouse .peripherals .dotstars .brightness * 255 ),
127
103
"state" : "on" if funhouse .peripherals .dotstars .brightness > 0 else "off" ,
128
104
"color" : funhouse .peripherals .dotstars [0 ],
129
105
}
130
106
# Publish the Dotstar State
131
107
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 ))
133
109
funhouse .peripherals .led = False
134
110
135
-
136
111
# Initialize a new MQTT Client object
137
112
funhouse .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" ) ,
142
117
)
143
118
funhouse .network .on_mqtt_connect = connected
144
119
funhouse .network .on_mqtt_disconnect = disconnected
145
120
funhouse .network .on_mqtt_message = message
146
121
147
- print ("Attempting to connect to {}" .format (secrets [ "mqtt_broker" ] ))
122
+ print ("Attempting to connect to {}" .format (os . getenv ( "MQTT_BROKER" ) ))
148
123
funhouse .network .mqtt_connect ()
149
124
150
125
last_publish_timestamp = None
@@ -162,7 +137,6 @@ def publish_light_state():
162
137
last_peripheral_state ["pir_sensor" ] = funhouse .peripherals .pir_sensor
163
138
164
139
environment = {}
165
- update_enviro ()
166
140
last_environment_timestamp = time .monotonic ()
167
141
168
142
# Provide Initial light state
@@ -172,7 +146,20 @@ def publish_light_state():
172
146
if not environment or (
173
147
time .monotonic () - last_environment_timestamp > ENVIRONMENT_CHECK_DELAY
174
148
):
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 )
176
163
last_environment_timestamp = time .monotonic ()
177
164
output = environment
178
165
0 commit comments