Overview

This examples shows how to build HTTP server using Mongoose library and run it on STM32 Nucleo-F429Z development board with FreeRTOS and LWIP.

The source code for this tutorial is located at https://github.com/mongoose-examples/stm32-nucleo-h429z

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:

  1. Using make command
  2. 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 select Existing Projects into Workspace:STMCube open project
  • Pick a project directory as Root directory, make sure, that NUCLEO-F429x-simple-web-server is checked and click Finish:
  • 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:

  1. Choose Run -> Debug configuration:
  2. Under STM32 Application select NUCLEO-F429Zx-simple-web-server configuration. Note: if you don't see NUCLEO-F429Zx-simple-web-server configuration, close dialog and start debug session via Debug as -> STM Application to create it:
  3. Goto Debugger tab
  4. Check Enabled in Serial Wire Viewer section
  5. Update Core clock setting (for NUCLEO-F429Z set it to 180MHz)
  6. Click Apply and then click Debug to start debug session.
  7. Once application stopped at the beginning of main function open SWV Console (Window->Show View->SWV->SWV ITM Data Console)
  8. Click Add port and add port 0
  9. Click Configure trace and enable port 0
  10. Click Start trace
  11. Resume program execution
  12. 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)
  1. 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:

  1. 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.

  2. 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

  3. If you want to evaluate STM and Mongoose and fast and simple as possible, but you don't have NUCLEO-F429Z 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 and web_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.