Page 1 of 1

Async main and WebREPL

Posted: Thu Jan 07, 2021 4:14 pm
by Divergentti
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?

Re: Async main and WebREPL

Posted: Fri Jan 08, 2021 9:08 am
by Lennyz1988
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

Re: Async main and WebREPL

Posted: Fri Jan 08, 2021 9:35 am
by Divergentti
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