Skip to main content
sunil
Associate
January 20, 2021
Solved

websocket connection and transmit data from stm32f7 using LWIP

  • January 20, 2021
  • 4 replies
  • 5343 views

Hi

I am developing the project with STM32F746 (STMCUBE IDE)

sending the data to the server . want to use WEBSOCKET protocol to do this

Is there any Library with example for WEBSOCKET with LWIP in STM32

This topic has been closed for replies.
Best answer by Imen.D

Hello @sunil​  and welcome to the STM32 Community :smiling_face_with_smiling_eyes:,

Have a look to the LwIP example under STM32CubeF7 MCU package, that can help you as an implementation example: 

STM32Cube_FW_F7_V1.16.0\Projects\STM32746G-Discovery\Applications\LwIP\LwIP_HTTP_Server_Netconn_RTOS

For more details about LwIP, please refer to this UM1713 : “Developing applications on STM32Cube with LwIP TCP/IP stack�?.

Imen

4 replies

Imen.DAnswer
Technical Moderator
January 29, 2021

Hello @sunil​  and welcome to the STM32 Community :smiling_face_with_smiling_eyes:,

Have a look to the LwIP example under STM32CubeF7 MCU package, that can help you as an implementation example: 

STM32Cube_FW_F7_V1.16.0\Projects\STM32746G-Discovery\Applications\LwIP\LwIP_HTTP_Server_Netconn_RTOS

For more details about LwIP, please refer to this UM1713 : “Developing applications on STM32Cube with LwIP TCP/IP stack�?.

Imen

"When your question is answered, please close this topic by clicking ""Accept as Solution"".ThanksImen"
sunil
sunilAuthor
Associate
February 6, 2021

Hi @Imen DAHMEN​ ,

Thanks for your support. I have tried LwIP_HTTP_Server_Netconn_RTOS from the example code and it works in NUCLEO-F746ZG. and also i searched example for SOCKET_API but it is not there for this NUCLEO-F746ZG board. I tried on own with help of STM example from some other board. The initialization was working fine but during entering IP address

it return the value -1

ret1 = read(conn, recv_buffer, buflen);

 if(ret1 < 0) return;

and the full http-server-socket code is

void http_server_serve(int conn)

{

 int buflen = 1500;

 //int ret;

 struct fs_file file;

 unsigned char recv_buffer[1500];

 /* Read in the request */

 ret1 = read(conn, recv_buffer, buflen);

 if(ret1 < 0) return;

 /* Check if request to get ST.gif */

 if (strncmp((char *)recv_buffer,"GET /STM32F7xx_files/ST.gif",27)==0)

 {

   fs_open(&file, "/STM32F7xx_files/ST.gif");

   write(conn, (const unsigned char*)(file.data), (size_t)file.len);

   fs_close(&file);

 }

 /* Check if request to get stm32.jpeg */

 else if (strncmp((char *)recv_buffer,"GET /STM32F7xx_files/stm32.jpg",30)==0)

 {

   fs_open(&file, "/STM32F7xx_files/stm32.jpg");

   write(conn, (const unsigned char*)(file.data), (size_t)file.len);

   fs_close(&file);

 }

 /* Check if request to get ST logo.jpeg */

 else if (strncmp((char *)recv_buffer,"GET /STM32F7xx_files/logo.jpg", 29) == 0)

 {

   fs_open(&file, "/STM32F7xx_files/logo.jpg");

   write(conn, (const unsigned char*)(file.data), (size_t)file.len);

   fs_close(&file);

 }

 else if(strncmp((char *)recv_buffer, "GET /STM32F7xxTASKS.html", 24) == 0)

 {

   /* Load dynamic page */

   DynWebPage(conn);

 }

 else if((strncmp((char *)recv_buffer, "GET /STM32F7xx.html", 19) == 0)||(strncmp((char *)recv_buffer, "GET / ", 6) == 0))

 {

   /* Load STM32F7xxpage */

   fs_open(&file, "/STM32F7xx.html");

   write(conn, (const unsigned char*)(file.data), (size_t)file.len);

   fs_close(&file);

 }

 else

 {

   /* Load 404 page */

   fs_open(&file, "/404.html");

   write(conn, (const unsigned char*)(file.data), (size_t)file.len);

   fs_close(&file);

 }

 /* Close connection socket */

 close(conn);

}

/**

 * @brief http server thread

 * @param arg: pointer on argument(not used here)

 * @retval None

 */

static void http_server_socket_thread(void *arg)

{

 int sock, newconn, size;

 struct sockaddr_in address, remotehost;

 int buflen = 1500;

 struct fs_file file;

 unsigned char recv_buffer[1500];

 /* create a TCP socket */

 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)

 {

   return;

 }

 /* bind to port 80 at any interface */

 address.sin_family = AF_INET;

 address.sin_port = htons(80);

 address.sin_addr.s_addr = INADDR_ANY;

 if (bind(sock, (struct sockaddr *)&address, sizeof (address)) < 0)

 {

   return;

 }

 /* listen for incoming connections (TCP listen backlog = 5) */

 listen(sock, 5);

 size = sizeof(remotehost);

 while (1)

 {

   newconn = accept(sock, (struct sockaddr *)&remotehost, (socklen_t *)&size); 

   http_server_serve(newconn);

 }

}

and is there any STM example for websocket message transferring

I didn't find any STM example for LWIP for the STM32F407. If it there please provide the link

sunil
sunilAuthor
Associate
February 6, 2021

I found that problem in socket accept function

see the attach picture

it returns value newconn as -1 with listen(sock,5) and also listen(sock,8)

sunil
sunilAuthor
Associate
February 6, 2021

0693W000007ECgPQAW.png