How to use main.py to deploy my code

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
ctrlf
Posts: 5
Joined: Mon Oct 10, 2016 1:18 pm

How to use main.py to deploy my code

Post by ctrlf » Mon Oct 10, 2016 9:21 pm

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.

chrisgp
Posts: 41
Joined: Fri Apr 01, 2016 5:29 pm

Re: How to use main.py to deploy my code

Post by chrisgp » Tue Oct 11, 2016 4:40 am

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.

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

Re: How to use main.py to deploy my code

Post by pythoncoder » Tue Oct 11, 2016 9:01 am

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

Code: Select all

import mymodule
mymodule.run()
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.
Peter Hinch
Index to my micropython libraries.

ctrlf
Posts: 5
Joined: Mon Oct 10, 2016 1:18 pm

Re: How to use main.py to deploy my code

Post by ctrlf » Tue Oct 11, 2016 12:29 pm

I tried the webrepl and it worked fine although I had to type first

Code: Select all

import webrepl
webrepl.start()
the documentation says it's by default enabled I couldn't connect. Anyway thank you all and @pythoncoder that's nice trick!

NTL2009
Posts: 20
Joined: Wed Oct 26, 2016 10:07 pm

Re: How to use main.py to deploy my code

Post by NTL2009 » Wed Oct 26, 2016 10:18 pm

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

User avatar
ernitron
Posts: 89
Joined: Fri Jun 03, 2016 5:53 pm
Location: The Netherlands

Re: How to use main.py to deploy my code

Post by ernitron » Sat Oct 29, 2016 10:50 am

NTL2009 wrote:..
I found the app 'ampy', and it includes a "put" command....
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.py

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.

NTL2009
Posts: 20
Joined: Wed Oct 26, 2016 10:07 pm

Re: How to use main.py to deploy my code

Post by NTL2009 » Sat Oct 29, 2016 1:11 pm

ernitron wrote:
NTL2009 wrote:..
I found the app 'ampy', and it includes a "put" command....
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.py

....

Anyway for what is worth, have a look at it.
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'):

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'

User avatar
ernitron
Posts: 89
Joined: Fri Jun 03, 2016 5:53 pm
Location: The Netherlands

Re: How to use main.py to deploy my code

Post by ernitron » Sat Oct 29, 2016 2:19 pm

NTL2009 wrote:
ernitron wrote:
NTL2009 wrote:..
I found the app 'ampy', and it includes a "put" command....
I am afraid that will not work with microython and ESP8266 firmware..
... However, 'ampy' is working for me, so I'm not sure why you say it will not work with microython and ESP8266 firmware?
YOU'RE RIGHT! Maybe when I tested it was just my wrong setup. Sorry.

NTL2009
Posts: 20
Joined: Wed Oct 26, 2016 10:07 pm

Re: How to use main.py to deploy my code

Post by NTL2009 » Sat Oct 29, 2016 3:05 pm

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?

User avatar
ernitron
Posts: 89
Joined: Fri Jun 03, 2016 5:53 pm
Location: The Netherlands

Re: How to use main.py to deploy my code

Post by ernitron » Sat Oct 29, 2016 6:27 pm

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.

Post Reply