Page 1 of 1

retrieving data from coro (esp822)

Posted: Tue Jun 04, 2019 3:25 pm
by cemox
Hi, I have managed to read GPS data from UART0 (disabling REPL when reading) and write to UART1 (tx only) on my ESP8266 Lolin board. Using coro's solved lots of problems (thanks to Peter Hinch's fantastic library and tutorial). But, for the time being the "receiver" coro updates the global variables and "sender" coro sends the global variables to UART1. I do not like playing with global variables. I want to pass the GPS data retrieved from "receiver" coro and pass it to "sender" coro. (asyncio is a new territory for me, there may be some stupid lines in the code ). thanks for your help.

Code: Select all

## works
import uos, utime
import uasyncio as asyncio
from machine import UART

uart0 = UART(0)
uart1 = UART(1, 9600)

# res = ""
GPRMC = ""
GPGGA = ""
print("starting now")
utime.sleep(1)

uos.dupterm(None, 1)
uart0.init(9600, rxbuf=80)

uart1.write("should be seen in uart1")

async def sender():
    global GPRMC
    global GPGGA
    swriter = asyncio.StreamWriter(uart1, {})
    while True:
        # uart1.write("**")
        await swriter.awrite(GPRMC)
        await swriter.awrite(GPGGA)

        # await asyncio.sleep_ms(500)
        await asyncio.sleep_ms(1200)
       
async def receiver():
    global GPRMC
    global GPGGA
    sreader = asyncio.StreamReader(uart0)
    # swriter = asyncio.StreamWriter(uart1, {})
    uart1.write("async reader")
    while True:
        GPRMC = ""
        GPRMC = await sreader.readline()
        GPGGA = await sreader.readline()
        # await swriter.awrite(res)

loop = asyncio.get_event_loop()
# loop.create_task(sender())
loop.create_task(receiver())
loop.create_task(sender())
loop.run_forever()

Re: retrieving data from coro (esp822)

Posted: Tue Jun 04, 2019 5:53 pm
by kevinkk525
Since you only send what you receive, I ask myself why you don't just do everything in one coro.

But to answer your question you can use uasynio events.

I'm not answering from my pc so this will be very short:

Ev=Event()

In reader:
Ev.set(read_data)

In writer:
Await Ev
Swriter.write(Ev.data)
Ev.reset() # can't remember the correct method, look it up.

You can find an event class in Peter Hinchs repository or in his mqtt-as library.

Re: retrieving data from coro (esp822)

Posted: Wed Jun 05, 2019 7:22 am
by pythoncoder
Coroutines may be bound methods of a class. This is usually the easiest way to share data between them (via bound variables). You might like to look at my GPS solution which uses this technique.

The synchronisation primitives in my repo are intended to address cases where one coro needs to wait for another to produce data. Polling a bound variable (or global) would be inefficient. So the coro sourcing the data can set an Event when it has data available. The receiving coro waits on the Event, accesses the data, and clears the Event. It's worth reading the doc to see if these can help you.

Re: retrieving data from coro (esp822)

Posted: Wed Jun 05, 2019 10:50 am
by cemox
I actually went through your tutorial and your GPS solution, but I think I should check them once more in detail. Thank you.