asyncio.start_server example (!)

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
cemox
Posts: 32
Joined: Mon Oct 08, 2018 5:31 pm
Location: Turkey

Re: asyncio.start_server example (!)

Post by cemox » Wed May 06, 2020 12:22 pm

I will change the utime.sleep() to asyncio.sleep(). @pythoncoder and @jomas, thank you both for your comments.

I can listen to the radio channels pretty well now.

Now, I have another problem. I want to control my web radio from a remote web browser, my esp8266 board now works as a (AP) server and (STA) client now. For keeping things simple, I just want to change the radio channel by clicking a button. On the client side,

Code: Select all

reader, writer = await asyncio.open_connection(host, port)
hadles the incoming audio data pretty well. On the server side

Code: Select all

loop.create_task(asyncio.start_server(myWeb.serve, "0.0.0.0", 80))
does the job. All project runs in a asyncio loop. But when I click "Next Station" button on the web browser, it succesfully triggers "change channel function" (I confirmed that this function works), ESP8266 looses internet connection. So the work flow is like this:

1) system initiation
2) picks the firs radio station link
3) plays it
4) starts server, shows a button
5) pushing button triggers "change the channel" function (normallly works fine, tested)
6) cannot connect internet (!)

I suspect these two lines of code clash with each other, bu I cannot figure out how.

As a last resort, I will use two esp8266 boards, one will be responsible for the server side and the other for the client side.

User avatar
cemox
Posts: 32
Joined: Mon Oct 08, 2018 5:31 pm
Location: Turkey

Re: asyncio.start_server example (!)

Post by cemox » Wed May 06, 2020 1:42 pm

Thank you Jomas, waiting for the blank lines solved both the server and the client side communication problems :)
jomas wrote:
Tue May 05, 2020 7:34 pm
cemox wrote:
Tue May 05, 2020 3:59 pm
At last it worked !

Putting a small delay right after

Code: Select all

writer.awrite(resp)
, solved the problem. The whole working code is :

Code: Select all

import uasyncio as asyncio
import utime

async def serve(reader, writer):
    t = utime.ticks_ms()
    resp = b"HTTP/1.0 200 OK\r\n\r\n" + "Ticks = {TICKS}\r\n".format(TICKS=t) 
    l = await reader.read(256)
    print(l)
    await writer.awrite(resp)
    utime.sleep(0.2)
    await writer.wait_closed()

loop = asyncio.get_event_loop()
loop.create_task(asyncio.start_server(serve, "0.0.0.0", 80))
try: 
    loop.run_forever()
except KeyboardInterrupt:
    print("closing")
    loop.close()
You really should have a look how the http protocol works.
In your code you read 256 bytes. Instead you should read lines until you hit a double blank line.
Then you should send your response. But this should better be html code not just text.
After you have send the response you can close the connection

A delay should not be needed if you follow the rules and certainly you should not use 'utime.sleep' because that will block asyncio.
Also better not use 'awrite' because it is legacy code.

Post Reply