The code below is used to get the MAC addresses of the WiFi and Bluetooth interfaces of an ESP32 microcontroller. The code is the combinaton of the two previuos examples:
ESP-IDF: ESP32 WiFi controller MAC address retrieving
ESP-IDF: ESP32 Bluetooth controller MAC address retrieving
I have to mention again please don’t forget to enable Wifi, Bluetooth and Bluetooth Classic mode functions in the ESP-IDF SDK Configuration Editor (menuconfig). You can use the gear icon on the bottom bat in VS Code to enter the menuconfig.
Within the building of the project I received an error code, see here:
FAILED: esp-idf/esptool_py/CMakeFiles/app_check_size
.
.
.
Error: app partition is too small for binary ESP32_GET_WIFI_AND_BT_MAC_ADDRESS.bin size 0x1348a0: - Part 'factory' 0/0 @ 0x10000 size 0x100000 (overflow 0x348a0) ninja: build stopped: subcommand failed.
The solve this error I went to the menuconfig and found the Partition Table and set the parameter from Single factory app. no OTA to Single factory app (large). no OTA.
The code:
/**********************************************************************************************************
ESP32: ESP-IDF code creted in VS Code to retrieve the MAC address of the Wifi and Bluetooth controllers
Code created by AI and OK1TK with support of the Espressive ESP32 forum
**********************************************************************************************************/
#include <stdio.h>
#include "esp_wifi.h"
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "esp_log.h"
#include "nvs_flash.h"
void app_main()
{
esp_err_t ret;
esp_err_t retB;
//Initialize the default NVS partition.
ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// If NVS initialization fails due to uncleaned pages or a new version, we clear NVS and try again
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
// TCP/IP stack initialization
esp_netif_init();
esp_event_loop_create_default();
// Initialization WiFi
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ret = esp_wifi_init(&cfg);
if (ret != ESP_OK) {
ESP_LOGE("WiFi", "WiFi initialization FAILED!: %s", esp_err_to_name(ret));
return;
}
// Initialization of Bluetooth controller configuration
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ESP_LOGI("BT", "Call esp_bt_controller_init(&bt_cfg)");
if ((retB = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
ESP_LOGE("BT", "%s initialize controller FAILED!: %s\n", __func__, esp_err_to_name(retB));
return;
}
else{
ESP_LOGI("BT", "Initialize controller OK");
}
// Enable Bluetooth controller in CLASSIC Bluetooth mode
ESP_LOGI("BT", "Call esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)");
if ((retB = esp_bt_controller_enable(CONFIG_BT_CLASSIC_ENABLED)) != ESP_OK) {
ESP_LOGE("BT", "%s enable controller FAILED!: %s\n", __func__, esp_err_to_name(retB));
return;
}
else{
ESP_LOGI("BT", "Enable controller OK");
}
// Initialization of Bluedroid (necessary to be initialzed before calling "esp_bt_dev_get_address()")
ESP_LOGI("BT", "Call esp_bluedroid_init()");
if ((retB = esp_bluedroid_init()) != ESP_OK) {
ESP_LOGE("BT", "%s initialize bluedroid FAILED!: %s\n", __func__, esp_err_to_name(retB));
return;
}
else{
ESP_LOGI("BT", "Initialize bluedroid OK!");
}
// Eneable Bluedroid
ESP_LOGI("BT", "Call esp_bluedroid_enable()");
if ((retB = esp_bluedroid_enable()) != ESP_OK) {
ESP_LOGE("BT", "%s enable bluedroid FAILED!: %s\n", __func__, esp_err_to_name(retB));
return;
}
else{
ESP_LOGI("BT", "Enable bluedroid OK!");
}
// Retrieving the the MAC address of the WiFi interface
uint8_t macWiFi[6]; // Declaration of a variable to store the WiFi MAC address
ret = esp_wifi_get_mac(ESP_IF_WIFI_STA, macWiFi);
if (ret != ESP_OK) {
ESP_LOGE("WiFi", "Unable to retrieve the MAC address of the WiFi interface: %s", esp_err_to_name(ret));
return;
}
// Retrieving the the MAC address of the Bluetooth interface
const uint8_t *macBT; // Declaration of a variable to store the MAC address
macBT = esp_bt_dev_get_address();
if (macBT == NULL) {
ESP_LOGE("BT", "Unable to retrieve the MAC address of the Bluetooth interface");
return;
}
// Writing the MAC address of the WiFi interface to the terminal
ESP_LOGI("WiFi", "The WiFi interface MAC address: %02x:%02x:%02x:%02x:%02x:%02x",
macWiFi[0], macWiFi[1], macWiFi[2], macWiFi[3], macWiFi[4], macWiFi[5]);
// Writing the MAC address of the Bluetooth interface to the terminal
ESP_LOGI("BT", "The Bluetooth interface MAC address: %02x:%02x:%02x:%02x:%02x:%02x",
macBT[0], macBT[1], macBT[2], macBT[3], macBT[4], macBT[5]);
}