Arduino Master & ESP8266 Slave Modbus RTU (TTL)

In many occasions we have communicated our ESP8266 with Arduino via AT commands, but there are certain cases in which it is required to communicate both devices and send and/or receive large numbers of registers, almost in real time, although there are protocols such as I2c, and SPI with Libraries already implemented in arduino, we will use Modbus RTU, par excellence the communication protocol of field for devices of monitoring and control since it is easy its implementation, is safe, fast and approves of errors.

Arduino Configuration

In this case, it has been configured as a modbus master, using the second serial port (Serial1) of our board, since it has 4 ports available, modifications were made to the master modbus library based on previous tests.

Configuration ESP8266

It has been configured as a slave modbus, in this case the modbus protocol works through an extra serial port generated with the SoftwareSerial library, which allows to create a serial port generated by software with interruptions, it is recommended not to use it at very high speeds in this Case at 9600 bauds, for more information we recommend these previous references.

Connections

Since the communication is TTL (0-5v), the serial1 of arduino mega 2560 pins (Tx1 -18) (Rx1 – 19) is connected to the port of ESP8266 created in softwareserial pins D7 (13), D8 (15) RX , TX, to cross RX -Tx between both, following individual descriptions.

ESP8266 12E NodeMCU

Arduino MEGA 2560 R3

Recommendations

We recommend a speed of 9600 bauds for a good performance, since the serial software library is not Uart own the ESP8266, at high speeds there could be bit errors in the frame, since it works with interruptions.

Modifications have been made to the master and slave libraries for this specific test, the modifications are especially in the assignments and declaration of the serial ports.

Conclusions

We consider this an excellent solution given that a common and robust industrial protocol is used that would easily apply to communicate with an HMI, PLC, Meter, PAC, SCADA and / or other class of industrial controllers.

A great benefit is the modbus is the facility of transmitting a large number of registers, it should be noted that it allows for 16-bit integer registers.