My buddy and I are developing a sendspin hardware device, and were using an esp32-s3 + w5500 for ethernet.
We came across the issue where the Mac address is incrementing by 3 just as in:
But the solution in this is to just manually set the Mac address in the yaml.
I'm wondering if anyone has an idea on how we could automate this, as we're planning to do some production runs of our board once it's ready, and generating macs and hardcoding them into the config for each unit seems impractical.
But also, shipping a device with a changing Mac seems cursed....
I looked at the PR, but didn't see anything concerning "incrementing by 3", so may I ask, what does "incrementing by 3" mean? Are you saying everytime one power restarts the ESP with w5500 that the MAC address changes?
The “corect” way to handle this is to internally genate a random “LAA” (Locally Administatrated Address) You need a write a GOOD random number gaterator. This is hard unless you start with a readom seed. So invent some silly hack like multiply the WiFi MAC by the number of second past the hour and then truncate to the correct length
But then you set a specif bit to “1” to mark the MAC is locally generated so it will never collide with a “real” MAC that is bnurned into product. The second-least significant bit of the first byte (the most significant byte) must be set to 1. Every MAC with this bit set is an LAA. (but use Google to verify I’m correct). Al l burned in ROM MACs have this bit set to zero.
this algorithm is used mostly for Virtual machines that of course can’t have hardware MACs. You just have to (1) mark them and (2) use an algorithm that can’t ever produce the same number twice. So combine the date/time with a real unique MAC and then stire up the bits. This will handle the case where a user buys five of you devices and powers them all up at the same time. The algorithm mixes bits from a unique “assigned” MAC so there in no race condition where you have to lock a counter ior something.
If youwant to be really good you TEST the proposed MAC to make sure it is not alredy in use on the local network. If building a comercial product, you really should. It is not hard.
The trouble of course is that ESPHome is a no-code development system but I think you could hide something small like this is a multi-line Lambda
BTW you are going to have the same problem with ESPHomne device names. They all need to be unique. One trick for that is to append the MAC to the name or maybe just the low order N hex digits.
“(LAA): You can assign a custom address. To ensure it is recognized as a local address (and not conflict with global ones), the second-least significant bit of the first byte (the most significant byte) must be set to 1.
Yeah I use to work for a company that manufactured all kinds of Ethernet based products, and the general practice was to have an id-prom on the device that was programmed in the factory that contained device specific information which included an Ethernet MAC address. This address BTW was a Universally Administered Address (UAA) and the company had their own IEEE registered OUI(s). So the firmware on the device would read the MAC address from the prom and configure it in the Ethernet hardware.
I have a Waveshare ESP32-S3 with w5500 as a BT-Proxy, and I have repowered it a couple of times, and according to my HA ESPHome Integration, the MAC Address has not changed. I'm not using DHCP, but the router does see an address that is +3 higher, but have not seen it change. The address btw is a UAA (using Espressif OUI).
I've looked at a couple of Products/Boards containing ESP32s with w5500 and I don't see anything that looks like an id-prom, which I'm not surprised about.
I'm not an ESPHome expert, but just glancing at some of the code here, and the call it makes to an espressif routine that can be found here, the code comment says the MAC comes from the w5500 factory burned in.
[EDIT] On the otherhand, I could not find anything in the w5500 datasheet on a burnt in MAC address and looking at the MAC address being used, in my case, it contains the Espressif OUI, so in spite of the code comment, the MAC address is probably coming from the ESP itself which could be from this section of code.
Sooooo .... I'm sorry to say that I can't figure out why you are seeing the MAC change.
I’ve been reading…. The LAA is both randomly generated and persistent.
I think the best idea might be to generate a random address, then store it in the ESP32’s Non-Volatile Storage (NVS). It would then survive power cycles. I think a lot of us have this problem with having multiple of the same ESPHome device, and I’d like to find a way out of editing the file every time the device is flashed. That is what I do now.
I just looked up how to make a persistent value that will survive reboots. This explains it. Store the CA and device name in a Global.
Logically there is only two way to make a random MAC. Either before you flash the ESP32 or after you flash it. Doing it before means modifying the YAML for each device. You coud likey set up a Phython or use M4 macros to create each new YAML file but hat is too slow as you have to compile each one. Better if you make the MAC the first time the device is powered up and then save it because now you can flash identical binaries to each device.
Yes, I know it should be generated, but my issue seems to be that the W5500 doesnt have a way to store it, and it keeps changing on its own for some reason.....
its looking like the best option I have is to change from w5500 to some other chip....