ESP32 PyCharm all ok but nothing happens

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
neuberfran
Posts: 16
Joined: Mon May 20, 2019 3:23 pm

ESP32 PyCharm all ok but nothing happens

Post by neuberfran » Tue Aug 27, 2019 7:00 pm

Hi,

When I go to Tools/Micropython/Micropython REPL I can turn on/off LED. But with this code below, nothing happens.

Code: Select all

import utime
from machine import Pin
def main():
    led = Pin(23, Pin.OUT)
    enabled = False
    while True:
        if enabled:
            led.off()
        else:
            led.on()
        utime.sleep_ms(1000)
        enabled = not enabled
if __name__ == '__main__':
    main()

Why?
Attachments
whats wrong.png
whats wrong.png (109.76 KiB) Viewed 261 times

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

Re: ESP32 PyCharm all ok but nothing happens

Post by pythoncoder » Wed Aug 28, 2019 6:34 am

I'm not familiar with PyCharm but it looks as if you're copying a file __init__.py to the target and expecting it to run after a soft reboot. That isn't how MicroPython works. After a reboot the file main.py runs. Typically this will contain a single line to import your own module, or a line to import it followed by one to start it. For example, if you named your module my_module, main.py would be:

Code: Select all

import my_module
my_module.main()
I'm not sure why you called your module __init__.py - I would reserve this name for its intended use in Python packages.
Peter Hinch

neuberfran
Posts: 16
Joined: Mon May 20, 2019 3:23 pm

Re: ESP32 PyCharm all ok but nothing happens

Post by neuberfran » Wed Aug 28, 2019 7:03 pm

I create a main.py but I have issue:
Attachments
main issue.png
main issue.png (90.62 KiB) Viewed 217 times

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

Re: ESP32 PyCharm all ok but nothing happens

Post by Roberthh » Wed Aug 28, 2019 7:36 pm

I cannot tell it from the screen shot, but the file you upload must be called "module.py" (even if that is a confusing name). Then, "import module" should work.

neuberfran
Posts: 16
Joined: Mon May 20, 2019 3:23 pm

Re: ESP32 PyCharm all ok but nothing happens

Post by neuberfran » Wed Aug 28, 2019 9:33 pm

Not solved yet. I tried main.py and Without py
Attachments
issue15.png
issue15.png (87.11 KiB) Viewed 204 times
issue14.png
issue14.png (96.12 KiB) Viewed 204 times

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

Re: ESP32 PyCharm all ok but nothing happens

Post by dhylands » Wed Aug 28, 2019 10:34 pm

If your module directory is stored inside a directory called zeze then you need to add the zeze directory to your sys.path in the main.py file before you try to import it. Otherwise micropython doesn't know where to look.

rpr
Posts: 60
Joined: Sat Oct 27, 2018 5:17 pm

Re: ESP32 PyCharm all ok but nothing happens

Post by rpr » Wed Aug 28, 2019 11:59 pm

Is the main.py inside the zeze directory as well?

neuberfran
Posts: 16
Joined: Mon May 20, 2019 3:23 pm

Re: ESP32 PyCharm all ok but nothing happens

Post by neuberfran » Thu Aug 29, 2019 1:25 am

dhylands wrote:
Wed Aug 28, 2019 10:34 pm
If your module directory is stored inside a directory called zeze then you need to add the zeze directory to your sys.path in the main.py file before you try to import it. Otherwise micropython doesn't know where to look.
How to I add the zeze directory to my sys.path?
Attachments
issue145.png
issue145.png (94.24 KiB) Viewed 191 times

neuberfran
Posts: 16
Joined: Mon May 20, 2019 3:23 pm

Re: ESP32 PyCharm all ok but nothing happens

Post by neuberfran » Thu Aug 29, 2019 1:26 am

rpr wrote:
Wed Aug 28, 2019 11:59 pm
Is the main.py inside the zeze directory as well?
yes, C:\Users\neube\PycharmProjects\zeze\zeze

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

Re: ESP32 PyCharm all ok but nothing happens

Post by pythoncoder » Thu Aug 29, 2019 7:23 am

You need to consider the directory structure on the ESP32 rather than that on the PC. To get started I suggest you keep it simple and avoid subdirectories on the device.

There are ways to use subdirectories. You can append to sys.path as suggested by @dhylands, or use Python packages. But for simple projects there is no real need.
Peter Hinch

Post Reply