3

I have the following MicroPython code running on an ESP32:

import network

wlan_sta = network.WLAN(network.STA_IF)
wlan_sta.active(True)

wlan_mac = wlan_sta.config('mac')
print("MAC Address:", wlan_mac)  # Show MAC for peering

The output looks like this:

MAC Address: b'0\xae\xa4z\xa7$'

I would like to display it in the more familiar format of six pairs of hex digits, like this:

MAC Address: AABBCC112233

After searching for a solution on the internet, I've tried:

print("MAC Address:", str(wlan_mac)) but it displays the same as when not using str()

print("MAC Address:", hex(wlan_mac)) but it results in TypeError: can't convert bytes to int

print("MAC Address:", wlan_mac.hex()) but it says AttributeError: 'bytes' object has no attribute 'hex'

I am also a little suspicious of the bytes retrieved from wlan_sta.config('mac'). I would have expected something that looked more like b'\xaa\xbb\xcc\x11\x22\x33' instead of b'0\xae\xa4z\xa7$'. The z and the $ seem very out of place for something that should be hexadecimal and it seems too short for what should be six pairs of digits.

So my question is two-fold:

  1. Am I using the correct method to get the MAC address?
  2. If it is correct, how can I format it as six pairs of hex digits?
Dave H.
  • 538
  • 5
  • 11

3 Answers3

8

I am also a little suspicious of the bytes retrieved from wlan_sta.config('mac'). I would have expected something that looked more like b'\xaa\xbb\xcc\x11\x22\x33' instead of b'0\xae\xa4z\xa7$'. The z and the $ seem very out of place for something that should be hexadecimal and it seems too short for what should be six pairs of digits.

You're not getting back a hexadecimal string, you're getting a byte string. So if the MAC address contains the value 7A, then the byte string will contain z (which has ASCII value 122 (hex 7A)).


Am I using the correct method to get the MAC address?

You are!

If it is correct, how can I format it as six pairs of hex digits?

If you want to print the MAC address as a hex string, you can use the ubinascii.hexlify method:

>>> import ubinascii
>>> import network
>>> wlan_sta = network.WLAN(network.STA_IF)
>>> wlan_sta.active(True)
>>> wlan_mac = wlan_sta.config('mac')
>>> print(ubinascii.hexlify(wlan_mac).decode())
30aea47aa724

Or maybe:

>>> print(ubinascii.hexlify(wlan_mac).decode().upper())
30AEA47AA724
larsks
  • 277,717
  • 41
  • 399
  • 399
  • 4
    Also found that `hexlify()` takes a separator argument, so `hex_mac = hexlify(wlan_mac, ':').decode().upper()` can be used to print the MAC with familiar colon separators, like so: 30:AE:A4:7A:A7:24 Though not part of my original question, I figured I'd add it for anyone finding this answer later and wondering how to do it. – Dave H. Apr 17 '22 at 15:36
0

You can use:

def wifi_connect(ssid, pwd):
    sta_if = None
    import network

    sta_if = network.WLAN(network.STA_IF)

    if not sta_if.isconnected():
        print("connecting to network...")
        sta_if.active(True)
        sta_if.connect(ssid, pwd)
        while not sta_if.isconnected():
            pass
    print("----------------------------------------")
    print("network config:", sta_if.ifconfig())
    print("----------------------------------------")

    get_my_mac_addr(sta_if)

Then:

 def get_my_mac_addr(sta_if):
    import ubinascii
    import network

    wlan_mac = sta_if.config('mac')
    my_mac_addr = ubinascii.hexlify(wlan_mac).decode()

    my_mac_addr = format_mac_addr(my_mac_addr)

Then:

def format_mac_addr(addr):

    mac_addr = addr
    mac_addr = mac_addr.upper()
    
    new_mac = ""
    
    for i in range(0, len(mac_addr),2):
        #print(mac_addr[i] + mac_addr[i+1])
        
        if (i == len(mac_addr) - 2):
            new_mac = new_mac + mac_addr[i] + mac_addr[i+1]
        else:
            new_mac = new_mac + mac_addr[i] + mac_addr[i+1] + ":"
    print("----------------------------------------")
    print("My MAC Address:" + new_mac)
    print("----------------------------------------")
    return new_mac

Return:

----------------------------------------
My MAC Address:xx:xx:xx:xx:xx:xx
----------------------------------------
0

try this function:

def mac2Str(mac): 
    return ':'.join([f"{b:02X}" for b in mac])

MAC = b'0\xae\xa4z\xa7$'
print(f"MAC: {MAC} -> {mac2Str(MAC)}")

MAC: b'0\xae\xa4z\xa7$' -> 30:AE:A4:7A:A7:24
gwb
  • 46
  • 2