Problem connecting to wifi access point

Arkaik
Posts: 13
Joined: Mon Jun 12, 2017 12:36 pm

Problem connecting to wifi access point

Postby Arkaik » Wed Nov 29, 2017 6:07 pm

Hi guys,

I'm trying to connect to a wifi network with my ESP32 devKitC. I can connect when I use macros as ssid and password, however when I try with variables it fails even if the byte sequence is the same.

With macros

Code: Select all

void wifi_connect()
{
	int i;

	if(NULL == self)
	{
		ERROR("Wifi was not created");
	}
	else
	{
		ESP_ERROR_CHECK(esp_event_loop_init(esp32_wifi_eventHandler, NULL));
	        nvs_flash_init();
		tcpip_adapter_init();

		wifi_init_config_t wifiConfig = WIFI_INIT_CONFIG_DEFAULT();

		ESP_ERROR_CHECK(esp_wifi_init(&wifiConfig));
		ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
		ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
		
		wifi_config_t sta_config = {
				.sta =
				{
					.ssid = AP_SSID,
					.password = AP_PWD,
					.bssid_set = 0
				}
			};

		printf("SSID : ");
		for(i=0; i<32; i++)
		{
			printf("%x ", sta_config.sta.ssid[i]);
		}
		printf("\n");
		printf("PWD : ");
		for(i=0; i<64; i++)
		{
			printf("%x ", sta_config.sta.password[i]);
		}
		printf("\n");

		ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_config));
		ESP_ERROR_CHECK(esp_wifi_start());
		ESP_ERROR_CHECK(esp_wifi_connect());
	}
}
And the output

Code: Select all

I (1019) wifi: wifi firmware version: 8a1ad86
I (1019) wifi: config NVS flash: enabled
I (1019) wifi: config nano formating: disabled
I (1024) wifi: Init dynamic tx buffer num: 32
I (1025) wifi: Init dynamic rx buffer num: 32
I (1029) wifi: wifi driver task: 3ffbc9f8, prio:23, stack:4096
I (1034) wifi: Init static rx buffer num: 10
I (1038) wifi: Init dynamic rx buffer num: 32
I (1042) wifi: Init rx ampdu len mblock:7
I (1046) wifi: Init lldesc rx ampdu entry mblock:4
I (1050) wifi: wifi power manager task: 0x3ffc1dd0 prio: 21 stack: 2560
SSID : 56 56 56 5f 52 4e 4e 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
PWD : 78 4e 12 24 37 4e 51 62 5f 72 42 71 2a 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
I (1077) wifi: wifi timer task: 3ffc2e60, prio:22, stack:3584
I (1097) wifi: mode : sta (24:0a:c4:05:71:88)
I (1220) wifi: n:1 0, o:1 0, ap:255 255, sta:1 0, prof:1
I (1877) wifi: state: init -> auth (b0)
I (1881) wifi: state: auth -> assoc (0)
I (1886) wifi: state: assoc -> run (10)
I (1905) wifi: connected with VVV_RNN, channel 1
[INFO] Connected to VVV_RNN
	IP: 192.168.128.105
	Netmask: 255.255.255.0
	Gateway: 192.168.128.254

[INFO] PASSED

With variables

Code: Select all

void wifi_connect()
{
	int ssid_length = strlen(self->ssid);
	int password_length = strlen(self->password);
	int i;

	if(NULL == self)
	{
		ERROR("Wifi was not created");
	}
	else
	{
		ESP_ERROR_CHECK(esp_event_loop_init(esp32_wifi_eventHandler, NULL));
	    nvs_flash_init();
		tcpip_adapter_init();

		wifi_init_config_t wifiConfig = WIFI_INIT_CONFIG_DEFAULT();

		ESP_ERROR_CHECK(esp_wifi_init(&wifiConfig));
		ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
		ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));

		wifi_config_t sta_config;
		memcpy(sta_config.sta.ssid, self->ssid, ssid_length);
		memcpy(sta_config.sta.password, self->password, password_length);

		for(i=ssid_length; i<32; i++)
		{
			sta_config.sta.ssid[i] = '\0';
		}
		for(i=password_length; i<64; i++)
		{
			sta_config.sta.password[i] = '\0';
		}

		printf("SSID : ");
		for(i=0; i<32; i++)
		{
			printf("%x ", sta_config.sta.ssid[i]);
		}
		printf("\n");
		printf("PWD : ");
		for(i=0; i<64; i++)
		{
			printf("%x ", sta_config.sta.password[i]);
		}
		printf("\n");

		ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_config));
		ESP_ERROR_CHECK(esp_wifi_start());
		ESP_ERROR_CHECK(esp_wifi_connect());
	}
}
And the output

Code: Select all

I (1019) wifi: wifi firmware version: 8a1ad86
I (1019) wifi: config NVS flash: enabled
I (1019) wifi: config nano formating: disabled
I (1024) wifi: Init dynamic tx buffer num: 32
I (1025) wifi: Init dynamic rx buffer num: 32
I (1029) wifi: wifi driver task: 3ffbc9f8, prio:23, stack:4096
I (1034) wifi: Init static rx buffer num: 10
I (1038) wifi: Init dynamic rx buffer num: 32
I (1042) wifi: Init rx ampdu len mblock:7
I (1046) wifi: Init lldesc rx ampdu entry mblock:4
I (1050) wifi: wifi power manager task: 0x3ffc1dd0 prio: 21 stack: 2560
SSID : 56 56 56 5f 52 4e 4e 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
PWD : 78 4e 12 24 37 4e 51 62 5f 72 42 71 2a 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
I (1077) wifi: wifi timer task: 3ffc2e60, prio:22, stack:3584
I (1125) wifi: mode : sta (24:0a:c4:05:71:88)
Common content

I'm using the following structure as my wifi "object" and the wifi_isConnected function to check if the connection is ok.
The wifi handler is also presented here.

Code: Select all

typedef struct
{
	char* ssid;
	char* password;
	ip4_addr_t net_ip;
	ip4_addr_t net_gw;
	ip4_addr_t net_msk;
	bool isConnected;
}wifi_t;

bool wifi_isConnected()
{
	int ret;

	if(NULL == self)
	{
		ERROR("Wifi was not created");
		ret = false;
	}
	else
	{
		ret = self->isConnected;
	}
	return ret;
}

esp_err_t esp32_wifi_eventHandler(void *ctx, system_event_t *event)
{
	if (event->event_id == SYSTEM_EVENT_STA_GOT_IP)
	{
	    self->net_ip = event->event_info.got_ip.ip_info.ip;
	    self->net_gw = event->event_info.got_ip.ip_info.gw;
	    self->net_msk = event->event_info.got_ip.ip_info.netmask;
		self->isConnected = true;

		INFO("Connected to %s", self->ssid);
		printf("\tIP: %s\n", inet_ntoa(self->net_ip));
		printf("\tNetmask: %s\n", inet_ntoa(self->net_msk));
		printf("\tGateway: %s\n\n", inet_ntoa(self->net_gw));
	}
	return ESP_OK;
}
and here is the main application

Code: Select all

void app_main(void)
{
	wifi_t* wifiConnection = NULL;
	wifi_constructor(wifiConnection, <ssid>, <password>);
	wifi_connect();

	while(!wifi_isConnected())
	{
	}

	INFO("PASSED");
}
I don't understand why I can't connect when I give the ssid and password with variables instead of macros. The byte sentence is the same so it should connect with no problem.

I'm certainly missing something but I can't found out what is it :mrgreen:

Thanks in advance ;)

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Problem connecting to wifi access point

Postby kolban » Thu Nov 30, 2017 5:33 am

My guess is that your initialization of wifi_sta_config_t is different in both code fragments. In the one with macros, you are creating the variable and initializing it. My guess is that all the fields are nulled during initialization except the ones your specified. However in your variables example, you are creating a variable (which will have random garbage in it) and then only setting two fields (userid and password) which means that the others will be random. Try and add the following:

memset(&sta_config, 0, sizeof(sta_config));

just after creation but before assigning the userid/password pair.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Arkaik
Posts: 13
Joined: Mon Jun 12, 2017 12:36 pm

Re: Problem connecting to wifi access point

Postby Arkaik » Thu Nov 30, 2017 11:45 am

Awesome ! That was the problem. Thanks a lot

qquevin
Posts: 1
Joined: Tue Jun 25, 2019 4:35 pm

Re: Problem connecting to wifi access point

Postby qquevin » Tue Jun 25, 2019 4:42 pm

kolban wrote:
Thu Nov 30, 2017 5:33 am
My guess is that your initialization of wifi_sta_config_t is different in both code fragments. In the one with macros, you are creating the variable and initializing it. My guess is that all the fields are nulled during initialization except the ones your specified. However in your variables example, you are creating a variable (which will have random garbage in it) and then only setting two fields (userid and password) which means that the others will be random. Try and add the following:

memset(&sta_config, 0, sizeof(sta_config));

just after creation but before assigning the userid/password pair.
Thank you!!!

Who is online

Users browsing this forum: Bing [Bot] and 95 guests