如何在 ESP32 上的 MicroPython 中检索和格式化 wifi MAC 地址?
How to retrieve and format wifi MAC address in MicroPython on ESP32?
我在 ESP32 上有以下 MicroPython 代码 运行:
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
输出如下所示:
MAC Address: b'0\xae\xa4z\xa7$'
我想以更熟悉的六对十六进制数字格式显示它,如下所示:
MAC Address: AABBCC112233
在网上搜索解决方案后,我试过:
print("MAC Address:", str(wlan_mac))
但显示与不使用 str()
时相同
print("MAC Address:", hex(wlan_mac))
但会导致 TypeError: can't convert bytes to int
print("MAC Address:", wlan_mac.hex())
但它说 AttributeError: 'bytes' object has no attribute 'hex'
我也对从 wlan_sta.config('mac')
检索到的字节有点怀疑。我本以为看起来更像 b'\xaa\xbb\xcc\x11\x22\x33'
而不是 b'0\xae\xa4z\xa7$'
。 z 和 $ 对于应该是十六进制的东西来说似乎非常不合适,对于应该是六对数字的东西来说似乎太短了。
所以我的问题有两个方面:
- 我获取 MAC 地址的方法是否正确?
- 如果正确,如何将其格式化为六对十六进制数字?
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.
您返回的不是十六进制字符串,而是字节字符串。因此,如果 MAC 地址包含值 7A
,则字节字符串将包含 z
(其 ASCII 值 122(十六进制 7A
))。
Am I using the correct method to get the MAC address?
你是!
If it is correct, how can I format it as six pairs of hex digits?
如果要将 MAC 地址打印为十六进制字符串,可以使用
ubinascii.hexlify
方法:
>>> 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
或者也许:
>>> print(ubinascii.hexlify(wlan_mac).decode().upper())
30AEA47AA724
我在 ESP32 上有以下 MicroPython 代码 运行:
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
输出如下所示:
MAC Address: b'0\xae\xa4z\xa7$'
我想以更熟悉的六对十六进制数字格式显示它,如下所示:
MAC Address: AABBCC112233
在网上搜索解决方案后,我试过:
print("MAC Address:", str(wlan_mac))
但显示与不使用 str()
print("MAC Address:", hex(wlan_mac))
但会导致 TypeError: can't convert bytes to int
print("MAC Address:", wlan_mac.hex())
但它说 AttributeError: 'bytes' object has no attribute 'hex'
我也对从 wlan_sta.config('mac')
检索到的字节有点怀疑。我本以为看起来更像 b'\xaa\xbb\xcc\x11\x22\x33'
而不是 b'0\xae\xa4z\xa7$'
。 z 和 $ 对于应该是十六进制的东西来说似乎非常不合适,对于应该是六对数字的东西来说似乎太短了。
所以我的问题有两个方面:
- 我获取 MAC 地址的方法是否正确?
- 如果正确,如何将其格式化为六对十六进制数字?
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.
您返回的不是十六进制字符串,而是字节字符串。因此,如果 MAC 地址包含值 7A
,则字节字符串将包含 z
(其 ASCII 值 122(十六进制 7A
))。
Am I using the correct method to get the MAC address?
你是!
If it is correct, how can I format it as six pairs of hex digits?
如果要将 MAC 地址打印为十六进制字符串,可以使用
ubinascii.hexlify
方法:
>>> 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
或者也许:
>>> print(ubinascii.hexlify(wlan_mac).decode().upper())
30AEA47AA724