This example covers how to integrate a HTML webserver with help of the Ethernet over EtherCAT (EoE) mailbox protocol and LWIP. To learn more about EoE, please refer to the Ethernet over EtherCAT FAQ.
ecSubDeviceWebserver.c
ecSubDeviceWebserver.h
EtherCAT_SubDevice_Webserver.c
ESL_eoeDemo.c
ESL_eoeDemoData.h
LWIP is a Lightweight TCP/IP stack. It is written in ANSI C and provides the following features:
The LWIP project is distributed under the BSD-3-Clause license. The project can be downloaded here. It is also part of the MCU+SDK.
The LWIP settings for the Webserver Example can be found in the files located in the source/industrial_comms/ethercat_slave/stack/lwip/lwip-config/ directory.
More information on these files can be found in the official LWIP documentation. Changes to these files require a rebuild of the LWIP libraries. This can be achieved through the makefile delivered with the SDK under source/industrial_comms/ethercat_slave/stack/lwip/. The command to (re-)build the libraries is:
Make sure make is in your systems path variable. Otherwise you can use the make binary which is part of the MCU+SDK.
The resulting libraries can be found in the /lib folder.
LWIP debugging can be enabled in the lwipopts.h file by setting the LWIP_DEBUG macro to a non-zero value and selecting the needed depth of debug prints.
The Webserver example distinguishes itself from the Simple example by creating
After running the application and setting up TwinCAT (or some other MainDevice capable of EoE) and reloading the SubDevice, you should be able to connect to the webserver. To do this, you need to type the correct IP address in the address bar of a web browser of your choice. There are three different pages which can be displayed depending on the requested file:
404 Error page will be displayed:
The relevant parts for the Webserver Application are the registration of the EoE callbacks (and the callback handling, which is done by the stack in the background):
The function copies the received MAC, IP, subnet, gateway, and DNS addresses to the corresponding variables of the global netif structure of LWIP.
This task can be torn down into three segments:
It is important to notice that these functions are only called once network settings are received and the corresponding flag is set in the eoeContext_s handler. Otherwise, the EC_SLV_APP_EoE_SS_task is idle.
The function netif_if_init creates a semaphore to properly initialize the LWIP TCP/IP thread with tcpip_init(). This is necessary to run LWIP in an OS environment. Once the initialization is done, the semaphore is freed and the actual network interface is added with the LWIP function netif_add(). The function sets the IP, subnet, and gateway addresses, as well as the type of incoming frames with the parameter ethernet_input. This is important because EoE sends its data in Ethernet packages. Other flags that are set include the MAC address, the instruction to handle ARP packets, and the registration of the low-level output function. This function calls the EC_API_SLV_EoE_sendFrame API to send Ethernet frames through EoE. Finally, other necessary netif functions are called.
The function EC_SLV_APP_EoE_SS_initializeWebServer creates the socket structure and binds it to a port, then listens to it. The bind function in LWIP is used to associate a socket with a local address. In this case, the socket is bound to the port number 80, which is the standard port used by HTTP servers. The listen function is used to set the socket in a state in which it is listening for incoming connections. In the LWIP implementation, this call also sets the socket in a state in which it is ready to accept incoming connections.
After the webserver is initialized, the application enters a loop in which it waits for incoming TCP/IP requests. The function accept is called to accept a connection from a client. Once a connection is accepted, the LWIP recv function is called to receive a TCP/IP frame. If a TCP/IP frame is received, the application checks the frame to see if it is a GET request. A GET request is used to retrieve a specific resource from the server.
In the context of the EoE webserver example, the application handles two types of GET requests:
In either cases the function EC_SLV_APP_EoE_SS_WebSrvProcessGetAndRespond is called and responds with response_200_content_html or response_200_content_image back to the client. If the received frame is a GET request for any other resource, the application sends the content of the response_404 back to the client.
The loop ends with closing the connection. Once a connection is closed, the application returns to waiting for a new connection. This cycle of accepting a connection, receiving a frame, processing the frame, and responding continues until the application is stopped.
The source code contains some disabled serial printouts which can be helpful when debugging. To enable them, define the macro EOE_DEBUG_PRINTS in ESL_eoeDemo.h.