Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Main.py ideas
#1
Hi All:

I'm a new member and new to ESP32 and MicroPython.  I first wanted to thank everyone here, particularly loboris, for the tremendous accomplismment you all have achieved in providing this incredible technology to the world!  Thank you!

I have been working on my first project: a web-based digital readout for lathes and milling machines.  I hope to post the code, etc. shortly.  Meanwhile I thought I'd share some ideas about the development process, and Main.py in particular.

A few things I've found useful, in no particular order:

When setting up a new hardware module, I try not to mess with boot.py beyond the basic, but rather I customize Start.py.  Using loboris' example as a starting point, I have added a few little things:

By default I have the unit start Wi-Fi in Station Mode with my local Wi-Fi credentials hard-coded.  However if the startup times out, I have it automatically start in Access Point mode.  This is useful for taking the device on the road or out of range of the home Wi-Fi.   I flash the onboard LED to signal if the Wi-Fi process passes, and if it is in Station or Access Point mode.  Very useful to know what is going on when a terminal is not available.

I also connect a toggle switch (or jumper) to one of the GPIO pins so that I can use it to bypass Station Mode and go directly to AP mode.  I have found this useful for AP mode testing.  Be sure to either use GPIOs that have internally-programmable pull-ups (not all GPIOs do) , or add external pull-ups.  If you don't, you risk unpredictable behavior if the pins are left floating.

I also use another GPIO pin to test what to do after the Wi-Fi process completes.  In one case, I start FTP and TELNET, then let the REPL take over.  This is good for development, where I can interact with the board by TELNET, or upload a new application file via FTP.  In the other case, I import my embedded application and run it.  I use the fixed file name "myApp.py"  for the uploaded code file.  Using this process, I've found it nearly impossible to 'brick' a board.  If anything goes wrong, just flip the switch to "development" mode, upload a corrected myApp.py, flip the switch to "run" mode, and press the RESET switch on the board.

I can also keep multiple projects going on one hardware board, and just copy the target file of interest to myApp.py.

I hope this is useful.
Thanks again, and best wishes to all.
DC

Here is my Main.py:


#-----------------------------------------
import network
from network import mDNS, ftp, telnet
import utime
import machine

# Pin assignments are for the TTGO T8 Dev PCB
boot_sw = machine.Pin(25, machine.Pin.IN, machine.Pin.PULL_UP)
sta_sw = machine.Pin(26, machine.Pin.IN, machine.Pin.PULL_UP)
led = machine.Pin(21, machine.Pin.OUT)

def blink(n):
    for i in range(n):
        led.value(1)
        utime.sleep_ms(200)
        led.value(0)
        utime.sleep_ms(200)
    utime.sleep_ms(800)
    led.value(1)


led.value(0)
if sta_sw.value() == 0:
    print("")
    print("Starting WiFi Access Point Mode...")
    ap_if = network.WLAN(network.AP_IF)
    ap_if.active(True)
    blink(2)

else:
    print("")
    print("Starting WiFi Station Mode...")
    sta_if = network.WLAN(network.STA_IF)
    _ = sta_if.active(True)
    sta_if.connect("WIFI_SSID_GOES HERE", "WIFI_PASSWORD_GOES_HERE")
    tmo = 100
    while not sta_if.isconnected():
        utime.sleep_ms(100)
        tmo -= 1
        if tmo == 0:
            print("Failed to start Station Mode")
            print("Starting Access Point Mode...")
            ap_if = network.WLAN(network.AP_IF)
            ap_if.active(True)
            blink(2)
            break
    if tmo > 0:
        ifcfg = sta_if.ifconfig()
        print("WiFi started, IP:", ifcfg[0])
        blink(4)
        utime.sleep_ms(500)
        rtc = machine.RTC()
        print("Synchronizing time from NTP server ...")
        rtc.ntp_sync(server="hr.pool.ntp.org")
        tmo = 100
        while not rtc.synced():
            utime.sleep_ms(100)
            tmo -= 1
            if tmo == 0:
                break

        if tmo > 0:
            utime.sleep_ms(200)
            print("Time set:", utime.strftime("%c"))
            print("")
            _ = network.ftp.start()
print("Starting DNS as mPy...")
utime.sleep_ms(500)
mdns = mDNS()
mdns.start('mPy', 'MicroPython ESP32')
if boot_sw.value() == 0:
    print("")
    print("Launching myApp.py...")
    import myApp
    myApp.run()
else:
    print("Starting FTP...")
    utime.sleep_ms(500)
    ftp.start(user='ftp_username_goes_here', password='ftp_password_goes_here')
    print("Starting TELNET...")
    utime.sleep_ms(500)
    telnet.start(user='telnet_username_goes_here', password='telnet_password_goes_here')
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)