Modbus RTU gateway with Ethernet support

There is a wide variety of WI-FI modules based on the ESP32 chip. As a rule, the main channel for transmitting and receiving data is WI-FI, less often Bluetooth, i.e. wireless technologies that do not require additional “body kit”. It is enough to create a web page and use the browser to manage all the settings. Using this concept, I was able to create a series of interface boards in different form factors, from embedded to external universal with additional functions. It’s time to add and test the wired Ethernet interface.

From simple to complex

For wi-fi board options, the first module installed on the board is the simplest and most common ESP-WROOM-32.

Further, with the addition of new functions and protocols, I ran into a lack of HEAP memory while running several services at the same time, TelegramBot and HomeKit turned out to be the most “gluttonous”, it was decided to use the module with built-in memory psram ESP32-WROVER, simultaneously adapting the installation site for both module options.

Ethernet and esp32 pico d4

In order not to increase the dimensions of the board and maintain compatibility with previous versions, it was decided to use the ESP32-PICO-D4 SIP module instead of ESP32 modules, the smallest module from the entire Espressif ESP32 line. Its 48-pin QFN package packs just about everything under the ESP-WROOM-32’s metal shield. Its use was successfully tested in a miniature “traveling” version of the interface in USB flash drive format.

Using the PICO-D4 saves a lot of board space, but there is no option to install extended psram memory. The good news is that when using an Ethernet channel, wi-fi can be turned off, in this case there is almost 2 times more free memory than when using WI-FI.

WI-FI remains a backup channel in case the Ethernet cable is disconnected, in which case the access point starts on the module. You can connect to it from your phone or computer to configure the module via the WEB interface. Also, the access point turns on briefly when power is applied to the interface board in order to find out the received IP address. After 15 seconds. if there was no connection in the access point mode, WI-FI turns off, if after this time the phone or computer remains connected, then the AP mode turns off after disconnecting all devices from the Wi-FI module.

Since the ESP32 has a built-in Ethernet controller, just add a PHY level chip and an RJ45 connector, I use the HR911105A with a transformer inside.

To work with the Ethernet interface, it is necessary to connect an external device with a built-in PHY to the chip. This connection is possible using a 17-signal “Media Independent Interface (MII)” line or a nine-signal “Reduced Media Independent Interface (RMII)” line. Since, in addition to Ethernet, the board uses a slot for a microSD card, three lines for the Modbus driver, two universal GPIOs, and an information LED for free ports for connecting PHY, only the RMII version on 9 lines remains.

The LAN8720A chip was chosen as a device with an external PHY, in a compact 24-pin QFN package.

To clock the PHY chip and the Ethernet controller in esp32, you also need a signal with a frequency of 50MHz. Three options can be used:

– Internal 50MHz signal source with esp

-external quartz 25Mhz

-external oscillator 50MHz

I checked options 1 and 3

In principle, in some cases, the generator can not be installed, but the built-in generator from the esp32 module can be used. At the layout stage, I tried this option, the module started up, but Espressif in the configuration settings warns: “Output RMII clock from GPIO0 (Experimental!)”. Espressif does not recommend using the internal clocking option on the GPIO0 pin, as the signal on the pin may be unstable. The recommendation is to use an external signal on the GPIO0 input.

There are two more gpios from which you can get the clock speed for Ethernet, these are gpio 16 and 17. Since I use pico d4, these options are not suitable due to the fact that signals for psram memory expansion are assigned to these pins, in pico d4 psram is not used, but it is used in the ESP32-WROVER module and in order not to lose compatibility, it was decided to use an external 50 MHz generator.

But even here there is a “rake”, since the output from the generator goes to both the PHY LAN7820 chip and the GPIO0 input, then at startup a low-level situation may occur at this output, in order to avoid this, it is recommended to use an additional output to turn off the generator during startup esp32 module. I foresaw this option by using almost the last unused output, but some of the generators, when checking, did not transfer their output to the third state when turned off by an external signal, and when esp32 started, it entered bootloader mode.

As a result, the generator shutdown pin remained in reserve, and a capacitor was installed between the generator and the GPIO0 input instead of a resistor, which made it possible to start when power was applied without the risk of entering Boot mode.

The issue with clocking during debugging took the most time, the rest of the circuitry starts up without any problems. Schematic features can be specified in pdf on LAN8720, and from manufacturers of similar solutions such as Olimex. For my tests, I used this version of the board with LAN8720.

As a start, I used an example from espressif in esp-idf to test the functionality

examples/Ethernet/basic and examples/Ethernet/eth2ap

The first example demonstrates the basic use of an ethernet driver along with tcpip_adapter, if everything is configured and connected correctly, the board will receive an IP address that can be pinged.

The second example is more complex and implements a simple “router” that supports packet forwarding between the Ethernet port and the Wi-Fi access point interface. In this case, the Ethernet must play the role of WAN (i.e. have access to an external network) so that the mobile device can access the Internet when connected to the ESP32 via Wi-Fi.

As a result, having collected all the accumulated experience and soldering the board, it was possible to implement a universal device, depending on the firmware used, the board can work both with the WI-Fi interface and via cable. In both cases, all functions work the same way.

For the WI-Fi version, I added the ability to connect an external antenna; using a jumper, you can switch the signal to an external pigtail or to a printed antenna on the board.

When using Ethernet, some of the settings in the web interface are not used, unused fields are hidden. The color of the web interface has also been changed to red, similar to the color of the boards.

Purpose

The main purpose of these devices is to quickly add the missing functionality and services to simple programmable devices with an RS485 interface.

The interface is the link between the person and the device. From the user’s side, access to all module settings and its functions is provided, as well as software updates over the air (OTA). From the side of the device, the Modbus RTU information exchange channel, by reading and writing values ​​from the memory registers in the network interface, you can quickly receive information using various protocols and from various external devices.

The following network interface modes are currently available:

Modbus TCP

TCP Gateway to RTU

MQTT

HomeKit

TelegramBot

1-wire (ds18b20 up to 16 pcs.)

Logger (microsd or external file on PC)

Digital I/O mode

– PWM 2 channels

– Generator 0-100kHz

-Manual encoder

-2 DI

-2 DO

-I2C expander MCP23017 16 I/O

-i2c display 1306

-i2c htu21 humidity and temperature sensor

-i2c barometric pressure and temperature sensor bmp180

– led strip ws2812

-i2c nfc card reader (pn532)

-i2c current/voltage meter INA219

More details on working with all functions can be found in the series videoas well as in the wiki at github.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *