unable to access webrepl when main script is running

The official PYBD running MicroPython, and its accessories.
Target audience: Users with a PYBD
MrRobot
Posts: 31
Joined: Mon May 11, 2020 11:30 am

unable to access webrepl when main script is running

Post by MrRobot » Fri Oct 30, 2020 4:30 pm

So I have a data logger running in a infinite loop with a few sensors attached to my Pyboard D.

I want to be able to access the repl using webrepl.

I'm only able to access webrepl when no code is being executed. webrepl won't let me log in if code is running.

Due to the nature of my application I can't stop the infinite loop remotely, as the device is in a waterproof case.

I can see in this thread that someone uses a callback timer : viewtopic.php?f=2&t=7416&p=42743&hilit=webrepl#p42743

But I'm unsure how to implement it for my project.

Essentially my code is this :

Code: Select all


While True:

	sampleInstruments()
	
	sendData()

So is there anywhere I can put a callback or give me a chance to log in to webrepl?

Any help would be greatly appreciated :D

User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: unable to access webrepl when main script is running

Post by MostlyHarmless » Sat Oct 31, 2020 1:29 am

It appears that such a busy loop will block all the polling background services, like uftpd, utelnetserver and webrepl. Inserting a small sleep_ms into it will give them time to react:

Code: Select all

import utime
while True:
    utime.sleep_ms(20)
That said I haven't found a way to deliver a KeyboardInterrupt (^C) when using the in-browser terminal (term.js). It just disconnects with the main program still running. I don't have that problem with telnet and utelnetserver.


Regards, Jan

MrRobot
Posts: 31
Joined: Mon May 11, 2020 11:30 am

Re: unable to access webrepl when main script is running

Post by MrRobot » Thu Nov 05, 2020 4:33 pm

Hi I've tried putting in a small sleep in every loop.

But until I kill the main process running I can't access the webrepl.

I can only enter a password, it won't accept it

Please help me if you can

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: unable to access webrepl when main script is running

Post by davef » Thu Nov 05, 2020 6:46 pm

On a ESP32 I have also had inconsistent access. At one stage I thought I was corrupting the filesystem when I accessed WebREPL at random times so I tried this in the main processing block:

Code: Select all

     #  stop the program here
        for x in range(10):
            seconds_left = 10 - x
            print('you have ' + str(seconds_left) + ' seconds left to hit Ctrl C to stop the program')  
            utime.sleep(1)

        print('times-up!!')
It "seemed" better.

Then trying to track down random resets I removed:

Code: Select all

nic=network.WLAN(network.AP_IF)
nic.active(1)
as suggested elsewhere before running my datalog program. I think that has crippled access through WebREPL, but until I sort out my main problem I'll keep using rshell via USB. I couldn't find a definitive statement on the pre-conditions for WebREPL.

Good luck!

User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: unable to access webrepl when main script is running

Post by MostlyHarmless » Thu Nov 05, 2020 7:04 pm

MrRobot wrote:
Thu Nov 05, 2020 4:33 pm
Hi I've tried putting in a small sleep in every loop.

But until I kill the main process running I can't access the webrepl.

I can only enter a password, it won't accept it

Please help me if you can
Have you actually tried it with a 20ms sleep?


Jan

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: unable to access webrepl when main script is running

Post by Roberthh » Fri Nov 06, 2020 8:31 am

When main code is running with some sleep statements in it, I can access the device though webrepl. Obviously I get not REPL prompt, since the main code is running. But at least the same output (if any) as the one one USB.

MrRobot
Posts: 31
Joined: Mon May 11, 2020 11:30 am

Re: unable to access webrepl when main script is running

Post by MrRobot » Fri Nov 06, 2020 9:24 am

I've found a way to access the webrepl using a timer callback like this:

Code: Select all


timer = Timer(-1)
timer.init(period=1, mode=Timer.ONE_SHOT, callback=preSleep)

for countdown in range(20,0,-1):
    print('Sleeping in ',countdown)
    time.sleep(1)

machine.deepsleep(sleepTime)

and my callback function is just this:

Code: Select all

def preSleep(timer):
    print('Giving a user a chance to connect')
    input()
So essentially the user has 20 seconds to connect before the system sleeps.

It's not exactly elegant but it does allow me to login to the webrepl.

But unfortunately I can't stop the main thread running.

Can anyone think of a way of doing this?

I tried putting a machine.soft_reset() after the input() but nothing works so far

MrRobot
Posts: 31
Joined: Mon May 11, 2020 11:30 am

Re: unable to access webrepl when main script is running

Post by MrRobot » Fri Nov 06, 2020 10:59 am

So putting an input() statement works but hangs the entire device.

So it's not a real solution to the problem.

My project is a data logger that is located in a Marine environment,
so having the pyboard D accessible by Wi-Fi would be hugely beneficial.

Otherwise to access the device I need to open the case (which is risky when you're on a boat)

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: unable to access webrepl when main script is running

Post by davef » Fri Nov 06, 2020 5:52 pm

Some more input after spending a day trying to get my datalogger working on ESP32 and a ESP8266 platforms. Access doesn't seem to be a problem for me so I describe my infinite loop timings.

The main loop runs about once every 500-600ms flashing a heartbeat LED. Then every minute goes to a process loop that has one 750ms delay to read 1-wire sensors and another 750ms delay so that I only get one loop execution for that minute.

I don't have any problems issuing a Ctrl C in WebREPL, updating files and doing a Ctrl D to get it running again. As mentioned before I try to do that during a 10 second pause in the "process loop", as I didn't want to do a Ctrl C during file reads and writes (paranoid).

Do you have some specific program execution timings?

User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: unable to access webrepl when main script is running

Post by MostlyHarmless » Sat Nov 07, 2020 12:40 am

750ms to read sensors sounds like a lot. Can you give more details on that?


Jan

Post Reply