New sensors: SHT4x, BMP390

This commit is contained in:
Ádám Kovács
2023-11-12 10:30:29 +01:00
parent bae126dfad
commit c540ffffe1
7 changed files with 815 additions and 15 deletions

139
main.py
View File

@@ -4,12 +4,31 @@ import socket
import ubinascii
from machine import Pin, I2C
import micropython_mcp9808.mcp9808 as mcp9808
import mcp9808.mcp9808 as mcp9808
import sht4x.sht4x as sht4x
import bmp390.bmp390 as bmp390
from sensor_pack.bus_service import I2cAdapter
from secrets import secrets
GET_PATH_START = 4
POST_PATH_START = 5
def pa_mmhg(value: float) -> float:
"""Convert air pressure from Pa to mm Hg"""
return value*7.50062E-3
def with_fallback(value, fallback):
if value is None:
return fallback
else:
return value
def with_fallback_to_str(value, fallback: str) -> str:
if value is None:
return fallback
else:
return str(value)
led = Pin('LED', Pin.OUT)
def blink_onboard_led(num_blinks):
for i in range(num_blinks):
@@ -30,9 +49,6 @@ if len(devices) != 0:
else:
print("No device found")
mcp9808 = mcp9808.MCP9808(i2c)
ssid = secrets['ssid']
password = secrets['pw']
@@ -65,9 +81,9 @@ blink_onboard_led(2)
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
serverSocket = socket.socket()
serverSocket.bind(addr)
serverSocket.listen(1)
print('listening on', addr)
@@ -81,9 +97,36 @@ def result_notfound(cl, response = None):
cl.send(response if response is not None else "Not Found")
cl.close()
while True:
cl, addr = s.accept()
if devices.count(0x18) > 0:
mcp9808 = mcp9808.MCP9808(i2c)
else:
mcp9808 = None
if devices.count(0x44) > 0:
sht4x = sht4x.SHT4X(i2c)
else:
sht4x = None
if devices.count(0x77) > 0:
adaptor = I2cAdapter(i2c)
bmp390 = bmp390.Bmp390(adaptor)
calibration_data = [bmp390.get_calibration_data(index) for index in range(0, 14)]
print(f"Calibration data: {calibration_data}")
#
bmp390.set_oversampling(2, 3)
bmp390.set_sampling_period(5)
bmp390.set_iir_filter(2)
#
#bmp390.start_measurement(True, True, 1)
else:
bmp390 = None
while True:
print('waiting for client')
cl, addr = serverSocket.accept()
try:
print('client connected from', addr)
request = cl.recv(1024)
@@ -92,15 +135,83 @@ while True:
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_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)
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:
temp = str(mcp9808.temperature)
result_ok(cl, "{ \"temperature\": \"" + temp + "\" }", 'application/json')
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')
elif request.find('/prometheus') == GET_PATH_START:
temp = str(mcp9808.temperature)
content = """# HELP temperature Temperature in Celsius
attrs = "{mac=\"""" + mac_readable + "\",ip=\""+ ip +"\"} "
content = (
"""# HELP temperature Temperature in Celsius
# TYPE temperature gauge
temperature{mac=\"""" + mac_readable + "\",ip=\""+ ip +"\"} " + temp
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)