Arduino – communication via Ethernet network


For many years, the creation of large computer networks ceased to be used only for connecting computers. Falling prices and increasing computing power for small microcontrollers have initiated the rapid process of connecting low-power devices, primarily those providing control and measurement functions, to local Ethernet networks or even the global Internet network. . In addition, these solutions have started to appear in professional industrial networks as well, gradually replacing old systems based on RS232 and derivatives. So, at the start of the 21st century, the era of the so-called Internet of Things (IoT) began. Although the current IoT market is dominated by devices communicating mainly via wireless networks and standards Wi-Fi, ZigBee, BLE or Z-Wave, always in many hardware solutions (mainly from the so-called IIoT – Industrial Internet of Things), requiring reliable transmission and data security, the Ethernet network remains one of the most popular solutions. The creators of the Arduino platform did not leave the demand of IIoT device designers unanswered, and they extended the standard range of Arduino modules with Ethernet Shield 2, addressed to individual users, or Arduino MKR ETH SHIELD for professional solutions, based on WIZnet W5100 / W5200 / W5500 controllers and integrating MAC and PHY circuits in an integrated circuit. This offering was quickly expanded by independent producers, who added new, much cheaper modules based on the popular ENC28J60. This article contains a brief description of both solutions: the official solution, based on the W5x00 series chips, and mainly Open Source / Open Hardware solutions developed by the community and based on ENC28J60 modules.

Communication Using WIZnet W5x00 Modules and Arduino Ethernet Library

An important advantage of official modules based on the L5x00 Serial systems (including their hardware counterparts, e.g. OKYSTAR OKY2102 or DFROBOT DFR0125 overlays) is to provide full software support in the form of the Ethernet library built into the Arduino stack. Thus, the user can start creating the program right after launching the Arduino IDE, without the need to install additional software packages.

Figure 1. OKY2102 (top) and DFR0125 (bottom) modules, equipped with WIZnet W5100 controller

Depending on the WIZnet system variant and the amount of available RAM, the Ethernet library supports a maximum of four (for W5100 chip and RAM

As is the case with the Arduino platform, all the complex operations of the program are implemented directly in the library provided – the developer is given a limited but very functional set of APIs, so the development process is fast and does not require detailed knowledge of network stacks. So let’s analyze the construction of the simplest server application, supplied with the Ethernet library, whose task is to listen for incoming connections from the Telnet protocol client.

The server application code begins to add the header files necessary to establish the SPI communication (WIZnet modules exchange data with the microcontroller using this protocol) and the header files of the Ethernet library:

#include #include

The next step is to configure the network parameters (controller MAC address, access gateway IP address and subnet mask) and create a listening server on port number 23 (the default port for Telnet protocol):

mac byte[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; IPAddress ip (192,168,1, 177); IPAddress gateway (192,168,1, 1); IPAddress subnet (255, 255, 0, 0); EthernetServer server ( 23);

In the body of the setup () function, it is necessary to initialize the Ethernet library and start the listening process. In addition, the serial port configuration is available, thanks to which messages about the server address, the new client connection and the data received during the established session can be displayed:

void setup () {Ethernet.begin (mac, ip, gateway, subnet); server.begin (); Serial.begin (9600); while (! Serial) {} Serial.print (“Mail server address:”); Serial.println (Ethernet.localIP ());}

The main loop of the loop () program waits for a connection from the client and checks for readable data. When receiving data, it sends this data back to the client, unchanged, thus performing a simple echo function:

void loop () {EthernetClient client = server.available (); if (client) {if (! alreadyConnected) {client.flush (); Serial.println (“We have a new customer”); client.println (“Hello, client!”); alreadyConnected = true; } if (client.available ()> 0) {char thisChar = (); server.write (thisChar); Serial.write (thisChar); }}}

The correct functioning of the above application can be tested using any Telnet protocol client (e.g. Putty on Windows or telnet command on Linux) or with the use of another Arduino Kit and the EthernetClient class.

Communication using ENC28J60 modules and external libraries

Alternatively, instead of being officially supported WIZnet W5x00 systems, modules based on the ENC28J60 controller (eg OKYSTAR OKY3486 or ETH CLICK) can be used. With a lower price and an easier package to install manually (unlike the circuits contained in the W5x00 80 pin LQFP packages The ENC28J60 controller is available in 28-pin SSOP, SOIC, QFN packages, as well as in the SPDIP housing, intended for through-hole mounting), this circuit is very popular among hobbyists.

Figure 2. OKY3486 (top) and ETH CLICK (bottom) modules equipped with ENC28J60 controller

Despite the lack of official support from Arduino, many open source libraries have been made available to programmers, ensuring rapid integration of the ENC28J60 chips with the software. Particular attention must be paid to the UIPEthernet and EtherCard libraries, the latter being made available under the GPLv2 license. The undoubted advantage of the first is the compatibility of the API interface with the official Arduino Ethernet library, thanks to which the application development process can be independent of the choices made between the W5x00 systems and the ENC28J60 system in the hardware. The other project – EtherCard – implements an independent programming interface which, depending on the preferences of the programmer, may prove to be an attractive alternative. As in the case of Arduino Ethernet Library, the implementation of a rather complex functionality (for example the implementation of the DHCP client) can be done in a few lines of code:

#include mymac static byte[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; Ethernet byte :: buffer[700]; void setup () {Serial.begin (57600); Serial.println (F (“[testDHCP]”)); if (ether.begin (sizeof Ethernet :: buffer, mymac, SS) == 0) Serial.println (F (” Cannot access Ethernet controller “)); Serial.println (F (” Configuration from DHCP “)); if (! ether.dhcpSetup ()) Serial.println (F (” DHCP failed “)); ether.printIp (” My IP: “, ether.myip); ether.printIp (” Netmask : “, ether.netmask); ether.printIp (” GW IP: “, ether.gwip); ether.printIp (” DNS IP: “, ether.dnsip);} void loop () {ether.packetLoop (ether. packetReceive ());}

Top image credit: © / au / xiaoliangge

Leave A Reply

Your email address will not be published.