Preparation for ESP-Now code

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
MMliam
Posts: 121
Joined: Mon May 07, 2018 1:08 pm

Preparation for ESP-Now code

Post by MMliam » Tue Jul 05, 2022 11:20 pm

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.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Preparation for ESP-Now code

Post by davef » Wed Jul 06, 2022 12:46 am

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))

MMliam
Posts: 121
Joined: Mon May 07, 2018 1:08 pm

Re: Preparation for ESP-Now code

Post by MMliam » Wed Jul 06, 2022 2:30 am

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?

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Preparation for ESP-Now code

Post by davef » Wed Jul 06, 2022 2:55 am

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'

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Preparation for ESP-Now code

Post by davef » Wed Jul 06, 2022 3:20 am

On one ESP32 STA-mode

Code: Select all

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

Code: Select all

b'083af267da94'

MMliam
Posts: 121
Joined: Mon May 07, 2018 1:08 pm

Re: Preparation for ESP-Now code

Post by MMliam » Wed Jul 06, 2022 3:36 am

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

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Preparation for ESP-Now code

Post by davef » Wed Jul 06, 2022 3:58 am

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

MMliam
Posts: 121
Joined: Mon May 07, 2018 1:08 pm

Re: Preparation for ESP-Now code

Post by MMliam » Wed Jul 06, 2022 12:56 pm

dave,
I'm going to PM you with some questions.

Post Reply