11
11
from adafruit_funhouse import FunHouse
12
12
13
13
PUBLISH_DELAY = 60
14
- ENVIRONMENT_CHECK_DELAY = 1
14
+ ENVIRONMENT_CHECK_DELAY = 5
15
15
ENABLE_PIR = True
16
16
MQTT_TOPIC = "funhouse/state"
17
17
LIGHT_STATE_TOPIC = "funhouse/light/state"
78
78
79
79
def update_enviro ():
80
80
global environment
81
- environment ["temperature" ] = aht20 .temperature
82
- environment ["pressure" ] = dps310 .pressure
83
- environment ["humidity" ] = aht20 .relative_humidity
84
- environment ["light" ] = funhouse .peripherals .light
85
81
86
- temp = environment [ " temperature" ]
82
+ temp = aht20 . temperature
87
83
unit = "C"
88
84
if USE_FAHRENHEIT :
89
85
temp = temp * (9 / 5 ) + 32
90
86
unit = "F"
91
- funhouse .set_text ("{:.1f}{}" .format (temp , unit ), temp_label )
87
+
88
+ environment ["temperature" ] = temp
89
+ environment ["pressure" ] = dps310 .pressure
90
+ environment ["humidity" ] = aht20 .relative_humidity
91
+ environment ["light" ] = funhouse .peripherals .light
92
+
93
+ funhouse .set_text ("{:.1f}{}" .format (environment ["temperature" ], unit ), temp_label )
92
94
funhouse .set_text ("{:.1f}%" .format (environment ["humidity" ]), hum_label )
93
95
funhouse .set_text ("{}kPa" .format (environment ["light" ]), pres_label )
94
96
@@ -109,7 +111,6 @@ def message(client, topic, payload):
109
111
print ("Topic {0} received new value: {1}" .format (topic , payload ))
110
112
if topic == LIGHT_COMMAND_TOPIC :
111
113
settings = json .loads (payload )
112
- print (settings )
113
114
if settings ["state" ] == "on" :
114
115
if "brightness" in settings :
115
116
funhouse .peripherals .dotstars .brightness = settings ["brightness" ] / 255
@@ -125,7 +126,7 @@ def message(client, topic, payload):
125
126
def publish_light_state ():
126
127
funhouse .peripherals .led = True
127
128
output = {
128
- "brightness" : funhouse .peripherals .dotstars .brightness * 255 ,
129
+ "brightness" : round ( funhouse .peripherals .dotstars .brightness * 255 ) ,
129
130
"state" : "on" if funhouse .peripherals .dotstars .brightness > 0 else "off" ,
130
131
"color" : funhouse .peripherals .dotstars [0 ],
131
132
}
@@ -148,6 +149,7 @@ def publish_light_state():
148
149
149
150
print ("Attempting to connect to {}" .format (secrets ["mqtt_broker" ]))
150
151
funhouse .network .mqtt_connect ()
152
+
151
153
last_publish_timestamp = None
152
154
153
155
last_peripheral_state = {
@@ -187,16 +189,16 @@ def publish_light_state():
187
189
188
190
if funhouse .peripherals .slider is not None :
189
191
output ["slider" ] = funhouse .peripherals .slider
192
+ peripheral_state_changed = True
190
193
191
- # every PUBLISH_DELAY, write temp/hum/press
194
+ # Every PUBLISH_DELAY, write temp/hum/press/light or if a peripheral changed
192
195
if (
193
196
last_publish_timestamp is None
194
197
or peripheral_state_changed
195
198
or (time .monotonic () - last_publish_timestamp ) > PUBLISH_DELAY
196
199
):
197
- print ("Sending data to MQTT!" )
198
200
funhouse .peripherals .led = True
199
- print ("Publishing to %s" % MQTT_TOPIC )
201
+ print ("Publishing to {}" . format ( MQTT_TOPIC ) )
200
202
funhouse .network .mqtt_publish (MQTT_TOPIC , json .dumps (output ))
201
203
funhouse .peripherals .led = False
202
204
last_publish_timestamp = time .monotonic ()
0 commit comments