Hosting Web Apps on Raspberry Pi Pico W with the HTTP Server

This tutorial shows how to turn the Raspberry Pi Pico W board into a Wi-Fi access point running a basic web app with its own CSS styles and API. We will start with our Raspberry Pi Pico W HTTP Server sample and will then extend it, adding a new dialog that can be used to make the on-board LED blink with the specified period.

Before you begin, we advise installing VisualGDB 5.6R7 or later, however you can also achieve the same results by editing the files manually and building the project via command line.

  1. Clone the repository to your machine and open the PicoHTTPServer.sln file in Visual Studio. If this is the first time you are using VisualGDB with Raspberry Pi Pico, it will suggest automatically downloading and installing the toolchain and the Pico SDK:
  2. Once the project loads, try building it. The initial build will fail with the missing “ip4_secondary_ip_address” error:
  3. This happens because the lwIP stack does not support assigning multiple IP addresses to the same netconn object, which is necessary to generate the “sign into network” messages. You can address it by applying the lwip.patch file to the lwIP directory inside the Pico SDK, or by manually adding the 2 lines from it to the ip4.c file:Now the project should build without errors.
  4. If you have connected the SWD pins on your Pico W (see this tutorial), right-click on the PicoHTTPServer project in Solution Explorer, go to the Debug Settings page and make sure the configuration matches your setup:You will then be able to program the FLASH memory and begin debugging by simply pressing F5.
    If not, you can deploy the built project into the Pico W by starting the board in the bootloader mode and copying the PicoHTTPServer.uf2 file onto it.
  5. Once the firmware starts, locate the PicoHTTP network from your computer or smartphone and click the “sign into network” link:
  6. The link will lead to a simple web app allowing you to control the board pins from the browser:
  7. We will now modify the web app, adding a new modal dialog and an underlying API to control the LED blinking period. First of all, locate the index.html file in Solution Explorer (or open it manually from the www subdirectory) and go to the <div> with id=”popup_root”:The index.html file is automatically served by the Pico W each time it is accessed via the browser. It contains HTML, CSS and JavaScript code necessary to run a basic web app. The <div> element we selected corresponds the semi-transparent layer that is shown over the main page whenever any popup is shown.
  8. Add the following HTML code inside the popup_root div after the settings_popup:

    It defines a dialog consisting of a header with a close button, a body and a footer. It includes one input field for the period and 3 buttons for sending various requests to the device.
  9. Now we need to add the styles so that the dialog will look like actual modal window (will have a blue header, close button shown in the right place, and sufficient padding around the edges). Locate the CSS stylesheet inside the index.html file and add the following code to it:
  10. Add a function to show and hide the popup inside the <script> element in index.html:
  11. Finally, modify the header to show a “blink” button near the settings editing button:

    Also download the stopwatch.png icon and place it into the www/img subdirectory.
  12. Build the project and start debugging it. The CMake build scripts will automatically invoke the SimpleFSBuilder tool to pack the modified index.html and stopwatch.png into a simple archive readable by the Pico HTTP server. Make sure you are still connected to the Wi-Fi network and refresh the page in the browser, then click the new stopwatch button:
  13. If you still see the old index page, make sure the “Always Consider Outdated” setting is enabled in the CMake project properties:Otherwise, changing the web server payload without modifying any of the source files will not trigger a rebuild.
  14. The buttons in the newly added dialog won’t work yet because we have not implemented the call_blink_api() function used by them. Add the following code inside the <script> element in index.html:

    Each time you press either of the buttons, your browser will send a GET or POST request to the /api/blink/<…> endpoint. If the request returns anything other than the “OK” message, the browser will display the returned text in an error message.
  15. The final thing we need to is handle these requests from the C code. Locate the calls to http_server_add_zone() in main.c and modify them as shown below:

    This ensures that each request to /api/blink/<…> will get routed to the do_handle_blink_api_call() function. Make sure you register the /api/blink zone AFTER the generic /api zone, as the HTTP server checks the zone list in the backward order.
  16. Add a function for actually handling the requests:

    Note how it cerates the Blink task in response to the start call, deletes it in response to stop, and updates the period in response to the period call. Signaling the s_BlinkSemaphore after updating the period is done to immediately wake up blink_task() without waiting for the previously set period to elapse.
  17. Press F5 to build the project and start debugging it again. Open the blink dialog in the browser, click “Start” and try changing the period. The on-board LED will blink according to the entered value. If you try setting the period to 0 or a non-number, the browser will display the “Invalid period!” message relayed from the do_handle_blink_api_call() function:
  18. You can view various debugging output from the project by using the Fast Semihosting feature of VisualGDB. As RP2040 device does not support detecting whether a debugger is attached, the example project comes with the semihosting disabled. You can enable it by setting “When running without debugger: wait for debugger to attach” in VisualGDB Project Properties:
  19. The debug output will appear in the Debug->Windows->VisualGDB Output window:You can also view various lwIP statistics (e.g. number of processed packets) in real time via Debug->Windows->Live Watch->lwip_stats.

You can find the code shown in this tutorial in the blink branch of our GitHub repository. See this commit for the exact changes described above.