3

When I run the following code, it runs and print ("Listening, connect your APP to http://192.168.4.1:8080/") and waiting request as web server. During the web server mode, I want the LED to blink that's why I have applied asyncio.

However, unless it receives any request (which activates While True: loop in web server), LED does not respond. I have tried many ways but I could not find a way to toggle of LED during web server mode. You can see the comment regarding to await asyncio.sleep(20) in the code below:

import uasyncio as asyncio
from machine import Pin
import time

LED_PIN = 13
led = Pin(LED_PIN, Pin.OUT, value=1)

async def toggle():
    while True:
        await asyncio.sleep_ms(500)
        led.value(not led.value()) # toggling        

async def webServer(ipAddress):
    s = socket.socket()
    ai = socket.getaddrinfo(ipAddress, 8080)
    print("Bind address info:", ai)
    addr = ai[0][-1]
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind(addr)
    s.listen(2)
    print("Listening, connect your APP to http://%s:8080/" % ipAddress)

    counter = 0
    # await asyncio.sleep(20) # !! if i applied await here, LED toggling 20 secs but web server does not accept any request because "while True" below is not activated during 20 secs.
    while True:
        res = s.accept()
        client_sock = res[0]
        client_addr = res[1]
        print("Client address:", client_addr)
        print("Client socket:", client_sock)

        req = client_sock.recv(1024)
        print("Payload: %s" % req.decode())
        client_sock.send(CONTENT % counter)
        client_sock.close()
        counter += 1
        print()

loop = asyncio.get_event_loop()
loop.create_task(toggle())
loop.create_task(webServer('192.168.4.1'))
loop.run_forever()
1
  • I want to know what is your embedded board name? Thanks in advance. Commented Jun 3, 2018 at 7:31

1 Answer 1

2

Your webServer async function is not really async because it uses blocking IO. At a minimum you need to set the socket to non-blocking mode and use the socket operations provided by asyncio, or even better you should use asyncio.start_server to implement an asynchronous network server.

See the asyncio documentation or e.g. this answer for examples.

Sign up to request clarification or add additional context in comments.

5 Comments

loop.create_task(asyncio.start_server(handle_client, (ap_if.ifconfig()[0]), 8080)) & async def handle_client(reader, writer) is solved the issue, Thanks. Only one issue, it is not overwriting the open connection. It enters web server mode only after reset. In sockets method, s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) was solving the issue, but i could not find a way in asyncio, how it can be corrected.
@Sunrise17 Try passing reuse_addr=True to start_server. (This should be the default under POSIX OS-es.)
After 5 or 6 request as following below, from mobile App(URLSession.shared.dataTask(with: request)) to asyncio server installed on esp8266, server has timed out after a while. Request path: / Content Length: 22 Request headers: Host: 0.0.0.0:10000 Content-Type: application/x-www-form-urlencoded Connection: keep-alive Accept: */* User-Agent: Chroma Accept-Language: Content-Length: 22 Accept-Encoding: gzip, deflate Request payload: {"COLOR":[255,21,226]}
@Sunrise17 Maybe you should ask a different question about this problem, with a minimal example etc...
It was related to asyncio server, in any case, i will open a new question about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.