From 07bcb4f05ba0d6e566055f9ada05c18a66ed1b4b Mon Sep 17 00:00:00 2001 From: androiddrew Date: Tue, 24 Dec 2019 10:13:55 -0500 Subject: [PATCH] Added simple webserver --- webserver/README.md | 3 +++ webserver/boot.py | 24 +++++++++++++++++++++ webserver/main.py | 17 +++++++++++++++ webserver/webserver.py | 49 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 webserver/README.md create mode 100644 webserver/boot.py create mode 100644 webserver/main.py create mode 100644 webserver/webserver.py 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 = """ +

ESP32 Sensor Output

+ + + + + """ + 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')
MEASUREMENTVALUE
Temp. Celsius""" + str(context.get('cel')) + """
Temp. Fahrenheit""" + str(context.get('fahr')) + """F
Light""" + str(context.get('light')) + """%
Humidity""" + str(context.get('hum')) + """%