Skip to content

Instantly share code, notes, and snippets.

@abcd-ca
Created May 27, 2019 04:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abcd-ca/556e67d09e5619b2dd534c173b65624b to your computer and use it in GitHub Desktop.
Save abcd-ca/556e67d09e5619b2dd534c173b65624b to your computer and use it in GitHub Desktop.
Example of getting unique mac address using Ethernet Featherwing
#import "macAddress.h"
void setup () {
byte mac[6];
getUniqueMacAddress(mac);
Serial.begin(115200);
Serial.print("mac: ");
for (uint8_t i = 0; i < sizeof(mac); i++) {
Serial.print(mac[i], 16); Serial.print(" ");
}
}
void loop () {
}
#include "Arduino.h"
#ifndef macAddress_H
#define macAddress_H
/**
* Gets a unique mac address at runtime
*
* The Ethernet chip doesn't come with a mac address set on it so you have to supply one.
* This line ensures that the mac address is unique among devices on the lan – at least among
* RSG devices using ESP8266 – by using the unique chip id that the ESP8266 does come with (not
* to be confused with the ethernet featherwing chip), and using that as the unique last four
* bytes of the mac address
*
* @param mac 6 byte mac address container to be overwritten
* @author Andrew Blair
*/
void getUniqueMacAddress(uint8_t* mac) {
union ChipID {
byte bytes[4];
uint32_t value;
};
union ChipID chipId;
chipId.value = ESP.getChipId();
// 6-byte mac address
mac[0] = 0xDE; // arbitrary
mac[1] = 0xAD; // arbitrary
mac[2] = chipId.bytes[3]; // 4th byte of uint32_t...
mac[3] = chipId.bytes[2];
mac[4] = chipId.bytes[1];
mac[5] = chipId.bytes[0];
}
#endif //macAddress_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment