Go Down

Topic: strange question, need some help, thanks (Read 184 times) previous topic - next topic

ericwgq

My board is ESP8266.
In the smartConfig() function, get the ssid and password, print them(display the right infomation i want) and then pass the value to the external variables.
In the loop function, print the ssid, but the result is something like "??".

so what is the problem,  i have spent much time on it. thank you very much if you can give me some information about it.

here is my code


Code: [Select]


#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
const char* ssid;
const char* password;

bool smartConfig(){
    WiFi.mode(WIFI_STA);
    Serial.println("\r\nWait for Smartconfig");
    WiFi.beginSmartConfig();
    while (1){
        delay(500);
        Serial.println("waiting for smartconfig");
        if (WiFi.smartConfigDone()){
            delay(1000);
            ssid= WiFi.SSID().c_str();
            password = WiFi.psk().c_str();
            Serial.println(ssid); // the right result like "mywifi"
            Serial.println(password); // the right result like "mypasswd"
             Serial.println("SmartConfig Success");
            break;
        }
    }
return true;
}
 
 
void setup() {
    Serial.begin(115200);
    Serial.println("");
    delay(1000);
    smartConfig();
    Serial.println(ssid);
    Serial.println(password);
}
 
 
void loop() {
    delay(1000);
    Serial.println(ssid);// not the "mywifi",but "?????"
    Serial.println(password);//not the "mypasswd",but "?????"
}

Delta_G

#1
Jul 26, 2017, 04:54 am Last Edit: Jul 26, 2017, 04:54 am by Delta_G
Why a while(1) and a break on success?  Why not :

Code: [Select]
while(!WiFi.smartConfigDone())!
// And then once it is successful
// all the stuff inside that if statement


Why do you have that function returning a bool and all it every returns is true?  Why is it not declared as void in that case?  The return value you have now doesn't mean anything and you don't even use it. 


Code: [Select]
ssid= WiFi.SSID().c_str();

What does WiFi.SSID return?  Are you setting ssid equal to a pointer that isn't going to point at anything anymore once that function exits?
If at first you don't succeed, up - home - sudo - enter.

ericwgq

i just post the simplified code, delete the unrelated.

the WiFi.SSID() return "A String containing the SSID the WiFi shield is currently connected to"

here is the link https://www.arduino.cc/en/Reference/WiFiSSID

the strange thing is that, in the smartConfig function, the print result is right, but the loop function is not

Delta_G

#3
Jul 26, 2017, 05:07 am Last Edit: Jul 26, 2017, 05:13 am by Delta_G
the strange thing is that, in the smartConfig function, the print result is right, but the loop function is not

Yeah, that's why I suspect a pointer going out of scope.  I don't know that I would trust a library written for something as hungry as WiFi support on a microcontroller that made extensive use of the String class. 



If at first you don't succeed, up - home - sudo - enter.

Delta_G

OK, so look at the WiFi.cpp file and that function is here:

Code: [Select]
char* WiFiClass::SSID()
{
    return WiFiDrv::getCurrentSSID();
}


Looks like it is returning a char array already.  So you shouldn't need the c_str() call.  Just use it as is. 
If at first you don't succeed, up - home - sudo - enter.

Delta_G

Or better yet, don't just assume that the WiFi library is going to keep it in scope for you and use strcpy to copy that string into a char array in your program that you have control over. 
If at first you don't succeed, up - home - sudo - enter.

ericwgq

ssid = WiFi.SSID();// error can not convert String to const char*.


ericwgq

the strcpy works, thank you very much!!!

Go Up