Light and motion data
This commit is contained in:
50
main.py
50
main.py
@@ -85,6 +85,9 @@ motion_timeout_ms = 18000 # Default value, it will be recalculated
|
|||||||
motion_started_ms = 0
|
motion_started_ms = 0
|
||||||
motion_state_on = False
|
motion_state_on = False
|
||||||
|
|
||||||
|
light_raw = 0
|
||||||
|
light = 0
|
||||||
|
|
||||||
running = True
|
running = True
|
||||||
|
|
||||||
def web_thread():
|
def web_thread():
|
||||||
@@ -113,31 +116,40 @@ def web_thread():
|
|||||||
serverSocket.listen()
|
serverSocket.listen()
|
||||||
|
|
||||||
last_wlan_status = wlan.status()
|
last_wlan_status = wlan.status()
|
||||||
|
last_wlan_isconnected = wlan.isconnected()
|
||||||
|
|
||||||
while running:
|
while running:
|
||||||
try:
|
try:
|
||||||
reconnect_wifi = False
|
reconnect_wifi = False
|
||||||
|
|
||||||
if wlan.status() != last_wlan_status:
|
wlan_status = wlan.status()
|
||||||
last_wlan_status = wlan.status()
|
wlan_is_connected = wlan.isconnected()
|
||||||
|
|
||||||
|
if wlan_status != last_wlan_status or wlan_is_connected != last_wlan_isconnected:
|
||||||
|
print("WLAN status changed " + str(wlan_status) + " " + str(wlan_is_connected))
|
||||||
|
|
||||||
|
if wlan_status != last_wlan_status:
|
||||||
print('WLAN status changed to ' + str(last_wlan_status))
|
print('WLAN status changed to ' + str(last_wlan_status))
|
||||||
|
|
||||||
if wlan.status() < 0 or wlan.status() > 3:
|
if wlan_status < 0 or wlan_status > 3:
|
||||||
reconnect_wifi = True
|
reconnect_wifi = True
|
||||||
|
|
||||||
if last_wlan_status == network.STAT_IDLE:
|
if last_wlan_status == network.STAT_IDLE:
|
||||||
reconnect_wifi = True
|
reconnect_wifi = True
|
||||||
|
|
||||||
if not wlan.isconnected():
|
if not wlan_is_connected:
|
||||||
reconnect_wifi = True
|
reconnect_wifi = True
|
||||||
|
|
||||||
|
last_wlan_isconnected = wlan_is_connected
|
||||||
|
last_wlan_status = wlan_status
|
||||||
|
|
||||||
if reconnect_wifi:
|
if reconnect_wifi:
|
||||||
print('Disconnecting from WiFi')
|
print('Disconnecting from WiFi')
|
||||||
disconnect_wifi(wlan)
|
disconnect_wifi(wlan)
|
||||||
print('Reconnecting to WiFi')
|
print('Reconnecting to WiFi')
|
||||||
while not connect_wifi(wlan, ssid, pw, blink_onboard_led):
|
while not connect_wifi(wlan, ssid, pw, blink_onboard_led):
|
||||||
print('Could not connect to WiFi, retrying in 5 seconds')
|
print('Could not connect to WiFi, retrying in 5 seconds')
|
||||||
time.sleep(5)
|
time.sleep(10)
|
||||||
print('Reconnected to WiFi')
|
print('Reconnected to WiFi')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("caught exception in web loop in wlan checking {} {}".format(type(e).__name__, e))
|
print("caught exception in web loop in wlan checking {} {}".format(type(e).__name__, e))
|
||||||
@@ -162,7 +174,10 @@ def web_thread():
|
|||||||
result_ok(cl,
|
result_ok(cl,
|
||||||
"{" +
|
"{" +
|
||||||
"\"humidity\": \"" + with_fallback_to_str(humidity, "NaN") + "\"," +
|
"\"humidity\": \"" + with_fallback_to_str(humidity, "NaN") + "\"," +
|
||||||
"\"temp\": \"" + with_fallback_to_str(temperature, "NaN")+"\""+
|
"\"temp\": \"" + with_fallback_to_str(temperature, "NaN")+"\","+
|
||||||
|
"\"motion\": \"" + with_fallback_to_str(motion_state_on, "NaN")+"\","+
|
||||||
|
"\"light_raw\": \"" + with_fallback_to_str(light_raw, "NaN")+"\","+
|
||||||
|
"\"light\": \"" + with_fallback_to_str(light, "NaN")+"\""+
|
||||||
"}",
|
"}",
|
||||||
'application/json')
|
'application/json')
|
||||||
elif request.find('/toggle_temp') == GET_PATH_START:
|
elif request.find('/toggle_temp') == GET_PATH_START:
|
||||||
@@ -239,7 +254,15 @@ temperature""" + attrs + with_fallback_to_str(temperature, "NaN") +
|
|||||||
"""
|
"""
|
||||||
# HELP humidity Relative humidity in %
|
# HELP humidity Relative humidity in %
|
||||||
# TYPE humidity gauge
|
# TYPE humidity gauge
|
||||||
humidity""" + attrs + with_fallback_to_str(humidity, "NaN"))
|
humidity""" + attrs + with_fallback_to_str(humidity, "NaN") +
|
||||||
|
"""
|
||||||
|
# HELP light Normalized light level
|
||||||
|
# TYPE light gauge
|
||||||
|
light""" + attrs + with_fallback_to_str(light, "NaN") +
|
||||||
|
"""
|
||||||
|
# HELP light_raw Raw light resistance value 0-65535
|
||||||
|
# TYPE light_raw gauge
|
||||||
|
light_raw""" + attrs + with_fallback_to_str(light_raw, "NaN"))
|
||||||
result_ok(cl, content)
|
result_ok(cl, content)
|
||||||
else:
|
else:
|
||||||
result_notfound(cl)
|
result_notfound(cl)
|
||||||
@@ -268,6 +291,9 @@ def display_thread():
|
|||||||
global temp_sht4x
|
global temp_sht4x
|
||||||
global humidity_sht4x
|
global humidity_sht4x
|
||||||
|
|
||||||
|
global light_raw
|
||||||
|
global light
|
||||||
|
|
||||||
global enable_temp
|
global enable_temp
|
||||||
global enable_hum
|
global enable_hum
|
||||||
global dim_light
|
global dim_light
|
||||||
@@ -295,9 +321,9 @@ def display_thread():
|
|||||||
sensor_update_delta = 3000
|
sensor_update_delta = 3000
|
||||||
last_sensor_update_time = current_time_ms - sensor_update_delta - 1000
|
last_sensor_update_time = current_time_ms - sensor_update_delta - 1000
|
||||||
|
|
||||||
|
sensor_lock_acquired = False
|
||||||
while running:
|
while running:
|
||||||
try:
|
try:
|
||||||
sensor_lock_acquired = False
|
|
||||||
try:
|
try:
|
||||||
if current_time_ms >= last_sensor_update_time + sensor_update_delta:
|
if current_time_ms >= last_sensor_update_time + sensor_update_delta:
|
||||||
sensor_lock_acquired = True
|
sensor_lock_acquired = True
|
||||||
@@ -321,14 +347,14 @@ def display_thread():
|
|||||||
minute = ltime[4]
|
minute = ltime[4]
|
||||||
|
|
||||||
# Light on a 0 - 65535 scale
|
# Light on a 0 - 65535 scale
|
||||||
light = photoRes.read_u16()
|
light_raw = photoRes.read_u16()
|
||||||
|
|
||||||
# We want to use the minimum brightness in darker environments
|
# We want to use the minimum brightness in darker environments
|
||||||
# So we scale the light value to a 0 - 15 scale but uses a 18 (0-17) scale with zeroes at the beggining
|
# So we scale the light value to a 0 - 15 scale but uses a 18 (0-17) scale with zeroes at the beggining
|
||||||
# Because of this the lower (18 [extended range] - 16 [real range] + 1 [0 is 0 on the real range]) * 100 / 18 = 16.6%
|
# Because of this the lower (18 [extended range] - 16 [real range] + 1 [0 is 0 on the real range]) * 100 / 18 = 16.6%
|
||||||
# the brightness will be 0
|
# the brightness will be 0
|
||||||
# We also chop of the top so instead of 18 we use a 20 scale but only subtract 3 from
|
# We also chop of the top so instead of 18 we use a 20 scale but only subtract 3 from
|
||||||
light = light * 19 // 65535 - 3
|
light = light_raw * 19 // 65535 - 3
|
||||||
if light < 0:
|
if light < 0:
|
||||||
light = 0
|
light = 0
|
||||||
|
|
||||||
@@ -346,10 +372,12 @@ def display_thread():
|
|||||||
display.set_brightness(15)
|
display.set_brightness(15)
|
||||||
last_brightness = 15
|
last_brightness = 15
|
||||||
|
|
||||||
|
movement = True if pir_sensor.value() == 1 else False
|
||||||
|
|
||||||
if motion_state_on and current_time_ms >= motion_started_ms + motion_timeout_ms:
|
if motion_state_on and current_time_ms >= motion_started_ms + motion_timeout_ms:
|
||||||
motion_state_on = False
|
motion_state_on = False
|
||||||
|
|
||||||
if motion_state_on == False and enable_motion_detection and pir_sensor.value() == 1:
|
if motion_state_on == False and enable_motion_detection and movement:
|
||||||
motion_state_on = True
|
motion_state_on = True
|
||||||
motion_started_ms = current_time_ms
|
motion_started_ms = current_time_ms
|
||||||
display_mode = 0
|
display_mode = 0
|
||||||
|
|||||||
Reference in New Issue
Block a user