diff --git a/webserver/README.md b/webserver/README.md new file mode 100644 index 0000000..eaa93fc --- /dev/null +++ b/webserver/README.md @@ -0,0 +1,3 @@ +# ESP32 Webserver + +This project makes use of the ESP32's wifi capabilities and the `usocket` library to respond to a simple get request and return and HTML response. \ No newline at end of file diff --git a/webserver/boot.py b/webserver/boot.py new file mode 100644 index 0000000..1b308f7 --- /dev/null +++ b/webserver/boot.py @@ -0,0 +1,24 @@ +import esp +import gc +import network + +esp.osdebug(None) +gc.collect() + + +def connect(): + sta_if = network.WLAN(network.STA_IF) + if not sta_if.isconnected(): + print("connecting to wireless network....") + sta_if.active(True) + sta_if.connect(b"Candy", b"whatisdelicious") + while not sta_if.isconnected(): + pass + print("network config:", sta_if.ifconfig()) + + # def no_debug(): + # import esp + # esp.osdebug(None) + + +connect() diff --git a/webserver/main.py b/webserver/main.py new file mode 100644 index 0000000..7c76345 --- /dev/null +++ b/webserver/main.py @@ -0,0 +1,17 @@ +from webserver import render_html, http_respond + +SENSOR_DATA = { + "cel": 22, + "fahr": 71.6, + "light": 90, + "hum": 35, +} + +def report(): + """Mock function to return sensor data""" + return SENSOR_DATA + +while True: + context = report() + html = render_html(context) + http_respond(html) \ No newline at end of file diff --git a/webserver/webserver.py b/webserver/webserver.py new file mode 100644 index 0000000..5206ca7 --- /dev/null +++ b/webserver/webserver.py @@ -0,0 +1,49 @@ +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 = """
+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')) + """% |