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

178
main.py
View File

@@ -4,6 +4,7 @@ import socket
import ubinascii
from machine import Pin, I2C
from mcp9808.mcp9808 import RESOLUTION_0_625_C
import mcp9808.mcp9808 as mcp9808
import sht4x.sht4x as sht4x
import bmp390.bmp390 as bmp390
@@ -128,99 +129,102 @@ if devices.count(0x77) > 0:
else:
bmp390 = None
while True:
print('waiting for client')
cl, addr = serverSocket.accept()
try:
print('client connected from', addr)
request = cl.recv(1024)
try:
while True:
print('waiting for client')
cl, addr = serverSocket.accept()
try:
print('client connected from', addr)
request = cl.recv(1024)
request = str(request)
request = request[2:-1] # remove b' and ' from string
print(request)
request = str(request)
request = request[2:-1] # remove b' and ' from string
print(request)
temp_mcp9808 = str(mcp9808.temperature) if mcp9808 is not None else None
temp_sht4x, humidity_sht4x = sht4x.measurements if sht4x is not None else (None,None)
temp_mcp9808 = str(mcp9808.temperature) if mcp9808 is not None else None
temp_sht4x, humidity_sht4x = sht4x.measurements if sht4x is not None else (None,None)
temp_bmp390, pressure_bmp390, time_bmp390 = None, None, None
if bmp390 is not None:
#bmp390.start_measurement(True, True, 1)
s = bmp390.get_status()
while not s[2] or not s[1]:
time.sleep_ms(10)
temp_bmp390, pressure_bmp390, time_bmp390 = None, None, None
if bmp390 is not None:
#bmp390.start_measurement(True, True, 1)
s = bmp390.get_status()
temp_bmp390, pressure_bmp390, time_bmp390 = bmp390.get_temperature(), bmp390.get_pressure(), bmp390.get_sensor_time()
while not s[2] or not s[1]:
time.sleep_ms(10)
s = bmp390.get_status()
temp_bmp390, pressure_bmp390, time_bmp390 = bmp390.get_temperature(), bmp390.get_pressure(), bmp390.get_sensor_time()
if request.find('/ ') == GET_PATH_START:
result_ok(cl,
"{" +
"\"temp_mcp9808\": \"" + with_fallback_to_str(temp_mcp9808, "NaN") + "\","+
"\"temp_sht4x\": \"" + with_fallback_to_str(temp_sht4x, "NaN")+"\","+
"\"humidity_sht4x\": \"" + with_fallback_to_str(humidity_sht4x, "NaN") + "\"," +
"\"temp_bmp390\": \"" + with_fallback_to_str(temp_bmp390, "NaN") + "\"," +
"\"pressure_bmp390\": \"" + with_fallback_to_str(pressure_bmp390, "NaN") + "\"," +
"\"time_bmp390\": \"" + with_fallback_to_str(time_bmp390, "NaN") + "\"" +
"}",
'application/json')
if request.find('/ ') == GET_PATH_START:
result_ok(cl,
"{" +
"\"temp_mcp9808\": \"" + with_fallback_to_str(temp_mcp9808, "NaN") + "\","+
"\"temp_sht4x\": \"" + with_fallback_to_str(temp_sht4x, "NaN")+"\","+
"\"humidity_sht4x\": \"" + with_fallback_to_str(humidity_sht4x, "NaN") + "\"," +
"\"temp_bmp390\": \"" + with_fallback_to_str(temp_bmp390, "NaN") + "\"," +
"\"pressure_bmp390\": \"" + with_fallback_to_str(pressure_bmp390, "NaN") + "\"," +
"\"time_bmp390\": \"" + with_fallback_to_str(time_bmp390, "NaN") + "\"" +
"}",
'application/json')
if request.find('/homeassistant ') == GET_PATH_START:
result_ok(cl,
"{" +
"\"temperature\": \"" + with_fallback_to_str(temp_mcp9808, "NaN") + "\","+
"\"humidity\": \"" + with_fallback_to_str(humidity_sht4x, "NaN") + "\"," +
"\"pressure\": \"" + with_fallback_to_str(pressure_bmp390, "NaN") + "\"," +
"\"temp_mcp9808\": \"" + with_fallback_to_str(temp_mcp9808, "NaN") + "\","+
"\"temp_sht4x\": \"" + with_fallback_to_str(temp_sht4x, "NaN")+"\","+
"\"humidity_sht4x\": \"" + with_fallback_to_str(humidity_sht4x, "NaN") + "\"," +
"\"temp_bmp390\": \"" + with_fallback_to_str(temp_bmp390, "NaN") + "\"," +
"\"pressure_bmp390\": \"" + with_fallback_to_str(pressure_bmp390, "NaN") + "\"," +
"\"time_bmp390\": \"" + with_fallback_to_str(time_bmp390, "NaN") + "\"" +
"}",
'application/json')
if request.find('/homeassistant ') == GET_PATH_START:
result_ok(cl,
"{" +
"\"temperature\": \"" + with_fallback_to_str(temp_mcp9808, "NaN") + "\","+
"\"humidity\": \"" + with_fallback_to_str(humidity_sht4x, "NaN") + "\"," +
"\"pressure\": \"" + with_fallback_to_str(pressure_bmp390, "NaN") + "\"," +
"\"temp_mcp9808\": \"" + with_fallback_to_str(temp_mcp9808, "NaN") + "\","+
"\"temp_sht4x\": \"" + with_fallback_to_str(temp_sht4x, "NaN")+"\","+
"\"humidity_sht4x\": \"" + with_fallback_to_str(humidity_sht4x, "NaN") + "\"," +
"\"temp_bmp390\": \"" + with_fallback_to_str(temp_bmp390, "NaN") + "\"," +
"\"pressure_bmp390\": \"" + with_fallback_to_str(pressure_bmp390, "NaN") + "\"," +
"\"time_bmp390\": \"" + with_fallback_to_str(time_bmp390, "NaN") + "\"" +
"}",
'application/json')
elif request.find('/prometheus') == GET_PATH_START:
attrs = "{mac=\"""" + mac_readable + "\",ip=\""+ ip +"\"} "
content = (
"""# HELP temperature Temperature in Celsius
# TYPE temperature gauge
temperature""" + attrs + with_fallback_to_str(temp_mcp9808, "NaN") +
"""
# HELP humidity Relative humidity in %
# TYPE humidity gauge
humidity""" + attrs + with_fallback_to_str(humidity_sht4x, "NaN") +
"""
# HELP pressure Pressure in Pa
# TYPE pressure gauge
pressure""" + attrs + with_fallback_to_str(pressure_bmp390, "NaN") +
"""
# HELP temp_mcp9808 Temperature in Celsius
# TYPE temp_mcp9808 gauge
temp_mcp9808""" + attrs + with_fallback_to_str(temp_mcp9808, "NaN") +
"""
# HELP humidity_sht4x Relative humidity in %
# TYPE humidity_sht4x gauge
humidity_sht4x""" + attrs + with_fallback_to_str(humidity_sht4x, "NaN") +
"""
# HELP temp_sht4x Temperature in Celsius
# TYPE temp_sht4x gauge
temp_sht4x""" + attrs + with_fallback_to_str(temp_sht4x, "NaN") +
"""
# HELP bmp390_temp Temperature in Celsius
# TYPE bmp390_temp gauge
bmp390_temp""" + attrs + with_fallback_to_str(temp_bmp390, "NaN") +
"""
# HELP bmp390_pressure Pressure in Pa
# TYPE bmp390_pressure gauge
bmp390_pressure""" + attrs + with_fallback_to_str(pressure_bmp390, "NaN") +
"""
# HELP bmp390_time Time in ms
# TYPE bmp390_time gauge
bmp390_time""" + attrs + with_fallback_to_str(time_bmp390, "NaN"))
result_ok(cl, content)
else:
result_notfound(cl)
elif request.find('/prometheus') == GET_PATH_START:
attrs = "{mac=\"""" + mac_readable + "\",ip=\""+ ip +"\"} "
content = (
"""# HELP temperature Temperature in Celsius
# TYPE temperature gauge
temperature""" + attrs + with_fallback_to_str(temp_mcp9808, "NaN") +
"""
# HELP humidity Relative humidity in %
# TYPE humidity gauge
humidity""" + attrs + with_fallback_to_str(humidity_sht4x, "NaN") +
"""
# HELP pressure Pressure in Pa
# TYPE pressure gauge
pressure""" + attrs + with_fallback_to_str(pressure_bmp390, "NaN") +
"""
# HELP temp_mcp9808 Temperature in Celsius
# TYPE temp_mcp9808 gauge
temp_mcp9808""" + attrs + with_fallback_to_str(temp_mcp9808, "NaN") +
"""
# HELP humidity_sht4x Relative humidity in %
# TYPE humidity_sht4x gauge
humidity_sht4x""" + attrs + with_fallback_to_str(humidity_sht4x, "NaN") +
"""
# HELP temp_sht4x Temperature in Celsius
# TYPE temp_sht4x gauge
temp_sht4x""" + attrs + with_fallback_to_str(temp_sht4x, "NaN") +
"""
# HELP bmp390_temp Temperature in Celsius
# TYPE bmp390_temp gauge
bmp390_temp""" + attrs + with_fallback_to_str(temp_bmp390, "NaN") +
"""
# HELP bmp390_pressure Pressure in Pa
# TYPE bmp390_pressure gauge
bmp390_pressure""" + attrs + with_fallback_to_str(pressure_bmp390, "NaN") +
"""
# HELP bmp390_time Time in ms
# TYPE bmp390_time gauge
bmp390_time""" + attrs + with_fallback_to_str(time_bmp390, "NaN"))
result_ok(cl, content)
else:
result_notfound(cl)
except OSError as e:
cl.close()
print('connection closed')
except OSError as e:
cl.close()
print('connection closed')
except:
serverSocket.close()