Wiznet makers

ronpang

Published December 04, 2023 ©

125 UCC

10 WCC

32 VAR

0 Contests

1 Followers

0 Following

Original Link

17. W5100S/W5500+RP2040 Raspberry Pi Pico<HTTP Server web page display>

17. W5100S/W5500+RP2040 Raspberry Pi Pico<HTTP Server web page display>

COMPONENTS Hardware components

WIZnet - W5100S-EVB-Pico

x 1


WIZnet - W5500-EVB-Pico

x 1


PROJECT DESCRIPTION

1 Introduction

HTTP is the most widely used network protocol on the Internet, and all www files must comply with this standard. HTTP is the abbreviation of Hypertext Transfer Protocol, which is used to transfer hypertext from the www server to the local browser. It makes browsers more efficient, network transfers faster, and easier to access between computers and mobile devices.

 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 DHCP application to help readers better master this technology.

2 Brief introduction

2.1 What is HTTP?

Hypertext Transfer Protocol HTTP is a communication protocol that works using the TCP protocol and the default port number is 80. It allows the transfer of Hypertext Markup Language (HTML) documents from a web server to a web browser. HTMl is a markup language for creating documents that contain relevant links. You can click a link to access other documents, images, or multimedia objects and obtain additional information about the linked item.

2.2 Advantages of HTTP

The advantages of HTTP server include:

Cross-platform and cross-language: HTTP server can run on various operating systems and programming language environments, making it adaptable to different needs and scenarios.

Scalability: HTTP server supports an extensible plug-in architecture, and new functions and modules can be added as needed.

Flexible routing and request processing mechanism: HTTP server supports flexible routing and request processing mechanism, which can process requests according to different request types and URL paths.

Support static file serving and dynamic request processing: HTTP server can handle static file serving and dynamic request processing at the same time, allowing it to efficiently provide web pages and other resources.

Support authentication and secure connection: HTTP server supports authentication and secure connection, such as SSL/TLS protocol, ensuring the security of transmitted data.

High performance and scalability: HTTP servers are generally high performance and scalable and can handle a large number of concurrent requests and data transfers.

2.3 How HTTP works

The seven steps of the HTTP communication mechanism are as follows:

Establishing a TCP connection: Before the HTTP work begins, the web browser must first establish a connection with the web server through the network, and the connection is completed through TCP.

The web browser sends a request command to the web server: Once the TCP connection is established, the web browser sends a request command to the web server.

The web browser sends request header information: After the browser sends its request command, it also sends some other information to the web server in the form of header information. Then the browser sends a blank line to notify the server that it has ended the header Sending of information.

The web server sends a response to the web browser: After receiving the request command and header information, the web server analyzes the request and then sends a response to the web browser.

The web browser receives the response: The web browser receives the response, including status code, response header, response body and other information.

Web browser parses the response: The web browser parses the response and processes the response based on information such as status code and response body.

Close the connection: After the web browser and web server complete the data transfer, the connection will be closed.

2.4 HTTP application scenarios

The application scenarios of HTTP server are very wide. The following are some common application scenarios:

Static file service: HTTP server can be used to provide downloading and access of static files, such as downloading and browsing of pictures, documents, audio, video and other files on the website.

Dynamic web page service: HTTP server can cooperate with the web page server to provide dynamic web page services. Dynamic web pages can return different content according to different user requests to implement interactive functions.

Proxy service: HTTP server can serve as a proxy server, forwarding client requests to other servers and returning the server's response to the client. This can hide the client's real address and identity and improve security.

Load balancing: HTTP server can be used as a load balancer to evenly distribute responses from multiple servers to clients to improve the server's processing power and throughput.

Cache service: HTTP server can cache web page content locally, reduce the number of visits to the original server, and improve the loading speed and response speed of web pages.

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 HTTP Network Settings 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

W5100SIO 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: Add the corresponding .h file to the http_server.c file.

Step 2: Define the macros required for DHCP configuration and the macros for the HTTP sever's maximum connection socket.

Step 3: Configure network information, turn on DHCP mode, and define the HTTP server socket table.

Step 4: Write a timer callback processing function for the DHCP 1-second tick timer processing function.

Step 5: The main function first defines a timer structure parameter to trigger the timer callback function, initializes the serial port and SPI, then writes the network configuration parameters of W5100S, initializes DHCP and starts DHCP to obtain the IP. Print the obtained IP. When the number of acquisitions exceeds the maximum number of acquisitions, a static IP will be used. After initializing the HTTP server, the server display interface will be set. The main loop will pass in the socket number and execute the loopback test function to wait for the client to connect.

#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/spi.h"

#include "wizchip_conf.h"
#include "bsp_spi.h"
#include "dhcp.h"       // Use dhcp
#include "socket.h"     // Use socket
#include "httpServer.h" // Use http
#include "webpage.h"    //Web Source Code

#define SOCKET_ID 0                      // Socket number
#define ETHERNET_BUF_MAX_SIZE (1024 * 2) // Send and receive cache size
#define DHCP_RETRY_COUNT 5               // DHCP retry times
#define MAX_HTTPSOCK 3  //MAX HTTP SOCKET

/**
* @brief   Timer callback processing function, used for dhcp timing processing
* @param   repeating :Timer structure
* @return bool
*/
bool repeating_timer_callback(struct repeating_timer *t);

/**
* @brief   Initialization of chip network information
* @param   conf_info :Static configuration information
* @return none
*/
void network_init(wiz_NetInfo *conf_info);

/* Network information to be configured. */
wiz_NetInfo net_info = {
  .mac = {0x00, 0x44, 0x55, 0x66, 0xed, 0x2e}, // Configured MAC address
  .ip = {192, 168, 1, 10},                     // Configured IP address
  .sn = {255, 255, 255, 0},                    // Configured subnet mask
  .gw = {192, 168, 1, 1},                      // Configured gateway
  .dns = {8, 8, 8, 8},                         // Configured domain address
  .dhcp = NETINFO_DHCP};                       // Configured dhcp model,NETINFO_DHCP:use dhcp; NETINFO_STATIC: use static ip.

static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {
   0,
};
static uint8_t http_tx_ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {
   0,
};
static uint8_t http_rx_ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {
   0,
};
static uint8_t socknumlist[] = {1, 2, 3}; // http socknumlist
static uint8_t breakout_flag = 0;            // Define the DHCP acquisition flag

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
   
   /*dhcp init*/
   DHCP_init(SOCKET_ID, ethernet_buf);                                   // DHCP initialization
   add_repeating_timer_ms(1000, repeating_timer_callback, NULL, &timer); // Add DHCP 1s Tick Timer handler

   printf("wiznet chip http 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_page); // netinfo.html : Network information example page
   while (true)
  {
       for (i = 0; i < MAX_HTTPSOCK; i++)
      {
           httpServer_run(i);
      }
  }
}

void network_init(wiz_NetInfo *conf_info)
{
   int count = 0;
   uint8_t dhcp_retry = 0;

   if (conf_info->dhcp == NETINFO_DHCP)
  {
       while (true)
      {
           switch (DHCP_run()) // Do the DHCP client
          {
           case DHCP_IP_LEASED: // DHCP resolves the domain name successfully
          {
               if (breakout_flag == 0)
              {
                   printf("DHCP success\r\n");
                   getIPfromDHCP((*conf_info).ip);
                   getGWfromDHCP((*conf_info).gw);
                   getSNfromDHCP((*conf_info).sn);
                   getDNSfromDHCP((*conf_info).dns);
                   wizchip_setnetinfo(conf_info); // Configuring Network Information
                   close(SOCKET_ID);              // After dhcp close the socket, avoid errors in later use
                   breakout_flag = 1;
              }
               break;
          }
           case DHCP_FAILED:
          {
               printf(" DHCP failed \r\n");
               count++;
               if (count <= DHCP_RETRY_COUNT) // If the number of times is less than or equal to the maximum number of times, try again
              {
                   printf("DHCP timeout occurred and retry %d \r\n", count);
              }
               else if (count > DHCP_RETRY_COUNT) // If the number of times is greater than DHCP fails
              {
                   breakout_flag = 1; // if DHCP fail, use the static
                   DHCP_stop();       // Stop processing DHCP protocol
                   conf_info->dhcp = NETINFO_STATIC;
                   wizchip_setnetinfo(conf_info); // Configuring Network Information
                   break;
              }
               break;
          }
          }
           if (breakout_flag)
          {
               printf("config success\r\n");
               break;
          }
      }
  }
   else
  {
       wizchip_setnetinfo(conf_info); // Configuring Network Information
  }
}

bool repeating_timer_callback(struct repeating_timer *t)
{
   DHCP_time_handler(); // DHCP 1s Tick Timer handler
   httpServer_time_handler();
   return true;
}

 

4.5 Results demonstration

1. Open Wiznet UartTool, fill in the parameters, open the browser and enter the IP obtained by the development board, and connect to the development board.

2. You can see the set service interface, and the serial port also prints connection information.

5 Precautions

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
  • Codes for this article

  • WIZnet Official Webstie

  • WIZnet Official IO library

  • YouTube Demo

Comments Write