Page 1 of 1

Can a python script know when webrepl is open?

Posted: Sun Nov 20, 2016 3:36 pm
by Khyrtsi
Can I somehow know in my script when someone is connected via webrepl?

My use case is a sensor node that measures and then deep sleeps for a minute, and repeats. I would like to check if someone has connected via webrepl before the node goes to deep sleeps and if there is a connection then stop and wait for user input. For example update script or something.

(using ESP8266 if that matters.)

Re: Can a python script know when webrepl is open?

Posted: Sun Nov 20, 2016 7:04 pm
by jwissing
Hi, i'm having similar problem with a sensor board in development.
I use the following approach to get into webrepl when i need to.
I have connected Pin 12 to ground normally and when i need to update the board i connect the pin to high.

Code: Select all

flag = Pin(12, Pin.IN)
if flag.value() == 1:
    print("starting webrepl")
    import webrepl as wr
    wr.start()
else:
    print("importing _main")
    import _main
This would not work if the sensor is no longer accessible.

Re: Can a python script know when webrepl is open?

Posted: Sun Nov 20, 2016 9:23 pm
by Khyrtsi
That's similar how I currently do it. But it does requires to physically go to the sensor, which I would very much like to avoid:)

Re: Can a python script know when webrepl is open?

Posted: Mon Nov 21, 2016 7:32 am
by pythoncoder
Is it not possible to use the webrepl to set a global boolean which is tested by the script before it goes to sleep?

Re: Can a python script know when webrepl is open?

Posted: Mon Nov 21, 2016 7:46 am
by Roberthh
It should be relatively easy to tell, if someone is connected, by calling uos.dupterm(). That's what webrepl does. If someone is connected, it will return True. That assume showever, that the only two possible REPL connections are USB and webrepl.

Update: Just tested that. On REPL,

Code: Select all

not not uos.dupterm()
returns False if no one is connected, and True if someone is connected. So in your script you can simply use:

Code: Select all

if uos.dupterm():
	# do not sleep
to tell. But on the other hand - if your device is at sleep, no one can connect anyhow. So the only chance to connect would be during the short measuring time slot.

Re: Can a python script know when webrepl is open?

Posted: Tue Nov 22, 2016 12:30 pm
by Khyrtsi
Roberthh wrote:It should be relatively easy to tell, if someone is connected, by calling uos.dupterm(). That's what webrepl does. If someone is connected, it will return True. That assume showever, that the only two possible REPL connections are USB and webrepl.

Update: Just tested that. On REPL,

Code: Select all

not not uos.dupterm()
returns False if no one is connected, and True if someone is connected. So in your script you can simply use:

Code: Select all

if uos.dupterm():
	# do not sleep
to tell.
I'll have to test that, thanks.
Roberthh wrote: But on the other hand - if your device is at sleep, no one can connect anyhow. So the only chance to connect would be during the short measuring time slot.
True, this is just some theoretical thinking at this stage.