This examples shows how to build HTTP server using Mongoose library and run it on STM32 Nucleo-H743Z development board with FreeRTOS and LWIP.
The source code for this tutorial is located at https://github.com/mongoose-examples/stm32-nucleo-h743z
The most of the example is a regular STM32CubeIDE project (a lot of generated
files, FreeRTOS and LWIP sources etc). Mongoose-related functions are located
Inc/web_server.h. Below is a general outline:
- Set IP address, network mask and gateway in
- Build an example (see below) and run it on a development board
- Firmware initializes network, and turns on a green LED if initialization was successful and red one otherwise
- If you see a red led turned on, make sure that the ethernet cable is connected to the board
- After initialization, an application starts mongoose event loop and blinks a blue LED
- Once a blue LED started to blink - the web server is ready
- Open your web browser and navigate to board IP address, you should see a "Hello, world" page
There are (at least) two options to build an example:
- Using STMCubeIDE IDE
Option 1: make
- Install Docker
- Install st-flash utility
- Start terminal, navigate to the project folder and from there, run
$ make build
- When build finshes, run
$ make flash
Done! Now firmware is flashed to the board and should signal its state via LED indicator (green or red).
Option 2: STM32Cube
STM32Cube IDE is a standard software provided by STMicroelectronics.
- Start STM32Cube
File -> Import...and then select
Existing Projects into Workspace:
- Pick a project directory as
Root directory, make sure, that
NUCLEO-H743Zx-simple-web-serveris checked and click
- After import, choose
Run As -> STM32 Application:
- Done! A firmware should lit LEDs
During execution, Mongoose can print debug information. There are several ways
to receive this information. This example re-routs
stdout to SWV ITM Console.
This allow to see output of
printf and similar functions directly in
STM32Cube IDE. For this, you need:
- Under STM32 Application select
NUCLEO-H743Zx-simple-web-serverconfiguration. Note: if you don't see
NUCLEO-H743Zx-simple-web-serverconfiguration, close dialog and start debug session via
Debug as -> STM Applicationto create it:
Serial Wire Viewersection
Core clocksetting (for NUCLEO-H743Z set it to 400MHz)
Applyand then click
Debugto start debug session.
- Once application stopped at the beginning of
mainfunction open SWV Console (
Window->Show View->SWV->SWV ITM Data Console)
Add portand add port 0
Configure traceand enable port 0
- Resume program execution
- You should see something like this in console window.
1970-01-01 00:00:00 I web_server.c:30:server Starting Mongoose v7.4 1970-01-01 00:00:00 I mongoose.c:3297:mg_listen 1 accepting on http://0.0.0.0:80 (port 80)
- Done! Now you can use printf function or LOG macro to output required information
In order to create your own mongoose-enabled application you have several ways:
The obvious way, is to add required functionality to this example. The sample includes all standard HAL Drivers. As you can see, in
BSP_LED_Onfunction is used to turn on LED. In the same way, you can have access to all HAL functions.
If, for some reason, you can't use this example as a base (for ex, you have you own big project to which you need to add mongoose) you can do the following:
- Add FreeRTOS and LWIP to your project (via
Device Configuration toolor manually, depending on your project type)
- Initialize FreeRTOS and LWIP. Read this STM article.
mongoose.c(and .h if you want) file to your project
- Add mongoose specific configuration flags
- Add the following defines in lwipopts.h file:
#define LWIP_SOCKET 1 #define LWIP_POSIX_SOCKETS_IO_NAMES 0 #define SO_REUSE 0
MG_ARCH=MG_ARCH_FREERTOS_LWIPto preprocessor symbols (
- Enable float usage- for printf (
Properties->C/C++ Build->Settings->MCU Settings->Use float with printf)
- Done! Now write code similar to one from
Src/web_server.c(read Mongoose documentation) and let the magic begin
- Add FreeRTOS and LWIP to your project (via
If you want to evaluate STM and Mongoose and fast and simple as possible, but you don't have NUCLEO-H743Z board (but do have another) try to use
LwIP_HTTP_Server_Netconn_RTOSexample: it supports a lot of boards.
- Copy example to your workspace
- Add the same mongoose specific configuration flags as in point 2
- Comment out netconn initialization (probably in StartThread function) and call
- That's it! Your application should build and run Mongoose.