How to use main.py to deploy my code
How to use main.py to deploy my code
Reading the documentation it didn't make clear (maybe I didn't notice) how to use main.py, how to open the file and insert your code and have it run on power on. I experimented a bit, created the file using f = open('main.py','w') and wrote a command inside and it worked but it doesn't seem elegant for larger code.
Googling about it I read that I could create a main.py in scripts folder and download the firmware.
How do you do it? Is there another way? I had trouble with firmware download so creating the file on my pc and downloading will cause delay.
Googling about it I read that I could create a main.py in scripts folder and download the firmware.
How do you do it? Is there another way? I had trouble with firmware download so creating the file on my pc and downloading will cause delay.
Re: How to use main.py to deploy my code
The WebREPL allows you to upload a file from your computer to the ESP8266. You can use that to upload a main.py from your computer.
Check out the question "How to transfer files to my ESP8266?" in the FAQ for more options.
An alternative option is to try running an FTP Server on your device so you can use a standard ftp client to manage files on the device. This one is a bit more complicated but some might prefer it.
Check out the question "How to transfer files to my ESP8266?" in the FAQ for more options.
An alternative option is to try running an FTP Server on your device so you can use a standard ftp client to manage files on the device. This one is a bit more complicated but some might prefer it.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to use main.py to deploy my code
Before making code run at power on, it's best to debug it at the REPL first. Say you have a module mymodule.py, created on a PC and containing a function run(). Copy the module to the ESP using WebRepl or otherwise, then at the REPL issue
Once you have the module running to your satisfaction, create a main.py containing the above lines and copy that to the ESP8266 and the code should run at power on.
Code: Select all
import mymodule
mymodule.run()
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How to use main.py to deploy my code
I tried the webrepl and it worked fine although I had to type first
the documentation says it's by default enabled I couldn't connect. Anyway thank you all and @pythoncoder that's nice trick!
Code: Select all
import webrepl
webrepl.start()
Re: How to use main.py to deploy my code
Just FYI - I'm new to all this, and to keep things simple for my small brain, I wanted to copy my test file without learning how to set up the WEBREPL. Since I already had a wired serial connection set up, I wanted to use what I knew worked.
I found the app 'ampy', and it includes a "put" command. It will copy a file from my home directory (I am on Linux - Xubuntu 14.04), to the ESP board.
https://learn.adafruit.com/micropython- ... operations
sudo pip2 install adafruit-ampy
I am using the ESP8266 purchased from Amazon, I guess it is an ESP-12 config? HiLetgo New Version NodeMCU LUA WiFi Internet ESP8266. Re-flashed with MicroPython of course, with esptool (that went smoothly for me), esp8266-20161017-v1.8.5.bin.
Anyhow, maybe that option will help others. I couldn't find any easy way to do that with the terminal I tried (picocom).
I found the app 'ampy', and it includes a "put" command. It will copy a file from my home directory (I am on Linux - Xubuntu 14.04), to the ESP board.
https://learn.adafruit.com/micropython- ... operations
sudo pip2 install adafruit-ampy
I am using the ESP8266 purchased from Amazon, I guess it is an ESP-12 config? HiLetgo New Version NodeMCU LUA WiFi Internet ESP8266. Re-flashed with MicroPython of course, with esptool (that went smoothly for me), esp8266-20161017-v1.8.5.bin.
Anyhow, maybe that option will help others. I couldn't find any easy way to do that with the terminal I tried (picocom).
Re: How to use main.py to deploy my code
I am afraid that will not work with microython and ESP8266 firmware. I had the same problem, to accelerate devop cycle and I developed espsend.py. You can find it here: https://github.com/ernitron/uPython-esp ... espsend.pyNTL2009 wrote:..
I found the app 'ampy', and it includes a "put" command....
On ubuntu 14 or 16 it works opening ttyUSB and issuing command simulating user input.
Basically has some function like control C, reset the board, listing its contents. One of the major function is to upload via serial scripts. It is an awful kludge but it works at least for small files. I guess for larger files it get pretty unstable due to timings with tty and REPL.
Anyway for what is worth, have a look at it.
Re: How to use main.py to deploy my code
Thanks, I will take a look at 'espsend'. However, 'ampy' is working for me, so I'm not sure why you say it will not work with microython and ESP8266 firmware? I've been issuing the following commands as I develop my program (saving it on the ESP as 'main.py'):ernitron wrote:I am afraid that will not work with microython and ESP8266 firmware. I had the same problem, to accelerate devop cycle and I developed espsend.py. You can find it here: https://github.com/ernitron/uPython-esp ... espsend.pyNTL2009 wrote:..
I found the app 'ampy', and it includes a "put" command....
....
Anyway for what is worth, have a look at it.
to verify the connection and operation (prints boot.py & main.py):
~$ ampy -p /dev/ttyUSB0 -b 115200 ls
to copy/'put' my code from my HOME dir to the ESP board:
~$ ampy -p /dev/ttyUSB0 -b 115200 put 'main.py'
to read back the main.py file from the ESP to verify it (it doesn't seem to copy it, it just outputs it to the screen):
~$ ampy -p /dev/ttyUSB0 -b 115200 get 'main.py'
Re: How to use main.py to deploy my code
YOU'RE RIGHT! Maybe when I tested it was just my wrong setup. Sorry.NTL2009 wrote:... However, 'ampy' is working for me, so I'm not sure why you say it will not work with microython and ESP8266 firmware?ernitron wrote:I am afraid that will not work with microython and ESP8266 firmware..NTL2009 wrote:..
I found the app 'ampy', and it includes a "put" command....
Re: How to use main.py to deploy my code
No problem. Glad it is working for you now (maybe some configuration problem, or some sort of glitch caused problems for you before).
Good to have some verification that 'ampy' is working for others, seems like a reasonable way to go for now. Maybe this (or equiv?) should be added to the documentation, especially since they plan to remove default enabling of WEBREPL?
Good to have some verification that 'ampy' is working for others, seems like a reasonable way to go for now. Maybe this (or equiv?) should be added to the documentation, especially since they plan to remove default enabling of WEBREPL?
Re: How to use main.py to deploy my code
Again, thank you as ampy looks a lot like the one I was attempting to develop (my espsend is just NOT worth! forget it).
I don't know your devop cycle but actually mine is:
1 erase
2 flash
3 edit
4 copy *.py and *.mpy on image
5 test
6 get back to 3 (or maybe 1)
Steps 3 to 6 should get really fast and that's why we were looking for direct methods to upload files into the board bypassing webrepl. Still there are some security issues because repl doesn't require any password. But for fast devop cycles is awesome.
Now my problem is when program is running there is no way to enter in REPL with ampy while my super-ugly espsend succeeds and I would know why so to get completely rid of my espsend.
I don't know your devop cycle but actually mine is:
1 erase
2 flash
3 edit
4 copy *.py and *.mpy on image
5 test
6 get back to 3 (or maybe 1)
Steps 3 to 6 should get really fast and that's why we were looking for direct methods to upload files into the board bypassing webrepl. Still there are some security issues because repl doesn't require any password. But for fast devop cycles is awesome.
Now my problem is when program is running there is no way to enter in REPL with ampy while my super-ugly espsend succeeds and I would know why so to get completely rid of my espsend.