AWS, Cloud Computing

6 Mins Read

AWS IoT Core Connectivity with Modbus Devices using Node-Red : Part I

Voiced by Amazon Polly


With the advent of Industry 4.0 and IoT, an era of connected devices has started. Thus, all industries, manufacturing or exploration, are rushing to upgrade their equipment to stay in the race. But in this scenario, hindrance comes in the form of cost and time, the cost of replacing old PLC/PACs from the process units with IoT-enabled Smart Controller/DAQs, and the time to achieve this whole process.

To solve this issue, IoT Gateway devices and IoT Edge devices are introduced by many vendors, and AWS is endorsing a few. A gateway device is an intermediary that accepts all or a few industry-standard protocols such as Profibus, Foundation Fieldbus, Modbus-TCP/RTU, etc. It converts them to MQTT protocol for data ingestion to AWS IoT core.

What is a Modbus?

Modbus is a serial communication protocol developed by Modicon in 1979 to use with its programmable logic controllers (PLCs). It is a data communication protocol based on a request-response model, previously referred to as a single master-multi slave protocol. The Modbus Organization replaced master and slave with server and client in 2020. It is an “Open Protocol” i.e., it can be used by anyone paying any royalty, and its specifications are also available freely. Due to this, it is used by almost all industries.

There are several versions of the protocol exist for the serial port and Ethernet, and the most common are:

  • Modbus RTU:  Most commonly used via Serial Communication i.e., RS232 or RS485.
  • Modbus ASCII: It uses ASCII characters to begin and end messages.
  • Modbus TCP:  Modbus RTU encapsulated as TCP packets.
  • Modbus Plus: It is a peer-to-peer protocol that runs at 1 Mbps. The Modbus Plus protocol specifies the software layer as well as the hardware layer.

A Modbus data frame consists of

  1. Address field: It determines the slave address.
  2. Function code field: It determines what action to perform on data.

The following table contains all the details:


3. Data Field: Actual data to be sent or received.

4. CRC Field: Cyclic redundancy check for an error.

The Modbus TCP protocol used widely across industries is a Modbus RTU frame wrapped inside a TCP structure and runs on port 502 by default.

Customized Cloud Solutions to Drive your Business Success

  • Cloud Migration
  • Devops
  • AIML & IoT
Know More

What is Node-Red?

Node-RED is a programming tool for wiring together hardware devices, APIs, and online services in a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single click.

The Node-Red light-weight runtime is built on Node.js, giving the user the full advantage of its event-driven, non-blocking model. This makes it ideal for running at the edge of the network on low-cost hardware such as the Raspberry Pi and in the cloud. Below a snapshot of the Node-Red drag and drop flow environment is shown.



Figure.1.1: Node Red dashboard

After installing the Node.js and Node-Red using npm; To run the Node-Red on windows or a Raspberry Pi, the node-red command needs to be used. Once the Node-Red server starts, it can be accessed using any browser as with the Url: http://localhost:1880.


In this proposal, a Modbus slave device is simulated in a Windows PC using a simulator called diagslave. By default, the slave device opens a Modbus server and starts communicating via port 502. The Client device or the master is a Raspberry Pi 2B running Node-Red environment and acting as an edge device to connect to the AWS IoT core. The proposed architecture is shown below.



Figure.2.1. Proposed Architecture

Step-by-Step Guide

Step 1: Installation of Node-Red and diagslave:

Node-RED comes pre-installed on the full Raspbian SD image card, even though it is recommended to upgrade Node-Red using the following command.

After updating, the Node-Red can be run using the command node-red-start.

To access the GUI-based development environment, use the Url: http://localhost:1880 in chromium.

There is no need to install diagslave as it is an executable program that can be invoked using a command prompt using the command diagslave -m tcp. This leads to running the Modbus server on port 502 of the slave device. In this case, the diagslave was run on a Windows PC connected to the same network as the Raspberry Pi.


Figure.3.1. Simulated Modbus Slave/ Server using diagslave

Step 2: Creation of Things in AWS

Things are created in the AWS IoT Core for testing purposes, and respective policies are attached with the certificate.

Step 3: Writing data to the Modbus Device Output registers

The Node-Red running in raspberry pi is used to write the Modbus TCP servers’ holding registers using a flow called “Write Modbus”.  The main pallet used here was “node-red-contrib-modbus”, which can be installed using the “Manage palette” option from the menu button on the right top corner.


Figure 3.2. Installation of the node-red-contrib-modbus palette

To write the holding registers, “Modbus-Write” sub-palette was used with the following configuration.


Figure 3.3. Modbus-Write Configuration

In this case, since two registers are being written, two such sub-flows are used. The following figure shows the same concept.


Figure 3.4. Modbus-Write flow for holding registers.

Step 4. Reading data from the Modbus Device Holding registers and publishing to AWS IoT Core

A flow named “Read Modbus and send Mqtt” is developed to read the Holding registers. The Reading of the holding register is achieved using a Modbus-Read sub-palette configured as follows.


Figure 3.5. Modbus-Read configuration

The MQTT part is achieved using the “mqtt out” subpalette from the “network” palette. The configuration is shown below.


Figure 3.6. mqtt out palette configuration for AWS IoT Core

The flow of the intended purpose of reading from Holding registers and publishing via MQTT to AWS IoT Core is achieved by the following flow.


Figure 3.7. Flow for sending Modbus data to AWS IoT Core

The sub-pallet pushes the data received from the “Modbus-Read” block to the “mqtt out” sub-palette as an array.


The data sent from the Raspberry pi acting as a gateway device was received as an array in AWS IoT Core, which can further be used for creating datasets in AWS IoT Analytics and storing in Amazon S3 or visualization in BI tools such as Amazon Quicksight. A sample data frame received from the Raspberry Pi is shown below.


Figure 4.1. Modbus data received in AWS IoT Core


After implementing this process of acquiring industrial data from field instruments like PLC and other Modbus-compliant data sources, it can be assumed that using a cheap processor board like Raspberry Pi with freeware like Node-Red is a viable budget-friendly solution instead of a dedicated gateway for small industries for quicker deployment. Although the IIoT gateways have the benefit of robustness and reliability, Raspberry Pi provides better flexibility in choosing a programming-free environment like Node-Red. Node-Red is a programming-free solution that also provides code transportability among devices or different OSs as a JSON object document, which further reduces deployment time.

Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.

  • Cloud Training
  • Customized Training
  • Experiential Learning
Read More

About CloudThat

CloudThat is also the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft gold partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

Drop a query if you have any questions regarding AWS IoT Core Connectivity and I will get back to you quickly.

To get started, go through our Consultancy page and Managed Services Package that is CloudThat’s offerings.


1. Are Node-Red and diagslave used here freeware?

ANS: – Yes, Node-Red is a javascript-based development tool with tons of free libraries created by the community of users. Diagslave is also a software freely available for download. The diagslave can be downloaded from the following website. Free Modbus Slave Simulator and Test Tool (

2. Does Node-Red support Python codes?

ANS: – Yes, It is possible by making python functions available in Node-RED using a simple decorator and a library called “pynodered”, a server communicating with Node-RED to make your python functions as blocks available in Node-RED. This library can be downloaded from GitHub using the following URL:GitHub – ghislainp/pynodered: make python functions accessible from Node-RED

WRITTEN BY Rishi Raj Saikia

Rishi Raj Saikia is working as Sr. Research Associate - Data & AI IoT team at CloudThat.  He is a seasoned Electronics & Instrumentation engineer with a history of working in Telecom and the petroleum industry. He also possesses a deep knowledge of electronics, control theory/controller designing, and embedded systems, with PCB designing skills for relevant domains. He is keen on learning new advancements in IoT devices, IIoT technologies, and cloud-based technologies.



    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!