Add try catch for stopping webserver
This commit is contained in:
178
main.py
178
main.py
@@ -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,99 +129,102 @@ if devices.count(0x77) > 0:
|
|||||||
else:
|
else:
|
||||||
bmp390 = None
|
bmp390 = None
|
||||||
|
|
||||||
while True:
|
try:
|
||||||
print('waiting for client')
|
while True:
|
||||||
cl, addr = serverSocket.accept()
|
print('waiting for client')
|
||||||
try:
|
cl, addr = serverSocket.accept()
|
||||||
print('client connected from', addr)
|
try:
|
||||||
request = cl.recv(1024)
|
print('client connected from', addr)
|
||||||
|
request = cl.recv(1024)
|
||||||
|
|
||||||
request = str(request)
|
request = str(request)
|
||||||
request = request[2:-1] # remove b' and ' from string
|
request = request[2:-1] # remove b' and ' from string
|
||||||
print(request)
|
print(request)
|
||||||
|
|
||||||
temp_mcp9808 = str(mcp9808.temperature) if mcp9808 is not None else 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_sht4x, humidity_sht4x = sht4x.measurements if sht4x is not None else (None,None)
|
||||||
|
|
||||||
temp_bmp390, pressure_bmp390, time_bmp390 = None, None, None
|
temp_bmp390, pressure_bmp390, time_bmp390 = None, None, None
|
||||||
if bmp390 is not None:
|
if bmp390 is not None:
|
||||||
#bmp390.start_measurement(True, True, 1)
|
#bmp390.start_measurement(True, True, 1)
|
||||||
s = bmp390.get_status()
|
|
||||||
while not s[2] or not s[1]:
|
|
||||||
time.sleep_ms(10)
|
|
||||||
s = bmp390.get_status()
|
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:
|
if request.find('/ ') == GET_PATH_START:
|
||||||
result_ok(cl,
|
result_ok(cl,
|
||||||
"{" +
|
"{" +
|
||||||
"\"temp_mcp9808\": \"" + with_fallback_to_str(temp_mcp9808, "NaN") + "\","+
|
"\"temp_mcp9808\": \"" + with_fallback_to_str(temp_mcp9808, "NaN") + "\","+
|
||||||
"\"temp_sht4x\": \"" + with_fallback_to_str(temp_sht4x, "NaN")+"\","+
|
"\"temp_sht4x\": \"" + with_fallback_to_str(temp_sht4x, "NaN")+"\","+
|
||||||
"\"humidity_sht4x\": \"" + with_fallback_to_str(humidity_sht4x, "NaN") + "\"," +
|
"\"humidity_sht4x\": \"" + with_fallback_to_str(humidity_sht4x, "NaN") + "\"," +
|
||||||
"\"temp_bmp390\": \"" + with_fallback_to_str(temp_bmp390, "NaN") + "\"," +
|
"\"temp_bmp390\": \"" + with_fallback_to_str(temp_bmp390, "NaN") + "\"," +
|
||||||
"\"pressure_bmp390\": \"" + with_fallback_to_str(pressure_bmp390, "NaN") + "\"," +
|
"\"pressure_bmp390\": \"" + with_fallback_to_str(pressure_bmp390, "NaN") + "\"," +
|
||||||
"\"time_bmp390\": \"" + with_fallback_to_str(time_bmp390, "NaN") + "\"" +
|
"\"time_bmp390\": \"" + with_fallback_to_str(time_bmp390, "NaN") + "\"" +
|
||||||
"}",
|
"}",
|
||||||
'application/json')
|
'application/json')
|
||||||
|
|
||||||
if request.find('/homeassistant ') == GET_PATH_START:
|
if request.find('/homeassistant ') == GET_PATH_START:
|
||||||
result_ok(cl,
|
result_ok(cl,
|
||||||
"{" +
|
"{" +
|
||||||
"\"temperature\": \"" + with_fallback_to_str(temp_mcp9808, "NaN") + "\","+
|
"\"temperature\": \"" + with_fallback_to_str(temp_mcp9808, "NaN") + "\","+
|
||||||
"\"humidity\": \"" + with_fallback_to_str(humidity_sht4x, "NaN") + "\"," +
|
"\"humidity\": \"" + with_fallback_to_str(humidity_sht4x, "NaN") + "\"," +
|
||||||
"\"pressure\": \"" + with_fallback_to_str(pressure_bmp390, "NaN") + "\"," +
|
"\"pressure\": \"" + with_fallback_to_str(pressure_bmp390, "NaN") + "\"," +
|
||||||
"\"temp_mcp9808\": \"" + with_fallback_to_str(temp_mcp9808, "NaN") + "\","+
|
"\"temp_mcp9808\": \"" + with_fallback_to_str(temp_mcp9808, "NaN") + "\","+
|
||||||
"\"temp_sht4x\": \"" + with_fallback_to_str(temp_sht4x, "NaN")+"\","+
|
"\"temp_sht4x\": \"" + with_fallback_to_str(temp_sht4x, "NaN")+"\","+
|
||||||
"\"humidity_sht4x\": \"" + with_fallback_to_str(humidity_sht4x, "NaN") + "\"," +
|
"\"humidity_sht4x\": \"" + with_fallback_to_str(humidity_sht4x, "NaN") + "\"," +
|
||||||
"\"temp_bmp390\": \"" + with_fallback_to_str(temp_bmp390, "NaN") + "\"," +
|
"\"temp_bmp390\": \"" + with_fallback_to_str(temp_bmp390, "NaN") + "\"," +
|
||||||
"\"pressure_bmp390\": \"" + with_fallback_to_str(pressure_bmp390, "NaN") + "\"," +
|
"\"pressure_bmp390\": \"" + with_fallback_to_str(pressure_bmp390, "NaN") + "\"," +
|
||||||
"\"time_bmp390\": \"" + with_fallback_to_str(time_bmp390, "NaN") + "\"" +
|
"\"time_bmp390\": \"" + with_fallback_to_str(time_bmp390, "NaN") + "\"" +
|
||||||
"}",
|
"}",
|
||||||
'application/json')
|
'application/json')
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
cl.close()
|
cl.close()
|
||||||
print('connection closed')
|
print('connection closed')
|
||||||
|
except:
|
||||||
|
serverSocket.close()
|
||||||
Reference in New Issue
Block a user