Async main and WebREPL

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Divergentti
Posts: 67
Joined: Fri Sep 04, 2020 9:27 am
Location: Hanko, Finland
Contact:

Async main and WebREPL

Post by Divergentti » Thu Jan 07, 2021 4:14 pm

I am trying to figure our why this code https://github.com/divergentti/kanalana ... in/main.py prohibits webepl to run. Code seems to work fine, distance is read, stepper motor is rotated, but if I try to connect to the ESP32 with WebREPL client, connection is never established. If I break main.py by ctrl+c from usb serial repl, then WebREPL start working.

The WebREPL is started in the boot.py https://github.com/divergentti/kanalana ... in/boot.py and is not asynchronous. Is that problem?

Lennyz1988
Posts: 6
Joined: Thu Aug 02, 2018 6:22 am

Re: Async main and WebREPL

Post by Lennyz1988 » Fri Jan 08, 2021 9:08 am

I am having the same problem. I cannot use WebREPL if I run other code in the While True: loop.

On Github there are multiple people reporting a similair issue with WebREPL.

https://github.com/micropython/micropython/issues/6748

https://github.com/micropython/micropython/issues/2100

https://github.com/micropython/micropython/issues/6604

Divergentti
Posts: 67
Joined: Fri Sep 04, 2020 9:27 am
Location: Hanko, Finland
Contact:

Re: Async main and WebREPL

Post by Divergentti » Fri Jan 08, 2021 9:35 am

I got this working. I had to change distance measurement loop to use await and add some delay:

Code: Select all

    async def measure_distance_cm(self):
        try:
            distance = self.sensor.distance_cm()
            if distance > 0:
                self.distancecm = distance
                await asyncio.sleep_ms(100)
            else:
                self.distancecm = None
        except OSError as ex:
            print('ERROR getting distance:', ex)

    async def measure_distance_cm_loop(self):
        while True:
            await self.measure_distance_cm()
            await asyncio.sleep_ms(100)
Now WebREPL seems to work fine. Limiter switch reading, motor rotation and continuous measurement seems to work fine. I will continue mqtt-part etc. Initial code here https://github.com/divergentti/kanalana ... in/main.py

3D printer objects for the ESP32 etc. case here https://www.thingiverse.com/thing:4714795

Post Reply