Gateway: ESP8266 Modbus RTU MQTT + HMI Industrial Panasonic

Given the growth of IoT applications using the MQTT protocol as a base and the Node-RED platform, we have created a possibility for industrial devices or devices using Modbus RTU protocol (RS232 or RS485) to interact with applications or IoT platforms that use MQTT in This Node-RED case.

In the industry there is a large number of Monitoring and Control Devices that have Modbus RTU such as Counters, Meters (Flow, Electrical, Temperature, Humidity, etc.), Industrial Controllers PLC, PAC, HMI Screens, Speed ​​Inverters, For which the only possibility of extracting and concentrating data through OPC servers.

We have configured a module ESP8266 12E NodeMCU as Master Modbus RTU serial via RS232 (CHIP MAX232) to request and send data to a GT01 Industrial Screen Panasonic brand, the reference I have is fed to 5V, The same ESP module is configured as an MQTT client, The routine created in Arduino IDE bridges Modbus read and write registers in this case “Holding Registers” and by the MQTT subscribing and publishing topics.

trialcommand.comSince the ESP8266 module has a serial port, we used the softwareserial library to create a second port that would be used only for Modbus RTU allowing programming and debugging without physically interrupting the modbus communication, we added an integrated MAX232 for protocol conversion to RS232.

Recommended References

trialcommand.comIn this specific case the test we will perform will be based on an industrial device, we will connect a Panasonic HMI GT01 Screen  configured as a Modbus RTU slave (RS232) with GTWIN software, why a screen? It would be very boring and simple to use a simulator and we want to implement real hardware.

Recommended References


We have done 2 tests one for presentation and one for explanation:

1 – Rapid HMI Test Panasonic and Node-RED Dashboard

  • We use only 2 modbus 1 registers for reading and one for writing.
  • We perform visualization and control with dashboard.

2- Complete explanation and test 10 Reading 10 writing

  • We will use 10 read registers and 10 write registers between Node-NET and the HMI.
  • We will explain the sending and receiving methods in Node-Red.

Quick Test HMI Panasonic and Node-RED Dashboard

In this example the HMI is configured as a slave modbus rtu only we will use 2 available registers:

  • Holding Register [0] – Read
  • Holding Register [10] – Write

A dashboard has been created in Node-RED and 2 MQTT nodes assigned to Send and Receive Topics have been previously set to the code in ESP8266.

The value entered on the screen will be sent to an MQTT Topic and the value sent from Node-RED for a topic will reach a screen record.

Downloads: Arduino code on the bottom


Receiving Holding Register from MQTT

The Holding Register [0] is received in an MQTT subscription node and the value individually and sent to 2 Widgets Text Node and Gauge Node for display in the Dashboard.

Note: Full explanation below “Node-RED reception from Panasonic HMI”

Sending Records via MQTT to Holding Registers

We add a slider node widget configured to send a value from 0 to 1000 which connects to the MQTT publication node to send the value in the Holding register [10] and connects to the Gauge node to display the change from the dashboard.

Note: Full explanation below “Shipping from Node-RED to Panasonic HMI”

Dashboard Settings

Order of widgets for visualization and control.

Node-RED Dashboard

Full Explanation

The following video will complete the explanation of the connection using 20 registers:

Panasonic HMI GT01 design in GTWIN

The screen is configured as slave Modbus RTU connection RS232, Previously on the GT01 Screen 2 screens have been designed the GTWIN Configuration software:

  1. Reading from 3 Holding Registers [0] [1] [2] from 3 MQTT records.
  2. Writing 3 Holding Registers [10] [11] [12] to 3 MQTT records.


In the previous test we only used 2 records in this new case we will use 10 read registers and 10 write registers.

MQTT Client Configuration on Node-RED

In this case we have previously installed the MQTT “Mosquitto” Broker installed on my Linux server (Lubuntu), to that MQTT broker connect Node-NET and the ESP8266 module.

This is the configuration of the connection MQTT of Node-RED to connect with mosquito and therefore with the ESP8266.

Shipping from Node-RED to HMI Panasonic

An Inject Node “Send 10 Registers” has been used which contains 10 values separated by commas “,” example: “1,2,3,4,5,6,7,8,9,10,” Note: have Note that modbus handles 16-bit integers to not send larger values.

It connects to the MQTT publishing node “MQTT_Holding_Array- Holding Registers [0] – Holdin Registers [9]” and 10 values will be sent to the top 10 holding registers on the screen.

Node-RED reception from Panasonic HMI

We have created 2 methods of receiving data from the ESP8266 Module:

Group reception 10 records a single MQTT topic

In this case, a MQTT subscription node “Holding_to_MQTT_Array – Holding Register [10] … Holding Register [19]” and the holding_to_MQTT_Array topic are used and you will receive all 10 registers of the following form 1,2,3, 4,5,6,7,8,9,10.

Individual reception 1 record 1 MQTT topic

Since in certain cases a value is required to be updated faster than others, it has been created that the ESP8266 sends in 1 single topic the value of 1 single holding register in specific, each subscriber node MQTT request individually, example Node “Holding Register [10]” and the “Holding_to_MQTT_1” topic.

Arduino IDE

The code integrates 2 tutorials previously made, ESP8266 as client MQTT and ESP8266 as Master Modbus RTU Via RS232.

  1. The first case requires the
    < PubSubClient.h >
    library that allows it to be an MQTT client.
  2. The second case requires the < ModbusMaster232.h > library for modbus and the < SoftwareSerial.h > library to emulate the serial port for modbus.

Important Note: To correctly understand the following code we recommend first to see the following tutorials:

This example sends read 10 registers and writes in 10 Modbus RTU registers,

NOTE: Inside the Arduino IDE Download Link.

Receiving MQTT and sending to Modbus RTU Slave

The reception of MQTT topics from Node-red is performed by the callback function, in summary:

  1. Esp8266 subscribes to the “MQTT_Holding_Array” topic.
  2. Node-RED publishes in the topic “Holding_to_MQTT_Array” and sends 1,2,3,4,5,6,7,8,9,10, in a String.
  3. Callback function receives String and a for loop divides values separated by commas “,” and stores each value in the string array MQTT_to_Holding [].
  4. The value in the array is converted to int and sent to the slave modbus in the following example node.writeSingleRegister (0, MQTT_to_Holding [0] .toInt ()); For the Holding Register [0].

Fragment of code callback function

Reading Modbus Registers and Sending MQTT

A single record reading is performed but there are 2 MQTT sending methods (individual and group) mentioned previously in “Node-RED Reception from Panasonic HMI”.

It must be taken into account that for the Modbus reading a delay is used to read the registers  delay (tdelay); 5 ms this value will vary depending on the number of records to request.

We combine 2 steps:

  1. Reading of Modbus (Holding Registers).
  2. Sending via group MQTT top eg: “Holding_to_MQTT_Array” and sending via individual MQTT topics eg: “Holding_to_MQTT_1”.

Code Fragment Reading of Holding Register [10] and submission MQTT topic “Holding_to_MQTT_1”.

Full Code


ESP8266 12E NodeMCU Lolin (Connection to MAX232)

Converter MAX232 (RS232)

Recommendations for implementation

  • Use Serial Communication Speed ​​9600 baud.
  • Depending on the number of modbus registers read, the tdelay variable must be modified, given a time for reading and deleting the modbus buffer, if there are modbus read failures, not exceeding 10 requested registers, we are still optimizing the modbus readings and Note that using softwareserial emulates a serial port.
  • There is another possibility of failure in this case we are making a constant sending of records both mqtt and modbus, it would be advisable to only send or receive MQTT records if required, not at all times as in this case.


In the industry we have seen devices such as Modbus RTU meters, and adding an ESP8266 and a TTL to RS232 (RS485 or RS485) we could monitor our electricity consumption without requiring an OPC or specific Hardware, as mentioned earlier MQTT protocol has been implemented in a variety of IoT applications and there are now a large variety of local and cloud platforms that could host our data.

To take into account, if you want to implement the tests described above in a real project, we detail that we have not tested 24/7 hardware esp8266, should take into account materials (pcb, welding, wiring), electrical protections, electrical insulation , Ambient temperature, Wifi signal strength, among other variables, but we consider that for basic automation applications taking into account the above mentioned would work correctly.



Arduino IDE
Node RED

2 Replies to “Gateway: ESP8266 Modbus RTU MQTT + HMI Industrial Panasonic”

  1. Pingback: Google

Leave a Reply