Skip to content

Now SmartConfig works! #807

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

Merged
merged 2 commits into from
Nov 6, 2017
Merged

Now SmartConfig works! #807

merged 2 commits into from
Nov 6, 2017

Conversation

allex1978
Copy link
Contributor

No description provided.

Now SmartConfig works! Tested on ESP32
Now SmartConfig works! Tested on ESP32
@bill-orange
Copy link

bill-orange commented Nov 5, 2017

I added:

sta_conf->bssid_set = 0;

to the .cpp file and recompiled. It made no difference. If I reset the ESP32 is searches forever until I use the phone app. Is there any else I need to do to effect the change?

Here's my connect function:

void setupWiFi () {
/* Set ESP32 to WiFi Station mode /
WiFi.mode(WIFI_AP_STA);
/
start SmartConfig */
WiFi.beginSmartConfig();

/* Wait for SmartConfig packet from mobile */
Serial.println("Waiting for SmartConfig.");
while (!WiFi.smartConfigDone()) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("SmartConfig done.");

/* Wait for WiFi to connect to AP */
Serial.println("Waiting for WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi Connected.");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}

Here's verbose debug:

entry 0x40078a3c
[D][WiFiGeneric.cpp:265] _eventCallback(): Event: 14 - AP_STACONNECTED
[D][WiFiGeneric.cpp:265] _eventCallback(): Event: 14 - AP_STACONNECTED
[D][WiFiGeneric.cpp:265] _eventCallback(): Event: 14 - AP_STACONNECTED
Waiting for SmartConfig.
.........[D][WiFiSTA.cpp:621] _smartConfigCallback(): Status: FIND_CHANNEL
.....................................................................

@allex1978
Copy link
Contributor Author

Hi bill-orange, you used just a SmartConfig sample code.
line WiFi.beginSmartConfig(); is always disconnect from old AP and start SmartConfig process.
If you not want to start SmartConfig after reset you have to rebuild the logic.

@bill-orange
Copy link

bill-orange commented Nov 5, 2017 via email

@beegee-tokyo
Copy link
Contributor

beegee-tokyo commented Nov 6, 2017

@bill-orange that is what I do

  1. try to connect via stored SSID and password
  2. if this fails (after timeout) start SmartConfig
    Example function that I call from within setup():
/**
 * Connect with SmartConfig
 * First try auto connect, if it fails
 * start SmartConfig
 *
 * @param confTimeout
 *        Time to wait to receive smart configuration
 *        if 0 it will wait forever
 * @param connTimeout
 *        Time to wait for connection to WiFi after
 *        configuration was received
 * @return <code>boolean</code>
 *        True if configuration and connection successfull
 *        False if timeout
 */
bool connSmartConfig(uint32_t confTimeout, uint32_t connTimeout) {
  WiFi.mode(WIFI_STA);
  WiFi.begin();
  uint32_t startTime = millis();
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    if (millis()-startTime > connTimeout) { // check if waiting time exceeded
      // Timeout, start SmartConfig now
      startTime = millis();
      //Init WiFi as Station, start SmartConfig
      WiFi.disconnect();
      WiFi.mode(WIFI_AP_STA);
      WiFi.beginSmartConfig();
      //Wait for SmartConfig packet from mobile
      while (!WiFi.smartConfigDone()) {
        if (confTimeout != 0) {
          if (millis()-startTime > confTimeout) { // check if waiting time exceeded
            return false;
          }
        }
      }
      //Wait for WiFi to connect to AP
      startTime = millis();
      while (WiFi.status() != WL_CONNECTED) {
        if (connTimeout != 0) {
          if (millis()-startTime > connTimeout) { // check if waiting time exceeded
            return false;
          }
        }
      }
      return true;
    }
  }
  return true;
}

@@ -625,6 +625,7 @@ void WiFiSTAClass::_smartConfigCallback(uint32_t st, void* result) {
} else if (status == SC_STATUS_LINK) {
wifi_sta_config_t *sta_conf = reinterpret_cast<wifi_sta_config_t *>(result);
log_d("SSID: %s", (char *)(sta_conf->ssid));
sta_conf->bssid_set = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is rather strange to make any difference

@bill-orange
Copy link

bill-orange commented Nov 6, 2017

beegee-tokyo , Thank you for the code. It worked for me when I set the timeouts correctly. If I connect to a WiFi network and then move to another WiFi network will the first one fail and allow SmartConnect to look for the new one?

Shouldn't ESP32 and ESP8266 code work the same for something like this?

void setupWiFi () {
/* Set ESP32 to WiFi Station mode /
WiFi.mode(WIFI_AP_STA);
/
start SmartConfig */

if (WiFi.reconnect()) {
Serial.println( "reconnected from stored credentials");
WiFi.printDiag(Serial); Serial.println(WiFi.localIP());
Serial.println("Connected to wifi");
return;
}

WiFi.beginSmartConfig();

/* Wait for SmartConfig packet from mobile */
Serial.println("Waiting for SmartConfig.");
while (!WiFi.smartConfigDone()) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("SmartConfig done.");

/* Wait for WiFi to connect to AP */
Serial.println("Waiting for WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi Connected.");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());

}

@beegee-tokyo
Copy link
Contributor

@bill-orange you are right, in my code is a mistake, should be WiFi.reconnect() instead of WiFi.begin(). But it worked for me before. Thanks for pointing out my mistake.

@me-no-dev me-no-dev merged commit 47cdfff into espressif:master Nov 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants