Overview
This examples shows how to build HTTP server using Mongoose library and run it on STM32 Nucleo-F746Z development board with FreeRTOS and LWIP.
The source code for this tutorial is located at https://github.com/mongoose-examples/stm32-nucleo-h746z
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
in files Src/web_server.c
and Inc/web_server.h
. Below is a general outline:
- Set IP address, network mask and gateway in
Inc/main.h
- 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
Build options
There are (at least) two options to build an example:
- Using
make
command - 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
- Choose
File -> Import...
and then selectExisting Projects into Workspace
: - Pick a project directory as
Root directory
, make sure, thatNUCLEO-F746Zx-simple-web-server
is checked and clickFinish
: - After import, choose
Run As -> STM32 Application
: - Done! A firmware should lit LEDs
Debug output
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:
- Choose
Run
->Debug configuration
: - Under STM32 Application select
NUCLEO-F746Zx-simple-web-server
configuration. Note: if you don't seeNUCLEO-F746Zx-simple-web-server
configuration, close dialog and start debug session viaDebug as -> STM Application
to create it: - Goto
Debugger
tab - Check
Enabled
inSerial Wire Viewer
section - Update
Core clock
setting (for NUCLEO-F746Z set it to 216MHz) - Click
Apply
and then clickDebug
to start debug session. - Once application stopped at the beginning of
main
function open SWV Console (Window->Show View->SWV->SWV ITM Data Console
) - Click
Add port
and add port 0 - Click
Configure trace
and enable port 0 - Click
Start trace
- 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
Custom application
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
Src/web_server.c
file,BSP_LED_On
function 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 tool
or manually, depending on your project type) - Initialize FreeRTOS and LWIP. Read this STM article.
- Add
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
- Add
MG_ARCH=MG_ARCH_FREERTOS_LWIP
to preprocessor symbols (Properties->C/C++ Build->Settings->Preprocessor
) - 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-F746Z board (but do have another) try to use
LwIP_HTTP_Server_Netconn_RTOS
example: it supports a lot of boards.- Copy example to your workspace
- Add
mongoose.c
andweb_server.c
to project - Add the same mongoose specific configuration flags as in point 2
- Comment out netconn initialization (probably in StartThread function) and call
start_mongoose
instead - That's it! Your application should build and run Mongoose.