SparkFun WiFi Shield/Arduino Newbee

I am trying to find the MAC address on my SparkFun WiFi Shield and I don't understand how to code this properly. Here is an extract from the SparkFunESP8266 library:

// From SparkFunESP8266.h
=========================
int16_t localMAC(char * mac);

// From SparkFunESP8266,cpp
===========================
int16_t ESP8266Class::localMAC(char * mac)
{
	sendCommand(ESP8266_GET_STA_MAC, ESP8266_CMD_QUERY); // Send "AT+CIPSTAMAC?"

	int16_t rsp = readForResponse(RESPONSE_OK, COMMAND_RESPONSE_TIMEOUT);
	if (rsp > 0)
	{
		// Look for "+CIPSTAMAC"
		char * p = strstr(esp8266RxBuffer, ESP8266_GET_STA_MAC);
		if (p != NULL)
		{
			p += strlen(ESP8266_GET_STA_MAC) + 2;
			char * q = strchr(p, '"');
			if (q == NULL) return ESP8266_RSP_UNKNOWN;
			strncpy(mac, p, q - p); // Copy string to temp char array:
			return 1;
		}
	}
	return rsp;
}

Can anybody help with the correct way to code to obtain the MAC address. I tried this:

int myMAC = esp8266.localMAC();
Serial.print(F("MAC Address: "));
Serial.println(myMAC);

It don't work.

int myMAC = esp8266.localMAC();
Serial.print(F("MAC Address: "));
Serial.println(myMAC);

It don't work.

Sure it did. It did exactly what you told it to do. The mac address is NOT an int, though.

Or would have, if the code had even compiled.

The localMAC() function takes an address where the function is to store the MAC address as a string.

Thanks for your reply.

I used to play with C for a while but I am still very vague about C++. Would you be kind enough to give me an example of how this works Please? From your answer I'm guessing I need an array ma by and the localMAC() is a pointer of some sort?

char myMacAddr[10];
   int rc = esp8266.localMAC(myMacAddr);
   if(rc == 0)
   {
      Serial.print("My MAC address is: [");
      Serial.print(myMacAddr);
      Serial.println("]");
   }

Thanks for that. I will try it out and hopefully learn from it. :slight_smile:

Once again, thank you for your help. This is the code I used in the end, and it works :o

int retMac = esp8266.localMAC(myMAC);
	if (retMac > 0)
	{
		Serial.print("My MAC address is: ");
		Serial.print(myMAC[0], HEX);
		Serial.print(":");
		Serial.print(myMAC[1], HEX);
		Serial.print(":");
		Serial.print(myMAC[2], HEX);
		Serial.print(":");
		Serial.print(myMAC[3], HEX);
		Serial.print(":");
		Serial.print(myMAC[4], HEX);
		Serial.print(":");
		Serial.println(myMAC[6], HEX);
	}

I know I could use a for next loop to do the Serial.print statements. I'll sort that latter. :slight_smile:

0, 1, 2, 3, 4, and SIX? I doubt that THAT worked. At least not for MY definition of work.