PicoWAccessPoint icon indicating copy to clipboard operation
PicoWAccessPoint copied to clipboard

Running the server in a thread.

Open saviourbot opened this issue 2 years ago • 0 comments

Thanks so much for the code it is a great help. As my application has a main thread for doing other things I attempted to run the server as a thread using _thread.

I created a proc:

def setupWebServer():
    # Define an access point, name it and then make it active
    ap = network.WLAN(network.AP_IF)
    ap.config(essid=config['ssid'], password=config['password'])
    ap.active(True)
    # Wait until it is active
    while ap.active == False:
        pass
    print("Access point active")
    print(ap.ifconfig())
    app = tinyweb.webserver()
    # Serve a simple Hello World! response when / is called
    # and turn the LED on/off using toggle()
    @app.route('/')
    async def index(request, response):
        # Start HTTP response with content-type text/html
        await response.start_html()
        # Send actual HTML page
        file=open(config["html"]["index"]["body"],"r")
        html=file.read()
        html = html.replace("xxxregxxx",config["reg"])
        file.close()
        await response.send(html)
        led.toggle()
    #return app
    app.run(host="0.0.0.0", port=80)

so that I can call it thus:

config = readConfig()
http_thread = _thread.start_new_thread(setupWebServer,())
while True:
    print(".", end='')
    sleep(1)
    gc.collect()
    #main thread

Whilst the server is working the main thread does not print out, is it possible for the server to run in a thread?

saviourbot avatar Oct 04 '23 11:10 saviourbot