Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to change STATION MAC address when using SOFT_AP #8554

Closed
jam3st opened this issue May 1, 2022 · 5 comments · Fixed by #8614
Closed

Unable to change STATION MAC address when using SOFT_AP #8554

jam3st opened this issue May 1, 2022 · 5 comments · Fixed by #8614

Comments

@jam3st
Copy link

jam3st commented May 1, 2022

I have some old ESP8266 NOOS CODE that works but goes buggy after a few days. I want to port this to arduino as this uses 3.x of the SDK and I have an old 2.0 version of the NO OSSDK. In the old code I can randomize both the STATION and SOFT AP at the same time. But in this sketch only the first one is chnaged the station MAC doesn't change

#include <ESP8266WiFi.h>
#include <ESP8266TrueRandom.h>
/* Set these to your desired credentials for ESP8266 AP. */
const char *ssid = "SOFTAP";
const char *password = "01234567";

// Setup credentials for original WiFi, that we plan to repeat
const char* ssidExt = "ISPWIFI";


void setup() {
  uint8_t newMACAddress[6];
  Serial.begin(76800);

  
  ESP8266TrueRandom.mac(&newMACAddress[0]);
  Serial.printf("SOFTAP_IF %02x:%02x:%02x:%02x:%02x:%02x\r\n", newMACAddress[0],newMACAddress[1],newMACAddress[2],newMACAddress[3],newMACAddress[4], newMACAddress[5]);
  WiFi.softAPmacAddress( &newMACAddress[0]);

  ESP8266TrueRandom.mac(&newMACAddress[0]);
  Serial.printf("STATION_IF %02x:%02x:%02x:%02x:%02x:%02x\r\n", newMACAddress[0],newMACAddress[1],newMACAddress[2],newMACAddress[3],newMACAddress[4], newMACAddress[5]);
  wifi_set_macaddr(STATION_IF, &newMACAddress[0]);

  WiFi.mode(WIFI_AP_STA);
  WiFi.begin(ssidExt, password);
  WiFi.softAP(ssid, password);



  IPAddress myIP = WiFi.softAPIP();
  IPAddress apIP(192, 168, 4, 1);

  Serial.print("AP IP address: ");
  Serial.println(myIP);
}

bool connected = false;
void loop()
{
  bool newConnected =  (WiFi.status() == WL_CONNECTED);
  if(!connected && newConnected) {
    

      Serial.print("Connected to ");
      Serial.println(WiFi.macAddress());

      Serial.println(ssidExt);
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
      Serial.print("dnsIP address: ");
      Serial.println(WiFi.dnsIP());
      Serial.print("gatewayIP address: ");
      Serial.println(WiFi.gatewayIP());
      Serial.print("subnetMask address: ");
      Serial.println(WiFi.subnetMask());
  } else if (connected && !newConnected) {
       Serial.print("Lost connection");
  }
  connected = newConnected;
  delay(5000);
}

@d-a-v
Copy link
Collaborator

d-a-v commented May 2, 2022

I want to port this to arduino as this uses 3.x of the SDK and I have an old 2.0 version of the NO OSSDK

esp8266-Arduino core v3.x still uses esp8266 nonos-sdk firmware v2.x

@d-a-v d-a-v self-assigned this May 2, 2022
@d-a-v d-a-v added this to the 3.1 milestone May 2, 2022
@d-a-v
Copy link
Collaborator

d-a-v commented May 2, 2022

wifi_set_macaddr()

This firmware function might not change anything because mac addresses are stored elsewhere when using lwip2 compared to lwip1.4, which was an option until esp8266 arduino core 2.7.

@d-a-v
Copy link
Collaborator

d-a-v commented Jun 18, 2022

About AP, what I can say so far is that this:

WiFi.softAPmacAddress( &newMACAddress[0]);

has never changed anything but overwrites newMACAddress with the AP address.
Using this instead currently works:

wifi_set_macaddr(SOFTAP_IF, &newMACAddress[0]);

@mcspr
Copy link
Collaborator

mcspr commented Jun 18, 2022

Does it depend AP interface being 'UP'?
As always when mentioning 3.0.0 update, we have both interfaces down at boot. Code above sets MAC first, then brings the up the interface. NONOS SDK API reference example is to set mode to AP, then set MAC.

@d-a-v
Copy link
Collaborator

d-a-v commented Jun 21, 2022

You got it right @mcspr.
STA interface needs to be up before changing its mac address:

  WiFi.mode(WIFI_AP_STA);
  wifi_set_macaddr(SOFTAP_IF, newMACAddressAP);
  wifi_set_macaddr(STATION_IF, newMACAddressSTA);
 ...
  WiFi.begin(ssidExt, password);
  WiFi.softAP(ssid, password);

wifi_set_macaddr is mentioned nowhere in this core's source code nor in its documentation.
Indeed the FW API reference mentions a call to wifi_set_opmode() before changing mac addresses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants