ESP32: ChipID の取得

こちらのページを参考にしました。
[ESP32-WROOM-32 チップ・メモリ・MACアドレス情報取得方法](https://www.mgo-tec.com/blog-entry-chip-info-esp-wroom-32-esp32.html)

Ubuntu 24.04 上の Arduino IDE 2.3.2 を使います。
ボードは、
ESP32 Dev Module
です。

プログラム

```text:get_chipid.ino
/*
// ---------------------------------------------------------------------
	get_chipid.ino

						Nov/16/2022
*/
// ---------------------------------------------------------------------
void setup(void) {
	Serial.begin(115200);
	Serial.println("\r\n-----------------------------");

	get_info_proc();
	delay(1000);
}

// ---------------------------------------------------------------------
void get_info_proc()
{ 
	uint64_t chipid;
	chipid=ESP.getEfuseMac();//The chip ID is essentially its MAC address(length: 6 bytes).
	Serial.printf("ESP32 Chip ID = %04X\r\n",(uint16_t)(chipid>>32));//print High 2 bytes
 
	Serial.printf("Chip Revision %d\r\n", ESP.getChipRevision());
	esp_chip_info_t chip_info;
	esp_chip_info(&chip_info);
	Serial.printf("Number of Core: %d\r\n", chip_info.cores);
	Serial.printf("CPU Frequency: %d MHz\r\n", ESP.getCpuFreqMHz());	
	Serial.printf("Flash Chip Size = %d byte\r\n", ESP.getFlashChipSize());
	Serial.printf("Flash Frequency = %d Hz\r\n", ESP.getFlashChipSpeed());
	Serial.printf("ESP-IDF version = %s\r\n", esp_get_idf_version());
	//利用可能なヒープのサイズを取得
	Serial.printf("Available Heap Size= %d\r\n", esp_get_free_heap_size());
	//利用可能な内部ヒープのサイズを取得
	Serial.printf("Available Internal Heap Size = %d\r\n", esp_get_free_internal_heap_size());
	//これまでに利用可能だった最小ヒープを取得します
	Serial.printf("Minimum Free Heap Ever Available Size = %d\r\n", esp_get_minimum_free_heap_size());
	Serial.println();
 
	uint8_t mac0[6];
	esp_efuse_mac_get_default(mac0);
	Serial.printf("Default Mac Address = %02X:%02X:%02X:%02X:%02X:%02X\r\n", mac0[0], mac0[1], mac0[2], mac0[3], mac0[4], mac0[5]);
 
	uint8_t mac3[6];
	esp_read_mac(mac3, ESP_MAC_WIFI_STA);
	Serial.printf("[Wi-Fi Station] Mac Address = %02X:%02X:%02X:%02X:%02X:%02X\r\n", mac3[0], mac3[1], mac3[2], mac3[3], mac3[4], mac3[5]);
 
	uint8_t mac4[7];
	esp_read_mac(mac4, ESP_MAC_WIFI_SOFTAP);
	Serial.printf("[Wi-Fi SoftAP] Mac Address = %02X:%02X:%02X:%02X:%02X:%02X\r\n", mac4[0], mac4[1], mac4[2], mac4[3], mac4[4], mac4[5]);
 
	uint8_t mac5[6];
	esp_read_mac(mac5, ESP_MAC_BT);
	Serial.printf("[Bluetooth] Mac Address = %02X:%02X:%02X:%02X:%02X:%02X\r\n", mac5[0], mac5[1], mac5[2], mac5[3], mac5[4], mac5[5]);
 
	uint8_t mac6[6];
	esp_read_mac(mac6, ESP_MAC_ETH);
	Serial.printf("[Ethernet] Mac Address = %02X:%02X:%02X:%02X:%02X:%02X\r\n", mac6[0], mac6[1], mac6[2], mac6[3], mac6[4], mac6[5]);
}
 
// ---------------------------------------------------------------------
void loop()
{
	get_info_proc();
  Serial.printf("----\r\n");
	delay(3000);
}

// ---------------------------------------------------------------------
```

Arduino IDE 2.3.2
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/2a50f710-4da0-b479-86d7-6ced22dfdd21.png)


実行結果

```text
$ cu -l /dev/ttyACM0 -s 115200
Connected.
ESP32 Chip ID = F430
Chip Revision 3
Number of Core: 2
CPU Frequency: 240 MHz
Flash Chip Size = 16777216 byte
Flash Frequency = 80000000 Hz
ESP-IDF version = v4.4.2
Available Heap Size= 271344
Available Internal Heap Size = 271344
Minimum Free Heap Ever Available Size = 265792

Default Mac Address = 58:BF:24:ED:30:F4
[Wi-Fi Station] Mac Address = 58:BF:24:ED:30:F4
[Wi-Fi SoftAP] Mac Address = 58:BF:24:ED:30:F5
[Bluetooth] Mac Address = 58:BF:24:ED:30:F6
[Ethernet] Mac Address = 58:BF:24:ED:30:F7
----
```

## スケッチ例を改造

ファイル -> スケッチ例 -> ESP32 -> ChipID
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/87d9ab82-e2e2-6746-4fcb-de23febb84a0.png)


```ino:GetChipID.ino	
uint32_t chipId = 0;

void setup() {
	Serial.begin(115200);
}

void loop() {
	for(int i=0; i<17; i=i+8) {
	  chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
	}

	Serial.printf("ESP32 Chip model = %s Rev %d\r\n", ESP.getChipModel(), ESP.getChipRevision());
	Serial.printf("This chip has %d cores\r\n", ESP.getChipCores());
  Serial.print("Chip ID: ");
  Serial.println(chipId);
  Serial.printf("ESP32 Chip ID = %08X\r\n",chipId);
  Serial.printf("\r\n");
	delay(3000);

}
```

実行結果

```text
cu -l /dev/ttyACM0 -s 115200
Connected.
ESP32 Chip model = ESP32-D0WDQ5 Rev 3
This chip has 2 cores
Chip ID: 15544564
ESP32 Chip ID = 00ED30F4
```