Get reference to existing WiFi interface object?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Get reference to existing WiFi interface object?

Post by Jibun no kage » Sat Aug 06, 2022 8:08 pm

I have been using mqtt_as as well as a couple of other MQTT classes for MicroPython, basically learning how they have been implemented. And this has exposed an interesting question, IMHO, anyway.

All of these classes basically own the network.WLAN object they create. Which means that interface is hidden effectively, other than changing the class to allow visible access to said interface. Is there any other way to get a reference to that (hidden) interface object? Some clever trick or method, of the network.WLAN object, that is not illustrated in the typical documentation?

An example is, say I need to grab the MAC address, so I can create a unique host name, similar to what the default host name for Tasmota based devices do, i.e. tasmota_XXXXXX.

Maybe tweaking a given class like mqtt_as, to explicitly expose the interface object, is the only or best option? Given my unique use case, not sure such would applicable to forking the given module/class or even suggesting it to the applicable author.

Of course, I can always create my own object, get what I need, then release it and let the MQTT class do its thing, but that seems like a lot of code to get from point A to B, when there might be a better way?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Get reference to existing WiFi interface object?

Post by jimmo » Wed Aug 17, 2022 11:56 am

Jibun no kage wrote:
Sat Aug 06, 2022 8:08 pm
All of these classes basically own the network.WLAN object they create. Which means that interface is hidden effectively, other than changing the class to allow visible access to said interface. Is there any other way to get a reference to that (hidden) interface object? Some clever trick or method, of the network.WLAN object, that is not illustrated in the typical documentation?
I'm not quite sure what you're asking sorry...

But if it helps, the network.WLAN is actually a singleton. So if you write `network.WLAN(network.STA_IF)` you always get back teh same object.

Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Re: Get reference to existing WiFi interface object?

Post by Jibun no kage » Wed Aug 17, 2022 7:22 pm

Let me qualify... in mqtt.simple, robust, and mqtt_as modules, the WiFi object, network.WLAN() is owned or hidden by the defined class. So if I want get some element of the the WiFi object, i.e. network.WLAN for example, I can't.

Say I want to get the MAC address, for example, I have to do it via <interface variable>.config('mac') but since I can't use the same interface variable (i.e. class) that mqtt.simple, robust, or mqtt_as created. Because I have no handle to the network.WLAN object.

The python way to address this would be to create my own class as using the above MQTTClient class as base object, and add a method or property to access the information I want to make visible.

So MQTTClient sees 'X' network.WLAN() and when I create my variable, I see 'X' network.WLAN() as well. Sneaky but works for me. Thanks.

Post Reply