Skip to main content

Configure MicroPython WLAN/AP/WebRepl startup with your own code

Project description

MPyConfigBase

Configure automatic startup of MicroPython WLAN / AP / WebRepl during boot

General information

Use this together with mpycntrl for automation of the setup/deployment and configuration of MicroPython base interfaces. see also mpycntrl on github

Best practice

  • send cntrl+c
  • upload the module file along with the configuration files directly (the config files are wlan.cfg, softap.cfg and webrepl_cfg.py)
  • send reset

Installation during deployment

The python class code can be retrieved by calling:

fnam, src = MPyConfigBase.get_class_source() # get the module code
cfgbootcode = MPyConfigBase.get_boot_install_code() # get the minimal autconfig code

Sample Code

Some sample code for automatic deployment and configuration. In case of timeouts (error message: could not enter raw-repl) consider to use with mpyc.timeout(1) as tout: code block.

This code is also available here on github

Reset the board during the deployment is here required since MicroPython needs to bring in the new code before the configuration can take place. If you follow the best practice given above this dont needs to be done.

import textwrap, time
import serial # its pyserial 
from mpycntrl import *
from mpyconfigbase import *


def sample_code():

    debug = True # display more information 
    trace = False # display no detail trace information 

    port = '/dev/ttyUSB0'
    baud = 115200
    bytesize = 8
    parity = 'N'
    stopbits = 1
    timeout = .35

    blk_size_esp8266 = 2048 # this size works always on my board...

    with serial.Serial(port=port, baudrate=baud,
                       bytesize=bytesize, parity=parity, stopbits=stopbits,
                       timeout=timeout) as ser:

        mpyc = MPyControl(ser,debug=debug,
                          trace=trace
                          )

        # enter raw-repl mode, try twice in case of timeout
        try:
            r = mpyc.send_cntrl_c()
            print( "received", r )
        except:
            r = mpyc.send_cntrl_c()
            print( "received", r )

        # get directory listing
        r = mpyc.cmd_ls()
        print( "received", r )

         # install the required py module

        fnam, src = MPyConfigBase.get_class_source() # get the module code
        cfgbootcode = MPyConfigBase.get_boot_install_code() # get the minimal autconfig code

        # put the module code onto the board
        # (its a hack here for the demo)
        # best practice is:
        # 1. send cntrl+c
        # 2. upload the module file along with the configuration files directly
        # 3. send reset
        r = mpyc.cmd_put( fnam, content = src.encode(), blk_size=blk_size_esp8266 )
        print( "received", r )

        # put some different code in boot.py to enable automatic configuration

        boot_org_code = """
            # This file is executed on every boot (including wake-boot from deepsleep)
            #import esp
            #esp.osdebug(None)
            import uos, machine
            #uos.dupterm(None, 1) # disable REPL on UART(0)

            import gc
            gc.collect()
        """

        custom_boot_code = """
            import time
            while True:
                print( "->", time.localtime( time.time() ) )
                time.sleep(1)
        """

        new_boot_code = "".join( [
            textwrap.dedent(boot_org_code),
            cfgbootcode, # <-- this contains the autoconfiguration module code, see above how to retrieve 
            textwrap.dedent(custom_boot_code) ] )

        r = mpyc.cmd_put( "boot.py", content = new_boot_code.encode(), blk_size=blk_size_esp8266 )
        print( "received", r )

        # reset micropython to bring in the new code
        r = mpyc.send_hardreset()
        print( "received", r )

        # enter raw-repl mode again
        try:
            r = mpyc.send_cntrl_c()
            print( "received", r )
        except:
            r = mpyc.send_cntrl_c()
            print( "received", r )

        # get directory listing
        with mpyc.timeout( 1 ) as tout:
            r = mpyc.cmd_ls()
            print( "received", r )

        # configure wlan, ap and webrepl autostartup
        # see below at the end how to disable it again by code
        cmd = """
            configbase.wlan_config( "your-ssid", "your-passwd" )
            configbase.webrepl_config( "123456" )
            configbase.softap_config( "MySSID-test", "12345678" )
            """
        with mpyc.timeout( 1 ) as tout:
            r = mpyc.sendcmd(cmd)
            print( "received", r )

        r = mpyc.cmd_ls()
        print( "received", r )

        # reset micropython to activate interfaces
        r = mpyc.send_hardreset()
        print( "received", r )

        # follow the output until cntrl+c is pressed
        # since enabling is async the config output will show some dummy ip adr (default behaviour)
        while True:
            r = mpyc.serial.readlines()
            print( "received", r )

        # 
        # execute manually via shell eg. mpfshell / rshell
        # and test the output by restarting with cntrl+d
        #
        # configbase.wlan_remove()
        #
        # configbase.webrepl_remove()
        #
        # configbase.softap_remove()
        # 

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for mpyconfigbase, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size mpyconfigbase-0.0.3-py3-none-any.whl (7.8 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size mpyconfigbase-0.0.3.tar.gz (5.2 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page