Arduino — communication using the Ethernet network

Transfer Multisort Elektronik Sp. z o.o.
Wednesday, 06 October, 2021


Arduino — communication using the Ethernet network

For many years, the creation of extensive computer networks has ceased to serve only for connecting computers. The drop in prices and the increase in the computing power of small microcontrollers began the rapid process of connecting low-power devices, mainly those performing control and measurement functions, to local Ethernet networks or even the global Internet network. Moreover, these solutions began to appear also in professional industrial networks, gradually replacing older systems based on RS232 and derivatives. Thus, at the beginning of the twenty-first 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 Wi-Fi, ZigBee, BLE or Z-Wave standards, still 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 from the designers of IIoT devices 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 controllers W5100/W5200/W5500 and integrating MAC and PHY circuits in one integrated circuit. This offer was quickly expanded by independent producers, who added to it new and much cheaper modules based on the popular ENC28J60. This article contains a short description of both solutions: the official one, based on the W5x00 series chips, and mainly community-developed Open Source/Open Hardware solutions based on ENC28J60 modules.

Communication using WIZnet W5x00 modules and the Arduino Ethernet library

An important advantage of the official modules based on the W5x00 series systems (including their hardware counterparts, for example OKYSTAR OKY2102 or DFROBOT DFR0125 overlays) is to provide full software support in the form of the Ethernet library embedded in 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 the WIZnet W5100 controller

Depending on the variant of the WIZnet system and the amount of available RAM, the Ethernet library supports a maximum of four (for the W5100 chip and RAM <= 2 kB) or eight (W5200 and W5500 systems) parallel incoming/outgoing connections. The software interface of the library has been divided into five classes, grouping individual functionalities. The Ethernet class is responsible for library initialization and configuration of network settings (including IP address, subnet address or access gateway settings). An IPAddress class has been created for IP addressing. To run a simple server application on the Arduino side, it will be necessary to use the EthernetServer class, which allows data to be recorded and read from all connected devices. A complementary class is the EthernetClient class, which enables, in a few simple calls, to prepare a functional network client that performs data write and read operations from the server. For UDP communication, the Ethernet library provides the EthernetUDP class.

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

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

#include <SPI.h>#include <Ethernet.h>

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

byte mac[] = {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. Additionally, the configuration of the serial port is available, thanks to which messages about the server address, new client connection and 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("Chat 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 such 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 client"); client.println("Hello, client!"); alreadyConnected = true; } if (client.available() > 0) { char thisChar = client.read(); server.write(thisChar); Serial.write(thisChar); } }}

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

Communication using ENC28J60 modules and external libraries

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

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

Despite the lack of official support from Arduino, many open source libraries have been made available to programmers, ensuring quick integration of ENC28J60 chips with the software. Particular attention should be paid to the UIPEthernet and the EtherCard libraries, the latter being made available under the GPLv2 license. The undoubted advantage of the former one is the compatibility of the API interface with the official Arduino Ethernet library, thanks to which the application development process can be independent from 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 programmer's preferences, may turn out to be an interesting alternative. Like in the case of the Arduino Ethernet library, implementation of a quite complex functionality (e.g. the implementation of the DHCP client) can be done in just a few lines of code:

#include <EtherCard.h>static byte mymac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};byte Ethernet::buffer[700];void setup () { Serial.begin(57600); Serial.println(F("[testDHCP]")); if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0) Serial.println(F("Failed to access Ethernet controller")); Serial.println(F("Setting up 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: ©stock.adobe.com/au/xiaoliangge  

Related Sponsored Contents

Organisations may be leaving their data open for exploitation

Satellite is attractive as a backup to cellular or as a primary connection where cellular...

Four questions to consider when deploying enterprise private 5G

The advancement of 5G networks across Australia gives organisations a way to meet new...

What are smart city networks: IoT networks built for scalability

Smart city technologies spearheaded by both public and private organisations create a better...


  • All content Copyright © 2024 Westwick-Farrow Pty Ltd