HiveMQ Edge-to-Cloud AI Pipeline

Anomaly Detection using HiveMQ Cloud, Python (PyOD), and Flask

This article explains the full data pipeline shown in the diagram starting from a PLC on the shopfloor, sending data through HiveMQ Edge and Cloud, into an AI model built with Python + PyOD, and finally sending the anomaly result back to the HiveMQ Edge for visualization.

Kindly refer to last article to learn how to send factory data to HiveMQ Cloud. This article continues from there to further send data to ML for anomaly detection.

System Overview

This architecture demonstrates how vibration data from a machine is collected at the OT (Operational Technology) level, transported securely to the Cloud, evaluated using an AI model, and returned to the shop-floor for further action.

It consists of:

  1. Input Layer – Reading vibration data from a PLC (via OPC UA)

  2. Transport Layer – Sending data from HiveMQ Edge β†’ HiveMQ Cloud (via MQTT)

  3. AI Layer – ML server running Python + Flask + PyOD locally on the computer

  4. Output Layer – Returning anomaly alerts via MQTT β†’ HiveMQ Edge β†’ PLC (optional)


Step-by-Step Breakdown of the Pipeline

1. Input Layer β€” PLC to HiveMQ Edge (OPC UA)

  • A Siemens S7-1500 PLC measures vibration (simulated) from the machine.

  • This data is exposed through OPC UA.

  • HiveMQ Edge connects to the PLC's OPC UA server.

  • It maps the OPC UA tag (e.g., vibration) to an MQTT topic:

Industrial data becomes IIoT data.


2. Transport Layer β€” HiveMQ Edge to HiveMQ Cloud (MQTT)

HiveMQ Edge publishes the MQTT message (machine/s71500/vib) to HiveMQ Cloud, which acts as a central cloud broker.

Benefits:

  • Low latency

  • Very small message size β†’ suitable for industrial IoT

  • Secure TLS transport and easy to scale

Learn more about that in our earlier posts on HiveMQ where you will learn the complete workflow: PLC to Cloud via HiveMQ (Edge + Cloud) β€” OPC UA β†’ MQTT β†’ Confluent


3. AI Layer β€” ML server running Python + Flask + PyOD locally on the compute

This is where the anomaly detection happens. Let's first understand what is Anomaly detection and why we need it

What is Anomaly detection?

Anomaly detection is the process of identifying data points or behavior that deviate from what’s considered normal. In simple terms, it spots when something unusual is happening.

In Machine Learning, anomaly detection helps models understand patterns and flag unexpected behaviour without needing labels. In IIoT, it's essential because machines generate huge amounts of real-time data, and even small deviations can signal issues like equipment failure, quality defects, cyber-attacks, or unsafe operating conditions.

Why we need it:

  • Detect problems early before they become costly

  • Improve uptime and reliability

  • Enhance safety and Reduce maintenance costs

🧠 PyOD β€” Python Outlier Detection Library

PyOD is a widely-used ML library that provides 40+ algorithms for anomaly detection, such as:

  • Isolation Forest (IForest)

  • AutoEncoders

  • One-Class SVM

  • LOF (Local Outlier Factor)

In the example, Isolation Forest (IForest) is used. It learns what β€œnormal vibration” looks like from training data:

When new data comes, PyOD predicts:

  • 0 β†’ Normal

  • 1 β†’ Anomaly

Learn more about Pyod here: https://pyod.readthedocs.io/en/latest/

Installing Pyod and testing Anomaly detection

In our example, the Pyod has been installed in the windows computer. The following are the steps:

1

Install pyod on Windows PC

Make sure Python is installed in your computer. In our case, we are using Python 3. To install Python visit here: https://www.python.org/downloads/

2

Create a python file for testing:

test_pyod.py

3

Add the following code in that

4

Run the Python file

5

Validate the output

β€˜Anomaly detected’ or β€˜Normal’ based on the value sent in the code (0.45)

Anomaly detection using Pyod and Node-RED

Now, we are going to test the code using Node-RED. In this case, we setup a flask server in Node-RED so that we can execute the Python code using API. This makes it easy to send data to the Python file and get anomaly results as feedback.

Make sure latest version of Node-RED and Python is installed in your system.

Once, the Node-RED is installed proceed with the following steps:

1

Convert Your Python Code into a Simple API Server.

We will not run Python file directly from Node-RED, we will create a Python web API for data exchange.

Create a new Python file named 'server.py' with the following code:

2

Install Flask

3

Run your Python Server

4

Validate

You should see the following:

Your anomaly detection API is now LIVE at: http://localhost:5000/predict

5

Start Node-RED and test the Anomaly detection

You can use the 'http request node' in Node-RED to send sample data and fetch anomaly results as shown below.

Kindly check the reference Node-RED flow below:


Testing Anomaly detection with PLC Data

Now, let's use the above example and sends the PLC data to the Python server to get anomaly results.

For the sake of demonstration, we are considering the normal values within the range 0.0∼5.00.0∼5.0 and anomalies outside this range. So, in our Python code, we have created a dataset of 500500 samples in the range of 0.00.0 to 5.05.0. Our model will learn from this dataset, as shown in the code below.

1

Update your Python code in the server.py file

The code trains an anomaly detection model, exposes it through a REST API, and lets you retrain the model using real machine data.

This script creates a small AI-powered anomaly detection API using Flask and the PyOD Isolation Forest model.

  1. It generates normal vibration data (0–5 mm/s) and uses it to train an Isolation Forest model.

  2. It scales all vibration values using StandardScaler so the model can understand them correctly.

  3. It exposes a /predict API endpoint where you send one vibration value, and the server returns:

    • Normal or Anomaly

    • Anomaly score

    • Scaled value

  4. It provides a /retrain endpoint that allows you to send new vibration samples (from Node-RED or a PLC) and retrain the model on the fly.

  5. Flask runs the server on port 5000, making it easy to integrate with IIoT systems.

Learn more about this code here: Anomaly detection Code Explanation

2

Update the Node-RED flow

Now we need to subscribe the following HiveMQ Cloud topic to get the LIVE vibration data:

Every time vibration data arrives from HiveMQ Cloud β†’ the ML server receives it, runs inference, and give the result back to Node-RED


4. Output Layer – Returning anomaly alerts via MQTT β†’ HiveMQ Edge β†’ PLC (optional)

Now we need to send the result back to HiveMQ Edge, visualize it on the dashboard or sends to the PLC for further action.

1

Add MQTT out node in the flow

We will take another MQTT topic to filter and publish the result

The following flow can be used as a reference.

2

Bridge the HiveMQ Cloud MQTT topic with HiveMQ Edge MQTT topic

Go to MQTT bridge in HiveMQ Edge and add a remote subscription in your current bridge as shown below:

Now navigate to Broker Configuration and 'Enable loop prevention'. This will prevent looping of data flow

3

Visualize Anomaly in HiveMQ Edge

Now that the anomaly arrives to MQTT Edge, you can visualize it with tools like Node-RED as shown below:

You can further sends the anomaly results back to PLC via OPC UA.


Complete Workflow

β™₯️ Work With Me

I regularly test industrial automation and IIoT devices. If you’d like me to review your product or showcase it in my courses and YouTube channel:

πŸ“§ Email: [email protected] or drop me a message on LinkedIn

Last updated

Was this helpful?