asyncio + Lora

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
mvcorrea
Posts: 1
Joined: Tue Nov 17, 2020 12:35 pm

asyncio + Lora

Post by mvcorrea » Tue Nov 17, 2020 2:03 pm

New to python here but able to do some javascript.
Trying to build a gps tracker sending coords via lorawan!
When learning from you, I found asyncio. So an explendid idea putting the gps read(every second) and lora write(every ~minute) in the event loop!
So I code the gps read using the "task = asyncio.create_task(receiver(gnssData))" and look working ok (code attached and comments are welcome)
Then I reach most of the lora libraries around! I am able to see some "sleeps" mostly to get the hardware up, but my concern is how to make the spi.write asynchronous! thinking thats the blocking part of the code (correct-me if I am wrong). Anyway to make it async? Any async libraries around?
I already have lora working with synchronous code apart from the gps code and I noticed its hard to debug (I see no acknowledge from the gateway)!
Any Help? regards,

Code: Select all

### main.py (using heltec wireless stick)
import uasyncio as asyncio
from gps import receiver
gnssData = {}

async def main():
    task = asyncio.create_task(receiver(gnssData))
    while True:
        await asyncio.sleep(1)
        print('main', gnssData)
        
asyncio.run(main())

### gps.py
import uasyncio as asyncio
import re, utime
from machine import UART

u2 = UART(2, 9600, tx=17, rx=22)	# changing pins to make oled available
u2.init(9600, bits=8, parity=None, stop=1)

def parseGNRMC(line, gnssData):
    if re.match(r'\$GNRMC', line):
        arr = line.decode().strip('\r\n').split(',')        # gps line to array
        gnssData['vel'] = float(arr[7]) * 1.852             # velocity (km/h)
        gnssData['hea'] = float(arr[8])                     # heading (deg)
        gnssData['lat'] = (int(arr[3][0:2]) + (float(arr[3][2:8]) / 60)) * (1 if arr[4] == 'N' else -1)
        gnssData['lng'] = (int(arr[5][0:3]) + (float(arr[5][3:8]) / 60)) * (1 if arr[6] == 'E' else - 1)
        
async def receiver(gnssData):
    sreader = asyncio.StreamReader(u2)
    while True:
        res = await sreader.readline()
        parseGNRMC(res, gnssData)

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

Re: asyncio + Lora

Post by pythoncoder » Tue Nov 17, 2020 6:05 pm

There is an asynchronous GPS library documented here.
Peter Hinch
Index to my micropython libraries.

Post Reply