HTTP server on AP and STA

Franck
Posts: 7
Joined: Fri May 25, 2018 3:44 am

HTTP server on AP and STA

Postby Franck » Tue Aug 21, 2018 11:36 am

Hi,
I am trying to run and HTTP server on both SoftAP and STA interfaces simultaneously so that the same web page is displayed when connecting to the ESP32 via the SoftAP or when connecting to the same router as the ESP32 STA interface.

When in STA mode, I can access the web page.
However, when in SoftAP+STA, I can only access the web page by connecting to the SoftAP.
If I connect to the same router as the STA, I can ping the IP address of the ESP32, but I cannot get an answer form the netconn interface using a web browser.

Any idea why the netconn would only work on the SoftAP when both interfaces are active?
Below is my netconn binding code.
Thanks.

Code: Select all

void HTTP_Server(void)
{
	struct netconn *conn, *newconn;
	err_t err;
	thisPrintf(("HTTP Server: Start\n"));
	conn = netconn_new(NETCONN_TCP);
	netconn_bind(conn, IP_ADDR_ANY, 80);
	netconn_listen(conn);
	do
	{
		err = netconn_accept(conn, &newconn);
		if(err == ERR_OK)
		{
			thisPrintf(("HTTP Server: New Conn\n"));
			http_server_netconn_serve(newconn);
			netconn_delete(newconn);
		}
		vTaskDelay((TickType_t)10); /* allows the freeRTOS scheduler to take over if needed */
	}while(err == ERR_OK);
	netconn_close(conn);
	netconn_delete(conn);
}

Franck
Posts: 7
Joined: Fri May 25, 2018 3:44 am

Re: HTTP server on AP and STA

Postby Franck » Wed Aug 29, 2018 10:35 am

Hi,
I am still trying to get an HTTP server to work on the STA interface when the WiFi mode is set to WIFI_MODE_APSTA.

In the code below, instead of using IP_ADDR_ANY to bind netconn, I have tried to specify the esp address on my local network: 192.168.4.106.
It did not work :(.
Netconn_bind does not return any error, but netconn_accept stays blocked.
However, if I change the netconn_bind IP address to the ESP softAP (192.168.4.1) and connect directly to the AP, everything works fine.

It looks like the WIFI_MODE_APSTA mode only allows netconn to work on the softAP interface.
Any thought on the subject would be useful...

Code: Select all

static void http_server(void *pvParameters)
{
	struct netconn *conn, *newconn;
	err_t err;
	ip_addr_t IP;

	// Waiting for STA connection
	xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE, portMAX_DELAY);

	// Bind Netconn to STA IP address
	conn = netconn_new(NETCONN_TCP);
	IP4_ADDR(&IP.u_addr.ip4,192,168,4,106);	// 192,168,4,1
	netconn_bind(conn, &IP, 80);
	netconn_listen(conn);
	do
	{
		err = netconn_accept(conn, &newconn);
		printf("netconn_accept\n");
		if(err == ERR_OK)
		{
			http_server_netconn_serve(newconn);
			netconn_delete(newconn);
		}
	}while(err == ERR_OK);

	netconn_close(conn);
	netconn_delete(conn);
}

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: HTTP server on AP and STA

Postby ESP_Angus » Wed Aug 29, 2018 11:06 am

What is the netmask for the IP addresses in use on your local AP and your ESP32 SoftAP?

192.168.4.106 and 192.168.4.1 are usually private class C networks by default (ie 192.168.4.0/24, aka netmask 255.255.255.0). If either network has a netmask of this kind then it will be impossible for a single machine (the ESP32) to route packets to both of them.

Try changing the AP on the ESP32 to a different network, for example 192.168.5.0/24.

It's also worth checking for error results from calls to the netconn API, in case some operation is outright failing.

Angus

Franck
Posts: 7
Joined: Fri May 25, 2018 3:44 am

Re: HTTP server on AP and STA

Postby Franck » Wed Aug 29, 2018 1:38 pm

Hi Angus,
Thanks for the answer, that was the problem!
The AP netmask is 255.255.255.0.
Changing the AP address to 192.168.5.0/24 fixed the issue.

Also, to get it to work I have to use 2 tasks. One with netconn bound to the STA IP (192.168.4.106) and one to the softAP IP (192.168.5.1).
When I used netconn_bind(conn, IP_ADDR_ANY, 80), the server would work for the softAP as long as I don't query the STA server. Once the STA server has accepted a connection, the softAP server would not work anymore.

Who is online

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