From 7bdd85d677d1bf64f3527248b609832077894b8d Mon Sep 17 00:00:00 2001 From: mutatrum Date: Sat, 8 Apr 2023 16:57:01 +0200 Subject: [PATCH] Fix web server only serving file once and wifi country configuration --- .../automation2040w/web_io_interface/main.py | 24 +++++-------------- .../cosmic_unicorn/http_text/html_text.py | 12 ++-------- 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/micropython/examples/automation2040w/web_io_interface/main.py b/micropython/examples/automation2040w/web_io_interface/main.py index dd5b70f9..32217bfb 100644 --- a/micropython/examples/automation2040w/web_io_interface/main.py +++ b/micropython/examples/automation2040w/web_io_interface/main.py @@ -39,9 +39,7 @@ try: from tinyweb.server import webserver except ImportError: - # WIFI settings - WIFI_COUNTRY = "GB" # Change to your local two-letter ISO 3166-1 country code - network_manager = NetworkManager(WIFI_COUNTRY, status_handler=status_handler) + network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler) uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK)) # Install missing module import upip @@ -53,16 +51,6 @@ except ImportError: app = webserver() -# Static page -if is_mini: - html_file = open('index_mini.html', 'r') -else: - html_file = open('index.html', 'r') - -# WIFI settings -WIFI_COUNTRY = "GB" # Change to your local two-letter ISO 3166-1 country code - - class LEDs: def not_exists(self): @@ -196,10 +184,10 @@ class relays: # Index page @app.route('/') async def index(request, response): - # Start HTTP response with content-type text/html - await response.start_html() - # Send actual HTML page - await response.send(html_file.read()) + if is_mini: + await response.send_file('index_mini.html', content_type='text/html') + else: + await response.send_file('index.html', content_type='text/html') # HTTP redirection @@ -211,7 +199,7 @@ async def redirect(request, response): def run(): # Setup wifi - network_manager = NetworkManager(WIFI_COUNTRY, status_handler=status_handler) + network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler) app.add_resource(outputs, '/outputs') app.add_resource(relays, '/relays') diff --git a/micropython/examples/cosmic_unicorn/http_text/html_text.py b/micropython/examples/cosmic_unicorn/http_text/html_text.py index a8845763..5f51c835 100644 --- a/micropython/examples/cosmic_unicorn/http_text/html_text.py +++ b/micropython/examples/cosmic_unicorn/http_text/html_text.py @@ -63,20 +63,12 @@ def status_handler(mode, status, ip): # Create web server application app = webserver() -# Static page -html_file = open('index.html', 'r') - -# WIFI settings -WIFI_COUNTRY = "GB" # Change to your local two-letter ISO 3166-1 country code - # Index page @app.route('/') async def index(request, response): - # Start HTTP response with content-type text/html - await response.start_html() # Send actual HTML page - await response.send(html_file.read()) + await response.send_file('index.html', content_type='text/html') # HTTP redirection @@ -120,7 +112,7 @@ def outline_text(text, x, y): def run(): # Setup wifi - network_manager = NetworkManager(WIFI_COUNTRY, status_handler=status_handler) + network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler) app.add_resource(text, '/update')