So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Johnd2117
#66700 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.
User avatar
By Johnd2117
#66735
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.
User avatar
By martinayotte
#66806 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
}
User avatar
By Johnd2117
#66832
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. :)