Wiznet makers

ronpang

Published December 22, 2023 ©

125 UCC

10 WCC

32 VAR

0 Contests

1 Followers

0 Following

Original Link

29. W5100S/W5500+RP2040 Raspberry Pi Pico<Web socket Server>

29. W5100S/W5500+RP2040 Raspberry Pi Pico<Web socket Server>

COMPONENTS Hardware components

WIZnet - W5100S-EVB-Pico

x 1


WIZnet - W5500-EVB-Pico

x 1


PROJECT DESCRIPTION

1 Introduction

In the world of the Internet, real-time and efficient data exchange is crucial. The WebSocket protocol, as a powerful communication tool, was born to meet this need. Unlike the traditional HTTP protocol, the WebSocket protocol allows the server to actively send data to the client without requiring the client to initiate a request. This feature gives the WebSocket protocol significant advantages in scenarios that require real-time data exchange, such as online chat, multiplayer online games, etc.

 W5100S/W5500 is an embedded Ethernet controller integrating a full hardware TCP/IP protocol stack. It is also an industrial-grade Ethernet control chip. This tutorial will introduce the basic principles, usage steps, application examples and precautions of W5100S/W5500 Ethernet Web socket application to help readers better master this technology.

2 Protocol Introduction

2.1 What is the WebSocket protocol?

WebSocket is a network protocol that allows two-way communication between servers and clients. Unlike HTTP which uses a request and response pattern, WebSocket peers can send messages in either direction at any time. WebSockets are commonly used in chat applications and other applications that require continuous communication between the server and client.

2.2 Working principle of WebSocket protocol

The client makes an HTTP request to the server to establish a WebSocket connection.

If the server uses the WebSocket protocol, it will accept the upgrade and send a response.

If the initial handshake is successful, the client and server have agreed to use the existing TCP/IP connection established for the HTTP request as the WebSocket connection.

Data can now flow over this connection using the basic Frame Message protocol.

Once both parties confirm that the WebSocket connection should be closed, the TCP connection is torn down.

2.3 Advantages of WebSocket protocol

The advantages of MODBUS TCP protocol include:

Full-duplex communication: WebSocket supports full-duplex communication, which means the server and client can send and receive information at the same time.

Real-time: Using WebSocket, data can be sent and received immediately, faster than HTTP. Additionally, they are faster than AJAX.

Cross-origin communication: WebSocket supports cross-origin communication, although this may pose security risks.

Cross-platform compatibility: WebSocket supports cross-platform compatibility, including web, desktop and mobile devices.

Low overhead: HTTP requires about 2000 bytes of overhead, while WebSocket only requires 2 bytes.

Alternative to long polling: WebSocket can replace long polling.

Data types: Unlike AJAX calls that can only send string data types, WebSockets are data typed.

Persistent connection: WebSocket is a persistent connection between the client and the server, which allows the server to send data to the client at any time without a request from the client.

Low latency: Since the connection is already established, the organization of WebSocket data frames is very efficient (mainly 6 extra bytes, 2 bytes for headers, 4 bytes for mask) and can be compared to that by including headers Send data more efficiently through HTTP requests such as headers, cookies, etc.

Reduced bandwidth usage: Compared to HTTP-based real-time mechanisms such as HTTP Long Polling, the WebSocket protocol uses persistent connections instead of continuous HTTP request/response cycles. WebSocket requires less bandwidth and provides lower latency compared to HTTP, taking the load off the client and server.

Improved performance and responsiveness: The advantage of WebSockets is that they enable real-time communication between client and server without frequent HTTP requests/responses. This brings benefits such as reduced latency, improved performance and responsive web applications.

2.4 WebSocket application scenarios

The WebSocket protocol has a wide range of application scenarios. Here are some of the main areas:

Real-time event updates: such as real-time sports scores, stock market prices, etc.

User notifications: such as software updates, content updates.

Collaborative editing tools: multiple people can edit the same document online at the same time.

Information streams: such as real-time updates from social media and news.

Social media streams: such as Twitter or Facebook feeds updated in real time.

Chat applications: such as real-time online chat.

Multiplayer online games: For example, online games that require multiple people to be online at the same time.

Real-time location data updates: For example, real-time location updates for shared bikes or taxi-hailing apps.

Stock market applications: such as real-time updated stock prices.

Example of online web chat using WebSocket:

 

3 WIZnet Ethernet chip

WIZnet mainstream hardware protocol stack Ethernet chip parameter comparison

ModelEmbedded CoreHost I/FTX/RX BufferHW SocketNetwork Performance
W5100STCP/IPv4, MAC & PHY8bit BUS, SPI16KB4Max.25Mbps
W6100TCP/IPv4/IPv6, MAC & PHY8bit BUS, Fast SPI32KB8Max.25Mbps
W5500TCP/IPv4, MAC & PHYFast SPI32KB8Max 15Mbps

W5100S/W6100 supports 8-bit data bus interface, and the network transmission speed will be better than W5500.

W6100 supports IPV6 and is compatible with W5100S hardware. If users who already use W5100S need to support IPv6, they can be Pin to Pin compatible.

W5500 has more Sockets and send and receive buffers than W5100S.

4 WebSocket example overview and usage

4.1 Flowchart

The running block diagram of the program is as follows:

4.2 Core preparation work

Software

Visual Studio Code

WIZnet UartTool

Hardware

W5100S IO module + RP2040 Raspberry Pi Pico development board or WIZnet W5100S-EVB-Pico development board

Micro USB interface data cable

TTL to USB

cable

4.3 Connection method

Connect the USB port of the PC through the data cable (mainly used for burning programs, but can also be used as a virtual serial port)

Convert TTL serial port to USB and connect the default pin of UART0:

RP2040 GPIO0 (UART0 TX) <----> USB_TTL_RX

RP2040 GPIO1 (UART0 RX) <----> USB_TTL_TX

When wiring using module connection RP2040

RP2040 GPIO16 <----> W5100S MISO

RP2040 GPIO17 <----> W5100S CS

RP2040 GPIO18 <----> W5100S SCK

RP2040 GPIO19 <----> W5100S MOSI

RP2040 GPIO20 <----> W5100S RST

Connect the PC and device to the router LAN port through network cables

4.4 Main code overview

We are using the official ioLibrary_Driver library of WIZnet. The library supports rich protocols and is easy to operate. The chip integrates the TCP/IP protocol stack on the hardware. The library also encapsulates the protocols above the TCP/IP layer. We only need to simply call the corresponding function to complete the application of the protocol. .

Step 1: Reference the corresponding library file in the websocket_server.c file.

Step 2: Define macro constants and define global variables.

Step 3: Define four functions, including a 1-second timer callback function (used to handle DHCP timeout processing), a set network address function, a serial port initialization function and a serial port reception callback function.

Step 4: The main function first initializes the serial port and SPI and detects the link. Then set the network address of W5100S. First use DHCP to obtain it. If it fails, use the preset static IP address. Then the web page information is registered to the HTTP server, and finally the httpServer program and websocketServer program are run in the main loop.

The main function program is as follows:

int main()
{
   int i;
   struct repeating_timer timer; // Define the timer structure
   wiz_NetInfo get_info;
   /* MCU init */
   stdio_init_all();              // Initialize the main control peripheral
   wizchip_initialize();          // Initialize the chip interface
   wizchip_setnetinfo(&net_info); // Configure once first
   UART0_Init();                  // uart0 init

   /*dhcp init*/
   DHCP_init(SOCK_DHCP, ethernet_buf);                                   // DHCP initialization
   add_repeating_timer_ms(1000, repeating_timer_callback, NULL, &timer); // Add DHCP 1s Tick Timer handler

   printf("wiznet chip websocket server example.\r\n");
   network_init(&net_info);                                                                // Configuring Network Information
   print_network_information(&get_info);                                                   // Read back the configuration information and print it
   httpServer_init(http_tx_ethernet_buf, http_rx_ethernet_buf, MAX_HTTPSOCK, socknumlist); // HTTP Server initialization
   reg_httpServer_webContent((uint8_t *)"index.html", (uint8_t *)INDEX_HTML);              // netinfo.html : Network information example page
   while (true)
  {
       for (i = 0; i < MAX_HTTPSOCK; i++)
      {
           httpServer_run(i);
      }
       do_websocket_server(SOCK_WEBSOCKET);
  }
}

4.5 Results demonstration

5 Precautions

The content sent can only be in UTF-8 format. If it is in other formats, it may cause garbled characters and cause the webpage to crash, and it needs to be refreshed to normalize.

If we want to use WIZnet's W5500 to implement the example in this chapter, we only need to modify two places:

(1) Find the wizchip_conf.h header file under library/ioLibrary_Driver/Ethernet/ and modify the WIZCHIP macro definition to W5500.

(2) Find the CMakeLists.txt file under library and set COMPILE_SEL to ON. OFF is W5100S and ON is W5500.

Documents
  • Code for this article

  • YouTube

Comments Write