cancel
Showing results for 
Search instead for 
Did you mean: 

Mongoose not working with LWIP + FreeRTOS

marrr
Visitor
 
I'm trying to build a simple HTTP server using Mongoose with the LWIP IP stack on FreeRTOS CMSIS_V1. It is important that I let LWIP handle the network interface since I'm also using it to advertise a service using mDNS.

 

This is what my task looks like:

void StartMongooseTask(void const * argument)
{
	/* init code for LWIP */
	MX_LWIP_Init();

	/* init code for USB_DEVICE */
	MX_USB_DEVICE_Init();

	while (netif_default == NULL || !netif_is_up(netif_default)
			|| !ip4_addr_isany_val(*netif_ip4_addr(netif_default))
	) {
		osDelay(100);
	}

	extern struct dhcp_state_enum_t;

	struct dhcp *dhcp_client = netif_dhcp_data(netif_default);
	while (dhcp_client == NULL || dhcp_client->state != 10) {
		osDelay(100);
	}

	ip = *netif_ip4_addr(netif_default);
	printf("IP acquired: %u.%u.%u.%u", ip4_addr1(&ip), ip4_addr2(&ip),
			ip4_addr3(&ip), ip4_addr4(&ip));

	mdns_resp_init();
	mdns_resp_add_netif(netif_default, "22223335", 3600);

	mdns_resp_add_service(netif_default, "22223335", "_http",
			DNSSD_PROTO_TCP, 80, 3600, NULL, NULL);

	mdns_resp_announce(netif_default);

	// --------------- mongoose ---------------

	mg_log_set(MG_LL_DEBUG);

	struct mg_mgr mgr;
	mg_mgr_init(&mgr);
	mg_http_listen(&mgr, "http://0.0.0.0:80", http_handler, &mgr);

	for (;;) {
		mg_mgr_poll(&mgr, 10);   // 10 ms polling
		osDelay(1);              // Yield to other FreeRTOS tasks
	}
}

 

I am certain that the LWIP is getting a valid IP and advertising the service as expected, since I can ping to it. However, I cannot seem to access the http server. The mongoose (v7.19.0) functions are not returning errors, and it looks like they are set up correctly. The poll function will call the `http_handler`.

 

To try to integrate mongoose with LWIP, I set up the following flags:

#define MG_ENABLE_TCPIP_DRIVER_INIT 0
#define MG_ENABLE_LWIP 1

 

I also tried to set MG_ENABLE_TCPIP to 0, but when I do so, I get compilation errors saying undefined reference to both mg_mgr_poll and mg_open_listener.

My board is the NUCLEO-F767ZI. Can anyone help me try to diagnose the problem and hopefully find a solution? Thank you

1 REPLY 1
percy4458
Associate
You’re able to ping, so the interface and DHCP look fine—the problem is almost certainly at the socket layer or a port conflict. On STM32 + FreeRTOS + lwIP, Mongoose needs the lwIP BSD sockets API enabled; in CubeMX make sure LWIP_SOCKET (and usually LWIP_NETCONN) are on, and that the built-in lwIP HTTPD is off so it isn’t already binding to port 80. Build Mongoose once (a single mongoose.c compiled into your project) with MG_ENABLE_LWIP=1 and MG_ENABLE_TCPIP=0; mixing both or compiling multiple copies can cause the “undefined reference to mg_mgr_poll” you saw. In code, store the return of mg_http_listen and confirm it’s not nullptr; with mg_log_set(MG_LL_DEBUG) you should see a clear bind success/fail line. Also try binding to your actual IP instead of 0.0.0.0 (e.g., http://192.168.x.y:80) to rule out wildcard quirks in lwIP. Finally, run the event loop without yielding between polls for now—just for(;;) mg_mgr_poll(&mgr, 100);—to ensure you’re servicing sockets regularly in one thread. If sockets are enabled and no other server is holding :80, Mongoose will accept connections immediately; if it still doesn’t, the debug log from startup through the first connect attempt will pinpoint whether the bind failed, the accept is blocked, or the handler is only seeing timer events.