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.
User avatar
wwhalljr
Posts: 7
Joined: Fri Aug 19, 2016 12:02 am

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

Post by wwhalljr » Sat Oct 29, 2016 6:47 pm

For what it's worth, Ampy has been a great piece of software for me, a newcomer to embedded - simple, effective communications and file management between Mac OSX and my ESP8266 Huzzah breakout board from Adafruit. Couldn't be easier, really.

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 9:19 pm

ernitron wrote: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.
Yes, your steps 3-6 loop is what I've been doing (no need for me to re-flash yet - keeping fingers crossed). Keep in mind I'm very knew to much of this stuff, so I'm kind of poking and probing my way around.

I was also a little confused on what to do once I've loaded a program that auto-boots - it's running so can I use these tools? It seemed like a conflict of sorts. But I just experimented a bit, loaded a simple main.py that just loops and blinks the LED, and prints the count to the terminal once per second 20 times, then does a sys.exit().

When I hit the ESP8266 reset button, it does its thing, blinking the LED for 20 seconds. If I already have a picocom active, it also prints to the terminal. But it doesn't seem to care if the terminal is active or not, I guess it just 'prints' to 'air'. I can start picocom in the middle of the loop, and it starts printing the value of wherever it is at the time (7 seconds in for example).

But I can also use the CNTRL-C command to stop the program (and I can catch it with either the LED ON or OFF - it just stops there). And CNTRL-D does the soft reset of the board and it starts counting at zero again. Those commands are listed with >>> help() at the REPL prompt while running picocom.

But things acted differently with 'ampy'. Anytime I sent a command, the program stopped (I guess) with the LED ON. Not sure what that means, other than I can stop the program from picocom and I assume tools will work OK at that point?

This is probably super basic to the experienced programmers out there, but I'm like a babe in the woods with this stuff. I did mostly hardware, some instrumentation programming (HP BASIC, aka Rocky Mountain Basic), a little scripting on UNIX and my Linux machine, so python is manageable for me, but the tools, networking, and so much more is new territory. But even so, after just a few days, I've managed to get my ESP to respond to a request over my local network, read a text file that I stored on it, and send that line by line to my computer terminal, and send an EOF message and exit, while my terminal stores the file locally. A big step in some of the monitoring apps I want to create. It's been fun.

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 9:22 pm

As long as I'm talking basics here - is there a recommended way to power down the ESP8266 module? Seems all I can really do is pull the USB cable, that seems a little nasty with a running program.

Should I hold the rest button depressed as I pull power? Something else?

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

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

Post by pythoncoder » Sun Oct 30, 2016 7:20 am

I'd say it depends what your program is doing. If it opens files for writing there is an obvious hazard and I'd suggest something at the application level to enable you to tell the program to terminate properly - a pushbutton connected to a pin, perhaps. At the other extreme, if it were just flashing an LED pulling the plug shouldn't do any harm.
Peter Hinch

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

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

Post by NTL2009 » Sun Oct 30, 2016 3:44 pm

pythoncoder wrote:I'd say it depends what your program is doing. If it opens files for writing there is an obvious hazard and I'd suggest something at the application level to enable you to tell the program to terminate properly - a pushbutton connected to a pin, perhaps. At the other extreme, if it were just flashing an LED pulling the plug shouldn't do any harm.
Thanks for that, makes a lot of sense.

My app will only be doing very short writes, very occasionally, so that helps. I'll also make sure I do no writes within, say, 10 seconds of power up - that would give me time to pull the plug after doing a reset.

For apps with more intense file writes, I think it would make sense to do as you say, and provide software and/or hardware to close the files before power is pulled.

Lornioiz
Posts: 36
Joined: Wed Aug 03, 2016 11:39 am
Location: Florence, Italy

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

Post by Lornioiz » Sun Oct 30, 2016 4:53 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 have used "ampy" extensively with Micropython and ESP8266... both from MacOsX, Windows and Ubuntu Mate.
I don't know why it didn't worked for you.

eradicatore
Posts: 41
Joined: Thu Apr 20, 2017 9:19 pm

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

Post by eradicatore » Sun Apr 21, 2019 2:40 pm

pythoncoder wrote:
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.
Hi Peter,
So I tried this, and can't seem to get it to work. I use uPyLoader to load the files, and if I put all my source in main.py it works perfectly. But if I put the source in say "button.py" and then use "import button" followed by button.run() in main.py as you show, it won't run. Is there anything I could be missing? permissions?

User avatar
dhylands
Posts: 3213
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

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

Post by dhylands » Sun Apr 21, 2019 6:00 pm

What does "It won't run" mean exactly?

What output (or error messages) are you seeing?

For debugging I'd put a print at the top of button.py which you should then see printed as a result of doing the import and put a print inside your run function.

For example, if I create this main.py:

Code: Select all

print('Executing main.py')

import button
button.run()
and create button.py:

Code: Select all

print('Executing button.py')

def run():
    print('Executing run() from button.py')
then I see the following output when I do a soft-reset:

Code: Select all

PYB: soft reboot
Executing main.py
Executing button.py
Executing run() from button.py
MicroPython v1.10-8-g8b7039d7d on 2019-01-26; ESP module with ESP8266
Type "help()" for more information.
>>> 
For small files like these, it's also good to confirm that the files exist and check the contents of the files from within the REPL itself.

Code: Select all

>>> import os
>>> os.listdir('/')
['boot.py', 'hello.py', 'main.py', 'button.py']
>>> with open('button.py') as f:
...     for line in f:
...         print(line, end='')
... 
print('Executing button.py')

def run():
    print('Executing run() from button.py')
>>> 
>>> with open('main.py') as f:
...     for line in f:
...         print(line, end='')
... 
print('Executing main.py')

import button
button.run()
>>> 

Post Reply