Skip to content

Commit

Permalink
ESP Support
Browse files Browse the repository at this point in the history
Add ESP8266 Support
Add ESP32 Support
Add UniqueID8dump function
Add UniqueID8 example
  • Loading branch information
ricaun committed Apr 16, 2019
1 parent 08618b1 commit fb7552d
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 18 deletions.
56 changes: 50 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# ArduinoUniqueID

This Library gets the Unique Serial ID from the AVR Microcontroller.
This Library gets the Unique Serial ID from the AVR Microcontroller and ESP Microcontroller.

# AVR Microcontroller

## Unique Serial ID - Hidden Serial Number

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

## Dependencies

This library only works on AVR Microcontroller.

## Tested Microcontroller

* Atmega328pb - 10 bytes
* Atmega328p - 9 bytes
* Atmega2560 - 9 bytes
* Attiny85 - 9 bytes

# ESP Microcontroller

ESP microcontroller has basically two versions, ESP8266 and ESP32, each one has a specific function to request the chip id. <br/>

* ESP8266 - ESP.getChipId() - 4 bytes
* ESP32 - ESP.getEfuseMac() - 6 bytes

| UniqueID | ESP8266 | ESP32 |
| :-------: | :------: | :------:|
| Byte 0| Byte 0 | Byte 0 |
| Byte 1| Byte 1 | Byte 1 |
| Byte 2| Byte 2 | Byte 2 |
| Byte 3| Byte 3 | Byte 3 |
| Byte 4| - | Byte 4 |
| Byte 5| - | Byte 5 |

To make the variable UniqueID8 to work propably the library uses the default bytes to 0x00. <br/>

| UniqueID8 | ESP8266 | ESP32 |
| :-------: | :------: | :------:|
| Byte 0| 0x00 | 0x00 |
| Byte 1| 0x00 | 0x00 |
| Byte 2| 0x00 | Byte 0 |
| Byte 3| 0x00 | Byte 1 |
| Byte 4| Byte 0 | Byte 2 |
| Byte 5| Byte 1 | Byte 3 |
| Byte 6| Byte 2 | Byte 4 |
| Byte 7| Byte 3 | Byte 5 |

## Tested Microcontroller

* ESP8266 - 4 bytes
* ESP32 - 6 bytes

## Dependencies

This library only supports AVR Microcontroller and ESP Microcontroller.

## Installation

* Install the library by [Using the Library Manager](https://www.arduino.cc/en/Guide/Libraries#toc3)
* **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.
* **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.

## Examples

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

### Method: UniqueID8dump

Print the last eight hexadecimal bytes of the Unique Serial ID on the Stream.

```c
void UniqueID8dump(Stream);
```
Do you like this library? Please [star this project on GitHub](https://github.com/ricaun/ArduinoUniqueID/stargazers)!
4 changes: 2 additions & 2 deletions examples/ArduinoUniqueID/ArduinoUniqueID.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// ArduinoUniqueID.ino
//
// Example shows the Serial Unique ID on the Serial Monitor.
// Example shows the UniqueID on the Serial Monitor.
//

#include <ArduinoUniqueID.h>
Expand All @@ -10,7 +10,7 @@ void setup()
{
Serial.begin(115200);
UniqueIDdump(Serial);
Serial.print("Serial Unique ID: ");
Serial.print("UniqueID: ");
for (size_t i = 0; i < UniqueIDsize; i++)
{
if (UniqueID[i] < 0x10)
Expand Down
26 changes: 26 additions & 0 deletions examples/ArduinoUniqueID8/ArduinoUniqueID8.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// ArduinoUniqueID8.ino
//
// Example shows the last eight UniqueID on the Serial Monitor.
//

#include <ArduinoUniqueID.h>

void setup()
{
Serial.begin(115200);
UniqueID8dump(Serial);
Serial.print("UniqueID: ");
for (size_t i = 0; i < 8; i++)
{
if (UniqueID8[i] < 0x10)
Serial.print("0");
Serial.print(UniqueID8[i], HEX);
Serial.print(" ");
}
Serial.println();
}

void loop()
{
}
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ArduinoUniqueID KEYWORD1
#######################################

UniqueIDdump KEYWORD2
UniqueID8dump KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down
6 changes: 3 additions & 3 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=ArduinoUniqueID
version=1.0.3
version=1.0.4
author=Luiz Henrique Cassettari
maintainer=Luiz Henrique Cassettari <[email protected]>
sentence=Arduino Library to get the AVR microcontroler Unique ID / Manufacture Serial Number.
paragraph=The ArduinoUniqueID Library use the buildin feature to select the manufacture serial number from the microcontroler. Suported / Tested microcontroler: Atmega328pb, Atmega328p, Atmega2560 & Attiny85.
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.
category=Other
url=https://github.com/ricaun/ArduinoUniqueID
architectures=avr
architectures=avr, esp8266, esp32
includes=ArduinoUniqueID.h
25 changes: 24 additions & 1 deletion src/ArduinoUniqueID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,33 @@

ArduinoUniqueID::ArduinoUniqueID()
{
for(size_t i = 0; i < UniqueIDsize; i++)
#if defined(ARDUINO_ARCH_AVR)
for (size_t i = 0; i < UniqueIDsize; i++)
{
id[i] = boot_signature_byte_get(0x0E + i + (UniqueIDsize == 9 && i > 5 ? 1 : 0));
}
#elif defined(ARDUINO_ARCH_ESP8266)
uint32_t chipid = ESP.getChipId();
id[0] = 0;
id[1] = 0;
id[2] = 0;
id[3] = 0;
id[4] = chipid >> 24;
id[5] = chipid >> 16;
id[6] = chipid >> 8;
id[7] = chipid;

#elif defined(ARDUINO_ARCH_ESP32)
uint64_t chipid = ESP.getEfuseMac();
id[0] = 0;
id[1] = 0;
id[2] = chipid >> 40;
id[3] = chipid >> 32;
id[4] = chipid >> 24;
id[5] = chipid >> 16;
id[6] = chipid >> 8;
id[7] = chipid;
#endif
}

ArduinoUniqueID _UniqueID;
40 changes: 34 additions & 6 deletions src/ArduinoUniqueID.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,41 @@

#include <Arduino.h>

#if __AVR_ARCH__
#if defined(ARDUINO_ARCH_AVR)
#include <avr/boot.h>
#ifndef SIGRD
#define SIGRD 5
#endif
#elif defined(ARDUINO_ARCH_ESP8266)
#elif defined(ARDUINO_ARCH_ESP32)
#else
#error "ArduinoUniqueID only works on AVR Architecture"
#error "ArduinoUniqueID only works on AVR and ESP Architecture"
#endif

#if defined(ARDUINO_ARCH_AVR)

#if defined(__AVR_ATmega328PB__)
#define UniqueIDsize 10
#else
#define UniqueIDsize 9
#endif

#define UniqueID8 (_UniqueID.id + UniqueIDsize - 8)
#define UniqueID _UniqueID.id
#define UniqueIDbuffer UniqueIDsize

#elif defined(ARDUINO_ARCH_ESP8266)
#define UniqueIDsize 4
#define UniqueIDbuffer 8
#elif defined(ARDUINO_ARCH_ESP32)
#define UniqueIDsize 6
#define UniqueIDbuffer 8
#endif

#define UniqueID8 (_UniqueID.id + UniqueIDbuffer - 8)
#define UniqueID (_UniqueID.id + UniqueIDbuffer - UniqueIDsize)

#define UniqueIDdump(stream) \
{ \
stream.print("Serial Unique ID: "); \
stream.print("UniqueID: "); \
for (size_t i = 0; i < UniqueIDsize; i++) \
{ \
if (UniqueID[i] < 0x10) \
Expand All @@ -36,11 +51,24 @@
stream.println(); \
}

#define UniqueID8dump(stream) \
{ \
stream.print("UniqueID: "); \
for (size_t i = 0; i < 8; i++) \
{ \
if (UniqueID8[i] < 0x10) \
stream.print("0"); \
stream.print(UniqueID8[i], HEX); \
stream.print(" "); \
} \
stream.println(); \
}

class ArduinoUniqueID
{
public:
ArduinoUniqueID();
uint8_t id[UniqueIDsize];
uint8_t id[UniqueIDbuffer];
};

extern ArduinoUniqueID _UniqueID;
Expand Down

0 comments on commit fb7552d

Please sign in to comment.