Skip to content

WiFi.SSID returning null or empty using esp_wifi_sta_get_ap_info #1743

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

Closed
megazero1316 opened this issue Aug 9, 2018 · 14 comments
Closed

WiFi.SSID returning null or empty using esp_wifi_sta_get_ap_info #1743

megazero1316 opened this issue Aug 9, 2018 · 14 comments
Labels
Status: Stale Issue is stale stage (outdated/stuck)

Comments

@megazero1316
Copy link

megazero1316 commented Aug 9, 2018

Hardware:

Board: ESP32 Dev Module
Core Installation/update date: 30/jul/2018
IDE name: Arduino IDE
Flash Frequency: 40Mhz
Upload Speed: 92600

Description:

As far as i know that if esp32 connected to a wifi it store the configuration in the flash but calling WiFi.SSID() return null after restarting esp32, what i notice is that the SSID is called from esp_wifi_sta_get_ap_info while the psk is called esp_wifi_get_config in WiFiSTA.cpp
the way i solved this problem is i copied the code in WiFi.psk() and added to WiFi.SSID() like this :

String WiFiSTAClass::SSID() const
{
    if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
        return String();
    }
    wifi_ap_record_t info;
    if(!esp_wifi_sta_get_ap_info(&info)) {
        return String(reinterpret_cast<char*>(info.ssid));
    }
    else{
          wifi_config_t conf;
          esp_wifi_get_config(WIFI_IF_STA, &conf);
          return String(reinterpret_cast<char*>(conf.sta.ssid));    
    }

    return String();
}


and everything went good for me , is this consider a good solution for this problem ? as i was looking for others solutions but couldn`t find

Sketch:

#include <WiFi.h>
/*commented after 1st time connection*/
//const char* ssid     = "your-ssid";
//const char* password = "your-password";



void setup()
{
    Serial.begin(115200);
    delay(10);
    WiFi.mode(WIFI_STA);
    WiFi.persistent (true);
    // We start by connecting to a WiFi network

    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
}



void loop()
{
    
}

@javiercuellar73
Copy link

Great solution!!!!
This should be add to next version.

@lbernstone
Copy link
Contributor

You can also extract this directly from nvs.

#include <nvs_flash.h>
#include <nvs.h>

const char* get_ssid() {
  nvs_handle hndl;
  nvs_open("nvs.net80211", NVS_READONLY, &hndl);
  size_t ssid_len = 36;
  char ssid[ssid_len];
  esp_err_t err = nvs_get_blob(hndl, "sta.ssid", ssid, &ssid_len);
  String ssid_str;
  if (err == ESP_OK) 
    for(char x : ssid) 
      if (x>32) ssid_str += x;
  return ssid_str.c_str();
}

void setup() {
  Serial.begin(115200);
  nvs_flash_init();
  Serial.println(get_ssid());
}

void loop() {}

@johnty
Copy link

johnty commented May 16, 2019

just another ping on this: ran into this issue recently. @megazero1316's solution appears to work great. any idea why it hasn't been merged in yet?

@huexpub
Copy link

huexpub commented Jun 13, 2019

yes, is good solution

@stale
Copy link

stale bot commented Aug 12, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Aug 12, 2019
@everslick
Copy link
Contributor

https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/network/esp_wifi.html#_CPPv424esp_wifi_sta_get_ap_infoP16wifi_ap_record_t

looks like a bug in IDF. commenting here to stop stale[bot] from closing this issue.

@stale stale bot removed the Status: Stale Issue is stale stage (outdated/stuck) label Aug 15, 2019
everslick added a commit to everslick/Arduino-ESP32-1.0.0 that referenced this issue Aug 15, 2019
@stale
Copy link

stale bot commented Oct 14, 2019

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Oct 14, 2019
@everslick
Copy link
Contributor

@me-no-dev : is WiFiSTAClass::SSID() supposed to return the SSID that is configured or the one that it is connected to?

@stale
Copy link

stale bot commented Oct 14, 2019

[STALE_CLR] This issue has been removed from the stale queue. Please ensure activity to keep it openin the future.

@stale stale bot removed the Status: Stale Issue is stale stage (outdated/stuck) label Oct 14, 2019
@me-no-dev
Copy link
Member

it grabs the configuration so I imagine it's the one configured.

@everslick
Copy link
Contributor

no, the original code does not:

https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/src/WiFiSTA.cpp#L547

it grabs info.

I think IDF works as designed. it should not return an SSID if not currently connected. Question is, what does a Arduino user expect? Should we return the configured SSID if not connected?

@me-no-dev
Copy link
Member

this is a good question... maybe we should

@stale
Copy link

stale bot commented Dec 13, 2019

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Dec 13, 2019
@stale
Copy link

stale bot commented Dec 27, 2019

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Stale Issue is stale stage (outdated/stuck)
Projects
None yet
Development

No branches or pull requests

7 participants