scientifichackers / ampy

MicroPython Tool - Utility to interact with a MicroPython board over a serial connection.
MIT License
707 stars 155 forks source link

ampy not working while main.py is running #64

Open hyansuper opened 5 years ago

hyansuper commented 5 years ago

If I have a main.py file in which a while-loop runs forever, then I can not issue the ampy command. I don't think this is a bug, but I will have to use esptool.py to erase my esp32 borad, and flush firmware. Is there any other way to edit or delete the none-stoping main.py file

--EDIT--------- I had the issue above with Ubuntu16. Now on Win10, I don't have the issue any more, I can "ampy rm main.py" while main.py is running

jerryneedell commented 5 years ago

if you can open a terminal session to the REPL ( issue control C and enter to get to REPL) try:

import os
os.remove('main.py')

or
os.rename('main.py','was_main.py')
dhalbert commented 5 years ago

@jerryneedell Do you think ampy might send a ctrl-C itself to the REPL, always, before doing something, so that any running program is interrupted? Is that a good enhancement?

jerryneedell commented 5 years ago

@dhalbert I thought it did that already.

jerryneedell commented 5 years ago

@dhalbert it should call enter_raw_repl() https://github.com/adafruit/ampy/blob/master/ampy/pyboard.py#L175 before any operations. It's not clear why it won't break into main.py

dhalbert commented 5 years ago

Yes, it does: https://github.com/adafruit/ampy/blob/master/ampy/pyboard.py#L180. Yes, we need more info.

jerryneedell commented 5 years ago

This has come upon the past and been fixed then reappeared though I can't find the references to it. Playing with the "delay" parameter might help ...

jerryneedell commented 5 years ago

some discussion here: https://github.com/adafruit/circuitpython/issues/636

jerryneedell commented 5 years ago

FWIW -- I am able to do am ampy ls with an endless main.py on my esp32.


while True:
    print("looping in main.py")

jerryneedell@Ubuntu-Macmini:~/projects/esp32$ ampy ls
/aio_publish.py
/main.py

It did hang once but it recovered when I tried adding a longer delay ampy -d 1.5

I usuallh have the delay set to 0 -- here is my .ampy file

jerryneedell@Ubuntu-Macmini:~/projects/esp32$ cat .ampy
# Example .ampy file
# Please fill in your own port, baud rate, and delay
AMPY_PORT=/dev/ttyUSB0
AMPY_BAUD=115200
AMPY_DELAY=0

I am using ampy 1.0.5 -- Ubuntu 18.04 - micropython 1.9.4 MicroPython v1.9.4-631-g338635ccc on 2018-10-10; ESP32 module with ESP32

gkyle commented 5 years ago

I also had this problem, getting started with CircuitPython on esp8266. When I have a main.py that does not exit, all ampy operations fail in enter_raw_repl, when the device soft reboots and b'raw REPL; CTRL-B to exit\r\n' never appears on the buffer. Changing the delay did not help. I was able to "fix" with:

After soft reset, look for prompt to enter REPL and send Ctrl-A to enter raw REPL.

@@ -206,6 +206,11 @@ class Pyboard:
         time.sleep(0.1)           # (slight delay before second interrupt
         self.serial.write(b'\x03')
         # End modification above.
+
+        data = self.read_until(1, b'Press any key to enter the REPL.', 1)
+        if (data.endswith(b'Press any key to enter the REPL.') or (b'Adafruit CircuitPython' in data)):
+            self.serial.write(b'\r\x01')
+                        
         data = self.read_until(1, b'raw REPL; CTRL-B to exit\r\n')
         if not data.endswith(b'raw REPL; CTRL-B to exit\r\n'):
             print(data)

Second, after executing a command, I was seeing b'raw REPL; CTRL-B to exit\r\n' on the buffer before 'OK'.

@@ -248,8 +253,9 @@ class Pyboard:
         self.serial.write(b'\x04')

         # check if we could exec command
-        data = self.serial.read(2)
-        if data != b'OK':
+        data = self.serial.read_until(b'OK')
+        if not data.endswith(b'OK'):
+            print(data)
             raise PyboardError('could not exec command')
ladyada commented 5 years ago

Hiya! We are discontinuing support for ampy, and will no longer be maintaining it. We are leaving this repository available for continued use. If you would like to take over supporting it, please contact us on the Adafruit Discord server and we can transfer the repository to you. If you wish to continue developing it on your own, please fork the repository.

willcharlton commented 5 years ago

Does anyone know of a good replacement for ampy since it is no longer being supported?

jerryneedell commented 5 years ago

ampy is now maintained at https://github.com/pycampers/ampy

devxpy commented 5 years ago

@willcharlton Hey! I'm one of the members of pycampers; we just got maintainership of this project.

If you have any specific stuff you want to talk about, just ping me.

imranparuk commented 5 years ago

Still having this issue,

I have tried @gkyle approach, however no luck. In fact it stops me from being able to execute any commands.

devxpy commented 5 years ago

I don't have a clear solution, but I just wanna spit out my 2 cents :-

This project is originally based on pyboard.py, which to me, looks like a hack. It tries to control the device, by essentially, sending keyboard shortcuts, and waiting for absurd things like a > character to appear on the output. When it doesn't, (due to some connectivity issues, or keyboard events not being sent at the "right" time, or whatever), it panics, and we get issues like this, and many others. So it's essentially automating stuff that you would otherwise do inside a screen session.

What I would like to see, is a proper API for communicating with the device.

I admit, that haven't had the time to fully devote myself to this project, but this is one fundamental issue I see with micropython, in the limited time I have the chance to fire up my editor, and hack on ampy.

I would love any suggestions that you guys have on building a proper communication backend inside micropython.

I have one idea -- Maybe we can build a special, "debug" firmware, that contains a simple http/tcp server, that listens for commands from an ampy client. This way, we could avoid all sorts of issues that arise from doing flaky serial communication, and potentially, even enable in-browser apps that talk directly to the boards!

dhalbert commented 5 years ago

@devxpy ampy, rshell, and similar programs are designed to compensate for the lack of some other kind of interface, such as mass storage (MSC) access to the on-board filesystem. You might look at rshell to see if it works somewhat better, and adapt ampy to use its techniques, or simply stop supporting ampy.

As you see, trying to control things through the REPL can be less than satisfactory. That's one reason why Adafruit dropped support for ampy, and is also not supporting boards without MSC for CircuitPython 4.0 and up.

devxpy commented 4 years ago

@dhalbert Took me time to get there, but I've finally built a POC, that involves a development micropython firmware, which hosts an RPC server. This allows users to remotely execute code on the device, from their machine, without ever interacting through the REPL in serial mode.

More details here.

Needless to say, haven't quite given up on ampy just yet :)

pratik-pato commented 2 years ago

Adding a delay of 0.5 worked for me. Try ampy -p <port> -d 0.5 <rest of the command>

willcharlton commented 2 years ago

@devxpy - whoa, that is great news. I'm just seeing this now that you're the new maintainer. I will start thinking about features again. Thank you!

N1ckn1ght commented 2 years ago

Adding a delay of 0.5 worked for me. Try ampy -p <port> -d 0.5 <rest of the command>

This is working, except that for me it was more than 0.5, so for other people I suggest experimenting.