Keeping AP alive when changing STA

themindfactory
Posts: 58
Joined: Mon Mar 26, 2018 7:57 pm

Keeping AP alive when changing STA

Postby themindfactory » Mon Dec 17, 2018 4:45 am

I want to log onto the ESP32 as an AP, and send it commands (over the AP interface) to change the attached STA, however when I do this it drops the AP I am connected to....

I am programming in the Arduino IDE.

How??

RichardS

themindfactory
Posts: 58
Joined: Mon Mar 26, 2018 7:57 pm

Re: Keeping AP alive when changing STA

Postby themindfactory » Tue Dec 18, 2018 11:57 pm

I wrote an example sketch, attached below, to prove show issue.

1. Start CMD on windows machine and issue ping 192.168.4.1 -t

2. Change "your-ssid" and "your-password" to reflect your network.... then Run the sketch

3. find "myhost" in your wireless AP settings on the PC and log in.... (do it quickly you have 20 seconds)

4. you will see ping working

5. 20 seconds after the sketch starts the code will attempt to log onto an AP that does not exist....

6. the ping will start failing.....

WHY? The AP that the ESP32 has not changed...

Code: Select all

#include <WiFi.h>

void setup() {
  Serial.begin(500000);
}

char *hostName = "myhost";
char *hostPassword = "";
char *ssid = "your-ssid";
char *password = "your-password";
static char *oldssid = ssid;
int connected = 0;

void refresh(bool firstTime) {
  static bool Connected = false;
  static bool tryAgain = false;
  static int cnt = 0;

  connected = WiFi.status() ==  WL_CONNECTED || WiFi.isConnected();

  // runs this only once
  if (firstTime) {
    WiFi.mode(WIFI_AP_STA);
    WiFi.softAP(hostName, hostPassword, 10);
    Serial.printf("setting %s with %s\n", hostName, hostPassword);
    connected = false;
    tryAgain = true;
  }

  // if the ssid changed then reconnect
  if (ssid != oldssid) {
    oldssid = ssid;
    tryAgain = true;
  }

  // this runs when we change something
  if (tryAgain) {
    tryAgain = false;
    connected = false;
    cnt = 0;
    if (ssid != "" && password == "") {
      Serial.printf("Connecting to %s without password\n", ssid);
      WiFi.disconnect();
      WiFi.begin(ssid);
    }
    else if (ssid != "" && password != "") {
      Serial.printf("Connecting to %s with password\n", ssid);
      WiFi.disconnect();
      WiFi.begin(ssid, password);
    }
    else {
      Serial.printf("huh??\n");
      WiFi.disconnect();
      WiFi.begin();
    }
  }
}

void loop() {
  refresh(true); // runs once
  for (;;) {
    if (millis() / 1000 == 20) { // change the ssid in 20 seconds
      ssid = "tmp";
    }
    if (millis() % 500 == 0) { // show progress
      Serial.printf("%u connected = %u\n", millis() / 1000, connected);
      delay(10);
    }
    refresh(false);
  }
}

boarchuz
Posts: 604
Joined: Tue Aug 21, 2018 5:28 am

Re: Keeping AP alive when changing STA

Postby boarchuz » Wed Dec 19, 2018 7:52 pm

It only has a single radio. When you give it that dummy SSID, it starts scanning around on different channels looking for something that matches to connect to. Understandably, the connection between clients and the AP can then become a little unstable, and some client devices may even disconnect. You might be able to tweak some settings on your PC if you want it to stay connected to the AP in this situation.

I suggest turning off station mode if it fails to connect, to stabilise the AP. This works fine for me. You can turn it back on to try to connect again when the settings change, or perhaps after some interval to try again.

themindfactory
Posts: 58
Joined: Mon Mar 26, 2018 7:57 pm

Re: Keeping AP alive when changing STA

Postby themindfactory » Wed Dec 19, 2018 8:22 pm

This is similar to what I am seeing...

I was thinking of doing a scan for SSID in the neighborhood and seeing if we are trying to connect to one of them and if not don't do it!

I find however that the scan does not seem to break things, and that would have to seek around on channels.... (i think)

RichardS

Who is online

Users browsing this forum: No registered users and 81 guests