Page 1 of 1

Preparation for ESP-Now code

Posted: Tue Jul 05, 2022 11:20 pm
by MMliam
In an example provided on the micropython site titled "— Support for the ESP-NOW protocol" for ESP-Now coding the following MAC Address format was suggested for a given peer:
peer = b'\xbb\xbb\xbb\xbb\xbb\xbb'

While I could simply add the MAC Address by hand as indicated above, I am able the read the MAC Address by code directly, but it doesn't appear to be in the correct format as seemingly required above.

Here is a code snippet:
mac1 = ubinascii.hexlify(net.config('mac')) #Retrieves MAC Address from device
print(mac1)
b'84cca8803d6b'
print(type(mac1))
<class 'bytes'>
print(len(mac1))
12

# Manual data input; as per example.
mac2 = b'\x84\xcc\xa8\x80\x3d\x6b'
print(mac2)
b'\x84\xcc\xa8\x80=k'
print(type(mac2))
<class 'bytes'>
print(len(mac2))
6

You will note the byte length difference 12 vs. 6. Also I don't understand the printed output of mac2 (b'\x84\xcc\xa8\x80=k') from the manual input. It appears to only display the first 3 MAC Address codes, and I have no idea what the "=k" means. But this is what follows from manually inputting the MAC Address as per the example posted.
If the manual input format is correct, I would like a suggestion as to how to format the 12 byte mac1 to the seemingly 6 byte format of mac2.

Thank MM.

Re: Preparation for ESP-Now code

Posted: Wed Jul 06, 2022 12:46 am
by davef
This works for me:

Code: Select all

 w0 = network.WLAN(network.STA_IF)
    print (w0.config('mac'))
Think I tried ubinascii.hexlify once a long time ago.

The format should look like:

Code: Select all

b'\x08:\xf2\xab^\x04'
Maybe you need to use decode to print bytes or:

Code: Select all

print (str(mac1))

Re: Preparation for ESP-Now code

Posted: Wed Jul 06, 2022 2:30 am
by MMliam
Hi dave,

First can you explain the printed output I get when I manually enter the MAC address as per Glenn's example:
b'\x84\xcc\xa8\x80=k'

What does that mean, why does it display only the first three MAC address values, and what the heck is the "=k" mean?
Is there some flaw in microPython?

BTW: your print (w0.config('mac')) is essentially my
mac1 = ubinascii.hexlify(net.config('mac')) #Retrieves MAC Address from device
print(mac1)
b'84cca8803d6b'

I'll try your simpler version.

Mike

P.S. Tried your simple example, this is what I got:
net=network.WLAN(network.STA_IF)
print(net.config('mac'))
b'\x84\xcc\xa8\x80=k'
There's that pesky "=k" and not the full mac address that I get with my example above. How can this be passed to a ESP-Now peer?

Re: Preparation for ESP-Now code

Posted: Wed Jul 06, 2022 2:55 am
by davef
No, I can not explain.

That =k might just be part of the MAC address. It has the usual format.

Try:

Code: Select all

w0 = network.WLAN(network.AP_IF)
print (w0.config('mac'))
or try another ESP device

Here is another example of a valid MAC address:

Code: Select all

b'<a\x052T\x0c'

Re: Preparation for ESP-Now code

Posted: Wed Jul 06, 2022 3:20 am
by davef
On one ESP32 STA-mode

Code: Select all

b'\x08:\xf2g\xda\x94'

Code: Select all

b'083af267da94'

Re: Preparation for ESP-Now code

Posted: Wed Jul 06, 2022 3:36 am
by MMliam
I will say that the output I'm seeing is consistent with Glenn's manual data entry that I tried:
# Manual data input; as per Glenn.
mac2 = b'\x84\xcc\xa8\x80\x3d\x6b'
print(mac2)
b'\x84\xcc\xa8\x80=k'

and, your code:
net=network.WLAN(network.STA_IF)
print(net.config('mac'))
b'\x84\xcc\xa8\x80=k'

I guess I have to assume the peer is going to be happy with it, despite the way it prints out. The printout is from an ESP8266 running Glenn's v1.18 microPython. I also wonder why Glenn used a manual entry of the MAC address in his example, when it appears so easy to get it from the device itself, and the code would be generic to all peers.

BTW: Does Glenn have any connection with Damien George?

MM

Re: Preparation for ESP-Now code

Posted: Wed Jul 06, 2022 3:58 am
by davef
When you set up a link you need to obtain the MAC address at each end so that you can use one or both MAC addresses at the other end.

The MAC address he has in the code is for the peer not the MAC address of the device itself.

see PM

Re: Preparation for ESP-Now code

Posted: Wed Jul 06, 2022 12:56 pm
by MMliam
dave,
I'm going to PM you with some questions.