main.py does not appear to working after boot

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
rpr
Posts: 75
Joined: Sat Oct 27, 2018 5:17 pm

Re: main.py does not appear to working after boot

Post by rpr » Wed Jul 17, 2019 3:48 pm

Can you try a simple boot.py and main.py?

Edit to add: Can you use the

Code: Select all

 code 
block so that it can be easier to see the indentation?

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

Re: main.py does not appear to working after boot

Post by pythoncoder » Thu Jul 18, 2019 5:13 am

The usual way to program MicroPython devices is as follows.
  • Make no changes to boot.py (or absolutely minimal config changes).
  • main.py consists of a single statement: import my_module
my_module.py contains your code. That way you can develop your code at the REPL with an empty main.py. When it's working, adapt main.py to do the import.

I suggest you try that approach. Running significan amounts of code in boot.py is not recommended because it runs very early in the boot process.
Peter Hinch

smith.randallscott
Posts: 32
Joined: Mon May 13, 2019 12:42 pm

Re: main.py does not appear to working after boot

Post by smith.randallscott » Thu Jul 18, 2019 10:20 am

@pythoncoder Thank you I will try that

smith.randallscott
Posts: 32
Joined: Mon May 13, 2019 12:42 pm

Re: main.py does not appear to working after boot

Post by smith.randallscott » Thu Jul 18, 2019 6:35 pm

@pythoncoder There must be something wrong with the way I main creating main.py. I created my_module.py and called it from main.py; and it would work if issued a soft reboot (cntr+d) from the REPL. However, it would not work if I power cycled the board.

If however; with main.py deleted from the file system, I called my_module.py from boot.py all works as expected.

Can anyone explain this?

User avatar
Roberthh
Posts: 1701
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: main.py does not appear to working after boot

Post by Roberthh » Thu Jul 18, 2019 7:06 pm

Looking at you main.py and boot.py, it it quite a bit of code. Of the things I find surprising:
- the functions wifi_connect() is defined in boot.py, but called in main.py
- you import MQTT lib in boot.py and define client there, but call client.connect in main.py
- same with onewire, ds1820, main_running_led, and so on.
I would never do that, because it is not that boot.py imports main.py. They just happen to run in the same name space. If you import main.py (or my_module.py) from boot.py, the relation is more obvious, and things are guaranteed to work. That still does not explain why it works at Ctrl-D, and not on power on.
Also: You tell that main.py is running by the blinking LED in the main loop of main.py. But until then a lot could have failed. For the test like yours, I would add a LED loop at the very beginning of main.py, including the redefinition of the Pin object. Thus you can be sure that all you need for the test is locally present.

P.S.: The roles of boot.py and main.py are not clearly distinct. You can leave boot.py (almost) empty and put everything in main.py, or, like @pythoncoder suggested, let main.py only import your service code.

smith.randallscott
Posts: 32
Joined: Mon May 13, 2019 12:42 pm

Re: main.py does not appear to working after boot

Post by smith.randallscott » Thu Jul 18, 2019 7:39 pm

@Roberthh The current operating code is a follows
boot.py

Code: Select all

import my_module
my_module.py

Code: Select all

from time import sleep
from machine import Pin

def led_state():
	print(diag_led())


print('my_module loaded')

diag_led = Pin(25, Pin.OUT)
diag_led.on()

watch_dog_counter = 0
watch_dog_count_up = True

sleep(3)
diag_led.off()
I originally had the import statement in main.py, but it didn't work, so I moved it to boot.py, and it worked. I still don't understand why.

User avatar
Roberthh
Posts: 1701
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: main.py does not appear to working after boot

Post by Roberthh » Thu Jul 18, 2019 7:50 pm

That's indeed very strange. Can you try another ESP32 board?

smith.randallscott
Posts: 32
Joined: Mon May 13, 2019 12:42 pm

Re: main.py does not appear to working after boot

Post by smith.randallscott » Thu Jul 18, 2019 8:05 pm

Not right now, but hopefully in the near future.

Iyassou
Posts: 34
Joined: Sun Jun 26, 2016 9:15 am

Re: main.py does not appear to working after boot

Post by Iyassou » Tue Sep 17, 2019 10:59 am

I had the same issue but with a different ESP32-based board: the ESP32-DEVKITC 32U. As far as I'm aware this is just an ESP32-DevKitC V4 that's based on an ESP32-WROOM-32U.

This board comes with 2 buttons next to the microUSB port: EN and BOOT. When the board is plugged into a socket it does not execute the main.py script. However when it's plugged into my computer and I issue a soft reset (Ctrl+D) from the REPL the main.py script executes.

I found that in order to get the main.py script to execute when the board is fed power only I needed to press the EN button after plugging it in.

Could this be a hardware issue rather than a software one?

User avatar
Roberthh
Posts: 1701
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: main.py does not appear to working after boot

Post by Roberthh » Tue Sep 17, 2019 12:11 pm

I guess it is a hardware issue. EN is the reset pin/switch. If you push that, a proper start cycle is executed. That may not happen when just USB is plugged in. @OutOfTheBots told once, that soldering a 100nF capacitor between EN and GND solved his problem. If I look at the schematics of the Wemos devices (which never had a similar problem) and the Adafruit Huzzah32, then there is an external 10k pull-up resistor and a 1 µF capacitor to GND, which in combination results in a proper signal at EN.

Post Reply