Implement WebSocket Protocol on STM32?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-03-14 7:14 AM - last edited on ‎2025-03-14 7:16 AM by Andrew Neil
Hello,
I am currently working on a project with an HTTP server, successfully implemented using FreeRTOS. In this setup, I handle client requests for opening and collecting data from the client side. However, since I need to display real-time data on a web page (for an IoT application), it has become necessary to implement the WebSocket protocol.
I am looking to implement a WebSocket server on my STM32f746G-DISCO. Unfortunately, I have not been able to find any working examples of WebSocket implementation on STM32.
Could you kindly point me in the direction of any relevant examples or provide some guidance on how to get started with this? Also, I would appreciate any insights regarding whether I need to modify my CubeMX configuration when implementing WebSocket, or if the standard initialization is sufficient — enabling ETH, activating lwIP (httpd) without DHCP, and activating FreeRTOS with the default task where the following initialization is done:
MX_LWIP_Init();
http_server_init();
Any help would be greatly appreciated.
Thank you in advance!
Best regards,
- Labels:
-
LwIP
-
STM32F7 Series
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-03-15 12:39 AM
We did work out a websockets solution for H7 that work great but using AZRTOS as OS.
We had before a solution with FreeRtos but it was not rugged enough and we dropped a couple of years ago.
Our designs require a very resilient and robust performance, with true real time interaction with multiple clients.
Should you think to move to AZRTOS, let me know.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-10 5:50 AM
Hello @mbarg.1,
I'm working on a project for which I designed a board with an STM32H7R3L8H6H MCU, an APS256XXN-OBR-BG 256MBit PSRAM on HEXASPI, an MX25UW25645GXDI00-T 256MBit flash on OCTOSPI, and an EMMC08G-MV28-01J10 SDMMC, among other peripherals.
Here are some pictures of it:
I worked out all the major kinks, and the system runs pretty well. However, there's something that bothers me quite a bit. I'm running AZRTOS and have an HTTP server for the web interface for settings and manual operations of the device. I would like to optimize the system by getting rid of the long-running HTTP requests that I use to get feedback from the HTTP server to the web interface. Trust me, I tried almost every trick in the book, but nothing worked out better than what I have.
I tried SSE, but the HTTP server mixed up the requests. In the next step, I created a second HTTP server just for SSE, but the response headers to the client connection attempt have "Connection: Close" in them, which triggers the closing of the connection after the first event.
The WebSocket runs on the server side and requires Node.js, and I couldn't get something like this running on my system. In one of my last attempts to find answers, I accidentally stumbled over this post in which you state that you've worked out a solution for that.
I would be happy to try this on my system. Can you share your solution or at least some information on how you solved this issue?
Thank you, and to everyone else out there:
The secret is to keep banging the rocks together, guys.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-10 9:08 AM
Yes, I will be happy to share it with you BUT I am actually out of office for a business trip.
I will reply Monday, once back in office.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-10 3:38 PM
Thank you very much. Have a safe trip and a great weekend.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-14 2:47 AM
As per my previous reply, let 's talk about web sockets.
We choose to keep as much as possible NetXDuo code intact, as sometime in future there will be evolutions and we want to avoid incompatibilities; that force some extra effort, but enable to be more resilient.
We do not have yet a design on H7R, we have all our design on H743/H753/H723; do you have handy a nucleo H7x3 board to run our code?
I will assume you are skilled on hw and sw, I will bypass some details, but feel free to ask me more, if necessary.
In the meantime, let me explain how we appoached websockets.
A) Websockets run on top of HTTP; device subscribe to server with upgrade request.
We choose to intercept upgrade message using a custom function call provided by original code
/* Call the user supplied function to notify the user of the get request. */
status = (server_ptr -> nx_web_http_server_request_notify)(server_ptr, request_type, server_ptr -> nx_web_http_server_request_resource, packet_ptr);
in _nx_web_http_server_get_process() and send reply message to client keeping connection open.
Here we identify Websockets protocol, we store subscription data (essentially tcp session with its socket).
B) Websockets clients once subscribed send messages and commands to server as non ASCII messages; unforunately there is no callback functions for non-ASCII messages in original code, tiy are treated as errors and discaded.
We choose to edit _nx_web_http_server_receive_data() function; if first character of HTTP request ORed to 0x80 is true, we have a websocket message, otherwise we proceed with legacy code.
Websocket message is decoded and passed to application, packet is released.
C) Server send messages to websockets clients when user code requires.
We choose to have a thread managing system events; once we need to send some data to subscribers, we build a list of clients, prepare message and sen using opne socket.
In order to avoid race conditions, we added to TCP Server loop a case for servicing clients.
D) Protocols have a structure built around a client-server-application logic that you are free to forget and replace with your approach.
While step A is easy to implement, steps B and C require NetxDuo code heavvy modification and voids any certification.
Let me know about board availability and how to proceed.
Mike Bargauan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-14 1:58 PM - edited ‎2025-04-14 2:00 PM
Hey @mbarg.1,
Thank you for the answer. I do have several different boards available that I could utilize for something like this, here is a list:
- NUCLEO-H723ZG
- NUCLEO-H753ZI
- NUCLEO-H7S3L8
I made and extension board with an eMMC and an extension board with an eMMC + OctoSPI PSRAM. These boards work with those NUCLEOs. Here are some pictures:
NUCLEO-EXT with eMMC chip
NUCLEO-EXT with eMMC on NUCLEO-H7S3L8
NUCLEO-EXT with eMMC + OctoSPI PSRAM after reflow
NUCLEO-EXT with eMMC + OctoSPI on NUCLEO-H723ZG
NUCLEO-EXT with eMMC + OctoSPI PSRAM
NUCLEO-EXT with eMMC on NUCLEO-H753ZI
To be frank, I was already playing with the idea of making my own Websocket-Server. Unfortunately, my day has only 24 hours and I have to work for a living. Nevertheless, I am very curious to see your approach on this.
Thank you, and keep in mind:
"Be brave and attack.
If the enemy retreats, pursue.
If you encounter resistance, run away."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-16 7:00 AM - edited ‎2025-04-16 7:19 AM
Hi,
sorry for being late but in Italy (where I am based) we are facing 2 weeks of holiday and everybody has something to ask.
Here a project you can load on Nucleo-H743/753.
You should get the same result as you can see browsing HTTP://o6.mb-international.net (ipv6)
and HTTP://a.mb-international.net:8743/ (ipv4).
Don't be afraid of amount of code, this is a real design, already work in progress.
In MB_config/MB_lib_cfg.h you can turn on and off any part of the code; once turned off, you can remove all related code; but this will be later.
c5_az_h743orto.zip use our improved ethernet drivers.
I do include also c5_az_h743orto_stm,zip; is the same design but our drivers are disabled and uses STM package for ethernet.
For general discussion, we can keep on this forum; for more interaction, you can reach me @mikebargauan@mb-international.com.
