Strange behavior of WiFi code

Zeni241
Posts: 86
Joined: Tue Nov 20, 2018 4:28 am

Strange behavior of WiFi code

Postby Zeni241 » Fri Feb 01, 2019 1:32 pm

I have this code to connect to wifi.

Code: Select all

tcpip_adapter_init();
    ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
       ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
        char *myssid = "myssid";
        char *mypw = "mypassword";
      wifi_config_t wifi_config;
    strcpy((char *)wifi_config.sta.ssid,myssid);
    strcpy((char *)wifi_config.sta.password, mypw);
    printf("wifi_config.sta.ssid=%s\n wifi_config.sta.password=%s\n",wifi_config.sta.ssid,wifi_config.sta.password);
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
    ESP_ERROR_CHECK(esp_wifi_start());
If I put this code in app_main, it works, but if I put this code in an other function, say connectwifi(), and call that function in app_main, it does not work.


Further, if I use

Code: Select all

char myssid[] = "myssid";
 char mypw[] = "mypassword";
instead of

Code: Select all

char *myssid = "myssid";
 char *mypw = "mypassword";
in the above code, it does not work even in app_main.

What could be the reason(s)?

In fact idea is to get SSID and PASSWORD from NVS each time to connect to wifi.
If fails to connect to wifi in given time, wifi in access mode is started asking for user input of SSID and PASSWORD, save it to NVS, and again connect to wifi using values from NVS.

I have miserably failed :roll: :( to figure out how to use user input I get in AP mode, to connect to wifi in station mode.
I can get user input in AP mode, save and retrieve it from NVS, but those values just don't work in connecting in station mode.(though those retrieved values are correct when I get their print).
Any help please?
and sorry for such a long question :oops: :oops:

User avatar
brp80000
Posts: 138
Joined: Thu Oct 04, 2018 7:13 pm

Re: Strange behavior of WiFi code

Postby brp80000 » Fri Feb 01, 2019 3:11 pm

It looks like ESP32 after IDF-3.1 no longer has working WIFI. It's time to move on STM32 ))))

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: Strange behavior of WiFi code

Postby ESP_igrr » Sat Feb 02, 2019 4:01 am

Zeni241: When you declare wifi_config_t structure, do not forget to set all fields to zero. Either by calling 'memset' or using an initializer:

wifi_config_t wifi_config = { };

Or

wifi_config_t wifi_config;
memset(&wifi_config, 0, sizeof(wifi_config));

Zeni241
Posts: 86
Joined: Tue Nov 20, 2018 4:28 am

Re: Strange behavior of WiFi code

Postby Zeni241 » Sat Feb 02, 2019 5:30 am

Good morning.
Couldn't be a better start of morning :lol: . Thanks a lot ESP_igrr, this magic mantra wifi_config_t wifi_config = { }; solved the problem I was wrestling with many days.
Now I can use call my "wifi_connect" function (where I have placed the code to connect wifi) from any where successfully.
I can even pass this function values of SSID and password (wifi_connect(char *ssid, char *pw)).
I can also use:
char myssid[] = "myssid";
char mypw[] = "mypassword";


Thanks again to ESP_igrr. Image
Attachments
tycats.gif
tycats.gif (47.85 KiB) Viewed 7160 times
tycats.gif
tycats.gif (47.85 KiB) Viewed 7160 times

User avatar
brp80000
Posts: 138
Joined: Thu Oct 04, 2018 7:13 pm

Re: Strange behavior of WiFi code

Postby brp80000 » Sat Feb 02, 2019 6:29 am

Oh, you're a fun group.

Zeni241
Posts: 86
Joined: Tue Nov 20, 2018 4:28 am

Re: Strange behavior of WiFi code

Postby Zeni241 » Sat Feb 02, 2019 12:47 pm

:D
Did it worked for you?

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Strange behavior of WiFi code

Postby Ritesh » Sat Feb 02, 2019 1:35 pm

ESP_igrr wrote:
Sat Feb 02, 2019 4:01 am
Zeni241: When you declare wifi_config_t structure, do not forget to set all fields to zero. Either by calling 'memset' or using an initializer:

wifi_config_t wifi_config = { };

Or

wifi_config_t wifi_config;
memset(&wifi_config, 0, sizeof(wifi_config));
Yes. You are correct and we need to keep habit to memset it with zero before filling values into that.
Regards,
Ritesh Prajapati

Who is online

Users browsing this forum: artisdom, Bing [Bot], Sang_Huynh and 223 guests