ESP32 - Modbus

In this tutorial, we are going to learn:

What is Modbus?

Modbus is a protocol that uses to communicate between device and device, device and software/app, and devices to HMI. It is widely used in industrial areas to control/monitor devices.

In a simple word, Modbus is a language that a device uses to communicate with other devices, software/app, and HMI device.

This tutorial is about ESP32, so ESP32 represents for the device. By using Modbus, we can make:

  • Two ESP32s talk with each other to control/monitor the output/input
  • ESP32 talks with the software on PC or app on the smartphone
  • ESP32 talks with HMI device

For example, ESP32 #1 with 4 switches controls ESP32 #2 with 4 light bulbs over a long distance.

Why needs Modbus?

Are there any alternatives for Modbus? If have, when to use Modbus? when to use the alternatives?

To answer these questions, let's consider a case: A switch connected to ESP32 #1 controls an LED connected to ESP32 #2 over a long distance.

button control led over long distance

The simplest alternative way of Modbus is to define a simple protocol by ourselves:

  • ESP32 #1:
    • When the switch is switched to ON, ESP32 #1 sends a byte (command) with value 1 to ESP32 #2.
    • When the switch is switched to OFF, ESP32 #1 sends a byte (command) with value 0 to ESP32 #2.
  • ESP32 #2:
    • If the received byte is 1, Turn ON LED
    • If the received byte is 0, Turn OFF LED

The above protocol is defined by ourselves, not a standard protocol. We need to define more if:

  • ESP32 #1 wants to know whether the command is executed successfully by ESP32 #2
  • ESP32 #1 wants to check the state of the LED on ESP32 #2
  • ESP32 #1 with multiple switches wants to control multiple LED on ESP32 #2
  • ESP32 #1 wants to use a switch to control an LED on ESP32 #2 and ESP32 #2 also wants to use a switch to control an LED on ESP32 #1
  • And more use case.

It is not easy to define all commands by ourselves. The problem becomes more complicated when we mix the above requirements. And we also cannot guarantee error-free

Another problem occurs when defining the protocol by ourselves. If we want our ESP32 to work with other ESP32 used a protocol defined by another person, It is impossible unless we co-work and share the self-defined protocol.

Those are the reasons why we need Modbus protocol. If we use the Modbus protocol:

  • We do NOT need to define the commands, Modbus has defined it for every case. We just need to implement it.
  • Our device can work with other devices/software that support Modbus WITHOUT co-working with the device/software's creator.

If we use the Modbus protocol for ESP32, we can use many free software/app to control/monitor our ESP32.

When to use and NOT use Modbus

Modbus protocol is not easy for beginners to understand and implement it, even the Modbus library is available. Therefore, it is ok to use a simple self-defined protocol as described above if:

  • The system is simple, such as several switches on ESP32 #1 controls several LED on ESP32 #2
  • ESP32 does NOT need to work with other devices/software from other creators

We should use Modbus when:

  • The system is complicated
  • The system required the high reliability
  • ESP32 needs to work with other devices/software from other creators

※ NOTE THAT:

There are some other standard protocols and non-standard protocols that are alternatives for Modbus. They are out of the scope of this tutorial.

How Modbus protocol works

As described earlier, An ESP32 can communicate with software/app, HMI device, or another ESP32 via Modbus protocol. This tutorial takes communication between two ESP32 as an example of Modbus communication. The others are the same.

Concepts

There are some concepts in Modbus:

  • Master and Slave
  • Request and Response
  • Function Code (FC) and Address

Modbus protocol works in the Master-Slave model. Among two ESP32, one MUST work as a Master, and the other works as a Slave.

Master sends a request, and the Slave sends a response.

modbus Master Slave

A request sent by Master basically contains the following information:

  • Function Code (FC): 1 byte, is the command, which tells Slave what to do. For example, reads the state of a digital input pin, a digital output pin or analog input pin, controls a digital output pin.
  • Address: 2 bytes, is used to identified pin. Each digital input pin, digital output pin, the analog input pin is given an address.
  • Data (e,g control value)

Once receiving a request, the Slave takes the corresponding action and send back a response which contains the result of action or the information that Master requested.

Function Code

The Function Code was defined by the Modbus standard. There are many Function Code. Let's see some basic Function Code below:

  • FC 01 (Read Coils): is a command used to read the state of a single or several digital output pins
  • FC 05 (Write Single Coil): is a command used to control (write) the state of a single or several digital output pins
  • FC 02 (Read Discrete Inputs): is a command used to read the state of a single or several digital input pins

"FC 01" means the value of the Function Code is 0x01.

There are more Function Code. In order to avoid overloading newbies, this tutorial only present some essential Function Code.

When using ESP32 Modbus library, you need to:

  • Decide which Function Code to use
  • Give addresses for each ESP32 pin (only pins that you use)

The detailed instruction of using Modbus library will be presented in the next tutorial, like our Facebook page to get the new tutorial notification.

ESP32 Master can read the state of its digital input pin, and then control the digital output's state of ESP32 Slave, and vice versa

For example:

  • ESP32 Master is wired to LED #M, SWITCH #M
  • ESP32 Slave is wired to LED #S, SWITCH #S
  • Let's make SWITCH #M control LED #S, and SWITCH #S control LED #M
  • ESP32 modbus led switch

Solution:

  • Master reads the state of SWITCH #M → send a request to Slave to control LED #S according to the state of SWITCH #M → Slave controls LED #S and send the response
  • Master sends a request Slave to read the state of SWITCH #S → Slave responds with state of SWITCH #S → Master controls LED #M according to the state of SWITCH #S

※ NOTE THAT:

  • In the above, we use "digital input pin", "digital output pin" terms to make it easier for beginners. In fact, "digital input pin" represents for any device's state that read-only, "digital output pin" represents for any device's state that read/write
  • Modbus can support multiple Masters and multiple Slaves
  • Modbus can use not only for digital input/output but also for analog input/output

Modbus RTU/ASCII and Modbus TCP

There are three popular Modbus types: Modbus RTU, Modbus ASCII, and Modbus TCP. The working principles are the same for all of Modbus types, as described above. Each type of Modbus is designed to adapt to the network environment: Serial or TCP.

  • System diagram
  • modbus tcp vs modbus rtu/ascii
  • Protocol stack
  • modbus tcp vs modbus rtu/ascii

Differences between Modbus RTU/ASCII and Modbus TCP

Modbus RTU/ASCII Modbus TCP
Over serial (RS-232/RS-422/RS-485) Over TCP
Support single transaction(*) Support multiple transactions(**)


  • (*): Modbus RTU/ASCII supports a single transaction only. It means that when a Master sends a Modbus request, it MUST wait for a Modbus response or timeout before sending another request.
  • (**): Modbus TCP supports multiple transactions. It means that when a Master sends a Modbus request, it does NOT need to wait for a Modbus response. It can send multiple requests continuously. To identify a response corresponding to a request, Modbus TCP has an additional field, called "Transaction ID". The request and the corresponding response have the same Transaction ID.

How to use ESP32 Modbus library

Thanks to Modbus library, using Modbus with ESP32 is simplified.

We are going to make a tutorial about how to use Modbus library. Like our Facebook page to get the new tutorial notification.

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere