Page 1 of 1

ESP32 - sta_if not defined

Posted: Fri Mar 02, 2018 3:26 am
by Yuoso
I have recently been gifted an ESP32 board and I flashed MicroPython to it (build 20180302 v1.9.3). I'm new to the board, but I'm getting some unexpected behavior. I tried on a brand new board he had and it does the same thing. Help? Here is a link to some screenshots.

https://imgur.com/gallery/9wccE

Re: ESP32 - sta_if not defined

Posted: Fri Mar 02, 2018 7:50 am
by torwag
Hi,

welcome to the forum. Please try to be specific about the problem in the post itself. Screenshots on an external page my not work for long and then, this thread turns into a useless piece for other users.

As far as I can see you misunderstood the import nature of python.
If you use

Code: Select all

import network
you have to call all members and attributes of the class machine by typing the class name in front and concatenate the function call by a '.' e.g.

Code: Select all

sta_if = network.WLAN(network.STA_IF)

in the example the returned object of the type WLAN was assigned to the name sta_if (note the capital and non-capital writing)
Thus, later in the code you can access this object and all its members and attributes by typing 'sta_if.<name>'

As a side note, you can make it easier for typing by renaming classes during import by using the scheme

Code: Select all

import network as net
from thereon you can use 'net' as a replacement for 'network' which saves you some typing.
If you get even more into python people will suggest you to only import what you actual need and not the entire class. With

Code: Select all

from network import WLAN, STA_IF

would allow you to write

Code: Select all

sta_if = WLAN(STA_IF)

which is even shorter and the explicit import makes sure you do not start to mix naming up with other classes and own function names etc. (e.g. you know it would be a bad idea to call a function WLAN if you imported a function with this name already).
Just for completness using '*' as a wildcard for importing all, is considered a bad habit, as you never know what kind of function names you might overwrite by accident, debugging this can be a pain, as often you never call those functions by yourself, but by accident they have the same name as a self defined function
E.g. do not use

Code: Select all

from network import * #that reads import all and everything from the class network into the root name space.
now there would be a class you could call by typing 'WLAN()'. As you might not be aware of it you might define a class WLAN yourself in your own code

Code: Select all

from network import * 
sta_if = WLAN(STA_IF) # Here it will still work
class WLAN():
   print('My WLAN function')

sta_if2 = WLAN(STA_IF) #here it will fail as you overwrote the original class with your own class
In this example it is obvious. But if you have important silently a class, member or attribute, which you might not think of, you run easily in trouble.

However, all this is generic python stuff not specific for Micropython. You can find plenty infos about it.

Re: ESP32 - sta_if not defined

Posted: Fri Mar 02, 2018 9:07 pm
by Yuoso
Yeah, I figured that out shortly after I made this post. The help() command gave me the impression those had already been initialized for me. I was way too tired to be screwing with it still, but determined to win lol.

I would much prefer to include my screenshots in this post, but I couldn’t find the feature in this implementation of PHP BB...admittedly it’s been a while since I used a forum so I probably missed it.

Thanks for the direction and I look forward to helping out as I figure this system out :)