How to get MAC Address?

Surprised there’s not more questions about this? Just got my core setup and had to turn off my MAC Address filtering on my router. However, I want it back on but how do I find the MAC Address of my spark???

see

1 Like

I already saw that link and it’s of no help. I’ve got a fair bit of experience with Arduino, but green with the spark. The sample Arduino sketch is of no help as it has to be modified to use the suggested Network class. Tried to find info on the Network class to do this, but can’t find the docs on Spark. Have no idea what to do now… This thing is a tiny brick if I can’t secure my network… Crappy start experience with this product @zach . All I want is to get the MAC address before I move on and try to figure out how I can use this thing…

@deejayspinz, does your router’s web interface have a page that shows the connected devices? It migh be called “DHCP Client status” or something. That might show the MAC address there. With the Spark off, find that page, switch Spark on and refresh until a new device appears.

Paul

1 Like

Ok… After about an hour of messing around with the core, I solved the problem of getting the MAC Address of the core so I could enter it in my Router for MAC filtering… Anyways, here’s the suggested steps. If anyone has to go through this and finds a better way, let me know and I will update.

Goal: Get the MAC Address of the spark core to add it to router’s MAC Address filter list.

Steps:

  1. Assumes you have your core setup and talking to the online cloud. For the time being, turn off your MAC Address restrictions on your router and just get the spark connected.

  2. Go to the spark build and create a new sketch (sample below). The sketch will print the MAC Address to the serial window. Download it, and wait for the spark to restart. The blue LED should flash which should tell you the sketch is running.

  3. I am using Putty. Find the COM port that the spark is using (Device Manager on Win). Open Putty (or another Terminal app) and set to to connect to the spark COM port over Serial BUT DONT CONNECT JUST YET. (The default 9600,N,8,1 gobblygook should work).

  4. Now, click the reset button on your spark. It will go through the process to re-connect to your WiFi. Wait until it’s connected, then have Putty connect. If successful, the terminal window should come up and start showing the MAC Address (give it a few seconds to start).

Notes:

  • if Putty does not connect at first, try to connect again. You can’t connect while the spark is establishing it’s wifi connection. The LED should be slowly pulsing cyan in colour.
  • MAC Address: The response does not display leading 0’s. i.e. F:0:AA:35:48:33 is 0F:00…
  • When I first started doing this, I could not get Putty to connect to the spark when it was running. It said the COM port was already in use or some other error. Not sure what changed, but it works now.
  • Note that I did not go follow instructions in other posts to install the Windows USB driver manually. My spark installed a default driver the first time I plugged it in, and it works with that driver fine. it shows as “USB Serial (Communication Class, Abstract Control Model)” in Device Manager

//www.plastibots.com
byte mac[6]; // the MAC address of your Wifi shield
int LED = D7;

void setup()
{
Serial.begin(9600);
pinMode(LED, OUTPUT);
Network.macAddress(mac);

}

void loop () {

Serial.print("MAC: ");

Serial.print(mac[5],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.println(mac[0],HEX);

//blink an LED - this is just here as a validation that it’s working…

digitalWrite(LED, HIGH);
delay(250);
digitalWrite(LED, LOW);
delay(1000);

}

3 Likes

In case anyone else comes here in hopes of finding a solution to getting their mac address, here are some helps:

Network.macAddress(mac);

doesn’t exist anymore, use

WiFi.macAddress(mac);

Also, this code above will print your MAC address backwards and not zero padded. YMMV, but try this:

//www.plastibots.com
byte mac[6]; // the MAC address of your Wifi shield
int LED = D7;

void setup()
{
 Serial.begin(9600);
 pinMode(LED, OUTPUT);
 WiFi.macAddress(mac);
    
}

void loop () {
Serial.print("MAC: ");

Serial.print(mac[0],HEX);
 Serial.print(":");
 Serial.print(mac[1],HEX);
 Serial.print(":");
 Serial.print(mac[2],HEX);
 Serial.print(":");
 Serial.print(mac[3],HEX);
 Serial.print(":");
 Serial.print(mac[4],HEX);
 Serial.print(":");
 Serial.println(mac[5],HEX);

//blink an LED - this is just here as a validation that it's working...

digitalWrite(LED, HIGH);
 delay(250);
 digitalWrite(LED, LOW);
 delay(1000);

}

If you just need to know your MAC address, you can put your device in Listening Mode and press m
https://docs.particle.io/support/troubleshooting/troubleshooting-tools/photon/#display-mac-address

I have a peculiar situation… The MAC Address on my Photon has a ‘0’ in it and when I print the mac address it gives me the letter ‘b’ for the segment and iterates through the rest of the segments as expected. I printed the DEC value and it gave me 11 which when converted to HEX is ‘b’.

I’m using the following code as provided in the reference docs:

WiFi.macAddress(mac);

  for (int i=0; i<6; i++) {
    if (i) Serial.print(":");
    Serial.print(mac[i], HEX);
  }

Any help is greatly appreciated.

To make the story more interesting, I followed the link above and (press ‘m’ when in listening mode) and it generates the MAC Address with the correct segment: ‘0b’.

Hi @tinkererer

Hex numbers are not printed with leading zeros in Arduino land so the behavior you are seeing is correct. There are lots of ways to work around this. Here’s one:

  for( int i=0; i<6; i++) {
    Serial.print(mac[i]>>4,HEX);
    Serial.print(mac[i]&0x0f,HEX);
    if (i != 5) {
      Serial.print(":");
    }
  }
2 Likes

Thanks @bko That explains a lot and worked like a charm!

Serial.printf("%02x%s", mac[i], i != 5 ? ":" : "");

would also work


Docs updated with your examples @bko @ScruffR https://github.com/spark/docs/commit/3224e9c13b24461ef50a41e51b548e0e7fb07c11 Thanks!

3 Likes