Skip to content

Commit 7bdd85d

Browse files
committed
Fix web server only serving file once and wifi country configuration
1 parent 7b73521 commit 7bdd85d

File tree

2 files changed

+8
-28
lines changed
  • micropython/examples

2 files changed

+8
-28
lines changed

micropython/examples/automation2040w/web_io_interface/main.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ def status_handler(mode, status, ip):
3939
from tinyweb.server import webserver
4040

4141
except ImportError:
42-
# WIFI settings
43-
WIFI_COUNTRY = "GB" # Change to your local two-letter ISO 3166-1 country code
44-
network_manager = NetworkManager(WIFI_COUNTRY, status_handler=status_handler)
42+
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler)
4543
uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK))
4644
# Install missing module
4745
import upip
@@ -53,16 +51,6 @@ def status_handler(mode, status, ip):
5351
app = webserver()
5452

5553

56-
# Static page
57-
if is_mini:
58-
html_file = open('index_mini.html', 'r')
59-
else:
60-
html_file = open('index.html', 'r')
61-
62-
# WIFI settings
63-
WIFI_COUNTRY = "GB" # Change to your local two-letter ISO 3166-1 country code
64-
65-
6654
class LEDs:
6755

6856
def not_exists(self):
@@ -196,10 +184,10 @@ def post(self, data):
196184
# Index page
197185
@app.route('/')
198186
async def index(request, response):
199-
# Start HTTP response with content-type text/html
200-
await response.start_html()
201-
# Send actual HTML page
202-
await response.send(html_file.read())
187+
if is_mini:
188+
await response.send_file('index_mini.html', content_type='text/html')
189+
else:
190+
await response.send_file('index.html', content_type='text/html')
203191

204192

205193
# HTTP redirection
@@ -211,7 +199,7 @@ async def redirect(request, response):
211199

212200
def run():
213201
# Setup wifi
214-
network_manager = NetworkManager(WIFI_COUNTRY, status_handler=status_handler)
202+
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler)
215203

216204
app.add_resource(outputs, '/outputs')
217205
app.add_resource(relays, '/relays')

micropython/examples/cosmic_unicorn/http_text/html_text.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,12 @@ def status_handler(mode, status, ip):
6363
# Create web server application
6464
app = webserver()
6565

66-
# Static page
67-
html_file = open('index.html', 'r')
68-
69-
# WIFI settings
70-
WIFI_COUNTRY = "GB" # Change to your local two-letter ISO 3166-1 country code
71-
7266

7367
# Index page
7468
@app.route('/')
7569
async def index(request, response):
76-
# Start HTTP response with content-type text/html
77-
await response.start_html()
7870
# Send actual HTML page
79-
await response.send(html_file.read())
71+
await response.send_file('index.html', content_type='text/html')
8072

8173

8274
# HTTP redirection
@@ -120,7 +112,7 @@ def outline_text(text, x, y):
120112

121113
def run():
122114
# Setup wifi
123-
network_manager = NetworkManager(WIFI_COUNTRY, status_handler=status_handler)
115+
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler)
124116

125117
app.add_resource(text, '/update')
126118

0 commit comments

Comments
 (0)