retrieving data from coro (esp822)

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
User avatar
cemox
Posts: 34
Joined: Mon Oct 08, 2018 5:31 pm
Location: Turkey

retrieving data from coro (esp822)

Post by cemox » Tue Jun 04, 2019 3:25 pm

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()

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: retrieving data from coro (esp822)

Post by kevinkk525 » Tue Jun 04, 2019 5:53 pm

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.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: retrieving data from coro (esp822)

Post by pythoncoder » Wed Jun 05, 2019 7:22 am

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.
Peter Hinch
Index to my micropython libraries.

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

Re: retrieving data from coro (esp822)

Post by cemox » Wed Jun 05, 2019 10:50 am

I actually went through your tutorial and your GPS solution, but I think I should check them once more in detail. Thank you.

Post Reply