while true in boot

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
vitor
Posts: 5
Joined: Sun Apr 02, 2017 12:09 am

while true in boot

Post by vitor » Sun Apr 02, 2017 2:06 am

Hello, can I have a while True loop inside boot?

I found this network config code (https://github.com/cpopp/MicroPythonSam ... kconfig.py). But if I put in boot.py the following code:

import networkconfig
networkconfig.start()

and in main.py I started another code, it wont start.

So, what could be wrong? Can't I have a while True statement in boot.py?

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

Re: while true in boot

Post by Roberthh » Sun Apr 02, 2017 6:34 am

Boot.py and main.py are executed sequentially, not in parallel, and if main.py finishes, you'll get the REPL prompt. If boot .py does not end, main.py will not run.
So: No, you must not have a while True loop in boot.py, if you want main.py to run. If you want to run the code you refer to, put it at the end of main.py.

torwag
Posts: 220
Joined: Fri Dec 13, 2013 9:25 am

Re: while true in boot

Post by torwag » Sun Apr 02, 2017 6:35 am

There is something else wrong. As you can see from your code example, you only define a few functions. They never get called. Thus, you should not expect that you reach the while true loop.

Could be as simple as a small error, which threw out some exceptions and stops execution. Hence, you main.py will never be called.

Personally, I would not add all this code to boot.py. Boot.py should be as slim as possible to be as fast as possible. Try to copy the code over to main.py and check what happens then.

Use the reply to get some feedback.

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

Re: while true in boot

Post by pythoncoder » Sun Apr 02, 2017 6:39 am

To offer some general advice. It's best to keep boot.py minimal. I do the same for main.py: simply have a line

Code: Select all

import mymodule
with mymodule doing the actual work. If mymodule has a bug which locks up or crashes the machine all that's required is to replace main.py with a vanilla one and debug the module at the REPL.

Why do you want to pull in code in boot.py?
Peter Hinch
Index to my micropython libraries.

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

Re: while true in boot

Post by Roberthh » Sun Apr 02, 2017 6:40 am

@torwag: As far as I understand the post, these two lines are in boot.py:

Code: Select all

import networkconfig
networkconfig.start()
so start() is called, which contains the while true loop.

User avatar
vitor
Posts: 5
Joined: Sun Apr 02, 2017 12:09 am

Re: while true in boot

Post by vitor » Sun Apr 02, 2017 12:54 pm

Thanks for all your answers. Indeed my boot.py and main.py are very small, they have only the calls to other .py files and functions.
Roberthh wrote:@torwag: As far as I understand the post, these two lines are in boot.py:

Code: Select all

import networkconfig
networkconfig.start()
so start() is called, which contains the while true loop.
Thats correct @Roberthh. @pythoncoder I did as you mentioned.
Roberthh wrote:Boot.py and main.py are executed sequentially, not in parallel, and if main.py finishes, you'll get the REPL prompt. If boot .py does not end, main.py will not run.
So: No, you must not have a while True loop in boot.py, if you want main.py to run. If you want to run the code you refer to, put it at the end of main.py.
Is there another approach? It's possible to change the code to take off the while True loop and have the same result?

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

Re: while true in boot

Post by Roberthh » Sun Apr 02, 2017 3:52 pm

Hello @vitor, it is not clear to me what you want to achieve. Clearly, the intended use of boot.py is to set up the initial state, and may of us use main.py for just calling the final script. But that is a matter of style.
It seems to me that you want to achieve concurrent execution of some code. That could be done with the asyncio module, although that and the networkconfig,py code will most likely blow up the RAM. For asyncio, @pythoncoder will surely give you some hints on how to use it.

User avatar
vitor
Posts: 5
Joined: Sun Apr 02, 2017 12:09 am

Re: while true in boot

Post by vitor » Sun Apr 02, 2017 5:39 pm

Roberthh wrote:Hello @vitor, it is not clear to me what you want to achieve. Clearly, the intended use of boot.py is to set up the initial state, and may of us use main.py for just calling the final script. But that is a matter of style.
It seems to me that you want to achieve concurrent execution of some code. That could be done with the asyncio module, although that and the networkconfig,py code will most likely blow up the RAM. For asyncio, @pythoncoder will surely give you some hints on how to use it.
Hi @Roberthh, My need is: turn available a way to the final user change the wlan station connection. I guess I can modify the networkconfig.py file to break the while loop if the wlan station is connected and execute the start function again if the user press a button.

I will wait pythoncoder hints.

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

Re: while true in boot

Post by Roberthh » Sun Apr 02, 2017 6:05 pm

The wlan connection is changed by a single call to network.WLAN.connect(), with SSID, auth mode and password supplied.
I typically have these statements in a main.py:

Code: Select all

import network
import time

wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True)       # activate the interface
if not wlan.isconnected():      # check if the station is connected to an AP
    wlan.connect('my_ssid', 'my_password') # connect to an AP
    for _ in range (10):
        if wlan.isconnected():      # check if the station is connected to an AP
            break
        print('.', end='')
        time.sleep(1)
print('\nnetwork config:', wlan.ifconfig())
If you want to change the connection later, you can just repeat that with different credentials.

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

Re: while true in boot

Post by pythoncoder » Mon Apr 03, 2017 6:43 am

@vitor We are talking at cross purposes on the subject of "small" scripts. A boot.py containing

Code: Select all

import huge_python_module
is not "small" in my book if the module contains hundreds of lines of code which run immediately! By "small" I mean one or two lines of Python which perform actions which are necessarily done early in the boot sequence.

By contrast main.py can legitimately import a large module which runs immediately: that is how it is used when you want a program to auto-run on boot.

As an example here is the boot.py from the Pyboard I have sitting next to me.

Code: Select all

import pyb
pyb.usb_mode('CDC') # act as a serial (CDC) and not a storage device (MSC)
Peter Hinch
Index to my micropython libraries.

Post Reply