Skip to content

Commit fb7552d

Browse files
committed
ESP Support
Add ESP8266 Support Add ESP32 Support Add UniqueID8dump function Add UniqueID8 example
1 parent 08618b1 commit fb7552d

File tree

7 files changed

+140
-18
lines changed

7 files changed

+140
-18
lines changed

README.md

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# ArduinoUniqueID
22

3-
This Library gets the Unique Serial ID from the AVR Microcontroller.
3+
This Library gets the Unique Serial ID from the AVR Microcontroller and ESP Microcontroller.
4+
5+
# AVR Microcontroller
46

57
## Unique Serial ID - Hidden Serial Number
68

@@ -23,21 +25,55 @@ Apparently, the chip Atmega328p have a hidden serial number with 9 bytes, and ot
2325
| 0x0016 | Byte 8 | Byte 7 |
2426
| 0x0017 | Byte 9 | Byte 8 |
2527

26-
## Dependencies
27-
28-
This library only works on AVR Microcontroller.
29-
3028
## Tested Microcontroller
3129

3230
* Atmega328pb - 10 bytes
3331
* Atmega328p - 9 bytes
3432
* Atmega2560 - 9 bytes
3533
* Attiny85 - 9 bytes
3634

35+
# ESP Microcontroller
36+
37+
ESP microcontroller has basically two versions, ESP8266 and ESP32, each one has a specific function to request the chip id. <br/>
38+
39+
* ESP8266 - ESP.getChipId() - 4 bytes
40+
* ESP32 - ESP.getEfuseMac() - 6 bytes
41+
42+
| UniqueID | ESP8266 | ESP32 |
43+
| :-------: | :------: | :------:|
44+
| Byte 0| Byte 0 | Byte 0 |
45+
| Byte 1| Byte 1 | Byte 1 |
46+
| Byte 2| Byte 2 | Byte 2 |
47+
| Byte 3| Byte 3 | Byte 3 |
48+
| Byte 4| - | Byte 4 |
49+
| Byte 5| - | Byte 5 |
50+
51+
To make the variable UniqueID8 to work propably the library uses the default bytes to 0x00. <br/>
52+
53+
| UniqueID8 | ESP8266 | ESP32 |
54+
| :-------: | :------: | :------:|
55+
| Byte 0| 0x00 | 0x00 |
56+
| Byte 1| 0x00 | 0x00 |
57+
| Byte 2| 0x00 | Byte 0 |
58+
| Byte 3| 0x00 | Byte 1 |
59+
| Byte 4| Byte 0 | Byte 2 |
60+
| Byte 5| Byte 1 | Byte 3 |
61+
| Byte 6| Byte 2 | Byte 4 |
62+
| Byte 7| Byte 3 | Byte 5 |
63+
64+
## Tested Microcontroller
65+
66+
* ESP8266 - 4 bytes
67+
* ESP32 - 6 bytes
68+
69+
## Dependencies
70+
71+
This library only supports AVR Microcontroller and ESP Microcontroller.
72+
3773
## Installation
3874

3975
* Install the library by [Using the Library Manager](https://www.arduino.cc/en/Guide/Libraries#toc3)
40-
* **OR** by [Importing the .zip library](https://www.arduino.cc/en/Guide/Libraries#toc4) using either the [master](https://github.com/ricaun/ArduinoUniqueID/archive/1.0.3.zip) or one of the [releases](https://github.com/ricaun/ArduinoUniqueID/releases) ZIP files.
76+
* **OR** by [Importing the .zip library](https://www.arduino.cc/en/Guide/Libraries#toc4) using either the [master](https://github.com/ricaun/ArduinoUniqueID/archive/1.0.4.zip) or one of the [releases](https://github.com/ricaun/ArduinoUniqueID/releases) ZIP files.
4177

4278
## Examples
4379

@@ -79,4 +115,12 @@ for(size_t i = 0; i < 8; i++)
79115
Serial.println(UniqueID8[i], HEX);
80116
```
81117

118+
### Method: UniqueID8dump
119+
120+
Print the last eight hexadecimal bytes of the Unique Serial ID on the Stream.
121+
122+
```c
123+
void UniqueID8dump(Stream);
124+
```
125+
82126
Do you like this library? Please [star this project on GitHub](https://github.com/ricaun/ArduinoUniqueID/stargazers)!

examples/ArduinoUniqueID/ArduinoUniqueID.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// ArduinoUniqueID.ino
33
//
4-
// Example shows the Serial Unique ID on the Serial Monitor.
4+
// Example shows the UniqueID on the Serial Monitor.
55
//
66

77
#include <ArduinoUniqueID.h>
@@ -10,7 +10,7 @@ void setup()
1010
{
1111
Serial.begin(115200);
1212
UniqueIDdump(Serial);
13-
Serial.print("Serial Unique ID: ");
13+
Serial.print("UniqueID: ");
1414
for (size_t i = 0; i < UniqueIDsize; i++)
1515
{
1616
if (UniqueID[i] < 0x10)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// ArduinoUniqueID8.ino
3+
//
4+
// Example shows the last eight UniqueID on the Serial Monitor.
5+
//
6+
7+
#include <ArduinoUniqueID.h>
8+
9+
void setup()
10+
{
11+
Serial.begin(115200);
12+
UniqueID8dump(Serial);
13+
Serial.print("UniqueID: ");
14+
for (size_t i = 0; i < 8; i++)
15+
{
16+
if (UniqueID8[i] < 0x10)
17+
Serial.print("0");
18+
Serial.print(UniqueID8[i], HEX);
19+
Serial.print(" ");
20+
}
21+
Serial.println();
22+
}
23+
24+
void loop()
25+
{
26+
}

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ArduinoUniqueID KEYWORD1
1313
#######################################
1414

1515
UniqueIDdump KEYWORD2
16+
UniqueID8dump KEYWORD2
1617

1718
#######################################
1819
# Constants (LITERAL1)

library.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=ArduinoUniqueID
2-
version=1.0.3
2+
version=1.0.4
33
author=Luiz Henrique Cassettari
44
maintainer=Luiz Henrique Cassettari <ricaun@gmail.com>
55
sentence=Arduino Library to get the AVR microcontroler Unique ID / Manufacture Serial Number.
6-
paragraph=The ArduinoUniqueID Library use the buildin feature to select the manufacture serial number from the microcontroler. Suported / Tested microcontroler: Atmega328pb, Atmega328p, Atmega2560 & Attiny85.
6+
paragraph=The ArduinoUniqueID Library use the buildin feature to select the manufacture serial number from the microcontroler. Suported microcontroler: Atmega328pb, Atmega328p, Atmega2560, Attiny85, ESP8266 & ESP32.
77
category=Other
88
url=https://github.com/ricaun/ArduinoUniqueID
9-
architectures=avr
9+
architectures=avr, esp8266, esp32
1010
includes=ArduinoUniqueID.h

src/ArduinoUniqueID.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,33 @@
55

66
ArduinoUniqueID::ArduinoUniqueID()
77
{
8-
for(size_t i = 0; i < UniqueIDsize; i++)
8+
#if defined(ARDUINO_ARCH_AVR)
9+
for (size_t i = 0; i < UniqueIDsize; i++)
910
{
1011
id[i] = boot_signature_byte_get(0x0E + i + (UniqueIDsize == 9 && i > 5 ? 1 : 0));
1112
}
13+
#elif defined(ARDUINO_ARCH_ESP8266)
14+
uint32_t chipid = ESP.getChipId();
15+
id[0] = 0;
16+
id[1] = 0;
17+
id[2] = 0;
18+
id[3] = 0;
19+
id[4] = chipid >> 24;
20+
id[5] = chipid >> 16;
21+
id[6] = chipid >> 8;
22+
id[7] = chipid;
23+
24+
#elif defined(ARDUINO_ARCH_ESP32)
25+
uint64_t chipid = ESP.getEfuseMac();
26+
id[0] = 0;
27+
id[1] = 0;
28+
id[2] = chipid >> 40;
29+
id[3] = chipid >> 32;
30+
id[4] = chipid >> 24;
31+
id[5] = chipid >> 16;
32+
id[6] = chipid >> 8;
33+
id[7] = chipid;
34+
#endif
1235
}
1336

1437
ArduinoUniqueID _UniqueID;

src/ArduinoUniqueID.h

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,41 @@
66

77
#include <Arduino.h>
88

9-
#if __AVR_ARCH__
9+
#if defined(ARDUINO_ARCH_AVR)
1010
#include <avr/boot.h>
1111
#ifndef SIGRD
1212
#define SIGRD 5
1313
#endif
14+
#elif defined(ARDUINO_ARCH_ESP8266)
15+
#elif defined(ARDUINO_ARCH_ESP32)
1416
#else
15-
#error "ArduinoUniqueID only works on AVR Architecture"
17+
#error "ArduinoUniqueID only works on AVR and ESP Architecture"
1618
#endif
1719

20+
#if defined(ARDUINO_ARCH_AVR)
21+
1822
#if defined(__AVR_ATmega328PB__)
1923
#define UniqueIDsize 10
2024
#else
2125
#define UniqueIDsize 9
2226
#endif
2327

24-
#define UniqueID8 (_UniqueID.id + UniqueIDsize - 8)
25-
#define UniqueID _UniqueID.id
28+
#define UniqueIDbuffer UniqueIDsize
29+
30+
#elif defined(ARDUINO_ARCH_ESP8266)
31+
#define UniqueIDsize 4
32+
#define UniqueIDbuffer 8
33+
#elif defined(ARDUINO_ARCH_ESP32)
34+
#define UniqueIDsize 6
35+
#define UniqueIDbuffer 8
36+
#endif
37+
38+
#define UniqueID8 (_UniqueID.id + UniqueIDbuffer - 8)
39+
#define UniqueID (_UniqueID.id + UniqueIDbuffer - UniqueIDsize)
40+
2641
#define UniqueIDdump(stream) \
2742
{ \
28-
stream.print("Serial Unique ID: "); \
43+
stream.print("UniqueID: "); \
2944
for (size_t i = 0; i < UniqueIDsize; i++) \
3045
{ \
3146
if (UniqueID[i] < 0x10) \
@@ -36,11 +51,24 @@
3651
stream.println(); \
3752
}
3853

54+
#define UniqueID8dump(stream) \
55+
{ \
56+
stream.print("UniqueID: "); \
57+
for (size_t i = 0; i < 8; i++) \
58+
{ \
59+
if (UniqueID8[i] < 0x10) \
60+
stream.print("0"); \
61+
stream.print(UniqueID8[i], HEX); \
62+
stream.print(" "); \
63+
} \
64+
stream.println(); \
65+
}
66+
3967
class ArduinoUniqueID
4068
{
4169
public:
4270
ArduinoUniqueID();
43-
uint8_t id[UniqueIDsize];
71+
uint8_t id[UniqueIDbuffer];
4472
};
4573

4674
extern ArduinoUniqueID _UniqueID;

0 commit comments

Comments
 (0)