cancel
Showing results for 
Search instead for 
Did you mean: 

Mongoose not working with LWIP + FreeRTOS

marrr
Associate
 
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

8 REPLIES 8
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.

Thank you for the detailed reply, I made sure that both LWIP_SOCKET and LWIP_NETCONN were on, and that HTTPD is off.Setting MG_ENABLE_TCPIP to 0 on the mongoose_config.h file will always cause the compilation problem regarding the undefined references... I wonder if it is because MG_ARCH is set to MG_ARCH_CUBE, but I can't seem to change it to MG_ARCH_FREERTOS without hundreds of compilation errors showing up.

 

With the MG_ENABLE_TCPIP on, I see that mg_http_listen does not return a NULL, and I do get a message saying that it is listening: "11d7 3 mongoose.c:4072:mg_listen 1 0 http://0.0.0.0:80"

 

Changing the IP from 0.0.0.0 to the actual one didn't change anything, and similar regarding removing the osDelay in the for loop, but I suppose all of this is because MG_ENABLE_TCPIP should be 0. I'm not sure how to deal with these errors

Hi marrr,

 

An example of using Mongoose with LwIP and FreeRTOS can be found here in the official Mongoose repository. Moreover, there is this tutorial that guides you through integrating Mongoose with LwIP and FreeRTOS, please make sure you follow it carefully, especially the "FreeRTOS and LwIP integration section" (this is the most important step in configuring LwIP with the correct parameters).

 

In short, you only need to set MG_ARCH to MG_ARCH_FREERTOS and MG_ENABLE_LWIP to 1. Do not enable MG_ENABLE_TCPIP (which in turn enables the Mongoose built-in TCPIP stack, however you need MG_ENABLE_LWIP to use the LwIP stack). You also do not need to change the listening address, it is not necessary to be the same as your actual IP address assigned via DHCP.

 

Heads up: I am part of the Mongoose development team, I hope this helps you. If you encounter any other issues, feel free to let me know.

Hi :)

I have followed the tutorials carefully. As soon as I set MG_ARCH to MG_ARCH_FREERTOS in the mongoose_config.h file, I get hundreds of compilation errors. By default this flag is set to MG_ARCH_CUBE. Do you have any idea of why this might be happening?

These compile issues are weird, they shouldn't happen. Could you please attach zip with your project to take a closer look?

I attached a zip with the full project, or you can also see it here: https://github.com/immarianaas/stm32-experiments/tree/4-http-server (this branch, 4-http-server, is where I'm trying to integrate mongoose).

Thank you for looking into it!

I'm not sure exactly what happened, but with the following configuration in the mongoose_config.h file, it seems to work!

#define MG_ARCH MG_ARCH_FREERTOS

#define MG_ENABLE_TCPIP_DRIVER_INIT 0
#define MG_ENABLE_TCPIP 0
#define MG_ENABLE_LWIP 1

I am sure I had tried this configuration before, but I used to change some of the flags (MG_ENABLE_TCPIP_DRIVER_INIT and MG_ENABLE_LWIP)  in mongoose.h instead of mongoose_config.h, so I wonder if that is the reason why it didn't work.

Thank you so much for the support!

Yes, exactly! I just had a look at your project. I only edited mongoose_config, enabled MG_ARCH_FREERTOS and MG_ENABLE_LWIP and the build works! You can also try to increase the heap size for FreeRTOS to 65536 (the guide shows you how to do it), I notice you left it at the default value, so it might cause some crashes if it's too small.

By the way, since you don't use Mongoose's built-in stack, you don't need to specify in mongoose_config.h the MG_ENABLE_TCPIP and MG_ENABLE_TCPIP_DRIVER_INIT macros, they are 0 by default :)

Also, the mongoose source files are not meant to be edited, the only changes you should make are in mongoose_config.h.

Happy to help!