Add try catch for stopping webserver

This commit is contained in:
Ádám Kovács
2023-11-28 18:05:14 +01:00
parent 7fb0347b84
commit dd92fbc62f

76
main.py
View File

@@ -4,6 +4,7 @@ import socket
import ubinascii import ubinascii
from machine import Pin, I2C from machine import Pin, I2C
from mcp9808.mcp9808 import RESOLUTION_0_625_C
import mcp9808.mcp9808 as mcp9808 import mcp9808.mcp9808 as mcp9808
import sht4x.sht4x as sht4x import sht4x.sht4x as sht4x
import bmp390.bmp390 as bmp390 import bmp390.bmp390 as bmp390
@@ -128,7 +129,8 @@ if devices.count(0x77) > 0:
else: else:
bmp390 = None bmp390 = None
while True: try:
while True:
print('waiting for client') print('waiting for client')
cl, addr = serverSocket.accept() cl, addr = serverSocket.accept()
try: try:
@@ -181,41 +183,41 @@ while True:
elif request.find('/prometheus') == GET_PATH_START: elif request.find('/prometheus') == GET_PATH_START:
attrs = "{mac=\"""" + mac_readable + "\",ip=\""+ ip +"\"} " attrs = "{mac=\"""" + mac_readable + "\",ip=\""+ ip +"\"} "
content = ( content = (
"""# HELP temperature Temperature in Celsius """# HELP temperature Temperature in Celsius
# TYPE temperature gauge # TYPE temperature gauge
temperature""" + attrs + with_fallback_to_str(temp_mcp9808, "NaN") + temperature""" + attrs + with_fallback_to_str(temp_mcp9808, "NaN") +
""" """
# HELP humidity Relative humidity in % # HELP humidity Relative humidity in %
# TYPE humidity gauge # TYPE humidity gauge
humidity""" + attrs + with_fallback_to_str(humidity_sht4x, "NaN") + humidity""" + attrs + with_fallback_to_str(humidity_sht4x, "NaN") +
""" """
# HELP pressure Pressure in Pa # HELP pressure Pressure in Pa
# TYPE pressure gauge # TYPE pressure gauge
pressure""" + attrs + with_fallback_to_str(pressure_bmp390, "NaN") + pressure""" + attrs + with_fallback_to_str(pressure_bmp390, "NaN") +
""" """
# HELP temp_mcp9808 Temperature in Celsius # HELP temp_mcp9808 Temperature in Celsius
# TYPE temp_mcp9808 gauge # TYPE temp_mcp9808 gauge
temp_mcp9808""" + attrs + with_fallback_to_str(temp_mcp9808, "NaN") + temp_mcp9808""" + attrs + with_fallback_to_str(temp_mcp9808, "NaN") +
""" """
# HELP humidity_sht4x Relative humidity in % # HELP humidity_sht4x Relative humidity in %
# TYPE humidity_sht4x gauge # TYPE humidity_sht4x gauge
humidity_sht4x""" + attrs + with_fallback_to_str(humidity_sht4x, "NaN") + humidity_sht4x""" + attrs + with_fallback_to_str(humidity_sht4x, "NaN") +
""" """
# HELP temp_sht4x Temperature in Celsius # HELP temp_sht4x Temperature in Celsius
# TYPE temp_sht4x gauge # TYPE temp_sht4x gauge
temp_sht4x""" + attrs + with_fallback_to_str(temp_sht4x, "NaN") + temp_sht4x""" + attrs + with_fallback_to_str(temp_sht4x, "NaN") +
""" """
# HELP bmp390_temp Temperature in Celsius # HELP bmp390_temp Temperature in Celsius
# TYPE bmp390_temp gauge # TYPE bmp390_temp gauge
bmp390_temp""" + attrs + with_fallback_to_str(temp_bmp390, "NaN") + bmp390_temp""" + attrs + with_fallback_to_str(temp_bmp390, "NaN") +
""" """
# HELP bmp390_pressure Pressure in Pa # HELP bmp390_pressure Pressure in Pa
# TYPE bmp390_pressure gauge # TYPE bmp390_pressure gauge
bmp390_pressure""" + attrs + with_fallback_to_str(pressure_bmp390, "NaN") + bmp390_pressure""" + attrs + with_fallback_to_str(pressure_bmp390, "NaN") +
""" """
# HELP bmp390_time Time in ms # HELP bmp390_time Time in ms
# TYPE bmp390_time gauge # TYPE bmp390_time gauge
bmp390_time""" + attrs + with_fallback_to_str(time_bmp390, "NaN")) bmp390_time""" + attrs + with_fallback_to_str(time_bmp390, "NaN"))
result_ok(cl, content) result_ok(cl, content)
else: else:
result_notfound(cl) result_notfound(cl)
@@ -224,3 +226,5 @@ bmp390_time""" + attrs + with_fallback_to_str(time_bmp390, "NaN"))
except OSError as e: except OSError as e:
cl.close() cl.close()
print('connection closed') print('connection closed')
except:
serverSocket.close()