httpserver_simplepolling.py
1 # SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries 2 # 3 # SPDX-License-Identifier: Unlicense 4 5 from secrets import secrets # pylint: disable=no-name-in-module 6 7 import socketpool 8 import wifi 9 10 from adafruit_httpserver import HTTPServer, HTTPResponse 11 12 ssid = secrets["ssid"] 13 print("Connecting to", ssid) 14 wifi.radio.connect(ssid, secrets["password"]) 15 print("Connected to", ssid) 16 print(f"Listening on http://{wifi.radio.ipv4_address}:80") 17 18 pool = socketpool.SocketPool(wifi.radio) 19 server = HTTPServer(pool) 20 21 22 @server.route("/") 23 def base(request): # pylint: disable=unused-argument 24 """Default reponse is /index.html""" 25 return HTTPResponse(filename="/index.html") 26 27 28 # startup the server 29 server.start(str(wifi.radio.ipv4_address)) 30 31 while True: 32 try: 33 # do something useful in this section, 34 # for example read a sensor and capture an average, 35 # or a running total of the last 10 samples 36 37 # process any waiting requests 38 server.poll() 39 except OSError: 40 continue