-->
Page 2 of 3

Re: Finding localMAC on SparkFun WiFi Shield/Arduino

PostPosted: Sat Jun 03, 2017 6:31 pm
by Johnd2117
I have extracted the following pieces of code from the libraries:
Code: Select all// 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;
}


I did play with C for a while but C++ is vague to me. I hope this code clarifies the problem.

Re: Finding localMAC on SparkFun WiFi Shield/Arduino

PostPosted: Sun Jun 04, 2017 10:13 pm
by Johnd2117
martinayotte wrote:A MAC address is an array of 6 bytes, so it is really strange that you use a single int16_t.

On Arduino, the functions are the following :

For AP :
Code: Select alluint8_t* softAPmacAddress(uint8_t* mac);
String softAPmacAddress(void);

For STA :
Code: Select alluint8_t * macAddress(uint8_t* mac);
String macAddress();


I have extracted the following from the SparhFunESP8266 library files:

Code: Select all// 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;
}


I hope this helps to clarify the problem.

Re: Finding localMAC on SparkFun WiFi Shield/Arduino

PostPosted: Tue Jun 06, 2017 7:57 am
by martinayotte
As we can see in the above code is that return value is NOT the MAC address, but an Error code.
The actual MAC address is written in the array of char provided in argument.

So, the call to that function would look like :
Code: Select allchar themac[32];
if (localMac(theMac) > 0) {
    // do something with the MAC
}

Re: Finding localMAC on SparkFun WiFi Shield/Arduino

PostPosted: Tue Jun 06, 2017 4:11 pm
by Johnd2117
martinayotte wrote:As we can see in the above code is that return value is NOT the MAC address, but an Error code.
The actual MAC address is written in the array of char provided in argument.

So, the call to that function would look like :
Code: Select allchar themac[32];
if (localMac(theMac) > 0) {
    // do something with the MAC
}


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