Basically WebREPL is the server side of what you look for.
From laptop to MicroPython
Using webrepl_cli.py
With webrepl_cli.py https://github.com/micropython/webrepl you can copy files to and from MicroPython, similar to "scp".
After you copied a module over to MicroPython, you can use dhyland's "reload(mod)" to reload the copied over module and make the changes effective without MicroPython reload:
viewtopic.php?f=2&t=413&start=30#p7609
Using webrepl_client.py
You can use WebREPL shell https://github.com/Hermann-SW/webrepl#webrepl-shell which is OTA shell in case of wireless connection. You can bring in arbitrary new code or renew code, see the last example in the README.md section I pointed to. Instead of "eval()" as you want to do, you will enter paste mode (CTRL-E), send whatever code you want (I used "cat sc.py" in the example) and make it active with CTRL-D.
From MicroPython to MicroPython
Both methods described do not work from MicroPython to MicroPython.
Yesterday I learned how to use websocket client from danni:
https://twitter.com/HermannSW/status/10 ... 2988877824
It is so simple to execute WebREPL shell commands, although the current logic is for normal mode only. For paste mode you would just need to wait for "=== " prompt instead of ">>> " after each command (in "wait_for_prompt()").
I import drop.py onto ESP01s MicroPython 192.168.178.121, do CTRL-D and "import drop".
That module connects to WebREPL 192.168.178.122:8266 and remotely executes the commands on 2nd ESP01s OTA.
The endless loop is for testing so that I can see toggle of 2nd module builtin LED every second.
I did let it run for >30 minutes just to verify that the method is reliable.
Code: Select all
$ cat drop.py
import uwebsockets.client
import os
import time
websocket = uwebsockets.client.connect('ws://192.168.178.122:8266/')
def wait_for_prompt():
resp = ""
while not(">>> " in resp):
resp = websocket.recv()
print("< {}".format(resp))
def do(cmd):
websocket.send(cmd+"\r\n")
wait_for_prompt()
resp = websocket.recv()
assert resp == "Password: ", resp
do("abcd")
do("import payload")
while True:
do("payload.flip()")
time.sleep(1)
$
P.S:
For installation of uwebsockets
https://github.com/danni/uwebsockets
follow the instructions. You can use "ampy" as described there, or "webrepl_cli.py" as described above to copy the files. I did create the directory on MicroPython with "uos.mkdir('uwebsockets')" before copying. For websocket client only two files "uwebsockets/uwebsockets/*.py" need to be copied into "uwebsockets" directory.
As mentioned at end of danni's README.md "logging" module is required.
I did copy this python script to top level directory on MicroPython, as well as my drop.py:
https://raw.githubusercontent.com/micro ... logging.py
P.P.S:
ESP01s modules (cheap and lightweight, 1.5g only) have wireless for free, and 1MB flash allows for full MicroPython to be installed.
P.P.P.S:
Here is the remote payload.py module for completeness:
Code: Select all
$ cat payload.py
import machine
pwm0 = machine.PWM(machine.Pin(0), freq=50)
led = machine.Pin(1, machine.Pin.OUT)
def lock():
# pwm0.duty(50)
led.value(1)
def drop():
# pwm0.duty(100)
led.value(0)
def flip():
if led.value():
drop()
else:
lock()
drop()
$