Starting WiFi with custom mac address

BaartCM
Posts: 17
Joined: Tue Mar 13, 2018 11:55 am

Starting WiFi with custom mac address

Postby BaartCM » Mon Sep 24, 2018 5:56 am

Hi Guys,

I have a project which involves a number of ESP32's with Ethernet modules reading from sensors and sending the sensor info to a Client PC running a Python app as and when the client requests said data. So that I can identify my ESP32 modules on the network, I assign them all a mac address starting EA and consisting only of numbers 0-9. This is how I do it;

Code: Select all

  // start WiFi to read current mac
  WiFi.begin();
  WiFi.macAddress(mac );
  // now set new all decimal mac starting 'EA'
  mac[0]=0xEA;
  for(int i= 1; i<6; i++)
  {
    int a = ((mac[i]%100)/10)*16;
    int b = ((mac[i]%100)%10);
    mac[i]=a+b;
  }
  Serial.begin(115200);
  Serial.println("App started ..... ");
  Serial.print("Unit MAC address : ");
  for(int i=0; i<5; i++)
  {
    Serial.print(mac[i], HEX);
    Serial.print(":");
  }
  Serial.println(mac[5], HEX);
  Ethernet.init(WIZ_CS);
  Ethernet.begin(mac);
The Python code calls 'arp -a' and parses the result looking for any mac addresses starting EA and containing only numbers 0-9 and stores them in a MySQL database so I can assign them.

I would also like to be able to offer units which are WiFi rather than PoE but I have been unable to find a way of starting the WiFi with my own mac rather than the one burned to the ESP32 chip. In the WiFi.begin process, there must be somewhere that the ESP32's mac is read from efuse and used. I know I can burn a new mac to efuse but I dont want to do that, I just want to create a new mac from the current mac, and start WiFi using my mac.

I realise that I will need to read the mac directly from efuse so that I can create my own mac before I call WiFi.begin()

Any suggestions?

Thanks, Steve.

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: Starting WiFi with custom mac address

Postby ESP_igrr » Mon Sep 24, 2018 12:38 pm

There is an example of specifying custom MAC addresses in ESP-IDF: https://github.com/espressif/esp-idf/bl ... ple_main.c. Basically, you need to call esp_base_mac_addr_set before starting WiFi.

To get the default address, you can use esp_efuse_mac_get_default:
https://docs.espressif.com/projects/esp ... tP7uint8_t

BaartCM
Posts: 17
Joined: Tue Mar 13, 2018 11:55 am

Re: Starting WiFi with custom mac address

Postby BaartCM » Tue Sep 25, 2018 9:01 am

Thanks ESP_igrr. I have (almost) succeeded. For anyone else wishing to do something similar, I am posting working code here.
First of all, you need to include

Code: Select all

#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include "esp_wifi.h"
byte            mac[6];
Then you can read the mac, change it and reset it using the following in your setup()

Code: Select all

  esp_efuse_mac_get_default(mac); // reads existing default mac
  // set your new mac here
  wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  esp_wifi_init(&cfg);
  esp_wifi_set_mac(WIFI_IF_STA, mac);
I get the following warning
"E (264) event: Event loop not initialized via esp_event_loop_init, but esp_event_send called"
but it has set the new mac correctly regardless and the rest of the code works as intended.

Hope this helps.
Steve.

Who is online

Users browsing this forum: No registered users and 44 guests