import gc
try:
import usocket as socket
except ImportError:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 80))
s.listen(5)
def render_html(context: dict = {}):
html = (
"""
ESP32 Sensor Output
MEASUREMENT | VALUE |
Temp. Celsius | """
+ str(context.get("cel"))
+ """ |
Temp. Fahrenheit | """
+ str(context.get("fahr"))
+ """F |
Light | """
+ str(context.get("light"))
+ """% |
Humidity | """
+ str(context.get("hum"))
+ """% |
"""
)
return html
def http_respond(html: str):
try:
if gc.mem_free() < 102000:
gc.collect()
conn, addr = s.accept()
conn.settimeout(3.0)
print("Got a connection from: {addr}".format(addr=addr))
request = conn.recv(1024)
conn.settimeout(None)
request = str(request)
print("Content = {}".format(request))
conn.send("HTTP/1.1 200 OK\n")
conn.send("Content-Type: text/html\n")
conn.send("Connection: close\n\n")
conn.sendall(html)
conn.close()
except OSError as err:
conn.close()
print("Connection closed")