Can a python script know when webrepl is open?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Khyrtsi
Posts: 5
Joined: Sat Nov 12, 2016 7:28 am
Location: Finland
Contact:

Can a python script know when webrepl is open?

Post by Khyrtsi » Sun Nov 20, 2016 3:36 pm

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.)

User avatar
jwissing
Posts: 29
Joined: Thu Jun 19, 2014 12:23 pm
Location: Germany

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

Post by jwissing » Sun Nov 20, 2016 7:04 pm

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.

Khyrtsi
Posts: 5
Joined: Sat Nov 12, 2016 7:28 am
Location: Finland
Contact:

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

Post by Khyrtsi » Sun Nov 20, 2016 9:23 pm

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:)

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

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

Post by pythoncoder » Mon Nov 21, 2016 7:32 am

Is it not possible to use the webrepl to set a global boolean which is tested by the script before it goes to sleep?
Peter Hinch
Index to my micropython libraries.

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

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

Post by Roberthh » Mon Nov 21, 2016 7:46 am

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.

Khyrtsi
Posts: 5
Joined: Sat Nov 12, 2016 7:28 am
Location: Finland
Contact:

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

Post by Khyrtsi » Tue Nov 22, 2016 12:30 pm

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.

Post Reply